blob: 7df4f7ddab9ff82cbd39deb75d24654297c9df57 [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));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static 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 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static 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 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000811static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static 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 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200841
842#ifdef EBCDIC
843static int compare_func_name __ARGS((const void *s1, const void *s2));
844static void sortFunctions __ARGS(());
845#endif
846
847
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848/* Character used as separated in autoload function/variable names. */
849#define AUTOLOAD_CHAR '#'
850
Bram Moolenaar33570922005-01-25 22:26:29 +0000851/*
852 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000853 */
854 void
855eval_init()
856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 int i;
858 struct vimvar *p;
859
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200860 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
861 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200862 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865
866 for (i = 0; i < VV_LEN; ++i)
867 {
868 p = &vimvars[i];
869 STRCPY(p->vv_di.di_key, p->vv_name);
870 if (p->vv_flags & VV_RO)
871 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
872 else if (p->vv_flags & VV_RO_SBX)
873 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
874 else
875 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000876
877 /* add to v: scope dict, unless the value is not always available */
878 if (p->vv_type != VAR_UNKNOWN)
879 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000881 /* add to compat scope dict */
882 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000884 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200885 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886
887#ifdef EBCDIC
888 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100889 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890 */
891 sortFunctions();
892#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000893}
894
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895#if defined(EXITFREE) || defined(PROTO)
896 void
897eval_clear()
898{
899 int i;
900 struct vimvar *p;
901
902 for (i = 0; i < VV_LEN; ++i)
903 {
904 p = &vimvars[i];
905 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000907 vim_free(p->vv_str);
908 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000909 }
910 else if (p->vv_di.di_tv.v_type == VAR_LIST)
911 {
912 list_unref(p->vv_list);
913 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 }
916 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000917 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918 hash_clear(&compat_hashtab);
919
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100921# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200922 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100923# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924
925 /* global variables */
926 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000927
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000928 /* autoloaded script names */
929 ga_clear_strings(&ga_loaded);
930
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 /* script-local variables */
932 for (i = 1; i <= ga_scripts.ga_len; ++i)
933 {
934 vars_clear(&SCRIPT_VARS(i));
935 vim_free(SCRIPT_SV(i));
936 }
937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1161 FALSE, FALSE, FALSE, FNE_CHECK_START);
1162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(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;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001864 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001867 /*
1868 * ":let" without "=": list variables
1869 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 if (*arg == '[')
1871 EMSG(_(e_invarg));
1872 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_glob_vars(&first);
1879 list_buf_vars(&first);
1880 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001883#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001884 list_script_vars(&first);
1885 list_func_vars(&first);
1886 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 eap->nextcmd = check_nextcmd(arg);
1889 }
1890 else
1891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 op[0] = '=';
1893 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 {
1896 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1897 op[0] = expr[-1]; /* +=, -= or .= */
1898 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
2250 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
2530 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002531 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 */
2534 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002535get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 typval_T *rettv;
2538 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 int unlet;
2540 int skip;
2541 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 char_u *expr_start, *expr_end;
2546 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 dictitem_T *v;
2548 typval_T var1;
2549 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558
2559 if (skip)
2560 {
2561 /* When skipping just find the end of the name. */
2562 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002563 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 }
2565
2566 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002567 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (expr_start != NULL)
2569 {
2570 /* Don't expand the name when we already know there is an error. */
2571 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2572 && *p != '[' && *p != '.')
2573 {
2574 EMSG(_(e_trailing));
2575 return NULL;
2576 }
2577
2578 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2579 if (lp->ll_exp_name == NULL)
2580 {
2581 /* Report an invalid expression in braces, unless the
2582 * expression evaluation has been cancelled due to an
2583 * aborting error, an interrupt, or an exception. */
2584 if (!aborting() && !quiet)
2585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 EMSG2(_(e_invarg2), name);
2588 return NULL;
2589 }
2590 }
2591 lp->ll_name = lp->ll_exp_name;
2592 }
2593 else
2594 lp->ll_name = name;
2595
2596 /* Without [idx] or .key we are done. */
2597 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2598 return p;
2599
2600 cc = *p;
2601 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002602 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (v == NULL && !quiet)
2604 EMSG2(_(e_undefvar), lp->ll_name);
2605 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 if (v == NULL)
2607 return NULL;
2608
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /*
2610 * Loop until no more [idx] or .key is following.
2611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002612 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2616 && !(lp->ll_tv->v_type == VAR_DICT
2617 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_("E689: Can only index a List or Dictionary"));
2621 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_("E708: [:] must come last"));
2627 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 len = -1;
2631 if (*p == '.')
2632 {
2633 key = p + 1;
2634 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2635 ;
2636 if (len == 0)
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!quiet)
2639 EMSG(_(e_emptykey));
2640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642 p = key + len;
2643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 else
2645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 if (*p == ':')
2649 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 else
2651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 empty1 = FALSE;
2653 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 if (get_tv_string_chk(&var1) == NULL)
2656 {
2657 /* not a number or string */
2658 clear_tv(&var1);
2659 return NULL;
2660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
2662
2663 /* Optionally get the second index [ :expr]. */
2664 if (*p == ':')
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 if (!empty1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (rettv != NULL && (rettv->v_type != VAR_LIST
2675 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = skipwhite(p + 1);
2684 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
2687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 if (get_tv_string_chk(&var2) == NULL)
2696 {
2697 /* not a number or string */
2698 if (!empty1)
2699 clear_tv(&var1);
2700 clear_tv(&var2);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719
2720 /* Skip to past ']'. */
2721 ++p;
2722 }
2723
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
2726 if (len == -1)
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (*key == NUL)
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
2733 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 lp->ll_list = NULL;
2739 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002740 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 /* When assigning to a scope dictionary check that a function and
2743 * variable name is valid (only variable name unless it is l: or
2744 * g: dictionary). Disallow overwriting a builtin function. */
2745 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 int prevval;
2748 int wrong;
2749
2750 if (len != -1)
2751 {
2752 prevval = key[len];
2753 key[len] = NUL;
2754 }
2755 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2756 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002758 || !valid_varname(key);
2759 if (len != -1)
2760 key[len] = prevval;
2761 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 return NULL;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* Can't add "v:" variable. */
2768 if (lp->ll_dict == &vimvardict)
2769 {
2770 EMSG2(_(e_illvar), name);
2771 return NULL;
2772 }
2773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (len == -1)
2780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 p = NULL;
2791 break;
2792 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 /* existing variable, need to check if it can be changed */
2794 else if (var_check_ro(lp->ll_di->di_flags, name))
2795 return NULL;
2796
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 }
2801 else
2802 {
2803 /*
2804 * Get the number and item for the only or first index of the List.
2805 */
2806 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 else
2809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002810 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var1);
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_list = lp->ll_tv->vval.v_list;
2815 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2816 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002818 if (lp->ll_n1 < 0)
2819 {
2820 lp->ll_n1 = 0;
2821 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2822 }
2823 }
2824 if (lp->ll_li == NULL)
2825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002828 if (!quiet)
2829 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832
2833 /*
2834 * May need to find the item or absolute index for the second
2835 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 * When no index given: "lp->ll_empty2" is TRUE.
2837 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 {
2848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2856 if (lp->ll_n1 < 0)
2857 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2858 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 {
2860 if (!quiet)
2861 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002868 }
2869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return p;
2871}
2872
2873/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 */
2876 static void
2877clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879{
2880 vim_free(lp->ll_exp_name);
2881 vim_free(lp->ll_newkey);
2882}
2883
2884/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002887 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 */
2889 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 listitem_T *ri;
2899 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900
2901 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 cc = *endp;
2906 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 if (op != NULL && *op != '=')
2908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002912 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 {
2915 if (tv_op(&tv, rettv, op) == OK)
2916 set_var(lp->ll_name, &tv, FALSE);
2917 clear_tv(&tv);
2918 }
2919 }
2920 else
2921 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 else if (tv_check_lock(lp->ll_newkey == NULL
2926 ? lp->ll_tv->v_lock
2927 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2928 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 else if (lp->ll_range)
2930 {
2931 /*
2932 * Assign the List values to the list items.
2933 */
2934 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2938 else
2939 {
2940 clear_tv(&lp->ll_li->li_tv);
2941 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 ri = ri->li_next;
2944 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2945 break;
2946 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002949 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 ri = NULL;
2952 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002953 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 lp->ll_li = lp->ll_li->li_next;
2956 ++lp->ll_n1;
2957 }
2958 if (ri != NULL)
2959 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 else if (lp->ll_empty2
2961 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 : lp->ll_n1 != lp->ll_n2)
2963 EMSG(_("E711: List value has not enough items"));
2964 }
2965 else
2966 {
2967 /*
2968 * Assign to a List or Dictionary item.
2969 */
2970 if (lp->ll_newkey != NULL)
2971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972 if (op != NULL && *op != '=')
2973 {
2974 EMSG2(_(e_letwrong), op);
2975 return;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002979 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 if (di == NULL)
2981 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002982 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2983 {
2984 vim_free(di);
2985 return;
2986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 else if (op != NULL && *op != '=')
2990 {
2991 tv_op(lp->ll_tv, rettv, op);
2992 return;
2993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 /*
2998 * Assign the value to the variable or list item.
2999 */
3000 if (copy)
3001 copy_tv(rettv, lp->ll_tv);
3002 else
3003 {
3004 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003005 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 }
3008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003009}
3010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3013 * Returns OK or FAIL.
3014 */
3015 static int
3016tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 typval_T *tv1;
3018 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 char_u *op;
3020{
3021 long n;
3022 char_u numbuf[NUMBUFLEN];
3023 char_u *s;
3024
3025 /* Can't do anything with a Funcref or a Dict on the right. */
3026 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3027 {
3028 switch (tv1->v_type)
3029 {
3030 case VAR_DICT:
3031 case VAR_FUNC:
3032 break;
3033
3034 case VAR_LIST:
3035 if (*op != '+' || tv2->v_type != VAR_LIST)
3036 break;
3037 /* List += List */
3038 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3039 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3040 return OK;
3041
3042 case VAR_NUMBER:
3043 case VAR_STRING:
3044 if (tv2->v_type == VAR_LIST)
3045 break;
3046 if (*op == '+' || *op == '-')
3047 {
3048 /* nr += nr or nr -= nr*/
3049 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#ifdef FEAT_FLOAT
3051 if (tv2->v_type == VAR_FLOAT)
3052 {
3053 float_T f = n;
3054
3055 if (*op == '+')
3056 f += tv2->vval.v_float;
3057 else
3058 f -= tv2->vval.v_float;
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_FLOAT;
3061 tv1->vval.v_float = f;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064#endif
3065 {
3066 if (*op == '+')
3067 n += get_tv_number(tv2);
3068 else
3069 n -= get_tv_number(tv2);
3070 clear_tv(tv1);
3071 tv1->v_type = VAR_NUMBER;
3072 tv1->vval.v_number = n;
3073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 }
3075 else
3076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (tv2->v_type == VAR_FLOAT)
3078 break;
3079
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 /* str .= str */
3081 s = get_tv_string(tv1);
3082 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3083 clear_tv(tv1);
3084 tv1->v_type = VAR_STRING;
3085 tv1->vval.v_string = s;
3086 }
3087 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088
3089#ifdef FEAT_FLOAT
3090 case VAR_FLOAT:
3091 {
3092 float_T f;
3093
3094 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3095 && tv2->v_type != VAR_NUMBER
3096 && tv2->v_type != VAR_STRING))
3097 break;
3098 if (tv2->v_type == VAR_FLOAT)
3099 f = tv2->vval.v_float;
3100 else
3101 f = get_tv_number(tv2);
3102 if (*op == '+')
3103 tv1->vval.v_float += f;
3104 else
3105 tv1->vval.v_float -= f;
3106 }
3107 return OK;
3108#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 }
3110 }
3111
3112 EMSG2(_(e_letwrong), op);
3113 return FAIL;
3114}
3115
3116/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 * Add a watcher to a list.
3118 */
3119 static void
3120list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 list_T *l;
3122 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123{
3124 lw->lw_next = l->lv_watch;
3125 l->lv_watch = lw;
3126}
3127
3128/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003129 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 * No warning when it isn't found...
3131 */
3132 static void
3133list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 list_T *l;
3135 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136{
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138
3139 lwp = &l->lv_watch;
3140 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3141 {
3142 if (lw == lwrem)
3143 {
3144 *lwp = lw->lw_next;
3145 break;
3146 }
3147 lwp = &lw->lw_next;
3148 }
3149}
3150
3151/*
3152 * Just before removing an item from a list: advance watchers to the next
3153 * item.
3154 */
3155 static void
3156list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 list_T *l;
3158 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159{
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003161
3162 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3163 if (lw->lw_item == item)
3164 lw->lw_item = item->li_next;
3165}
3166
3167/*
3168 * Evaluate the expression used in a ":for var in expr" command.
3169 * "arg" points to "var".
3170 * Set "*errp" to TRUE for an error, FALSE otherwise;
3171 * Return a pointer that holds the info. Null when there is an error.
3172 */
3173 void *
3174eval_for_line(arg, errp, nextcmdp, skip)
3175 char_u *arg;
3176 int *errp;
3177 char_u **nextcmdp;
3178 int skip;
3179{
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 typval_T tv;
3183 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184
3185 *errp = TRUE; /* default: there is an error */
3186
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 if (fi == NULL)
3189 return NULL;
3190
3191 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3192 if (expr == NULL)
3193 return fi;
3194
3195 expr = skipwhite(expr);
3196 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3197 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003198 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 return fi;
3200 }
3201
3202 if (skip)
3203 ++emsg_skip;
3204 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3205 {
3206 *errp = FALSE;
3207 if (!skip)
3208 {
3209 l = tv.vval.v_list;
3210 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003211 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213 clear_tv(&tv);
3214 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 else
3216 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003217 /* No need to increment the refcount, it's already set for the
3218 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 fi->fi_list = l;
3220 list_add_watch(l, &fi->fi_lw);
3221 fi->fi_lw.lw_item = l->lv_first;
3222 }
3223 }
3224 }
3225 if (skip)
3226 --emsg_skip;
3227
3228 return fi;
3229}
3230
3231/*
3232 * Use the first item in a ":for" list. Advance to the next.
3233 * Assign the values to the variable (list). "arg" points to the first one.
3234 * Return TRUE when a valid item was found, FALSE when at end of list or
3235 * something wrong.
3236 */
3237 int
3238next_for_item(fi_void, arg)
3239 void *fi_void;
3240 char_u *arg;
3241{
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 item = fi->fi_lw.lw_item;
3247 if (item == NULL)
3248 result = FALSE;
3249 else
3250 {
3251 fi->fi_lw.lw_item = item->li_next;
3252 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3253 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3254 }
3255 return result;
3256}
3257
3258/*
3259 * Free the structure used to store info used by ":for".
3260 */
3261 void
3262free_for_info(fi_void)
3263 void *fi_void;
3264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003267 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 list_unref(fi->fi_list);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 vim_free(fi);
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3276
3277 void
3278set_context_for_expression(xp, arg, cmdidx)
3279 expand_T *xp;
3280 char_u *arg;
3281 cmdidx_T cmdidx;
3282{
3283 int got_eq = FALSE;
3284 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (cmdidx == CMD_let)
3288 {
3289 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003290 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 {
3292 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003293 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 {
3295 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (vim_iswhite(*p))
3298 break;
3299 }
3300 return;
3301 }
3302 }
3303 else
3304 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3305 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 while ((xp->xp_pattern = vim_strpbrk(arg,
3307 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3308 {
3309 c = *xp->xp_pattern;
3310 if (c == '&')
3311 {
3312 c = xp->xp_pattern[1];
3313 if (c == '&')
3314 {
3315 ++xp->xp_pattern;
3316 xp->xp_context = cmdidx != CMD_let || got_eq
3317 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3318 }
3319 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003322 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3323 xp->xp_pattern += 2;
3324
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 else if (c == '$')
3328 {
3329 /* environment variable */
3330 xp->xp_context = EXPAND_ENV_VARS;
3331 }
3332 else if (c == '=')
3333 {
3334 got_eq = TRUE;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else if (c == '<'
3338 && xp->xp_context == EXPAND_FUNCTIONS
3339 && vim_strchr(xp->xp_pattern, '(') == NULL)
3340 {
3341 /* Function name can start with "<SNR>" */
3342 break;
3343 }
3344 else if (cmdidx != CMD_let || got_eq)
3345 {
3346 if (c == '"') /* string */
3347 {
3348 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3349 if (c == '\\' && xp->xp_pattern[1] != NUL)
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_NOTHING;
3352 }
3353 else if (c == '\'') /* literal string */
3354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3357 /* skip */ ;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '|')
3361 {
3362 if (xp->xp_pattern[1] == '|')
3363 {
3364 ++xp->xp_pattern;
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
3367 else
3368 xp->xp_context = EXPAND_COMMANDS;
3369 }
3370 else
3371 xp->xp_context = EXPAND_EXPRESSION;
3372 }
3373 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 /* Doesn't look like something valid, expand as an expression
3375 * anyway. */
3376 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 arg = xp->xp_pattern;
3378 if (*arg != NUL)
3379 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3380 /* skip */ ;
3381 }
3382 xp->xp_pattern = arg;
3383}
3384
3385#endif /* FEAT_CMDL_COMPL */
3386
3387/*
3388 * ":1,25call func(arg1, arg2)" function call.
3389 */
3390 void
3391ex_call(eap)
3392 exarg_T *eap;
3393{
3394 char_u *arg = eap->arg;
3395 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003397 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 linenr_T lnum;
3401 int doesrange;
3402 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003405 if (eap->skip)
3406 {
3407 /* trans_function_name() doesn't work well when skipping, use eval0()
3408 * instead to skip to any following command, e.g. for:
3409 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003410 ++emsg_skip;
3411 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3412 clear_tv(&rettv);
3413 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003414 return;
3415 }
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003418 if (fudi.fd_newkey != NULL)
3419 {
3420 /* Still need to give an error message for missing key. */
3421 EMSG2(_(e_dictkey), fudi.fd_newkey);
3422 vim_free(fudi.fd_newkey);
3423 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 if (tofree == NULL)
3425 return;
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 /* Increase refcount on dictionary, it could get deleted when evaluating
3428 * the arguments. */
3429 if (fudi.fd_dict != NULL)
3430 ++fudi.fd_dict->dv_refcount;
3431
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003432 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003433 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar532c7802005-01-27 14:44:31 +00003436 /* Skip white space to allow ":call func ()". Not good, but required for
3437 * backward compatibility. */
3438 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 if (*startarg != '(')
3442 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003443 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 goto end;
3445 }
3446
3447 /*
3448 * When skipping, evaluate the function once, to find the end of the
3449 * arguments.
3450 * When the function takes a range, this is discovered after the first
3451 * call, and the loop is broken.
3452 */
3453 if (eap->skip)
3454 {
3455 ++emsg_skip;
3456 lnum = eap->line2; /* do it once, also with an invalid range */
3457 }
3458 else
3459 lnum = eap->line1;
3460 for ( ; lnum <= eap->line2; ++lnum)
3461 {
3462 if (!eap->skip && eap->addr_count > 0)
3463 {
3464 curwin->w_cursor.lnum = lnum;
3465 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003466#ifdef FEAT_VIRTUALEDIT
3467 curwin->w_cursor.coladd = 0;
3468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 eap->line1, eap->line2, &doesrange,
3473 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 failed = TRUE;
3476 break;
3477 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
3479 /* Handle a function returning a Funcref, Dictionary or List. */
3480 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3481 {
3482 failed = TRUE;
3483 break;
3484 }
3485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (doesrange || eap->skip)
3488 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (aborting())
3495 break;
3496 }
3497 if (eap->skip)
3498 --emsg_skip;
3499
3500 if (!failed)
3501 {
3502 /* Check for trailing illegal characters and a following command. */
3503 if (!ends_excmd(*arg))
3504 {
3505 emsg_severe = TRUE;
3506 EMSG(_(e_trailing));
3507 }
3508 else
3509 eap->nextcmd = check_nextcmd(arg);
3510 }
3511
3512end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003514 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515}
3516
3517/*
3518 * ":unlet[!] var1 ... " command.
3519 */
3520 void
3521ex_unlet(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 ex_unletlock(eap, eap->arg, 0);
3525}
3526
3527/*
3528 * ":lockvar" and ":unlockvar" commands
3529 */
3530 void
3531ex_lockvar(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 int deep = 2;
3536
3537 if (eap->forceit)
3538 deep = -1;
3539 else if (vim_isdigit(*arg))
3540 {
3541 deep = getdigits(&arg);
3542 arg = skipwhite(arg);
3543 }
3544
3545 ex_unletlock(eap, arg, deep);
3546}
3547
3548/*
3549 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3550 */
3551 static void
3552ex_unletlock(eap, argstart, deep)
3553 exarg_T *eap;
3554 char_u *argstart;
3555 int deep;
3556{
3557 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 do
3563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003565 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3566 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 if (lv.ll_name == NULL)
3568 error = TRUE; /* error but continue parsing */
3569 if (name_end == NULL || (!vim_iswhite(*name_end)
3570 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 if (name_end != NULL)
3573 {
3574 emsg_severe = TRUE;
3575 EMSG(_(e_trailing));
3576 }
3577 if (!(eap->skip || error))
3578 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 break;
3580 }
3581
3582 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 {
3584 if (eap->cmdidx == CMD_unlet)
3585 {
3586 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3587 error = TRUE;
3588 }
3589 else
3590 {
3591 if (do_lock_var(&lv, name_end, deep,
3592 eap->cmdidx == CMD_lockvar) == FAIL)
3593 error = TRUE;
3594 }
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 if (!eap->skip)
3598 clear_lval(&lv);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 arg = skipwhite(name_end);
3601 } while (!ends_excmd(*arg));
3602
3603 eap->nextcmd = check_nextcmd(arg);
3604}
3605
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 int forceit;
3611{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 int ret = OK;
3613 int cc;
3614
3615 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 cc = *name_end;
3618 *name_end = NUL;
3619
3620 /* Normal name or expanded name. */
3621 if (check_changedtick(lp->ll_name))
3622 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3628 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 else if (lp->ll_range)
3630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632
3633 /* Delete a range of List items. */
3634 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3635 {
3636 li = lp->ll_li->li_next;
3637 listitem_remove(lp->ll_list, lp->ll_li);
3638 lp->ll_li = li;
3639 ++lp->ll_n1;
3640 }
3641 }
3642 else
3643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 /* unlet a List item. */
3646 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003649 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 }
3651
3652 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653}
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655/*
3656 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
3659 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hashtab_T *ht;
3665 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003667 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar33570922005-01-25 22:26:29 +00003669 ht = find_var_ht(name, &varname);
3670 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003672 hi = hash_find(ht, varname);
3673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003675 di = HI2DI(hi);
3676 if (var_check_fixed(di->di_flags, name)
3677 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
3679 delete_var(ht, hi);
3680 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 if (forceit)
3684 return OK;
3685 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687}
3688
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689/*
3690 * Lock or unlock variable indicated by "lp".
3691 * "deep" is the levels to go (-1 for unlimited);
3692 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3693 */
3694 static int
3695do_lock_var(lp, name_end, deep, lock)
3696 lval_T *lp;
3697 char_u *name_end;
3698 int deep;
3699 int lock;
3700{
3701 int ret = OK;
3702 int cc;
3703 dictitem_T *di;
3704
3705 if (deep == 0) /* nothing to do */
3706 return OK;
3707
3708 if (lp->ll_tv == NULL)
3709 {
3710 cc = *name_end;
3711 *name_end = NUL;
3712
3713 /* Normal name or expanded name. */
3714 if (check_changedtick(lp->ll_name))
3715 ret = FAIL;
3716 else
3717 {
3718 di = find_var(lp->ll_name, NULL);
3719 if (di == NULL)
3720 ret = FAIL;
3721 else
3722 {
3723 if (lock)
3724 di->di_flags |= DI_FLAGS_LOCK;
3725 else
3726 di->di_flags &= ~DI_FLAGS_LOCK;
3727 item_lock(&di->di_tv, deep, lock);
3728 }
3729 }
3730 *name_end = cc;
3731 }
3732 else if (lp->ll_range)
3733 {
3734 listitem_T *li = lp->ll_li;
3735
3736 /* (un)lock a range of List items. */
3737 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3738 {
3739 item_lock(&li->li_tv, deep, lock);
3740 li = li->li_next;
3741 ++lp->ll_n1;
3742 }
3743 }
3744 else if (lp->ll_list != NULL)
3745 /* (un)lock a List item. */
3746 item_lock(&lp->ll_li->li_tv, deep, lock);
3747 else
3748 /* un(lock) a Dictionary item. */
3749 item_lock(&lp->ll_di->di_tv, deep, lock);
3750
3751 return ret;
3752}
3753
3754/*
3755 * Lock or unlock an item. "deep" is nr of levels to go.
3756 */
3757 static void
3758item_lock(tv, deep, lock)
3759 typval_T *tv;
3760 int deep;
3761 int lock;
3762{
3763 static int recurse = 0;
3764 list_T *l;
3765 listitem_T *li;
3766 dict_T *d;
3767 hashitem_T *hi;
3768 int todo;
3769
3770 if (recurse >= DICT_MAXNEST)
3771 {
3772 EMSG(_("E743: variable nested too deep for (un)lock"));
3773 return;
3774 }
3775 if (deep == 0)
3776 return;
3777 ++recurse;
3778
3779 /* lock/unlock the item itself */
3780 if (lock)
3781 tv->v_lock |= VAR_LOCKED;
3782 else
3783 tv->v_lock &= ~VAR_LOCKED;
3784
3785 switch (tv->v_type)
3786 {
3787 case VAR_LIST:
3788 if ((l = tv->vval.v_list) != NULL)
3789 {
3790 if (lock)
3791 l->lv_lock |= VAR_LOCKED;
3792 else
3793 l->lv_lock &= ~VAR_LOCKED;
3794 if (deep < 0 || deep > 1)
3795 /* recursive: lock/unlock the items the List contains */
3796 for (li = l->lv_first; li != NULL; li = li->li_next)
3797 item_lock(&li->li_tv, deep - 1, lock);
3798 }
3799 break;
3800 case VAR_DICT:
3801 if ((d = tv->vval.v_dict) != NULL)
3802 {
3803 if (lock)
3804 d->dv_lock |= VAR_LOCKED;
3805 else
3806 d->dv_lock &= ~VAR_LOCKED;
3807 if (deep < 0 || deep > 1)
3808 {
3809 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3812 {
3813 if (!HASHITEM_EMPTY(hi))
3814 {
3815 --todo;
3816 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3817 }
3818 }
3819 }
3820 }
3821 }
3822 --recurse;
3823}
3824
Bram Moolenaara40058a2005-07-11 22:42:07 +00003825/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003826 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3827 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003828 */
3829 static int
3830tv_islocked(tv)
3831 typval_T *tv;
3832{
3833 return (tv->v_lock & VAR_LOCKED)
3834 || (tv->v_type == VAR_LIST
3835 && tv->vval.v_list != NULL
3836 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3837 || (tv->v_type == VAR_DICT
3838 && tv->vval.v_dict != NULL
3839 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3843/*
3844 * Delete all "menutrans_" variables.
3845 */
3846 void
3847del_menutrans_vars()
3848{
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003853 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 {
3856 if (!HASHITEM_EMPTY(hi))
3857 {
3858 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3860 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 }
3862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864}
3865#endif
3866
3867#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3868
3869/*
3870 * Local string buffer for the next two functions to store a variable name
3871 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3872 * get_user_var_name().
3873 */
3874
3875static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3876
3877static char_u *varnamebuf = NULL;
3878static int varnamebuflen = 0;
3879
3880/*
3881 * Function to concatenate a prefix and a variable name.
3882 */
3883 static char_u *
3884cat_prefix_varname(prefix, name)
3885 int prefix;
3886 char_u *name;
3887{
3888 int len;
3889
3890 len = (int)STRLEN(name) + 3;
3891 if (len > varnamebuflen)
3892 {
3893 vim_free(varnamebuf);
3894 len += 10; /* some additional space */
3895 varnamebuf = alloc(len);
3896 if (varnamebuf == NULL)
3897 {
3898 varnamebuflen = 0;
3899 return NULL;
3900 }
3901 varnamebuflen = len;
3902 }
3903 *varnamebuf = prefix;
3904 varnamebuf[1] = ':';
3905 STRCPY(varnamebuf + 2, name);
3906 return varnamebuf;
3907}
3908
3909/*
3910 * Function given to ExpandGeneric() to obtain the list of user defined
3911 * (global/buffer/window/built-in) variable names.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u *
3914get_user_var_name(xp, idx)
3915 expand_T *xp;
3916 int idx;
3917{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003918 static long_u gdone;
3919 static long_u bdone;
3920 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 static long_u tdone;
3923#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003924 static int vidx;
3925 static hashitem_T *hi;
3926 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003930 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 tdone = 0;
3933#endif
3934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* Global variables */
3937 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 else
3942 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 while (HASHITEM_EMPTY(hi))
3944 ++hi;
3945 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3946 return cat_prefix_varname('g', hi->hi_key);
3947 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949
3950 /* b: variables */
3951 ht = &curbuf->b_vars.dv_hashtab;
3952 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003956 else
3957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 while (HASHITEM_EMPTY(hi))
3959 ++hi;
3960 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return (char_u *)"b:changedtick";
3966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967
3968 /* w: variables */
3969 ht = &curwin->w_vars.dv_hashtab;
3970 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003972 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003974 else
3975 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981#ifdef FEAT_WINDOWS
3982 /* t: variables */
3983 ht = &curtab->tp_vars.dv_hashtab;
3984 if (tdone < ht->ht_used)
3985 {
3986 if (tdone++ == 0)
3987 hi = ht->ht_array;
3988 else
3989 ++hi;
3990 while (HASHITEM_EMPTY(hi))
3991 ++hi;
3992 return cat_prefix_varname('t', hi->hi_key);
3993 }
3994#endif
3995
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 /* v: variables */
3997 if (vidx < VV_LEN)
3998 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 vim_free(varnamebuf);
4001 varnamebuf = NULL;
4002 varnamebuflen = 0;
4003 return NULL;
4004}
4005
4006#endif /* FEAT_CMDL_COMPL */
4007
4008/*
4009 * types for expressions.
4010 */
4011typedef enum
4012{
4013 TYPE_UNKNOWN = 0
4014 , TYPE_EQUAL /* == */
4015 , TYPE_NEQUAL /* != */
4016 , TYPE_GREATER /* > */
4017 , TYPE_GEQUAL /* >= */
4018 , TYPE_SMALLER /* < */
4019 , TYPE_SEQUAL /* <= */
4020 , TYPE_MATCH /* =~ */
4021 , TYPE_NOMATCH /* !~ */
4022} exptype_T;
4023
4024/*
4025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4028 */
4029
4030/*
4031 * Handle zero level expression.
4032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004034 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * Return OK or FAIL.
4036 */
4037 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 char_u **nextcmd;
4042 int evaluate;
4043{
4044 int ret;
4045 char_u *p;
4046
4047 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (ret == FAIL || !ends_excmd(*p))
4050 {
4051 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /*
4054 * Report the invalid expression unless the expression evaluation has
4055 * been cancelled due to an aborting error, an interrupt, or an
4056 * exception.
4057 */
4058 if (!aborting())
4059 EMSG2(_(e_invexpr2), arg);
4060 ret = FAIL;
4061 }
4062 if (nextcmd != NULL)
4063 *nextcmd = check_nextcmd(p);
4064
4065 return ret;
4066}
4067
4068/*
4069 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004070 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 *
4072 * "arg" must point to the first non-white of the expression.
4073 * "arg" is advanced to the next non-white after the recognized expression.
4074 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004075 * Note: "rettv.v_lock" is not set.
4076 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
4085 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /*
4089 * Get the first variable.
4090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093
4094 if ((*arg)[0] == '?')
4095 {
4096 result = FALSE;
4097 if (evaluate)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 int error = FALSE;
4100
4101 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (error)
4105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107
4108 /*
4109 * Get the second variable.
4110 */
4111 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114
4115 /*
4116 * Check for the ":".
4117 */
4118 if ((*arg)[0] != ':')
4119 {
4120 EMSG(_("E109: Missing ':' after '?'"));
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125
4126 /*
4127 * Get the third variable.
4128 */
4129 *arg = skipwhite(*arg + 1);
4130 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4131 {
4132 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135 }
4136 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139
4140 return OK;
4141}
4142
4143/*
4144 * Handle first level expression:
4145 * expr2 || expr2 || expr2 logical OR
4146 *
4147 * "arg" must point to the first non-white of the expression.
4148 * "arg" is advanced to the next non-white after the recognized expression.
4149 *
4150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
Bram Moolenaar33570922005-01-25 22:26:29 +00004158 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 long result;
4160 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163 /*
4164 * Get the first variable.
4165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168
4169 /*
4170 * Repeat until there is no following "||".
4171 */
4172 first = TRUE;
4173 result = FALSE;
4174 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4175 {
4176 if (evaluate && first)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 if (error)
4182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 first = FALSE;
4184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 2);
4190 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4191 return FAIL;
4192
4193 /*
4194 * Compute the result.
4195 */
4196 if (evaluate && !result)
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (error)
4202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 if (evaluate)
4205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 rettv->v_type = VAR_NUMBER;
4207 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209 }
4210
4211 return OK;
4212}
4213
4214/*
4215 * Handle second level expression:
4216 * expr3 && expr3 && expr3 logical AND
4217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 long result;
4231 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat until there is no following "&&".
4242 */
4243 first = TRUE;
4244 result = TRUE;
4245 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4246 {
4247 if (evaluate && first)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 first = FALSE;
4255 }
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 2);
4261 if (eval4(arg, &var2, evaluate && result) == FAIL)
4262 return FAIL;
4263
4264 /*
4265 * Compute the result.
4266 */
4267 if (evaluate && result)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 if (evaluate)
4276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle third level expression:
4287 * var1 == var2
4288 * var1 =~ var2
4289 * var1 != var2
4290 * var1 !~ var2
4291 * var1 > var2
4292 * var1 >= var2
4293 * var1 < var2
4294 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 * var1 is var2
4296 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int evaluate;
4308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 char_u *p;
4311 int i;
4312 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004313 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int len = 2;
4315 long n1, n2;
4316 char_u *s1, *s2;
4317 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4318 regmatch_T regmatch;
4319 int ic;
4320 char_u *save_cpo;
4321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 p = *arg;
4329 switch (p[0])
4330 {
4331 case '=': if (p[1] == '=')
4332 type = TYPE_EQUAL;
4333 else if (p[1] == '~')
4334 type = TYPE_MATCH;
4335 break;
4336 case '!': if (p[1] == '=')
4337 type = TYPE_NEQUAL;
4338 else if (p[1] == '~')
4339 type = TYPE_NOMATCH;
4340 break;
4341 case '>': if (p[1] != '=')
4342 {
4343 type = TYPE_GREATER;
4344 len = 1;
4345 }
4346 else
4347 type = TYPE_GEQUAL;
4348 break;
4349 case '<': if (p[1] != '=')
4350 {
4351 type = TYPE_SMALLER;
4352 len = 1;
4353 }
4354 else
4355 type = TYPE_SEQUAL;
4356 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004357 case 'i': if (p[1] == 's')
4358 {
4359 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4360 len = 5;
4361 if (!vim_isIDc(p[len]))
4362 {
4363 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4364 type_is = TRUE;
4365 }
4366 }
4367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369
4370 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 if (type != TYPE_UNKNOWN)
4374 {
4375 /* extra question mark appended: ignore case */
4376 if (p[len] == '?')
4377 {
4378 ic = TRUE;
4379 ++len;
4380 }
4381 /* extra '#' appended: match case */
4382 else if (p[len] == '#')
4383 {
4384 ic = FALSE;
4385 ++len;
4386 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 ic = p_ic;
4390
4391 /*
4392 * Get the second variable.
4393 */
4394 *arg = skipwhite(p + len);
4395 if (eval5(arg, &var2, evaluate) == FAIL)
4396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 return FAIL;
4399 }
4400
4401 if (evaluate)
4402 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 if (type_is && rettv->v_type != var2.v_type)
4404 {
4405 /* For "is" a different type always means FALSE, for "notis"
4406 * it means TRUE. */
4407 n1 = (type == TYPE_NEQUAL);
4408 }
4409 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4410 {
4411 if (type_is)
4412 {
4413 n1 = (rettv->v_type == var2.v_type
4414 && rettv->vval.v_list == var2.vval.v_list);
4415 if (type == TYPE_NEQUAL)
4416 n1 = !n1;
4417 }
4418 else if (rettv->v_type != var2.v_type
4419 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4420 {
4421 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004422 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004424 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
4429 else
4430 {
4431 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004432 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4433 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 }
4438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004439 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4440 {
4441 if (type_is)
4442 {
4443 n1 = (rettv->v_type == var2.v_type
4444 && rettv->vval.v_dict == var2.vval.v_dict);
4445 if (type == TYPE_NEQUAL)
4446 n1 = !n1;
4447 }
4448 else if (rettv->v_type != var2.v_type
4449 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4450 {
4451 if (rettv->v_type != var2.v_type)
4452 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4453 else
4454 EMSG(_("E736: Invalid operation for Dictionary"));
4455 clear_tv(rettv);
4456 clear_tv(&var2);
4457 return FAIL;
4458 }
4459 else
4460 {
4461 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004462 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4463 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004464 if (type == TYPE_NEQUAL)
4465 n1 = !n1;
4466 }
4467 }
4468
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4470 {
4471 if (rettv->v_type != var2.v_type
4472 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4473 {
4474 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004475 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004477 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 clear_tv(rettv);
4479 clear_tv(&var2);
4480 return FAIL;
4481 }
4482 else
4483 {
4484 /* Compare two Funcrefs for being equal or unequal. */
4485 if (rettv->vval.v_string == NULL
4486 || var2.vval.v_string == NULL)
4487 n1 = FALSE;
4488 else
4489 n1 = STRCMP(rettv->vval.v_string,
4490 var2.vval.v_string) == 0;
4491 if (type == TYPE_NEQUAL)
4492 n1 = !n1;
4493 }
4494 }
4495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496#ifdef FEAT_FLOAT
4497 /*
4498 * If one of the two variables is a float, compare as a float.
4499 * When using "=~" or "!~", always compare as string.
4500 */
4501 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4502 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4503 {
4504 float_T f1, f2;
4505
4506 if (rettv->v_type == VAR_FLOAT)
4507 f1 = rettv->vval.v_float;
4508 else
4509 f1 = get_tv_number(rettv);
4510 if (var2.v_type == VAR_FLOAT)
4511 f2 = var2.vval.v_float;
4512 else
4513 f2 = get_tv_number(&var2);
4514 n1 = FALSE;
4515 switch (type)
4516 {
4517 case TYPE_EQUAL: n1 = (f1 == f2); break;
4518 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4519 case TYPE_GREATER: n1 = (f1 > f2); break;
4520 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4521 case TYPE_SMALLER: n1 = (f1 < f2); break;
4522 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4523 case TYPE_UNKNOWN:
4524 case TYPE_MATCH:
4525 case TYPE_NOMATCH: break; /* avoid gcc warning */
4526 }
4527 }
4528#endif
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /*
4531 * If one of the two variables is a number, compare as a number.
4532 * When using "=~" or "!~", always compare as string.
4533 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 n1 = get_tv_number(rettv);
4538 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 switch (type)
4540 {
4541 case TYPE_EQUAL: n1 = (n1 == n2); break;
4542 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4543 case TYPE_GREATER: n1 = (n1 > n2); break;
4544 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4545 case TYPE_SMALLER: n1 = (n1 < n2); break;
4546 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4547 case TYPE_UNKNOWN:
4548 case TYPE_MATCH:
4549 case TYPE_NOMATCH: break; /* avoid gcc warning */
4550 }
4551 }
4552 else
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 s1 = get_tv_string_buf(rettv, buf1);
4555 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4557 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4558 else
4559 i = 0;
4560 n1 = FALSE;
4561 switch (type)
4562 {
4563 case TYPE_EQUAL: n1 = (i == 0); break;
4564 case TYPE_NEQUAL: n1 = (i != 0); break;
4565 case TYPE_GREATER: n1 = (i > 0); break;
4566 case TYPE_GEQUAL: n1 = (i >= 0); break;
4567 case TYPE_SMALLER: n1 = (i < 0); break;
4568 case TYPE_SEQUAL: n1 = (i <= 0); break;
4569
4570 case TYPE_MATCH:
4571 case TYPE_NOMATCH:
4572 /* avoid 'l' flag in 'cpoptions' */
4573 save_cpo = p_cpo;
4574 p_cpo = (char_u *)"";
4575 regmatch.regprog = vim_regcomp(s2,
4576 RE_MAGIC + RE_STRING);
4577 regmatch.rm_ic = ic;
4578 if (regmatch.regprog != NULL)
4579 {
4580 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4581 vim_free(regmatch.regprog);
4582 if (type == TYPE_NOMATCH)
4583 n1 = !n1;
4584 }
4585 p_cpo = save_cpo;
4586 break;
4587
4588 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4589 }
4590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597
4598 return OK;
4599}
4600
4601/*
4602 * Handle fourth level expression:
4603 * + number addition
4604 * - number subtraction
4605 * . string concatenation
4606 *
4607 * "arg" must point to the first non-white of the expression.
4608 * "arg" is advanced to the next non-white after the recognized expression.
4609 *
4610 * Return OK or FAIL.
4611 */
4612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 int evaluate;
4617{
Bram Moolenaar33570922005-01-25 22:26:29 +00004618 typval_T var2;
4619 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int op;
4621 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622#ifdef FEAT_FLOAT
4623 float_T f1 = 0, f2 = 0;
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u *s1, *s2;
4626 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4627 char_u *p;
4628
4629 /*
4630 * Get the first variable.
4631 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004632 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return FAIL;
4634
4635 /*
4636 * Repeat computing, until no '+', '-' or '.' is following.
4637 */
4638 for (;;)
4639 {
4640 op = **arg;
4641 if (op != '+' && op != '-' && op != '.')
4642 break;
4643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004644 if ((op != '+' || rettv->v_type != VAR_LIST)
4645#ifdef FEAT_FLOAT
4646 && (op == '.' || rettv->v_type != VAR_FLOAT)
4647#endif
4648 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 {
4650 /* For "list + ...", an illegal use of the first operand as
4651 * a number cannot be determined before evaluating the 2nd
4652 * operand: if this is also a list, all is ok.
4653 * For "something . ...", "something - ..." or "non-list + ...",
4654 * we know that the first operand needs to be a string or number
4655 * without evaluating the 2nd operand. So check before to avoid
4656 * side effects after an error. */
4657 if (evaluate && get_tv_string_chk(rettv) == NULL)
4658 {
4659 clear_tv(rettv);
4660 return FAIL;
4661 }
4662 }
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /*
4665 * Get the second variable.
4666 */
4667 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004668 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return FAIL;
4672 }
4673
4674 if (evaluate)
4675 {
4676 /*
4677 * Compute the result.
4678 */
4679 if (op == '.')
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4682 s2 = get_tv_string_buf_chk(&var2, buf2);
4683 if (s2 == NULL) /* type error ? */
4684 {
4685 clear_tv(rettv);
4686 clear_tv(&var2);
4687 return FAIL;
4688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004689 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 rettv->v_type = VAR_STRING;
4692 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004694 else if (op == '+' && rettv->v_type == VAR_LIST
4695 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004696 {
4697 /* concatenate Lists */
4698 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4699 &var3) == FAIL)
4700 {
4701 clear_tv(rettv);
4702 clear_tv(&var2);
4703 return FAIL;
4704 }
4705 clear_tv(rettv);
4706 *rettv = var3;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 else
4709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 int error = FALSE;
4711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 f1 = rettv->vval.v_float;
4716 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 else
4719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721 n1 = get_tv_number_chk(rettv, &error);
4722 if (error)
4723 {
4724 /* This can only happen for "list + non-list". For
4725 * "non-list + ..." or "something - ...", we returned
4726 * before evaluating the 2nd operand. */
4727 clear_tv(rettv);
4728 return FAIL;
4729 }
4730#ifdef FEAT_FLOAT
4731 if (var2.v_type == VAR_FLOAT)
4732 f1 = n1;
4733#endif
4734 }
4735#ifdef FEAT_FLOAT
4736 if (var2.v_type == VAR_FLOAT)
4737 {
4738 f2 = var2.vval.v_float;
4739 n2 = 0;
4740 }
4741 else
4742#endif
4743 {
4744 n2 = get_tv_number_chk(&var2, &error);
4745 if (error)
4746 {
4747 clear_tv(rettv);
4748 clear_tv(&var2);
4749 return FAIL;
4750 }
4751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
4753 f2 = n2;
4754#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757
4758#ifdef FEAT_FLOAT
4759 /* If there is a float on either side the result is a float. */
4760 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4761 {
4762 if (op == '+')
4763 f1 = f1 + f2;
4764 else
4765 f1 = f1 - f2;
4766 rettv->v_type = VAR_FLOAT;
4767 rettv->vval.v_float = f1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#endif
4771 {
4772 if (op == '+')
4773 n1 = n1 + n2;
4774 else
4775 n1 = n1 - n2;
4776 rettv->v_type = VAR_NUMBER;
4777 rettv->vval.v_number = n1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 }
4783 return OK;
4784}
4785
4786/*
4787 * Handle fifth level expression:
4788 * * number multiplication
4789 * / number division
4790 * % number modulo
4791 *
4792 * "arg" must point to the first non-white of the expression.
4793 * "arg" is advanced to the next non-white after the recognized expression.
4794 *
4795 * Return OK or FAIL.
4796 */
4797 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 int op;
4806 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807#ifdef FEAT_FLOAT
4808 int use_float = FALSE;
4809 float_T f1 = 0, f2;
4810#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 /*
4814 * Get the first variable.
4815 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004816 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
4818
4819 /*
4820 * Repeat computing, until no '*', '/' or '%' is following.
4821 */
4822 for (;;)
4823 {
4824 op = **arg;
4825 if (op != '*' && op != '/' && op != '%')
4826 break;
4827
4828 if (evaluate)
4829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#ifdef FEAT_FLOAT
4831 if (rettv->v_type == VAR_FLOAT)
4832 {
4833 f1 = rettv->vval.v_float;
4834 use_float = TRUE;
4835 n1 = 0;
4836 }
4837 else
4838#endif
4839 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 if (error)
4842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 n1 = 0;
4846
4847 /*
4848 * Get the second variable.
4849 */
4850 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004851 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return FAIL;
4853
4854 if (evaluate)
4855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856#ifdef FEAT_FLOAT
4857 if (var2.v_type == VAR_FLOAT)
4858 {
4859 if (!use_float)
4860 {
4861 f1 = n1;
4862 use_float = TRUE;
4863 }
4864 f2 = var2.vval.v_float;
4865 n2 = 0;
4866 }
4867 else
4868#endif
4869 {
4870 n2 = get_tv_number_chk(&var2, &error);
4871 clear_tv(&var2);
4872 if (error)
4873 return FAIL;
4874#ifdef FEAT_FLOAT
4875 if (use_float)
4876 f2 = n2;
4877#endif
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884#ifdef FEAT_FLOAT
4885 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 if (op == '*')
4888 f1 = f1 * f2;
4889 else if (op == '/')
4890 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891# ifdef VMS
4892 /* VMS crashes on divide by zero, work around it */
4893 if (f2 == 0.0)
4894 {
4895 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004896 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004898 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004899 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004900 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901 }
4902 else
4903 f1 = f1 / f2;
4904# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 /* We rely on the floating point library to handle divide
4906 * by zero to result in "inf" and not a crash. */
4907 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004912 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 return FAIL;
4914 }
4915 rettv->v_type = VAR_FLOAT;
4916 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 n1 = n1 * n2;
4923 else if (op == '/')
4924 {
4925 if (n2 == 0) /* give an error message? */
4926 {
4927 if (n1 == 0)
4928 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4929 else if (n1 < 0)
4930 n1 = -0x7fffffffL;
4931 else
4932 n1 = 0x7fffffffL;
4933 }
4934 else
4935 n1 = n1 / n2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938 {
4939 if (n2 == 0) /* give an error message? */
4940 n1 = 0;
4941 else
4942 n1 = n1 % n2;
4943 }
4944 rettv->v_type = VAR_NUMBER;
4945 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 }
4949
4950 return OK;
4951}
4952
4953/*
4954 * Handle sixth level expression:
4955 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004956 * "string" string constant
4957 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 * &option-name option value
4959 * @r register contents
4960 * identifier variable value
4961 * function() function call
4962 * $VAR environment variable
4963 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004964 * [expr, expr] List
4965 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 *
4967 * Also handle:
4968 * ! in front logical NOT
4969 * - in front unary minus
4970 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * trailing [] subscript in String or List
4972 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * "arg" must point to the first non-white of the expression.
4975 * "arg" is advanced to the next non-white after the recognized expression.
4976 *
4977 * Return OK or FAIL.
4978 */
4979 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004980eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004984 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 long n;
4987 int len;
4988 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u *start_leader, *end_leader;
4990 int ret = OK;
4991 char_u *alias;
4992
4993 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 /*
5000 * Skip '!' and '-' characters. They are handled later.
5001 */
5002 start_leader = *arg;
5003 while (**arg == '!' || **arg == '-' || **arg == '+')
5004 *arg = skipwhite(*arg + 1);
5005 end_leader = *arg;
5006
5007 switch (**arg)
5008 {
5009 /*
5010 * Number constant.
5011 */
5012 case '0':
5013 case '1':
5014 case '2':
5015 case '3':
5016 case '4':
5017 case '5':
5018 case '6':
5019 case '7':
5020 case '8':
5021 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 {
5023#ifdef FEAT_FLOAT
5024 char_u *p = skipdigits(*arg + 1);
5025 int get_float = FALSE;
5026
5027 /* We accept a float when the format matches
5028 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005029 * strict to avoid backwards compatibility problems.
5030 * Don't look for a float after the "." operator, so that
5031 * ":let vers = 1.2.3" doesn't fail. */
5032 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 get_float = TRUE;
5035 p = skipdigits(p + 2);
5036 if (*p == 'e' || *p == 'E')
5037 {
5038 ++p;
5039 if (*p == '-' || *p == '+')
5040 ++p;
5041 if (!vim_isdigit(*p))
5042 get_float = FALSE;
5043 else
5044 p = skipdigits(p + 1);
5045 }
5046 if (ASCII_ISALPHA(*p) || *p == '.')
5047 get_float = FALSE;
5048 }
5049 if (get_float)
5050 {
5051 float_T f;
5052
5053 *arg += string2float(*arg, &f);
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_FLOAT;
5057 rettv->vval.v_float = f;
5058 }
5059 }
5060 else
5061#endif
5062 {
5063 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5064 *arg += len;
5065 if (evaluate)
5066 {
5067 rettv->v_type = VAR_NUMBER;
5068 rettv->vval.v_number = n;
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * String constant: "string".
5076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079
5080 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 break;
5085
5086 /*
5087 * List: [expr, expr]
5088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Dictionary: {key: val, key: val}
5094 */
5095 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5096 break;
5097
5098 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005099 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005101 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /*
5105 * Environment variable: $VAR.
5106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /*
5111 * Register contents: @r.
5112 */
5113 case '@': ++*arg;
5114 if (evaluate)
5115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005117 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 if (**arg != NUL)
5120 ++*arg;
5121 break;
5122
5123 /*
5124 * nested expression: (expression).
5125 */
5126 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (**arg == ')')
5129 ++*arg;
5130 else if (ret == OK)
5131 {
5132 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 ret = FAIL;
5135 }
5136 break;
5137
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141
5142 if (ret == NOTDONE)
5143 {
5144 /*
5145 * Must be a variable or function name.
5146 * Can also be a curly-braces kind of name: {expr}.
5147 */
5148 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 if (alias != NULL)
5151 s = alias;
5152
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 ret = FAIL;
5155 else
5156 {
5157 if (**arg == '(') /* recursive! */
5158 {
5159 /* If "s" is the name of a variable of type VAR_FUNC
5160 * use its contents. */
5161 s = deref_func_name(s, &len);
5162
5163 /* Invoke the function. */
5164 ret = get_func_tv(s, len, rettv, arg,
5165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005167
5168 /* If evaluate is FALSE rettv->v_type was not set in
5169 * get_func_tv, but it's needed in handle_subscript() to parse
5170 * what follows. So set it here. */
5171 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5172 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005173 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005174 rettv->v_type = VAR_FUNC;
5175 }
5176
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 /* Stop the expression evaluation when immediately
5178 * aborting on error, or when an interrupt occurred or
5179 * an exception was thrown but not caught. */
5180 if (aborting())
5181 {
5182 if (ret == OK)
5183 clear_tv(rettv);
5184 ret = FAIL;
5185 }
5186 }
5187 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005189 else
5190 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005192 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 }
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 *arg = skipwhite(*arg);
5196
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005197 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5198 * expr(expr). */
5199 if (ret == OK)
5200 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 /*
5203 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5204 */
5205 if (ret == OK && evaluate && end_leader > start_leader)
5206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005208 int val = 0;
5209#ifdef FEAT_FLOAT
5210 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 if (rettv->v_type == VAR_FLOAT)
5213 f = rettv->vval.v_float;
5214 else
5215#endif
5216 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 clear_tv(rettv);
5220 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else
5223 {
5224 while (end_leader > start_leader)
5225 {
5226 --end_leader;
5227 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005228 {
5229#ifdef FEAT_FLOAT
5230 if (rettv->v_type == VAR_FLOAT)
5231 f = !f;
5232 else
5233#endif
5234 val = !val;
5235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005236 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005237 {
5238#ifdef FEAT_FLOAT
5239 if (rettv->v_type == VAR_FLOAT)
5240 f = -f;
5241 else
5242#endif
5243 val = -val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246#ifdef FEAT_FLOAT
5247 if (rettv->v_type == VAR_FLOAT)
5248 {
5249 clear_tv(rettv);
5250 rettv->vval.v_float = f;
5251 }
5252 else
5253#endif
5254 {
5255 clear_tv(rettv);
5256 rettv->v_type = VAR_NUMBER;
5257 rettv->vval.v_number = val;
5258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
5262 return ret;
5263}
5264
5265/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005266 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5267 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5269 */
5270 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005273 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276{
5277 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 long len = -1;
5281 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 if (rettv->v_type == VAR_FUNC
5286#ifdef FEAT_FLOAT
5287 || rettv->v_type == VAR_FLOAT
5288#endif
5289 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 if (verbose)
5292 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 return FAIL;
5294 }
5295
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 /*
5299 * dict.name
5300 */
5301 key = *arg + 1;
5302 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5303 ;
5304 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 }
5308 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 /*
5311 * something[idx]
5312 *
5313 * Get the (first) variable from inside the [].
5314 */
5315 *arg = skipwhite(*arg + 1);
5316 if (**arg == ':')
5317 empty1 = TRUE;
5318 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5319 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5321 {
5322 /* not a number or string */
5323 clear_tv(&var1);
5324 return FAIL;
5325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326
5327 /*
5328 * Get the second variable from inside the [:].
5329 */
5330 if (**arg == ':')
5331 {
5332 range = TRUE;
5333 *arg = skipwhite(*arg + 1);
5334 if (**arg == ']')
5335 empty2 = TRUE;
5336 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 if (!empty1)
5339 clear_tv(&var1);
5340 return FAIL;
5341 }
5342 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5343 {
5344 /* not a number or string */
5345 if (!empty1)
5346 clear_tv(&var1);
5347 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 return FAIL;
5349 }
5350 }
5351
5352 /* Check for the ']'. */
5353 if (**arg != ']')
5354 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 if (verbose)
5356 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 clear_tv(&var1);
5358 if (range)
5359 clear_tv(&var2);
5360 return FAIL;
5361 }
5362 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 }
5364
5365 if (evaluate)
5366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 n1 = 0;
5368 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 n1 = get_tv_number(&var1);
5371 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 }
5373 if (range)
5374 {
5375 if (empty2)
5376 n2 = -1;
5377 else
5378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 n2 = get_tv_number(&var2);
5380 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 }
5382 }
5383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
5386 case VAR_NUMBER:
5387 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 len = (long)STRLEN(s);
5390 if (range)
5391 {
5392 /* The resulting variable is a substring. If the indexes
5393 * are out of range the result is empty. */
5394 if (n1 < 0)
5395 {
5396 n1 = len + n1;
5397 if (n1 < 0)
5398 n1 = 0;
5399 }
5400 if (n2 < 0)
5401 n2 = len + n2;
5402 else if (n2 >= len)
5403 n2 = len;
5404 if (n1 >= len || n2 < 0 || n1 > n2)
5405 s = NULL;
5406 else
5407 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5408 }
5409 else
5410 {
5411 /* The resulting variable is a string of a single
5412 * character. If the index is too big or negative the
5413 * result is empty. */
5414 if (n1 >= len || n1 < 0)
5415 s = NULL;
5416 else
5417 s = vim_strnsave(s + n1, 1);
5418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 clear_tv(rettv);
5420 rettv->v_type = VAR_STRING;
5421 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 break;
5423
5424 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 if (n1 < 0)
5427 n1 = len + n1;
5428 if (!empty1 && (n1 < 0 || n1 >= len))
5429 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005430 /* For a range we allow invalid values and return an empty
5431 * list. A list index out of range is an error. */
5432 if (!range)
5433 {
5434 if (verbose)
5435 EMSGN(_(e_listidx), n1);
5436 return FAIL;
5437 }
5438 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440 if (range)
5441 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 list_T *l;
5443 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444
5445 if (n2 < 0)
5446 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005447 else if (n2 >= len)
5448 n2 = len - 1;
5449 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005450 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 l = list_alloc();
5452 if (l == NULL)
5453 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 n1 <= n2; ++n1)
5456 {
5457 if (list_append_tv(l, &item->li_tv) == FAIL)
5458 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005459 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 return FAIL;
5461 }
5462 item = item->li_next;
5463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(rettv);
5465 rettv->v_type = VAR_LIST;
5466 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005467 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 else
5470 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005471 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 clear_tv(rettv);
5473 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476
5477 case VAR_DICT:
5478 if (range)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 if (len == -1)
5483 clear_tv(&var1);
5484 return FAIL;
5485 }
5486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
5489 if (len == -1)
5490 {
5491 key = get_tv_string(&var1);
5492 if (*key == NUL)
5493 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005494 if (verbose)
5495 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 clear_tv(&var1);
5497 return FAIL;
5498 }
5499 }
5500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005501 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005503 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005504 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005505 if (len == -1)
5506 clear_tv(&var1);
5507 if (item == NULL)
5508 return FAIL;
5509
5510 copy_tv(&item->di_tv, &var1);
5511 clear_tv(rettv);
5512 *rettv = var1;
5513 }
5514 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 }
5516 }
5517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 return OK;
5519}
5520
5521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 * Get an option value.
5523 * "arg" points to the '&' or '+' before the option name.
5524 * "arg" is advanced to character after the option name.
5525 * Return OK or FAIL.
5526 */
5527 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 int evaluate;
5532{
5533 char_u *option_end;
5534 long numval;
5535 char_u *stringval;
5536 int opt_type;
5537 int c;
5538 int working = (**arg == '+'); /* has("+option") */
5539 int ret = OK;
5540 int opt_flags;
5541
5542 /*
5543 * Isolate the option name and find its value.
5544 */
5545 option_end = find_option_end(arg, &opt_flags);
5546 if (option_end == NULL)
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 EMSG2(_("E112: Option name missing: %s"), *arg);
5550 return FAIL;
5551 }
5552
5553 if (!evaluate)
5554 {
5555 *arg = option_end;
5556 return OK;
5557 }
5558
5559 c = *option_end;
5560 *option_end = NUL;
5561 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if (opt_type == -3) /* invalid name */
5565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 EMSG2(_("E113: Unknown option: %s"), *arg);
5568 ret = FAIL;
5569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
5572 if (opt_type == -2) /* hidden string option */
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv->v_type = VAR_STRING;
5575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else if (opt_type == -1) /* hidden number option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_NUMBER;
5580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == 1) /* number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else /* string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 }
5593 else if (working && (opt_type == -2 || opt_type == -1))
5594 ret = FAIL;
5595
5596 *option_end = c; /* put back for error messages */
5597 *arg = option_end;
5598
5599 return ret;
5600}
5601
5602/*
5603 * Allocate a variable for a string constant.
5604 * Return OK or FAIL.
5605 */
5606 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 int evaluate;
5611{
5612 char_u *p;
5613 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 int extra = 0;
5615
5616 /*
5617 * Find the end of the string, skipping backslashed characters.
5618 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (*p == '\\' && p[1] != NUL)
5622 {
5623 ++p;
5624 /* A "\<x>" form occupies at least 4 characters, and produces up
5625 * to 6 characters: reserve space for 2 extra */
5626 if (*p == '<')
5627 extra += 2;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630
5631 if (*p != '"')
5632 {
5633 EMSG2(_("E114: Missing quote: %s"), *arg);
5634 return FAIL;
5635 }
5636
5637 /* If only parsing, set *arg and return here */
5638 if (!evaluate)
5639 {
5640 *arg = p + 1;
5641 return OK;
5642 }
5643
5644 /*
5645 * Copy the string into allocated memory, handling backslashed
5646 * characters.
5647 */
5648 name = alloc((unsigned)(p - *arg + extra));
5649 if (name == NULL)
5650 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 rettv->v_type = VAR_STRING;
5652 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 if (*p == '\\')
5657 {
5658 switch (*++p)
5659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 case 'b': *name++ = BS; ++p; break;
5661 case 'e': *name++ = ESC; ++p; break;
5662 case 'f': *name++ = FF; ++p; break;
5663 case 'n': *name++ = NL; ++p; break;
5664 case 'r': *name++ = CAR; ++p; break;
5665 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 case 'X': /* hex: "\x1", "\x12" */
5668 case 'x':
5669 case 'u': /* Unicode: "\u0023" */
5670 case 'U':
5671 if (vim_isxdigit(p[1]))
5672 {
5673 int n, nr;
5674 int c = toupper(*p);
5675
5676 if (c == 'X')
5677 n = 2;
5678 else
5679 n = 4;
5680 nr = 0;
5681 while (--n >= 0 && vim_isxdigit(p[1]))
5682 {
5683 ++p;
5684 nr = (nr << 4) + hex2nr(*p);
5685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687#ifdef FEAT_MBYTE
5688 /* For "\u" store the number according to
5689 * 'encoding'. */
5690 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
5693#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 break;
5697
5698 /* octal: "\1", "\12", "\123" */
5699 case '0':
5700 case '1':
5701 case '2':
5702 case '3':
5703 case '4':
5704 case '5':
5705 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 case '7': *name = *p++ - '0';
5707 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name = (*name << 3) + *p++ - '0';
5710 if (*p >= '0' && *p <= '7')
5711 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715
5716 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 if (extra != 0)
5719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 break;
5722 }
5723 /* FALLTHROUGH */
5724
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 }
5729 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 *arg = p + 1;
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 return OK;
5737}
5738
5739/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * Return OK or FAIL.
5742 */
5743 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005744get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 int evaluate;
5748{
5749 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 int reduce = 0;
5752
5753 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 */
5756 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 break;
5762 ++reduce;
5763 ++p;
5764 }
5765 }
5766
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 return FAIL;
5771 }
5772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 if (!evaluate)
5775 {
5776 *arg = p + 1;
5777 return OK;
5778 }
5779
5780 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 */
5783 str = alloc((unsigned)((p - *arg) - reduce));
5784 if (str == NULL)
5785 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 rettv->v_type = VAR_STRING;
5787 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 break;
5795 ++p;
5796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 *arg = p + 1;
5801
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 return OK;
5803}
5804
5805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * Allocate a variable for a List and fill it from "*arg".
5807 * Return OK or FAIL.
5808 */
5809 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 int evaluate;
5814{
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 list_T *l = NULL;
5816 typval_T tv;
5817 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818
5819 if (evaluate)
5820 {
5821 l = list_alloc();
5822 if (l == NULL)
5823 return FAIL;
5824 }
5825
5826 *arg = skipwhite(*arg + 1);
5827 while (**arg != ']' && **arg != NUL)
5828 {
5829 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5830 goto failret;
5831 if (evaluate)
5832 {
5833 item = listitem_alloc();
5834 if (item != NULL)
5835 {
5836 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005837 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 list_append(l, item);
5839 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005840 else
5841 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 }
5843
5844 if (**arg == ']')
5845 break;
5846 if (**arg != ',')
5847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 goto failret;
5850 }
5851 *arg = skipwhite(*arg + 1);
5852 }
5853
5854 if (**arg != ']')
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857failret:
5858 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005859 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 return FAIL;
5861 }
5862
5863 *arg = skipwhite(*arg + 1);
5864 if (evaluate)
5865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005866 rettv->v_type = VAR_LIST;
5867 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 ++l->lv_refcount;
5869 }
5870
5871 return OK;
5872}
5873
5874/*
5875 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005876 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005878 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879list_alloc()
5880{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881 list_T *l;
5882
5883 l = (list_T *)alloc_clear(sizeof(list_T));
5884 if (l != NULL)
5885 {
5886 /* Prepend the list to the list of lists for garbage collection. */
5887 if (first_list != NULL)
5888 first_list->lv_used_prev = l;
5889 l->lv_used_prev = NULL;
5890 l->lv_used_next = first_list;
5891 first_list = l;
5892 }
5893 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894}
5895
5896/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005897 * Allocate an empty list for a return value.
5898 * Returns OK or FAIL.
5899 */
5900 static int
5901rettv_list_alloc(rettv)
5902 typval_T *rettv;
5903{
5904 list_T *l = list_alloc();
5905
5906 if (l == NULL)
5907 return FAIL;
5908
5909 rettv->vval.v_list = l;
5910 rettv->v_type = VAR_LIST;
5911 ++l->lv_refcount;
5912 return OK;
5913}
5914
5915/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 * Unreference a list: decrement the reference count and free it when it
5917 * becomes zero.
5918 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005919 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005923 if (l != NULL && --l->lv_refcount <= 0)
5924 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925}
5926
5927/*
5928 * Free a list, including all items it points to.
5929 * Ignores the reference count.
5930 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005931 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005932list_free(l, recurse)
5933 list_T *l;
5934 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935{
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005938 /* Remove the list from the list of lists for garbage collection. */
5939 if (l->lv_used_prev == NULL)
5940 first_list = l->lv_used_next;
5941 else
5942 l->lv_used_prev->lv_used_next = l->lv_used_next;
5943 if (l->lv_used_next != NULL)
5944 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5945
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005948 /* Remove the item before deleting it. */
5949 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005950 if (recurse || (item->li_tv.v_type != VAR_LIST
5951 && item->li_tv.v_type != VAR_DICT))
5952 clear_tv(&item->li_tv);
5953 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 }
5955 vim_free(l);
5956}
5957
5958/*
5959 * Allocate a list item.
5960 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005961 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962listitem_alloc()
5963{
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965}
5966
5967/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005968 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
5970 static void
5971listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 vim_free(item);
5976}
5977
5978/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005979 * Remove a list item from a List and free it. Also clears the value.
5980 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005981 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005982listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 list_T *l;
5984 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005985{
5986 list_remove(l, item, item);
5987 listitem_free(item);
5988}
5989
5990/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 * Get the number of items in a list.
5992 */
5993 static long
5994list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 if (l == NULL)
5998 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000}
6001
6002/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003 * Return TRUE when two lists have exactly the same values.
6004 */
6005 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 list_T *l1;
6008 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006014 if (l1 == NULL || l2 == NULL)
6015 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006016 if (l1 == l2)
6017 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 if (list_len(l1) != list_len(l2))
6019 return FALSE;
6020
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 for (item1 = l1->lv_first, item2 = l2->lv_first;
6022 item1 != NULL && item2 != NULL;
6023 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006024 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 return FALSE;
6026 return item1 == NULL && item2 == NULL;
6027}
6028
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006029#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6030 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006031/*
6032 * Return the dictitem that an entry in a hashtable points to.
6033 */
6034 dictitem_T *
6035dict_lookup(hi)
6036 hashitem_T *hi;
6037{
6038 return HI2DI(hi);
6039}
6040#endif
6041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 * Return TRUE when two dictionaries have exactly the same key/values.
6044 */
6045 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006046dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 dict_T *d1;
6048 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006050 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051{
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 hashitem_T *hi;
6053 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006056 if (d1 == NULL || d2 == NULL)
6057 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006058 if (d1 == d2)
6059 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 if (dict_len(d1) != dict_len(d2))
6061 return FALSE;
6062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006063 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 if (!HASHITEM_EMPTY(hi))
6067 {
6068 item2 = dict_find(d2, hi->hi_key, -1);
6069 if (item2 == NULL)
6070 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006072 return FALSE;
6073 --todo;
6074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 }
6076 return TRUE;
6077}
6078
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079static int tv_equal_recurse_limit;
6080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006084 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085 */
6086 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 typval_T *tv1;
6089 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090 int ic; /* ignore case */
6091 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092{
6093 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006094 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006096 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 * recursiveness to a limit. We guess they are equal then.
6103 * A fixed limit has the problem of still taking an awful long time.
6104 * Reduce the limit every time running into it. That should work fine for
6105 * deeply linked structures that are not recursively linked and catch
6106 * recursiveness quickly. */
6107 if (!recursive)
6108 tv_equal_recurse_limit = 1000;
6109 if (recursive_cnt >= tv_equal_recurse_limit)
6110 {
6111 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006117 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 ++recursive_cnt;
6119 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6120 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006121 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122
6123 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 ++recursive_cnt;
6125 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6126 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006127 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128
6129 case VAR_FUNC:
6130 return (tv1->vval.v_string != NULL
6131 && tv2->vval.v_string != NULL
6132 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6133
6134 case VAR_NUMBER:
6135 return tv1->vval.v_number == tv2->vval.v_number;
6136
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006137#ifdef FEAT_FLOAT
6138 case VAR_FLOAT:
6139 return tv1->vval.v_float == tv2->vval.v_float;
6140#endif
6141
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006142 case VAR_STRING:
6143 s1 = get_tv_string_buf(tv1, buf1);
6144 s2 = get_tv_string_buf(tv2, buf2);
6145 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006146 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147
6148 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006149 return TRUE;
6150}
6151
6152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 * Locate item with index "n" in list "l" and return it.
6154 * A negative index is counted from the end; -1 is the last item.
6155 * Returns NULL when "n" is out of range.
6156 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006157 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 long n;
6161{
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 long idx;
6164
6165 if (l == NULL)
6166 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006167
6168 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006170 n = l->lv_len + n;
6171
6172 /* Check for index out of range. */
6173 if (n < 0 || n >= l->lv_len)
6174 return NULL;
6175
6176 /* When there is a cached index may start search from there. */
6177 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006179 if (n < l->lv_idx / 2)
6180 {
6181 /* closest to the start of the list */
6182 item = l->lv_first;
6183 idx = 0;
6184 }
6185 else if (n > (l->lv_idx + l->lv_len) / 2)
6186 {
6187 /* closest to the end of the list */
6188 item = l->lv_last;
6189 idx = l->lv_len - 1;
6190 }
6191 else
6192 {
6193 /* closest to the cached index */
6194 item = l->lv_idx_item;
6195 idx = l->lv_idx;
6196 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 }
6198 else
6199 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 if (n < l->lv_len / 2)
6201 {
6202 /* closest to the start of the list */
6203 item = l->lv_first;
6204 idx = 0;
6205 }
6206 else
6207 {
6208 /* closest to the end of the list */
6209 item = l->lv_last;
6210 idx = l->lv_len - 1;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006213
6214 while (n > idx)
6215 {
6216 /* search forward */
6217 item = item->li_next;
6218 ++idx;
6219 }
6220 while (n < idx)
6221 {
6222 /* search backward */
6223 item = item->li_prev;
6224 --idx;
6225 }
6226
6227 /* cache the used index */
6228 l->lv_idx = idx;
6229 l->lv_idx_item = item;
6230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 return item;
6232}
6233
6234/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006235 * Get list item "l[idx]" as a number.
6236 */
6237 static long
6238list_find_nr(l, idx, errorp)
6239 list_T *l;
6240 long idx;
6241 int *errorp; /* set to TRUE when something wrong */
6242{
6243 listitem_T *li;
6244
6245 li = list_find(l, idx);
6246 if (li == NULL)
6247 {
6248 if (errorp != NULL)
6249 *errorp = TRUE;
6250 return -1L;
6251 }
6252 return get_tv_number_chk(&li->li_tv, errorp);
6253}
6254
6255/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006256 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6257 */
6258 char_u *
6259list_find_str(l, idx)
6260 list_T *l;
6261 long idx;
6262{
6263 listitem_T *li;
6264
6265 li = list_find(l, idx - 1);
6266 if (li == NULL)
6267 {
6268 EMSGN(_(e_listidx), idx);
6269 return NULL;
6270 }
6271 return get_tv_string(&li->li_tv);
6272}
6273
6274/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275 * Locate "item" list "l" and return its index.
6276 * Returns -1 when "item" is not in the list.
6277 */
6278 static long
6279list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 list_T *l;
6281 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006282{
6283 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006285
6286 if (l == NULL)
6287 return -1;
6288 idx = 0;
6289 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6290 ++idx;
6291 if (li == NULL)
6292 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006293 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006294}
6295
6296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 * Append item "item" to the end of list "l".
6298 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006299 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 list_T *l;
6302 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303{
6304 if (l->lv_last == NULL)
6305 {
6306 /* empty list */
6307 l->lv_first = item;
6308 l->lv_last = item;
6309 item->li_prev = NULL;
6310 }
6311 else
6312 {
6313 l->lv_last->li_next = item;
6314 item->li_prev = l->lv_last;
6315 l->lv_last = item;
6316 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 item->li_next = NULL;
6319}
6320
6321/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006323 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006325 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *l;
6328 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331
Bram Moolenaar05159a02005-02-26 23:04:13 +00006332 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 copy_tv(tv, &li->li_tv);
6335 list_append(l, li);
6336 return OK;
6337}
6338
6339/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006340 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006341 * Return FAIL when out of memory.
6342 */
6343 int
6344list_append_dict(list, dict)
6345 list_T *list;
6346 dict_T *dict;
6347{
6348 listitem_T *li = listitem_alloc();
6349
6350 if (li == NULL)
6351 return FAIL;
6352 li->li_tv.v_type = VAR_DICT;
6353 li->li_tv.v_lock = 0;
6354 li->li_tv.vval.v_dict = dict;
6355 list_append(list, li);
6356 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 return OK;
6358}
6359
6360/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363 * Returns FAIL when out of memory.
6364 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006365 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006366list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006367 list_T *l;
6368 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006369 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006370{
6371 listitem_T *li = listitem_alloc();
6372
6373 if (li == NULL)
6374 return FAIL;
6375 list_append(l, li);
6376 li->li_tv.v_type = VAR_STRING;
6377 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006378 if (str == NULL)
6379 li->li_tv.vval.v_string = NULL;
6380 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006381 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 return FAIL;
6383 return OK;
6384}
6385
6386/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006387 * Append "n" to list "l".
6388 * Returns FAIL when out of memory.
6389 */
6390 static int
6391list_append_number(l, n)
6392 list_T *l;
6393 varnumber_T n;
6394{
6395 listitem_T *li;
6396
6397 li = listitem_alloc();
6398 if (li == NULL)
6399 return FAIL;
6400 li->li_tv.v_type = VAR_NUMBER;
6401 li->li_tv.v_lock = 0;
6402 li->li_tv.vval.v_number = n;
6403 list_append(l, li);
6404 return OK;
6405}
6406
6407/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 * If "item" is NULL append at the end.
6410 * Return FAIL when out of memory.
6411 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006412 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 list_T *l;
6415 typval_T *tv;
6416 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417{
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419
6420 if (ni == NULL)
6421 return FAIL;
6422 copy_tv(tv, &ni->li_tv);
6423 if (item == NULL)
6424 /* Append new item at end of list. */
6425 list_append(l, ni);
6426 else
6427 {
6428 /* Insert new item before existing item. */
6429 ni->li_prev = item->li_prev;
6430 ni->li_next = item;
6431 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 ++l->lv_idx;
6435 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006437 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439 l->lv_idx_item = NULL;
6440 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 }
6444 return OK;
6445}
6446
6447/*
6448 * Extend "l1" with "l2".
6449 * If "bef" is NULL append at the end, otherwise insert before this item.
6450 * Returns FAIL when out of memory.
6451 */
6452 static int
6453list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 list_T *l1;
6455 list_T *l2;
6456 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006459 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006461 /* We also quit the loop when we have inserted the original item count of
6462 * the list, avoid a hang when we extend a list with itself. */
6463 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6465 return FAIL;
6466 return OK;
6467}
6468
6469/*
6470 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6471 * Return FAIL when out of memory.
6472 */
6473 static int
6474list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 list_T *l1;
6476 list_T *l2;
6477 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478{
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006481 if (l1 == NULL || l2 == NULL)
6482 return FAIL;
6483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486 if (l == NULL)
6487 return FAIL;
6488 tv->v_type = VAR_LIST;
6489 tv->vval.v_list = l;
6490
6491 /* append all items from the second list */
6492 return list_extend(l, l2, NULL);
6493}
6494
6495/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499 * Returns NULL when out of memory.
6500 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006502list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 list_T *copy;
6508 listitem_T *item;
6509 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510
6511 if (orig == NULL)
6512 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513
6514 copy = list_alloc();
6515 if (copy != NULL)
6516 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 if (copyID != 0)
6518 {
6519 /* Do this before adding the items, because one of the items may
6520 * refer back to this list. */
6521 orig->lv_copyID = copyID;
6522 orig->lv_copylist = copy;
6523 }
6524 for (item = orig->lv_first; item != NULL && !got_int;
6525 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 {
6527 ni = listitem_alloc();
6528 if (ni == NULL)
6529 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 {
6532 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6533 {
6534 vim_free(ni);
6535 break;
6536 }
6537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006539 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 list_append(copy, ni);
6541 }
6542 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 if (item != NULL)
6544 {
6545 list_unref(copy);
6546 copy = NULL;
6547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 }
6549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 return copy;
6551}
6552
6553/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006555 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006557 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006558list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l;
6560 listitem_T *item;
6561 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562{
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 /* notify watchers */
6566 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 list_fix_watch(l, ip);
6570 if (ip == item2)
6571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573
6574 if (item2->li_next == NULL)
6575 l->lv_last = item->li_prev;
6576 else
6577 item2->li_next->li_prev = item->li_prev;
6578 if (item->li_prev == NULL)
6579 l->lv_first = item2->li_next;
6580 else
6581 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583}
6584
6585/*
6586 * Return an allocated string with the string representation of a list.
6587 * May return NULL.
6588 */
6589 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006592 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
6594 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 if (tv->vval.v_list == NULL)
6597 return NULL;
6598 ga_init2(&ga, (int)sizeof(char), 80);
6599 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006600 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 {
6602 vim_free(ga.ga_data);
6603 return NULL;
6604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 ga_append(&ga, ']');
6606 ga_append(&ga, NUL);
6607 return (char_u *)ga.ga_data;
6608}
6609
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006610typedef struct join_S {
6611 char_u *s;
6612 char_u *tofree;
6613} join_T;
6614
6615 static int
6616list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6617 garray_T *gap; /* to store the result in */
6618 list_T *l;
6619 char_u *sep;
6620 int echo_style;
6621 int copyID;
6622 garray_T *join_gap; /* to keep each list item string */
6623{
6624 int i;
6625 join_T *p;
6626 int len;
6627 int sumlen = 0;
6628 int first = TRUE;
6629 char_u *tofree;
6630 char_u numbuf[NUMBUFLEN];
6631 listitem_T *item;
6632 char_u *s;
6633
6634 /* Stringify each item in the list. */
6635 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6636 {
6637 if (echo_style)
6638 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6639 else
6640 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6641 if (s == NULL)
6642 return FAIL;
6643
6644 len = (int)STRLEN(s);
6645 sumlen += len;
6646
6647 ga_grow(join_gap, 1);
6648 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6649 if (tofree != NULL || s != numbuf)
6650 {
6651 p->s = s;
6652 p->tofree = tofree;
6653 }
6654 else
6655 {
6656 p->s = vim_strnsave(s, len);
6657 p->tofree = p->s;
6658 }
6659
6660 line_breakcheck();
6661 }
6662
6663 /* Allocate result buffer with its total size, avoid re-allocation and
6664 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6665 if (join_gap->ga_len >= 2)
6666 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6667 if (ga_grow(gap, sumlen + 2) == FAIL)
6668 return FAIL;
6669
6670 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6671 {
6672 if (first)
6673 first = FALSE;
6674 else
6675 ga_concat(gap, sep);
6676 p = ((join_T *)join_gap->ga_data) + i;
6677
6678 if (p->s != NULL)
6679 ga_concat(gap, p->s);
6680 line_breakcheck();
6681 }
6682
6683 return OK;
6684}
6685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006688 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006692list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006694 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006696 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006697 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699 garray_T join_ga;
6700 int retval;
6701 join_T *p;
6702 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6705 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6706
6707 /* Dispose each item in join_ga. */
6708 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710 p = (join_T *)join_ga.ga_data;
6711 for (i = 0; i < join_ga.ga_len; ++i)
6712 {
6713 vim_free(p->tofree);
6714 ++p;
6715 }
6716 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718
6719 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720}
6721
6722/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006723 * Garbage collection for lists and dictionaries.
6724 *
6725 * We use reference counts to be able to free most items right away when they
6726 * are no longer used. But for composite items it's possible that it becomes
6727 * unused while the reference count is > 0: When there is a recursive
6728 * reference. Example:
6729 * :let l = [1, 2, 3]
6730 * :let d = {9: l}
6731 * :let l[1] = d
6732 *
6733 * Since this is quite unusual we handle this with garbage collection: every
6734 * once in a while find out which lists and dicts are not referenced from any
6735 * variable.
6736 *
6737 * Here is a good reference text about garbage collection (refers to Python
6738 * but it applies to all reference-counting mechanisms):
6739 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006740 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741
6742/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 * Do garbage collection for lists and dicts.
6744 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 int
6747garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006748{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 buf_T *buf;
6751 win_T *wp;
6752 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006753 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006754 int did_free;
6755 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006756#ifdef FEAT_WINDOWS
6757 tabpage_T *tp;
6758#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006760 /* Only do this once. */
6761 want_garbage_collect = FALSE;
6762 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006763 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006764
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006765 /* We advance by two because we add one for items referenced through
6766 * previous_funccal. */
6767 current_copyID += COPYID_INC;
6768 copyID = current_copyID;
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /*
6771 * 1. Go through all accessible variables and mark all lists and dicts
6772 * with copyID.
6773 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774
6775 /* Don't free variables in the previous_funccal list unless they are only
6776 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006777 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006778 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6779 {
6780 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6781 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6782 }
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /* script-local variables */
6785 for (i = 1; i <= ga_scripts.ga_len; ++i)
6786 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6787
6788 /* buffer-local variables */
6789 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6790 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6791
6792 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6795
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006796#ifdef FEAT_WINDOWS
6797 /* tabpage-local variables */
6798 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6799 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6800#endif
6801
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006802 /* global variables */
6803 set_ref_in_ht(&globvarht, copyID);
6804
6805 /* function-local variables */
6806 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6807 {
6808 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6809 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6810 }
6811
Bram Moolenaard812df62008-11-09 12:46:09 +00006812 /* v: vars */
6813 set_ref_in_ht(&vimvarht, copyID);
6814
Bram Moolenaar1dced572012-04-05 16:54:08 +02006815#ifdef FEAT_LUA
6816 set_ref_in_lua(copyID);
6817#endif
6818
Bram Moolenaardb913952012-06-29 12:54:53 +02006819#ifdef FEAT_PYTHON
6820 set_ref_in_python(copyID);
6821#endif
6822
6823#ifdef FEAT_PYTHON3
6824 set_ref_in_python3(copyID);
6825#endif
6826
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006827 /*
6828 * 2. Free lists and dictionaries that are not referenced.
6829 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 did_free = free_unref_items(copyID);
6831
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006832 /*
6833 * 3. Check if any funccal can be freed now.
6834 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 for (pfc = &previous_funccal; *pfc != NULL; )
6836 {
6837 if (can_free_funccal(*pfc, copyID))
6838 {
6839 fc = *pfc;
6840 *pfc = fc->caller;
6841 free_funccal(fc, TRUE);
6842 did_free = TRUE;
6843 did_free_funccal = TRUE;
6844 }
6845 else
6846 pfc = &(*pfc)->caller;
6847 }
6848 if (did_free_funccal)
6849 /* When a funccal was freed some more items might be garbage
6850 * collected, so run again. */
6851 (void)garbage_collect();
6852
6853 return did_free;
6854}
6855
6856/*
6857 * Free lists and dictionaries that are no longer referenced.
6858 */
6859 static int
6860free_unref_items(copyID)
6861 int copyID;
6862{
6863 dict_T *dd;
6864 list_T *ll;
6865 int did_free = FALSE;
6866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 */
6870 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006873 /* Free the Dictionary and ordinary items it contains, but don't
6874 * recurse into Lists and Dictionaries, they will be in the list
6875 * of dicts or list of lists. */
6876 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 did_free = TRUE;
6878
6879 /* restart, next dict may also have been freed */
6880 dd = first_dict;
6881 }
6882 else
6883 dd = dd->dv_used_next;
6884
6885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of lists and free items without the copyID.
6887 * But don't free a list that has a watcher (used in a for loop), these
6888 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 */
6890 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6892 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006894 /* Free the List and ordinary items it contains, but don't recurse
6895 * into Lists and Dictionaries, they will be in the list of dicts
6896 * or list of lists. */
6897 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 did_free = TRUE;
6899
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006900 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 ll = first_list;
6902 }
6903 else
6904 ll = ll->lv_used_next;
6905
6906 return did_free;
6907}
6908
6909/*
6910 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6911 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006912 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913set_ref_in_ht(ht, copyID)
6914 hashtab_T *ht;
6915 int copyID;
6916{
6917 int todo;
6918 hashitem_T *hi;
6919
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006920 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 for (hi = ht->ht_array; todo > 0; ++hi)
6922 if (!HASHITEM_EMPTY(hi))
6923 {
6924 --todo;
6925 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6926 }
6927}
6928
6929/*
6930 * Mark all lists and dicts referenced through list "l" with "copyID".
6931 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006932 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933set_ref_in_list(l, copyID)
6934 list_T *l;
6935 int copyID;
6936{
6937 listitem_T *li;
6938
6939 for (li = l->lv_first; li != NULL; li = li->li_next)
6940 set_ref_in_item(&li->li_tv, copyID);
6941}
6942
6943/*
6944 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6945 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006946 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947set_ref_in_item(tv, copyID)
6948 typval_T *tv;
6949 int copyID;
6950{
6951 dict_T *dd;
6952 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953
6954 switch (tv->v_type)
6955 {
6956 case VAR_DICT:
6957 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006958 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /* Didn't see this dict yet. */
6961 dd->dv_copyID = copyID;
6962 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965
6966 case VAR_LIST:
6967 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006968 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006969 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 /* Didn't see this list yet. */
6971 ll->lv_copyID = copyID;
6972 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977}
6978
6979/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980 * Allocate an empty header for a dictionary.
6981 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006982 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983dict_alloc()
6984{
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006989 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006990 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 if (first_dict != NULL)
6992 first_dict->dv_used_prev = d;
6993 d->dv_used_next = first_dict;
6994 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006995 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996
Bram Moolenaar33570922005-01-25 22:26:29 +00006997 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006998 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006999 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007003 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004}
7005
7006/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007007 * Allocate an empty dict for a return value.
7008 * Returns OK or FAIL.
7009 */
7010 static int
7011rettv_dict_alloc(rettv)
7012 typval_T *rettv;
7013{
7014 dict_T *d = dict_alloc();
7015
7016 if (d == NULL)
7017 return FAIL;
7018
7019 rettv->vval.v_dict = d;
7020 rettv->v_type = VAR_DICT;
7021 ++d->dv_refcount;
7022 return OK;
7023}
7024
7025
7026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 * Unreference a Dictionary: decrement the reference count and free it when it
7028 * becomes zero.
7029 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007030 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007034 if (d != NULL && --d->dv_refcount <= 0)
7035 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036}
7037
7038/*
7039 * Free a Dictionary, including all items it contains.
7040 * Ignores the reference count.
7041 */
7042 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007043dict_free(d, recurse)
7044 dict_T *d;
7045 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007046{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 /* Remove the dict from the list of dicts for garbage collection. */
7052 if (d->dv_used_prev == NULL)
7053 first_dict = d->dv_used_next;
7054 else
7055 d->dv_used_prev->dv_used_next = d->dv_used_next;
7056 if (d->dv_used_next != NULL)
7057 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7058
7059 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007060 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007061 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064 if (!HASHITEM_EMPTY(hi))
7065 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007066 /* Remove the item before deleting it, just in case there is
7067 * something recursive causing trouble. */
7068 di = HI2DI(hi);
7069 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007070 if (recurse || (di->di_tv.v_type != VAR_LIST
7071 && di->di_tv.v_type != VAR_DICT))
7072 clear_tv(&di->di_tv);
7073 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 --todo;
7075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 vim_free(d);
7079}
7080
7081/*
7082 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 * The "key" is copied to the new item.
7084 * Note that the value of the item "di_tv" still needs to be initialized!
7085 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007087 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088dictitem_alloc(key)
7089 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090{
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007093 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007097 di->di_flags = 0;
7098 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100}
7101
7102/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 * Make a copy of a Dictionary item.
7104 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7112 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113 if (di != NULL)
7114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007116 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 copy_tv(&org->di_tv, &di->di_tv);
7118 }
7119 return di;
7120}
7121
7122/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 * Remove item "item" from Dictionary "dict" and free it.
7124 */
7125 static void
7126dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dict_T *dict;
7128 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129{
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 if (HASHITEM_EMPTY(hi))
7134 EMSG2(_(e_intern2), "dictitem_remove()");
7135 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 dictitem_free(item);
7138}
7139
7140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 * Free a dict item. Also clears the value.
7142 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007143 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 clear_tv(&item->di_tv);
7148 vim_free(item);
7149}
7150
7151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7153 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007154 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 * Returns NULL when out of memory.
7156 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007161 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162{
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dict_T *copy;
7164 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167
7168 if (orig == NULL)
7169 return NULL;
7170
7171 copy = dict_alloc();
7172 if (copy != NULL)
7173 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007174 if (copyID != 0)
7175 {
7176 orig->dv_copyID = copyID;
7177 orig->dv_copydict = copy;
7178 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007179 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 --todo;
7185
7186 di = dictitem_alloc(hi->hi_key);
7187 if (di == NULL)
7188 break;
7189 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 {
7191 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7192 copyID) == FAIL)
7193 {
7194 vim_free(di);
7195 break;
7196 }
7197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 else
7199 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7200 if (dict_add(copy, di) == FAIL)
7201 {
7202 dictitem_free(di);
7203 break;
7204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007209 if (todo > 0)
7210 {
7211 dict_unref(copy);
7212 copy = NULL;
7213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 }
7215
7216 return copy;
7217}
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007223 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 dict_T *d;
7226 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229}
7230
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007232 * Add a number or string entry to dictionary "d".
7233 * When "str" is NULL use number "nr", otherwise use "str".
7234 * Returns FAIL when out of memory and when key already exists.
7235 */
7236 int
7237dict_add_nr_str(d, key, nr, str)
7238 dict_T *d;
7239 char *key;
7240 long nr;
7241 char_u *str;
7242{
7243 dictitem_T *item;
7244
7245 item = dictitem_alloc((char_u *)key);
7246 if (item == NULL)
7247 return FAIL;
7248 item->di_tv.v_lock = 0;
7249 if (str == NULL)
7250 {
7251 item->di_tv.v_type = VAR_NUMBER;
7252 item->di_tv.vval.v_number = nr;
7253 }
7254 else
7255 {
7256 item->di_tv.v_type = VAR_STRING;
7257 item->di_tv.vval.v_string = vim_strsave(str);
7258 }
7259 if (dict_add(d, item) == FAIL)
7260 {
7261 dictitem_free(item);
7262 return FAIL;
7263 }
7264 return OK;
7265}
7266
7267/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007268 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007269 * Returns FAIL when out of memory and when key already exists.
7270 */
7271 int
7272dict_add_list(d, key, list)
7273 dict_T *d;
7274 char *key;
7275 list_T *list;
7276{
7277 dictitem_T *item;
7278
7279 item = dictitem_alloc((char_u *)key);
7280 if (item == NULL)
7281 return FAIL;
7282 item->di_tv.v_lock = 0;
7283 item->di_tv.v_type = VAR_LIST;
7284 item->di_tv.vval.v_list = list;
7285 if (dict_add(d, item) == FAIL)
7286 {
7287 dictitem_free(item);
7288 return FAIL;
7289 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007290 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007291 return OK;
7292}
7293
7294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 * Get the number of items in a Dictionary.
7296 */
7297 static long
7298dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 if (d == NULL)
7302 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304}
7305
7306/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 * Find item "key[len]" in Dictionary "d".
7308 * If "len" is negative use strlen(key).
7309 * Returns NULL when not found.
7310 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007311 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 char_u *key;
7315 int len;
7316{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317#define AKEYLEN 200
7318 char_u buf[AKEYLEN];
7319 char_u *akey;
7320 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (len < 0)
7324 akey = key;
7325 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007326 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 tofree = akey = vim_strnsave(key, len);
7328 if (akey == NULL)
7329 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007330 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 else
7332 {
7333 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007334 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 akey = buf;
7336 }
7337
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 vim_free(tofree);
7340 if (HASHITEM_EMPTY(hi))
7341 return NULL;
7342 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343}
7344
7345/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 * Get a string item from a dictionary.
7347 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007348 * Returns NULL if the entry doesn't exist or out of memory.
7349 */
7350 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352 dict_T *d;
7353 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355{
7356 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007357 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007358
7359 di = dict_find(d, key, -1);
7360 if (di == NULL)
7361 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007362 s = get_tv_string(&di->di_tv);
7363 if (save && s != NULL)
7364 s = vim_strsave(s);
7365 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366}
7367
7368/*
7369 * Get a number item from a dictionary.
7370 * Returns 0 if the entry doesn't exist or out of memory.
7371 */
7372 long
7373get_dict_number(d, key)
7374 dict_T *d;
7375 char_u *key;
7376{
7377 dictitem_T *di;
7378
7379 di = dict_find(d, key, -1);
7380 if (di == NULL)
7381 return 0;
7382 return get_tv_number(&di->di_tv);
7383}
7384
7385/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 * Return an allocated string with the string representation of a Dictionary.
7387 * May return NULL.
7388 */
7389 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393{
7394 garray_T ga;
7395 int first = TRUE;
7396 char_u *tofree;
7397 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 return NULL;
7405 ga_init2(&ga, (int)sizeof(char), 80);
7406 ga_append(&ga, '{');
7407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007408 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007409 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413 --todo;
7414
7415 if (first)
7416 first = FALSE;
7417 else
7418 ga_concat(&ga, (char_u *)", ");
7419
7420 tofree = string_quote(hi->hi_key, FALSE);
7421 if (tofree != NULL)
7422 {
7423 ga_concat(&ga, tofree);
7424 vim_free(tofree);
7425 }
7426 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007427 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428 if (s != NULL)
7429 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007431 if (s == NULL)
7432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007435 if (todo > 0)
7436 {
7437 vim_free(ga.ga_data);
7438 return NULL;
7439 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440
7441 ga_append(&ga, '}');
7442 ga_append(&ga, NUL);
7443 return (char_u *)ga.ga_data;
7444}
7445
7446/*
7447 * Allocate a variable for a Dictionary and fill it from "*arg".
7448 * Return OK or FAIL. Returns NOTDONE for {expr}.
7449 */
7450 static int
7451get_dict_tv(arg, rettv, evaluate)
7452 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 int evaluate;
7455{
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 dict_T *d = NULL;
7457 typval_T tvkey;
7458 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007459 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 /*
7465 * First check if it's not a curly-braces thing: {expr}.
7466 * Must do this without evaluating, otherwise a function may be called
7467 * twice. Unfortunately this means we need to call eval1() twice for the
7468 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 if (*start != '}')
7472 {
7473 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7474 return FAIL;
7475 if (*start == '}')
7476 return NOTDONE;
7477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478
7479 if (evaluate)
7480 {
7481 d = dict_alloc();
7482 if (d == NULL)
7483 return FAIL;
7484 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 tvkey.v_type = VAR_UNKNOWN;
7486 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 *arg = skipwhite(*arg + 1);
7489 while (**arg != '}' && **arg != NUL)
7490 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 goto failret;
7493 if (**arg != ':')
7494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007495 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 goto failret;
7498 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007499 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 key = get_tv_string_buf_chk(&tvkey, buf);
7502 if (key == NULL || *key == NUL)
7503 {
7504 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7505 if (key != NULL)
7506 EMSG(_(e_emptykey));
7507 clear_tv(&tvkey);
7508 goto failret;
7509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511
7512 *arg = skipwhite(*arg + 1);
7513 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7514 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007515 if (evaluate)
7516 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 goto failret;
7518 }
7519 if (evaluate)
7520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 if (item != NULL)
7523 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007524 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 clear_tv(&tv);
7527 goto failret;
7528 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 item = dictitem_alloc(key);
7530 clear_tv(&tvkey);
7531 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007534 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 if (dict_add(d, item) == FAIL)
7536 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 }
7538 }
7539
7540 if (**arg == '}')
7541 break;
7542 if (**arg != ',')
7543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007544 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 goto failret;
7546 }
7547 *arg = skipwhite(*arg + 1);
7548 }
7549
7550 if (**arg != '}')
7551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007552 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553failret:
7554 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007555 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 return FAIL;
7557 }
7558
7559 *arg = skipwhite(*arg + 1);
7560 if (evaluate)
7561 {
7562 rettv->v_type = VAR_DICT;
7563 rettv->vval.v_dict = d;
7564 ++d->dv_refcount;
7565 }
7566
7567 return OK;
7568}
7569
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 * Return a string with the string representation of a variable.
7572 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007573 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007576 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007577 */
7578 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007579echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007582 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 static int recurse = 0;
7586 char_u *r = NULL;
7587
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007590 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 *tofree = NULL;
7592 return NULL;
7593 }
7594 ++recurse;
7595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007596 switch (tv->v_type)
7597 {
7598 case VAR_FUNC:
7599 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 r = tv->vval.v_string;
7601 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007603 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604 if (tv->vval.v_list == NULL)
7605 {
7606 *tofree = NULL;
7607 r = NULL;
7608 }
7609 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7610 {
7611 *tofree = NULL;
7612 r = (char_u *)"[...]";
7613 }
7614 else
7615 {
7616 tv->vval.v_list->lv_copyID = copyID;
7617 *tofree = list2string(tv, copyID);
7618 r = *tofree;
7619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007621
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007623 if (tv->vval.v_dict == NULL)
7624 {
7625 *tofree = NULL;
7626 r = NULL;
7627 }
7628 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7629 {
7630 *tofree = NULL;
7631 r = (char_u *)"{...}";
7632 }
7633 else
7634 {
7635 tv->vval.v_dict->dv_copyID = copyID;
7636 *tofree = dict2string(tv, copyID);
7637 r = *tofree;
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 case VAR_STRING:
7642 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 *tofree = NULL;
7644 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007646
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007647#ifdef FEAT_FLOAT
7648 case VAR_FLOAT:
7649 *tofree = NULL;
7650 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7651 r = numbuf;
7652 break;
7653#endif
7654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659
7660 --recurse;
7661 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662}
7663
7664/*
7665 * Return a string with the string representation of a variable.
7666 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7667 * "numbuf" is used for a number.
7668 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007669 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 */
7671 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007672tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 char_u **tofree;
7675 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677{
7678 switch (tv->v_type)
7679 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 case VAR_FUNC:
7681 *tofree = string_quote(tv->vval.v_string, TRUE);
7682 return *tofree;
7683 case VAR_STRING:
7684 *tofree = string_quote(tv->vval.v_string, FALSE);
7685 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007686#ifdef FEAT_FLOAT
7687 case VAR_FLOAT:
7688 *tofree = NULL;
7689 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7690 return numbuf;
7691#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007699 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700}
7701
7702/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 * Return string "str" in ' quotes, doubling ' characters.
7704 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 */
7707 static char_u *
7708string_quote(str, function)
7709 char_u *str;
7710 int function;
7711{
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 char_u *p, *r, *s;
7714
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 len = (function ? 13 : 3);
7716 if (str != NULL)
7717 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007718 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 for (p = str; *p != NUL; mb_ptr_adv(p))
7720 if (*p == '\'')
7721 ++len;
7722 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 s = r = alloc(len);
7724 if (r != NULL)
7725 {
7726 if (function)
7727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 r += 10;
7730 }
7731 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 if (str != NULL)
7734 for (p = str; *p != NUL; )
7735 {
7736 if (*p == '\'')
7737 *r++ = '\'';
7738 MB_COPY_CHAR(p, r);
7739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 if (function)
7742 *r++ = ')';
7743 *r++ = NUL;
7744 }
7745 return s;
7746}
7747
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007748#ifdef FEAT_FLOAT
7749/*
7750 * Convert the string "text" to a floating point number.
7751 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7752 * this always uses a decimal point.
7753 * Returns the length of the text that was consumed.
7754 */
7755 static int
7756string2float(text, value)
7757 char_u *text;
7758 float_T *value; /* result stored here */
7759{
7760 char *s = (char *)text;
7761 float_T f;
7762
7763 f = strtod(s, &s);
7764 *value = f;
7765 return (int)((char_u *)s - text);
7766}
7767#endif
7768
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 * Get the value of an environment variable.
7771 * "arg" is pointing to the '$'. It is advanced to after the name.
7772 * If the environment variable was not set, silently assume it is empty.
7773 * Always return OK.
7774 */
7775 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 int evaluate;
7780{
7781 char_u *string = NULL;
7782 int len;
7783 int cc;
7784 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
7787 ++*arg;
7788 name = *arg;
7789 len = get_env_len(arg);
7790 if (evaluate)
7791 {
7792 if (len != 0)
7793 {
7794 cc = name[len];
7795 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 /* first try vim_getenv(), fast for normal environment vars */
7797 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799 {
7800 if (!mustfree)
7801 string = vim_strsave(string);
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
7804 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007805 if (mustfree)
7806 vim_free(string);
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 /* next try expanding things like $VIM and ${HOME} */
7809 string = expand_env_save(name - 1);
7810 if (string != NULL && *string == '$')
7811 {
7812 vim_free(string);
7813 string = NULL;
7814 }
7815 }
7816 name[len] = cc;
7817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818 rettv->v_type = VAR_STRING;
7819 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821
7822 return OK;
7823}
7824
7825/*
7826 * Array with names and number of arguments of all internal functions
7827 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7828 */
7829static struct fst
7830{
7831 char *f_name; /* function name */
7832 char f_min_argc; /* minimal number of arguments */
7833 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007835 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836} functions[] =
7837{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
7839 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007840 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007841#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007842 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007843 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"append", 2, 2, f_append},
7845 {"argc", 0, 0, f_argc},
7846 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007847 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007851 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007854 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"bufexists", 1, 1, f_bufexists},
7856 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7857 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7858 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7859 {"buflisted", 1, 1, f_buflisted},
7860 {"bufloaded", 1, 1, f_bufloaded},
7861 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007862 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"bufwinnr", 1, 1, f_bufwinnr},
7864 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007865 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#ifdef FEAT_FLOAT
7868 {"ceil", 1, 1, f_ceil},
7869#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007870 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007871 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007873 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007875#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007876 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007877 {"complete_add", 1, 1, f_complete_add},
7878 {"complete_check", 0, 0, f_complete_check},
7879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#ifdef FEAT_FLOAT
7883 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007884 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007888 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007889 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"delete", 1, 1, f_delete},
7891 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007892 {"diff_filler", 1, 1, f_diff_filler},
7893 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007894 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"eventhandler", 0, 0, f_eventhandler},
7898 {"executable", 1, 1, f_executable},
7899 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007900#ifdef FEAT_FLOAT
7901 {"exp", 1, 1, f_exp},
7902#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007903 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007904 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007905 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7907 {"filereadable", 1, 1, f_filereadable},
7908 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007910 {"finddir", 1, 3, f_finddir},
7911 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#ifdef FEAT_FLOAT
7913 {"float2nr", 1, 1, f_float2nr},
7914 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007915 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007917 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"fnamemodify", 2, 2, f_fnamemodify},
7919 {"foldclosed", 1, 1, f_foldclosed},
7920 {"foldclosedend", 1, 1, f_foldclosedend},
7921 {"foldlevel", 1, 1, f_foldlevel},
7922 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007923 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007926 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007928 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007929 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"getchar", 0, 1, f_getchar},
7931 {"getcharmod", 0, 0, f_getcharmod},
7932 {"getcmdline", 0, 0, f_getcmdline},
7933 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007934 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007936 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007937 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"getfsize", 1, 1, f_getfsize},
7939 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007940 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007941 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007942 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007943 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007944 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007945 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007946 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007947 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007949 {"gettabvar", 2, 3, f_gettabvar},
7950 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"getwinposx", 0, 0, f_getwinposx},
7952 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007953 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007954 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007955 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007957 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007958 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007959 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7961 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7962 {"histadd", 2, 2, f_histadd},
7963 {"histdel", 1, 2, f_histdel},
7964 {"histget", 1, 2, f_histget},
7965 {"histnr", 1, 1, f_histnr},
7966 {"hlID", 1, 1, f_hlID},
7967 {"hlexists", 1, 1, f_hlexists},
7968 {"hostname", 0, 0, f_hostname},
7969 {"iconv", 3, 3, f_iconv},
7970 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007972 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007974 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"inputrestore", 0, 0, f_inputrestore},
7976 {"inputsave", 0, 0, f_inputsave},
7977 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007979 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007981 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007984 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007986 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"libcall", 3, 3, f_libcall},
7988 {"libcallnr", 3, 3, f_libcallnr},
7989 {"line", 1, 1, f_line},
7990 {"line2byte", 1, 1, f_line2byte},
7991 {"lispindent", 1, 1, f_lispindent},
7992 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007994 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007995 {"log10", 1, 1, f_log10},
7996#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007997#ifdef FEAT_LUA
7998 {"luaeval", 1, 2, f_luaeval},
7999#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008001 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008002 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008003 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008004 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008005 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008007 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008008 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008009 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008010 {"max", 1, 1, f_max},
8011 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008012#ifdef vim_mkdir
8013 {"mkdir", 1, 3, f_mkdir},
8014#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008016#ifdef FEAT_MZSCHEME
8017 {"mzeval", 1, 1, f_mzeval},
8018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008020 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008021 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008022 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#ifdef FEAT_FLOAT
8024 {"pow", 2, 2, f_pow},
8025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008027 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008029#ifdef FEAT_PYTHON3
8030 {"py3eval", 1, 1, f_py3eval},
8031#endif
8032#ifdef FEAT_PYTHON
8033 {"pyeval", 1, 1, f_pyeval},
8034#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008036 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008037 {"reltime", 0, 2, f_reltime},
8038 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"remote_expr", 2, 3, f_remote_expr},
8040 {"remote_foreground", 1, 1, f_remote_foreground},
8041 {"remote_peek", 1, 2, f_remote_peek},
8042 {"remote_read", 1, 1, f_remote_read},
8043 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008044 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008046 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008048 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008049#ifdef FEAT_FLOAT
8050 {"round", 1, 1, f_round},
8051#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008052 {"screencol", 0, 0, f_screencol},
8053 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008054 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008055 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008056 {"searchpair", 3, 7, f_searchpair},
8057 {"searchpairpos", 3, 7, f_searchpairpos},
8058 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"server2client", 2, 2, f_server2client},
8060 {"serverlist", 0, 0, f_serverlist},
8061 {"setbufvar", 3, 3, f_setbufvar},
8062 {"setcmdpos", 1, 1, f_setcmdpos},
8063 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008064 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008065 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008066 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008067 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008069 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008070 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008072#ifdef FEAT_CRYPT
8073 {"sha256", 1, 1, f_sha256},
8074#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008075 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008076 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008080 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008082 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008083 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008084 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008085 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008086 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087#ifdef FEAT_FLOAT
8088 {"sqrt", 1, 1, f_sqrt},
8089 {"str2float", 1, 1, f_str2float},
8090#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008091 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008092 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008093 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#ifdef HAVE_STRFTIME
8095 {"strftime", 1, 2, f_strftime},
8096#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"strlen", 1, 1, f_strlen},
8100 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008101 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008103 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"submatch", 1, 1, f_submatch},
8105 {"substitute", 4, 4, f_substitute},
8106 {"synID", 3, 3, f_synID},
8107 {"synIDattr", 2, 3, f_synIDattr},
8108 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008109 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008110 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008111 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008112 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008113 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008114 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008115 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008116 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008117#ifdef FEAT_FLOAT
8118 {"tan", 1, 1, f_tan},
8119 {"tanh", 1, 1, f_tanh},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008122 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"tolower", 1, 1, f_tolower},
8124 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008125 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#ifdef FEAT_FLOAT
8127 {"trunc", 1, 1, f_trunc},
8128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008130 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008131 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"virtcol", 1, 1, f_virtcol},
8134 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008135 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"winbufnr", 1, 1, f_winbufnr},
8137 {"wincol", 0, 0, f_wincol},
8138 {"winheight", 1, 1, f_winheight},
8139 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008140 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008142 {"winrestview", 1, 1, f_winrestview},
8143 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008145 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008146 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147};
8148
8149#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8150
8151/*
8152 * Function given to ExpandGeneric() to obtain the list of internal
8153 * or user defined function names.
8154 */
8155 char_u *
8156get_function_name(xp, idx)
8157 expand_T *xp;
8158 int idx;
8159{
8160 static int intidx = -1;
8161 char_u *name;
8162
8163 if (idx == 0)
8164 intidx = -1;
8165 if (intidx < 0)
8166 {
8167 name = get_user_func_name(xp, idx);
8168 if (name != NULL)
8169 return name;
8170 }
8171 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8172 {
8173 STRCPY(IObuff, functions[intidx].f_name);
8174 STRCAT(IObuff, "(");
8175 if (functions[intidx].f_max_argc == 0)
8176 STRCAT(IObuff, ")");
8177 return IObuff;
8178 }
8179
8180 return NULL;
8181}
8182
8183/*
8184 * Function given to ExpandGeneric() to obtain the list of internal or
8185 * user defined variable or function names.
8186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 char_u *
8188get_expr_name(xp, idx)
8189 expand_T *xp;
8190 int idx;
8191{
8192 static int intidx = -1;
8193 char_u *name;
8194
8195 if (idx == 0)
8196 intidx = -1;
8197 if (intidx < 0)
8198 {
8199 name = get_function_name(xp, idx);
8200 if (name != NULL)
8201 return name;
8202 }
8203 return get_user_var_name(xp, ++intidx);
8204}
8205
8206#endif /* FEAT_CMDL_COMPL */
8207
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008208#if defined(EBCDIC) || defined(PROTO)
8209/*
8210 * Compare struct fst by function name.
8211 */
8212 static int
8213compare_func_name(s1, s2)
8214 const void *s1;
8215 const void *s2;
8216{
8217 struct fst *p1 = (struct fst *)s1;
8218 struct fst *p2 = (struct fst *)s2;
8219
8220 return STRCMP(p1->f_name, p2->f_name);
8221}
8222
8223/*
8224 * Sort the function table by function name.
8225 * The sorting of the table above is ASCII dependant.
8226 * On machines using EBCDIC we have to sort it.
8227 */
8228 static void
8229sortFunctions()
8230{
8231 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8232
8233 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8234}
8235#endif
8236
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238/*
8239 * Find internal function in table above.
8240 * Return index, or -1 if not found
8241 */
8242 static int
8243find_internal_func(name)
8244 char_u *name; /* name of the function */
8245{
8246 int first = 0;
8247 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8248 int cmp;
8249 int x;
8250
8251 /*
8252 * Find the function name in the table. Binary search.
8253 */
8254 while (first <= last)
8255 {
8256 x = first + ((unsigned)(last - first) >> 1);
8257 cmp = STRCMP(name, functions[x].f_name);
8258 if (cmp < 0)
8259 last = x - 1;
8260 else if (cmp > 0)
8261 first = x + 1;
8262 else
8263 return x;
8264 }
8265 return -1;
8266}
8267
8268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8270 * name it contains, otherwise return "name".
8271 */
8272 static char_u *
8273deref_func_name(name, lenp)
8274 char_u *name;
8275 int *lenp;
8276{
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278 int cc;
8279
8280 cc = name[*lenp];
8281 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008282 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287 {
8288 *lenp = 0;
8289 return (char_u *)""; /* just in case */
8290 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008291 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 }
8294
8295 return name;
8296}
8297
8298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * Allocate a variable for the result of a function.
8300 * Return OK or FAIL.
8301 */
8302 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8304 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 char_u *name; /* name of the function */
8306 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 char_u **arg; /* argument, pointing to the '(' */
8309 linenr_T firstline; /* first line of range */
8310 linenr_T lastline; /* last line of range */
8311 int *doesrange; /* return: function handled range */
8312 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 char_u *argp;
8316 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 int argcount = 0; /* number of arguments found */
8319
8320 /*
8321 * Get the arguments.
8322 */
8323 argp = *arg;
8324 while (argcount < MAX_FUNC_ARGS)
8325 {
8326 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8327 if (*argp == ')' || *argp == ',' || *argp == NUL)
8328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8330 {
8331 ret = FAIL;
8332 break;
8333 }
8334 ++argcount;
8335 if (*argp != ',')
8336 break;
8337 }
8338 if (*argp == ')')
8339 ++argp;
8340 else
8341 ret = FAIL;
8342
8343 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 {
8348 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008349 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008351 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 *arg = skipwhite(argp);
8358 return ret;
8359}
8360
8361
8362/*
8363 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008364 * Return OK when the function can't be called, FAIL otherwise.
8365 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 */
8367 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008368call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008370 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008374 typval_T *argvars; /* vars for arguments, must have "argcount"
8375 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 linenr_T firstline; /* first line of range */
8377 linenr_T lastline; /* last line of range */
8378 int *doesrange; /* return: function handled range */
8379 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#define ERROR_UNKNOWN 0
8384#define ERROR_TOOMANY 1
8385#define ERROR_TOOFEW 2
8386#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387#define ERROR_DICT 4
8388#define ERROR_NONE 5
8389#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int error = ERROR_NONE;
8391 int i;
8392 int llen;
8393 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#define FLEN_FIXED 40
8395 char_u fname_buf[FLEN_FIXED + 1];
8396 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008397 char_u *name;
8398
8399 /* Make a copy of the name, if it comes from a funcref variable it could
8400 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008401 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008402 if (name == NULL)
8403 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 /*
8406 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8407 * Change <SNR>123_name() to K_SNR 123_name().
8408 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 llen = eval_fname_script(name);
8411 if (llen > 0)
8412 {
8413 fname_buf[0] = K_SPECIAL;
8414 fname_buf[1] = KS_EXTRA;
8415 fname_buf[2] = (int)KE_SNR;
8416 i = 3;
8417 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8418 {
8419 if (current_SID <= 0)
8420 error = ERROR_SCRIPT;
8421 else
8422 {
8423 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8424 i = (int)STRLEN(fname_buf);
8425 }
8426 }
8427 if (i + STRLEN(name + llen) < FLEN_FIXED)
8428 {
8429 STRCPY(fname_buf + i, name + llen);
8430 fname = fname_buf;
8431 }
8432 else
8433 {
8434 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8435 if (fname == NULL)
8436 error = ERROR_OTHER;
8437 else
8438 {
8439 mch_memmove(fname, fname_buf, (size_t)i);
8440 STRCPY(fname + i, name + llen);
8441 }
8442 }
8443 }
8444 else
8445 fname = name;
8446
8447 *doesrange = FALSE;
8448
8449
8450 /* execute the function if no errors detected and executing */
8451 if (evaluate && error == ERROR_NONE)
8452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008453 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 error = ERROR_UNKNOWN;
8456
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 /*
8460 * User defined function.
8461 */
8462 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* Trigger FuncUndefined event, may load the function. */
8466 if (fp == NULL
8467 && apply_autocmds(EVENT_FUNCUNDEFINED,
8468 fname, fname, TRUE, NULL)
8469 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 fp = find_func(fname);
8473 }
8474#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008476 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 {
8478 /* loaded a package, search for the function again */
8479 fp = find_func(fname);
8480 }
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (fp != NULL)
8483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008484 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008488 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else
8493 {
8494 /*
8495 * Call the user function.
8496 * Save and restore search patterns, script variables and
8497 * redo buffer.
8498 */
8499 save_search_patterns();
8500 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008501 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008504 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8505 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8506 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008507 /* Function was unreferenced while being used, free it
8508 * now. */
8509 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 restoreRedobuff();
8511 restore_search_patterns();
8512 error = ERROR_NONE;
8513 }
8514 }
8515 }
8516 else
8517 {
8518 /*
8519 * Find the function name in the table, call its implementation.
8520 */
8521 i = find_internal_func(fname);
8522 if (i >= 0)
8523 {
8524 if (argcount < functions[i].f_min_argc)
8525 error = ERROR_TOOFEW;
8526 else if (argcount > functions[i].f_max_argc)
8527 error = ERROR_TOOMANY;
8528 else
8529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 error = ERROR_NONE;
8533 }
8534 }
8535 }
8536 /*
8537 * The function call (or "FuncUndefined" autocommand sequence) might
8538 * have been aborted by an error, an interrupt, or an explicitly thrown
8539 * exception that has not been caught so far. This situation can be
8540 * tested for by calling aborting(). For an error in an internal
8541 * function or for the "E132" error in call_user_func(), however, the
8542 * throw point at which the "force_abort" flag (temporarily reset by
8543 * emsg()) is normally updated has not been reached yet. We need to
8544 * update that flag first to make aborting() reliable.
8545 */
8546 update_force_abort();
8547 }
8548 if (error == ERROR_NONE)
8549 ret = OK;
8550
8551 /*
8552 * Report an error unless the argument evaluation or function call has been
8553 * cancelled due to an aborting error, an interrupt, or an exception.
8554 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 if (!aborting())
8556 {
8557 switch (error)
8558 {
8559 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 break;
8562 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008564 break;
8565 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008566 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 name);
8568 break;
8569 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008570 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008571 name);
8572 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008573 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008574 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 name);
8576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 }
8578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (fname != name && fname != fname_buf)
8581 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008582 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
8584 return ret;
8585}
8586
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008587/*
8588 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008589 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008590 */
8591 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008592emsg_funcname(ermsg, name)
8593 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 char_u *name;
8595{
8596 char_u *p;
8597
8598 if (*name == K_SPECIAL)
8599 p = concat_str((char_u *)"<SNR>", name + 3);
8600 else
8601 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008602 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008603 if (p != name)
8604 vim_free(p);
8605}
8606
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008607/*
8608 * Return TRUE for a non-zero Number and a non-empty String.
8609 */
8610 static int
8611non_zero_arg(argvars)
8612 typval_T *argvars;
8613{
8614 return ((argvars[0].v_type == VAR_NUMBER
8615 && argvars[0].vval.v_number != 0)
8616 || (argvars[0].v_type == VAR_STRING
8617 && argvars[0].vval.v_string != NULL
8618 && *argvars[0].vval.v_string != NUL));
8619}
8620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*********************************************
8622 * Implementation of the built-in functions
8623 */
8624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008626static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8627
8628/*
8629 * Get the float value of "argvars[0]" into "f".
8630 * Returns FAIL when the argument is not a Number or Float.
8631 */
8632 static int
8633get_float_arg(argvars, f)
8634 typval_T *argvars;
8635 float_T *f;
8636{
8637 if (argvars[0].v_type == VAR_FLOAT)
8638 {
8639 *f = argvars[0].vval.v_float;
8640 return OK;
8641 }
8642 if (argvars[0].v_type == VAR_NUMBER)
8643 {
8644 *f = (float_T)argvars[0].vval.v_number;
8645 return OK;
8646 }
8647 EMSG(_("E808: Number or Float required"));
8648 return FAIL;
8649}
8650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651/*
8652 * "abs(expr)" function
8653 */
8654 static void
8655f_abs(argvars, rettv)
8656 typval_T *argvars;
8657 typval_T *rettv;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 rettv->v_type = VAR_FLOAT;
8662 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8663 }
8664 else
8665 {
8666 varnumber_T n;
8667 int error = FALSE;
8668
8669 n = get_tv_number_chk(&argvars[0], &error);
8670 if (error)
8671 rettv->vval.v_number = -1;
8672 else if (n > 0)
8673 rettv->vval.v_number = n;
8674 else
8675 rettv->vval.v_number = -n;
8676 }
8677}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008678
8679/*
8680 * "acos()" function
8681 */
8682 static void
8683f_acos(argvars, rettv)
8684 typval_T *argvars;
8685 typval_T *rettv;
8686{
8687 float_T f;
8688
8689 rettv->v_type = VAR_FLOAT;
8690 if (get_float_arg(argvars, &f) == OK)
8691 rettv->vval.v_float = acos(f);
8692 else
8693 rettv->vval.v_float = 0.0;
8694}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695#endif
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 */
8700 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008701f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *argvars;
8703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008710 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008711 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008712 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 }
8715 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008716 EMSG(_(e_listreq));
8717}
8718
8719/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008720 * "and(expr, expr)" function
8721 */
8722 static void
8723f_and(argvars, rettv)
8724 typval_T *argvars;
8725 typval_T *rettv;
8726{
8727 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8728 & get_tv_number_chk(&argvars[1], NULL);
8729}
8730
8731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 * "append(lnum, string/list)" function
8733 */
8734 static void
8735f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738{
8739 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 list_T *l = NULL;
8742 listitem_T *li = NULL;
8743 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 long added = 0;
8745
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 lnum = get_tv_lnum(argvars);
8747 if (lnum >= 0
8748 && lnum <= curbuf->b_ml.ml_line_count
8749 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008750 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008751 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753 l = argvars[1].vval.v_list;
8754 if (l == NULL)
8755 return;
8756 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008758 for (;;)
8759 {
8760 if (l == NULL)
8761 tv = &argvars[1]; /* append a string */
8762 else if (li == NULL)
8763 break; /* end of list */
8764 else
8765 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 line = get_tv_string_chk(tv);
8767 if (line == NULL) /* type error */
8768 {
8769 rettv->vval.v_number = 1; /* Failed */
8770 break;
8771 }
8772 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008773 ++added;
8774 if (l == NULL)
8775 break;
8776 li = li->li_next;
8777 }
8778
8779 appended_lines_mark(lnum, added);
8780 if (curwin->w_cursor.lnum > lnum)
8781 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 else
8784 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787/*
8788 * "argc()" function
8789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008792 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796}
8797
8798/*
8799 * "argidx()" function
8800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809/*
8810 * "argv(nr)" function
8811 */
8812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 typval_T *argvars;
8815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 int idx;
8818
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008819 if (argvars[0].v_type != VAR_UNKNOWN)
8820 {
8821 idx = get_tv_number_chk(&argvars[0], NULL);
8822 if (idx >= 0 && idx < ARGCOUNT)
8823 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8824 else
8825 rettv->vval.v_string = NULL;
8826 rettv->v_type = VAR_STRING;
8827 }
8828 else if (rettv_list_alloc(rettv) == OK)
8829 for (idx = 0; idx < ARGCOUNT; ++idx)
8830 list_append_string(rettv->vval.v_list,
8831 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008835/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008836 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008837 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008838 static void
8839f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008840 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008841 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008843 float_T f;
8844
8845 rettv->v_type = VAR_FLOAT;
8846 if (get_float_arg(argvars, &f) == OK)
8847 rettv->vval.v_float = asin(f);
8848 else
8849 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850}
8851
8852/*
8853 * "atan()" function
8854 */
8855 static void
8856f_atan(argvars, rettv)
8857 typval_T *argvars;
8858 typval_T *rettv;
8859{
8860 float_T f;
8861
8862 rettv->v_type = VAR_FLOAT;
8863 if (get_float_arg(argvars, &f) == OK)
8864 rettv->vval.v_float = atan(f);
8865 else
8866 rettv->vval.v_float = 0.0;
8867}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868
8869/*
8870 * "atan2()" function
8871 */
8872 static void
8873f_atan2(argvars, rettv)
8874 typval_T *argvars;
8875 typval_T *rettv;
8876{
8877 float_T fx, fy;
8878
8879 rettv->v_type = VAR_FLOAT;
8880 if (get_float_arg(argvars, &fx) == OK
8881 && get_float_arg(&argvars[1], &fy) == OK)
8882 rettv->vval.v_float = atan2(fx, fy);
8883 else
8884 rettv->vval.v_float = 0.0;
8885}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#endif
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888/*
8889 * "browse(save, title, initdir, default)" function
8890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008893 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896#ifdef FEAT_BROWSE
8897 int save;
8898 char_u *title;
8899 char_u *initdir;
8900 char_u *defname;
8901 char_u buf[NUMBUFLEN];
8902 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 save = get_tv_number_chk(&argvars[0], &error);
8906 title = get_tv_string_chk(&argvars[1]);
8907 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8908 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 if (error || title == NULL || initdir == NULL || defname == NULL)
8911 rettv->vval.v_string = NULL;
8912 else
8913 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008914 do_browse(save ? BROWSE_SAVE : 0,
8915 title, defname, NULL, initdir, NULL, curbuf);
8916#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008918#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008920}
8921
8922/*
8923 * "browsedir(title, initdir)" function
8924 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008928 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008929{
8930#ifdef FEAT_BROWSE
8931 char_u *title;
8932 char_u *initdir;
8933 char_u buf[NUMBUFLEN];
8934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 title = get_tv_string_chk(&argvars[0]);
8936 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 if (title == NULL || initdir == NULL)
8939 rettv->vval.v_string = NULL;
8940 else
8941 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008942 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947}
8948
Bram Moolenaar33570922005-01-25 22:26:29 +00008949static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951/*
8952 * Find a buffer by number or exact name.
8953 */
8954 static buf_T *
8955find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008956 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957{
8958 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008960 if (avar->v_type == VAR_NUMBER)
8961 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008962 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008965 if (buf == NULL)
8966 {
8967 /* No full path name match, try a match with a URL or a "nofile"
8968 * buffer, these don't use the full path. */
8969 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8970 if (buf->b_fname != NULL
8971 && (path_with_url(buf->b_fname)
8972#ifdef FEAT_QUICKFIX
8973 || bt_nofile(buf)
8974#endif
8975 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008977 break;
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 }
8980 return buf;
8981}
8982
8983/*
8984 * "bufexists(expr)" function
8985 */
8986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992}
8993
8994/*
8995 * "buflisted(expr)" function
8996 */
8997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 buf_T *buf;
9003
9004 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008/*
9009 * "bufloaded(expr)" function
9010 */
9011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
9016 buf_T *buf;
9017
9018 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020}
9021
Bram Moolenaar33570922005-01-25 22:26:29 +00009022static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024/*
9025 * Get buffer by number or pattern.
9026 */
9027 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 int save_magic;
9033 char_u *save_cpo;
9034 buf_T *buf;
9035
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 if (tv->v_type == VAR_NUMBER)
9037 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009038 if (tv->v_type != VAR_STRING)
9039 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 if (name == NULL || *name == NUL)
9041 return curbuf;
9042 if (name[0] == '$' && name[1] == NUL)
9043 return lastbuf;
9044
9045 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9046 save_magic = p_magic;
9047 p_magic = TRUE;
9048 save_cpo = p_cpo;
9049 p_cpo = (char_u *)"";
9050
9051 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9052 TRUE, FALSE));
9053
9054 p_magic = save_magic;
9055 p_cpo = save_cpo;
9056
9057 /* If not found, try expanding the name, like done for bufexists(). */
9058 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061 return buf;
9062}
9063
9064/*
9065 * "bufname(expr)" function
9066 */
9067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *argvars;
9070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
9072 buf_T *buf;
9073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 buf = get_buf_tv(&argvars[0]);
9077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 --emsg_off;
9083}
9084
9085/*
9086 * "bufnr(expr)" function
9087 */
9088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 typval_T *argvars;
9091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009094 int error = FALSE;
9095 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009097 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009100 --emsg_off;
9101
9102 /* If the buffer isn't found and the second argument is not zero create a
9103 * new buffer. */
9104 if (buf == NULL
9105 && argvars[1].v_type != VAR_UNKNOWN
9106 && get_tv_number_chk(&argvars[1], &error) != 0
9107 && !error
9108 && (name = get_tv_string_chk(&argvars[0])) != NULL
9109 && !error)
9110 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9111
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "bufwinnr(nr)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#ifdef FEAT_WINDOWS
9127 win_T *wp;
9128 int winnr = 0;
9129#endif
9130 buf_T *buf;
9131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#ifdef FEAT_WINDOWS
9136 for (wp = firstwin; wp; wp = wp->w_next)
9137 {
9138 ++winnr;
9139 if (wp->w_buffer == buf)
9140 break;
9141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145#endif
9146 --emsg_off;
9147}
9148
9149/*
9150 * "byte2line(byte)" function
9151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#else
9160 long boff = 0;
9161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 (linenr_T)0, &boff);
9168#endif
9169}
9170
9171/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009172 * "byteidx()" function
9173 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009178{
9179#ifdef FEAT_MBYTE
9180 char_u *t;
9181#endif
9182 char_u *str;
9183 long idx;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 str = get_tv_string_chk(&argvars[0]);
9186 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009189 return;
9190
9191#ifdef FEAT_MBYTE
9192 t = str;
9193 for ( ; idx > 0; idx--)
9194 {
9195 if (*t == NUL) /* EOL reached */
9196 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009197 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009199 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009200#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009201 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009203#endif
9204}
9205
Bram Moolenaardb913952012-06-29 12:54:53 +02009206 int
9207func_call(name, args, selfdict, rettv)
9208 char_u *name;
9209 typval_T *args;
9210 dict_T *selfdict;
9211 typval_T *rettv;
9212{
9213 listitem_T *item;
9214 typval_T argv[MAX_FUNC_ARGS + 1];
9215 int argc = 0;
9216 int dummy;
9217 int r = 0;
9218
9219 for (item = args->vval.v_list->lv_first; item != NULL;
9220 item = item->li_next)
9221 {
9222 if (argc == MAX_FUNC_ARGS)
9223 {
9224 EMSG(_("E699: Too many arguments"));
9225 break;
9226 }
9227 /* Make a copy of each argument. This is needed to be able to set
9228 * v_lock to VAR_FIXED in the copy without changing the original list.
9229 */
9230 copy_tv(&item->li_tv, &argv[argc++]);
9231 }
9232
9233 if (item == NULL)
9234 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9236 &dummy, TRUE, selfdict);
9237
9238 /* Free the arguments. */
9239 while (argc > 0)
9240 clear_tv(&argv[--argc]);
9241
9242 return r;
9243}
9244
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009245/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246 * "call(func, arglist)" function
9247 */
9248 static void
9249f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009250 typval_T *argvars;
9251 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009252{
9253 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009255
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009256 if (argvars[1].v_type != VAR_LIST)
9257 {
9258 EMSG(_(e_listreq));
9259 return;
9260 }
9261 if (argvars[1].vval.v_list == NULL)
9262 return;
9263
9264 if (argvars[0].v_type == VAR_FUNC)
9265 func = argvars[0].vval.v_string;
9266 else
9267 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 if (*func == NUL)
9269 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009270
Bram Moolenaare9a41262005-01-15 22:18:47 +00009271 if (argvars[2].v_type != VAR_UNKNOWN)
9272 {
9273 if (argvars[2].v_type != VAR_DICT)
9274 {
9275 EMSG(_(e_dictreq));
9276 return;
9277 }
9278 selfdict = argvars[2].vval.v_dict;
9279 }
9280
Bram Moolenaardb913952012-06-29 12:54:53 +02009281 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009282}
9283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009284#ifdef FEAT_FLOAT
9285/*
9286 * "ceil({float})" function
9287 */
9288 static void
9289f_ceil(argvars, rettv)
9290 typval_T *argvars;
9291 typval_T *rettv;
9292{
9293 float_T f;
9294
9295 rettv->v_type = VAR_FLOAT;
9296 if (get_float_arg(argvars, &f) == OK)
9297 rettv->vval.v_float = ceil(f);
9298 else
9299 rettv->vval.v_float = 0.0;
9300}
9301#endif
9302
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009303/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009304 * "changenr()" function
9305 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009306 static void
9307f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009308 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009309 typval_T *rettv;
9310{
9311 rettv->vval.v_number = curbuf->b_u_seq_cur;
9312}
9313
9314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 * "char2nr(string)" function
9316 */
9317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *argvars;
9320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322#ifdef FEAT_MBYTE
9323 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009324 {
9325 int utf8 = 0;
9326
9327 if (argvars[1].v_type != VAR_UNKNOWN)
9328 utf8 = get_tv_number_chk(&argvars[1], NULL);
9329
9330 if (utf8)
9331 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9332 else
9333 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 else
9336#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338}
9339
9340/*
9341 * "cindent(lnum)" function
9342 */
9343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347{
9348#ifdef FEAT_CINDENT
9349 pos_T pos;
9350 linenr_T lnum;
9351
9352 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9355 {
9356 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 curwin->w_cursor = pos;
9359 }
9360 else
9361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
9364
9365/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009366 * "clearmatches()" function
9367 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009368 static void
9369f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009370 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009371 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009372{
9373#ifdef FEAT_SEARCH_EXTRA
9374 clear_matches(curwin);
9375#endif
9376}
9377
9378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 * "col(string)" function
9380 */
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 colnr_T col = 0;
9387 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009388 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009390 fp = var2fpos(&argvars[0], FALSE, &fnum);
9391 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
9393 if (fp->col == MAXCOL)
9394 {
9395 /* '> can be MAXCOL, get the length of the line then */
9396 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009397 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 else
9399 col = MAXCOL;
9400 }
9401 else
9402 {
9403 col = fp->col + 1;
9404#ifdef FEAT_VIRTUALEDIT
9405 /* col(".") when the cursor is on the NUL at the end of the line
9406 * because of "coladd" can be seen as an extra column. */
9407 if (virtual_active() && fp == &curwin->w_cursor)
9408 {
9409 char_u *p = ml_get_cursor();
9410
9411 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9412 curwin->w_virtcol - curwin->w_cursor.coladd))
9413 {
9414# ifdef FEAT_MBYTE
9415 int l;
9416
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009417 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 col += l;
9419# else
9420 if (*p != NUL && p[1] == NUL)
9421 ++col;
9422# endif
9423 }
9424 }
9425#endif
9426 }
9427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429}
9430
Bram Moolenaar572cb562005-08-05 21:35:02 +00009431#if defined(FEAT_INS_EXPAND)
9432/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009433 * "complete()" function
9434 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009435 static void
9436f_complete(argvars, rettv)
9437 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009439{
9440 int startcol;
9441
9442 if ((State & INSERT) == 0)
9443 {
9444 EMSG(_("E785: complete() can only be used in Insert mode"));
9445 return;
9446 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009447
9448 /* Check for undo allowed here, because if something was already inserted
9449 * the line was already saved for undo and this check isn't done. */
9450 if (!undo_allowed())
9451 return;
9452
Bram Moolenaarade00832006-03-10 21:46:58 +00009453 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9454 {
9455 EMSG(_(e_invarg));
9456 return;
9457 }
9458
9459 startcol = get_tv_number_chk(&argvars[0], NULL);
9460 if (startcol <= 0)
9461 return;
9462
9463 set_completion(startcol - 1, argvars[1].vval.v_list);
9464}
9465
9466/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009467 * "complete_add()" function
9468 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009469 static void
9470f_complete_add(argvars, rettv)
9471 typval_T *argvars;
9472 typval_T *rettv;
9473{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009474 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475}
9476
9477/*
9478 * "complete_check()" function
9479 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009480 static void
9481f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009482 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009483 typval_T *rettv;
9484{
9485 int saved = RedrawingDisabled;
9486
9487 RedrawingDisabled = 0;
9488 ins_compl_check_keys(0);
9489 rettv->vval.v_number = compl_interrupted;
9490 RedrawingDisabled = saved;
9491}
9492#endif
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494/*
9495 * "confirm(message, buttons[, default [, type]])" function
9496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009499 typval_T *argvars UNUSED;
9500 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9503 char_u *message;
9504 char_u *buttons = NULL;
9505 char_u buf[NUMBUFLEN];
9506 char_u buf2[NUMBUFLEN];
9507 int def = 1;
9508 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 char_u *typestr;
9510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 message = get_tv_string_chk(&argvars[0]);
9513 if (message == NULL)
9514 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9518 if (buttons == NULL)
9519 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009523 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9526 if (typestr == NULL)
9527 error = TRUE;
9528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 switch (TOUPPER_ASC(*typestr))
9531 {
9532 case 'E': type = VIM_ERROR; break;
9533 case 'Q': type = VIM_QUESTION; break;
9534 case 'I': type = VIM_INFO; break;
9535 case 'W': type = VIM_WARNING; break;
9536 case 'G': type = VIM_GENERIC; break;
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539 }
9540 }
9541 }
9542
9543 if (buttons == NULL || *buttons == NUL)
9544 buttons = (char_u *)_("&Ok");
9545
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009546 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009548 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#endif
9550}
9551
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552/*
9553 * "copy()" function
9554 */
9555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 typval_T *argvars;
9558 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009560 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009563#ifdef FEAT_FLOAT
9564/*
9565 * "cos()" function
9566 */
9567 static void
9568f_cos(argvars, rettv)
9569 typval_T *argvars;
9570 typval_T *rettv;
9571{
9572 float_T f;
9573
9574 rettv->v_type = VAR_FLOAT;
9575 if (get_float_arg(argvars, &f) == OK)
9576 rettv->vval.v_float = cos(f);
9577 else
9578 rettv->vval.v_float = 0.0;
9579}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009580
9581/*
9582 * "cosh()" function
9583 */
9584 static void
9585f_cosh(argvars, rettv)
9586 typval_T *argvars;
9587 typval_T *rettv;
9588{
9589 float_T f;
9590
9591 rettv->v_type = VAR_FLOAT;
9592 if (get_float_arg(argvars, &f) == OK)
9593 rettv->vval.v_float = cosh(f);
9594 else
9595 rettv->vval.v_float = 0.0;
9596}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009597#endif
9598
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 * "count()" function
9601 */
9602 static void
9603f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009604 typval_T *argvars;
9605 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607 long n = 0;
9608 int ic = FALSE;
9609
Bram Moolenaare9a41262005-01-15 22:18:47 +00009610 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009611 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 listitem_T *li;
9613 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009615
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 if ((l = argvars[0].vval.v_list) != NULL)
9617 {
9618 li = l->lv_first;
9619 if (argvars[2].v_type != VAR_UNKNOWN)
9620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 int error = FALSE;
9622
9623 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[3].v_type != VAR_UNKNOWN)
9625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 idx = get_tv_number_chk(&argvars[3], &error);
9627 if (!error)
9628 {
9629 li = list_find(l, idx);
9630 if (li == NULL)
9631 EMSGN(_(e_listidx), idx);
9632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 if (error)
9635 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 }
9637
9638 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009639 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 ++n;
9641 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 else if (argvars[0].v_type == VAR_DICT)
9644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009645 int todo;
9646 dict_T *d;
9647 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009649 if ((d = argvars[0].vval.v_dict) != NULL)
9650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 int error = FALSE;
9652
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653 if (argvars[2].v_type != VAR_UNKNOWN)
9654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 if (argvars[3].v_type != VAR_UNKNOWN)
9657 EMSG(_(e_invarg));
9658 }
9659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009660 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009662 {
9663 if (!HASHITEM_EMPTY(hi))
9664 {
9665 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009666 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009667 ++n;
9668 }
9669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 }
9671 }
9672 else
9673 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 rettv->vval.v_number = n;
9675}
9676
9677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9679 *
9680 * Checks the existence of a cscope connection.
9681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009684 typval_T *argvars UNUSED;
9685 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#ifdef FEAT_CSCOPE
9688 int num = 0;
9689 char_u *dbpath = NULL;
9690 char_u *prepend = NULL;
9691 char_u buf[NUMBUFLEN];
9692
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 if (argvars[0].v_type != VAR_UNKNOWN
9694 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 num = (int)get_tv_number(&argvars[0]);
9697 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#endif
9704}
9705
9706/*
9707 * "cursor(lnum, col)" function
9708 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009709 * Moves the cursor to the specified line and column.
9710 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
9717 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009718#ifdef FEAT_VIRTUALEDIT
9719 long coladd = 0;
9720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009722 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009723 if (argvars[1].v_type == VAR_UNKNOWN)
9724 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009728 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009729 line = pos.lnum;
9730 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009732 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009733#endif
9734 }
9735 else
9736 {
9737 line = get_tv_lnum(argvars);
9738 col = get_tv_number_chk(&argvars[1], NULL);
9739#ifdef FEAT_VIRTUALEDIT
9740 if (argvars[2].v_type != VAR_UNKNOWN)
9741 coladd = get_tv_number_chk(&argvars[2], NULL);
9742#endif
9743 }
9744 if (line < 0 || col < 0
9745#ifdef FEAT_VIRTUALEDIT
9746 || coladd < 0
9747#endif
9748 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (line > 0)
9751 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (col > 0)
9753 curwin->w_cursor.col = col - 1;
9754#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009755 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#endif
9757
9758 /* Make sure the cursor is in a valid position. */
9759 check_cursor();
9760#ifdef FEAT_MBYTE
9761 /* Correct cursor for multi-byte character. */
9762 if (has_mbyte)
9763 mb_adjust_cursor();
9764#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009765
9766 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009767 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009771 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 */
9773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009778 int noref = 0;
9779
9780 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009782 if (noref < 0 || noref > 1)
9783 EMSG(_(e_invarg));
9784 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009785 {
9786 current_copyID += COPYID_INC;
9787 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791/*
9792 * "delete()" function
9793 */
9794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 typval_T *argvars;
9797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
9799 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
9805/*
9806 * "did_filetype()" function
9807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009810 typval_T *argvars UNUSED;
9811 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815#endif
9816}
9817
9818/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009819 * "diff_filler()" function
9820 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009823 typval_T *argvars UNUSED;
9824 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009825{
9826#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828#endif
9829}
9830
9831/*
9832 * "diff_hlID()" function
9833 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009836 typval_T *argvars UNUSED;
9837 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009838{
9839#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009841 static linenr_T prev_lnum = 0;
9842 static int changedtick = 0;
9843 static int fnum = 0;
9844 static int change_start = 0;
9845 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009846 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847 int filler_lines;
9848 int col;
9849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009850 if (lnum < 0) /* ignore type error in {lnum} arg */
9851 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009852 if (lnum != prev_lnum
9853 || changedtick != curbuf->b_changedtick
9854 || fnum != curbuf->b_fnum)
9855 {
9856 /* New line, buffer, change: need to get the values. */
9857 filler_lines = diff_check(curwin, lnum);
9858 if (filler_lines < 0)
9859 {
9860 if (filler_lines == -1)
9861 {
9862 change_start = MAXCOL;
9863 change_end = -1;
9864 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9865 hlID = HLF_ADD; /* added line */
9866 else
9867 hlID = HLF_CHD; /* changed line */
9868 }
9869 else
9870 hlID = HLF_ADD; /* added line */
9871 }
9872 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009873 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009874 prev_lnum = lnum;
9875 changedtick = curbuf->b_changedtick;
9876 fnum = curbuf->b_fnum;
9877 }
9878
9879 if (hlID == HLF_CHD || hlID == HLF_TXD)
9880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 if (col >= change_start && col <= change_end)
9883 hlID = HLF_TXD; /* changed text */
9884 else
9885 hlID = HLF_CHD; /* changed line */
9886 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009887 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888#endif
9889}
9890
9891/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009892 * "empty({expr})" function
9893 */
9894 static void
9895f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009898{
9899 int n;
9900
9901 switch (argvars[0].v_type)
9902 {
9903 case VAR_STRING:
9904 case VAR_FUNC:
9905 n = argvars[0].vval.v_string == NULL
9906 || *argvars[0].vval.v_string == NUL;
9907 break;
9908 case VAR_NUMBER:
9909 n = argvars[0].vval.v_number == 0;
9910 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009911#ifdef FEAT_FLOAT
9912 case VAR_FLOAT:
9913 n = argvars[0].vval.v_float == 0.0;
9914 break;
9915#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009916 case VAR_LIST:
9917 n = argvars[0].vval.v_list == NULL
9918 || argvars[0].vval.v_list->lv_first == NULL;
9919 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 case VAR_DICT:
9921 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009924 default:
9925 EMSG2(_(e_intern2), "f_empty()");
9926 n = 0;
9927 }
9928
9929 rettv->vval.v_number = n;
9930}
9931
9932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 * "escape({string}, {chars})" function
9934 */
9935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 typval_T *argvars;
9938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939{
9940 char_u buf[NUMBUFLEN];
9941
Bram Moolenaar758711c2005-02-02 23:11:38 +00009942 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9943 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948 * "eval()" function
9949 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950 static void
9951f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 typval_T *argvars;
9953 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954{
9955 char_u *s;
9956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 s = get_tv_string_chk(&argvars[0]);
9958 if (s != NULL)
9959 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009961 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9962 {
9963 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009964 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009966 else if (*s != NUL)
9967 EMSG(_(e_trailing));
9968}
9969
9970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 * "eventhandler()" function
9972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * "executable()" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990}
9991
9992/*
9993 * "exists()" function
9994 */
9995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009997 typval_T *argvars;
9998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 char_u *p;
10001 char_u *name;
10002 int n = FALSE;
10003 int len = 0;
10004
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010005 no_autoload = TRUE;
10006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (*p == '$') /* environment variable */
10009 {
10010 /* first try "normal" environment variables (fast) */
10011 if (mch_getenv(p + 1) != NULL)
10012 n = TRUE;
10013 else
10014 {
10015 /* try expanding things like $VIM and ${HOME} */
10016 p = expand_env_save(p);
10017 if (p != NULL && *p != '$')
10018 n = TRUE;
10019 vim_free(p);
10020 }
10021 }
10022 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010025 if (*skipwhite(p) != NUL)
10026 n = FALSE; /* trailing garbage */
10027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 else if (*p == '*') /* internal or user defined function */
10029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010030 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032 else if (*p == ':')
10033 {
10034 n = cmd_exists(p + 1);
10035 }
10036 else if (*p == '#')
10037 {
10038#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010039 if (p[1] == '#')
10040 n = autocmd_supported(p + 2);
10041 else
10042 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043#endif
10044 }
10045 else /* internal variable */
10046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010047 char_u *tofree;
10048 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010050 /* get_name_len() takes care of expanding curly braces */
10051 name = p;
10052 len = get_name_len(&p, &tofree, TRUE, FALSE);
10053 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 if (tofree != NULL)
10056 name = tofree;
10057 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10058 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010060 /* handle d.key, l[idx], f(expr) */
10061 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10062 if (n)
10063 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 }
10065 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010066 if (*p != NUL)
10067 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010069 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 }
10071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010073
10074 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010077#ifdef FEAT_FLOAT
10078/*
10079 * "exp()" function
10080 */
10081 static void
10082f_exp(argvars, rettv)
10083 typval_T *argvars;
10084 typval_T *rettv;
10085{
10086 float_T f;
10087
10088 rettv->v_type = VAR_FLOAT;
10089 if (get_float_arg(argvars, &f) == OK)
10090 rettv->vval.v_float = exp(f);
10091 else
10092 rettv->vval.v_float = 0.0;
10093}
10094#endif
10095
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096/*
10097 * "expand()" function
10098 */
10099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 typval_T *argvars;
10102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 char_u *s;
10105 int len;
10106 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010107 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010110 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010113 if (argvars[1].v_type != VAR_UNKNOWN
10114 && argvars[2].v_type != VAR_UNKNOWN
10115 && get_tv_number_chk(&argvars[2], &error)
10116 && !error)
10117 {
10118 rettv->v_type = VAR_LIST;
10119 rettv->vval.v_list = NULL;
10120 }
10121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 if (*s == '%' || *s == '#' || *s == '<')
10124 {
10125 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010126 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010128 if (rettv->v_type == VAR_LIST)
10129 {
10130 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10131 list_append_string(rettv->vval.v_list, result, -1);
10132 else
10133 vim_free(result);
10134 }
10135 else
10136 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 else
10139 {
10140 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010141 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 if (argvars[1].v_type != VAR_UNKNOWN
10143 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010144 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 if (!error)
10146 {
10147 ExpandInit(&xpc);
10148 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010149 if (p_wic)
10150 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010151 if (rettv->v_type == VAR_STRING)
10152 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10153 options, WILD_ALL);
10154 else if (rettv_list_alloc(rettv) != FAIL)
10155 {
10156 int i;
10157
10158 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10159 for (i = 0; i < xpc.xp_numfiles; i++)
10160 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10161 ExpandCleanup(&xpc);
10162 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 }
10164 else
10165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 }
10167}
10168
10169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010170 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010171 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010172 */
10173 static void
10174f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 typval_T *argvars;
10176 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010177{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010178 char *arg_errmsg = N_("extend() argument");
10179
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 list_T *l1, *l2;
10183 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010184 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010186
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 l1 = argvars[0].vval.v_list;
10188 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010189 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010190 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 {
10192 if (argvars[2].v_type != VAR_UNKNOWN)
10193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 before = get_tv_number_chk(&argvars[2], &error);
10195 if (error)
10196 return; /* type error; errmsg already given */
10197
Bram Moolenaar758711c2005-02-02 23:11:38 +000010198 if (before == l1->lv_len)
10199 item = NULL;
10200 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010202 item = list_find(l1, before);
10203 if (item == NULL)
10204 {
10205 EMSGN(_(e_listidx), before);
10206 return;
10207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010208 }
10209 }
10210 else
10211 item = NULL;
10212 list_extend(l1, l2, item);
10213
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 copy_tv(&argvars[0], rettv);
10215 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10218 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 dict_T *d1, *d2;
10220 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 char_u *action;
10222 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010223 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010224 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225
10226 d1 = argvars[0].vval.v_dict;
10227 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010228 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010229 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 {
10231 /* Check the third argument. */
10232 if (argvars[2].v_type != VAR_UNKNOWN)
10233 {
10234 static char *(av[]) = {"keep", "force", "error"};
10235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 action = get_tv_string_chk(&argvars[2]);
10237 if (action == NULL)
10238 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010239 for (i = 0; i < 3; ++i)
10240 if (STRCMP(action, av[i]) == 0)
10241 break;
10242 if (i == 3)
10243 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010244 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 return;
10246 }
10247 }
10248 else
10249 action = (char_u *)"force";
10250
10251 /* Go over all entries in the second dict and add them to the
10252 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010253 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010256 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010258 --todo;
10259 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010260 if (d1->dv_scope != 0)
10261 {
10262 /* Disallow replacing a builtin function in l: and g:.
10263 * Check the key to be valid when adding to any
10264 * scope. */
10265 if (d1->dv_scope == VAR_DEF_SCOPE
10266 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10267 && var_check_func_name(hi2->hi_key,
10268 di1 == NULL))
10269 break;
10270 if (!valid_varname(hi2->hi_key))
10271 break;
10272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010273 if (di1 == NULL)
10274 {
10275 di1 = dictitem_copy(HI2DI(hi2));
10276 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10277 dictitem_free(di1);
10278 }
10279 else if (*action == 'e')
10280 {
10281 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10282 break;
10283 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010284 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010285 {
10286 clear_tv(&di1->di_tv);
10287 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 }
10290 }
10291
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 copy_tv(&argvars[0], rettv);
10293 }
10294 }
10295 else
10296 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010297}
10298
10299/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010300 * "feedkeys()" function
10301 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010302 static void
10303f_feedkeys(argvars, rettv)
10304 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010305 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010306{
10307 int remap = TRUE;
10308 char_u *keys, *flags;
10309 char_u nbuf[NUMBUFLEN];
10310 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010311 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010312
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010313 /* This is not allowed in the sandbox. If the commands would still be
10314 * executed in the sandbox it would be OK, but it probably happens later,
10315 * when "sandbox" is no longer set. */
10316 if (check_secure())
10317 return;
10318
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010319 keys = get_tv_string(&argvars[0]);
10320 if (*keys != NUL)
10321 {
10322 if (argvars[1].v_type != VAR_UNKNOWN)
10323 {
10324 flags = get_tv_string_buf(&argvars[1], nbuf);
10325 for ( ; *flags != NUL; ++flags)
10326 {
10327 switch (*flags)
10328 {
10329 case 'n': remap = FALSE; break;
10330 case 'm': remap = TRUE; break;
10331 case 't': typed = TRUE; break;
10332 }
10333 }
10334 }
10335
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010336 /* Need to escape K_SPECIAL and CSI before putting the string in the
10337 * typeahead buffer. */
10338 keys_esc = vim_strsave_escape_csi(keys);
10339 if (keys_esc != NULL)
10340 {
10341 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010342 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010343 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010344 if (vgetc_busy)
10345 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010346 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010347 }
10348}
10349
10350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 * "filereadable()" function
10352 */
10353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010354f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010355 typval_T *argvars;
10356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010358 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 char_u *p;
10360 int n;
10361
Bram Moolenaarc236c162008-07-13 17:41:49 +000010362#ifndef O_NONBLOCK
10363# define O_NONBLOCK 0
10364#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010366 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10367 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 {
10369 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010370 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 }
10372 else
10373 n = FALSE;
10374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010375 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
10377
10378/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010379 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 * rights to write into.
10381 */
10382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 typval_T *argvars;
10385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010387 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388}
10389
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010390static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010391
10392 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010393findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010394 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010395 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010396 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010397{
10398#ifdef FEAT_SEARCHPATH
10399 char_u *fname;
10400 char_u *fresult = NULL;
10401 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10402 char_u *p;
10403 char_u pathbuf[NUMBUFLEN];
10404 int count = 1;
10405 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010406 int error = FALSE;
10407#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010408
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010409 rettv->vval.v_string = NULL;
10410 rettv->v_type = VAR_STRING;
10411
10412#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010415 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10418 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010419 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 else
10421 {
10422 if (*p != NUL)
10423 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010426 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010428 }
10429
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010430 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10431 error = TRUE;
10432
10433 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 do
10436 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010437 if (rettv->v_type == VAR_STRING)
10438 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 fresult = find_file_in_path_option(first ? fname : NULL,
10440 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010441 0, first, path,
10442 find_what,
10443 curbuf->b_ffname,
10444 find_what == FINDFILE_DIR
10445 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010447
10448 if (fresult != NULL && rettv->v_type == VAR_LIST)
10449 list_append_string(rettv->vval.v_list, fresult, -1);
10450
10451 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010453
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010454 if (rettv->v_type == VAR_STRING)
10455 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010456#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457}
10458
Bram Moolenaar33570922005-01-25 22:26:29 +000010459static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10460static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010461
10462/*
10463 * Implementation of map() and filter().
10464 */
10465 static void
10466filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010467 typval_T *argvars;
10468 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010469 int map;
10470{
10471 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010472 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 listitem_T *li, *nli;
10474 list_T *l = NULL;
10475 dictitem_T *di;
10476 hashtab_T *ht;
10477 hashitem_T *hi;
10478 dict_T *d = NULL;
10479 typval_T save_val;
10480 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010482 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010483 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10484 char *arg_errmsg = (map ? N_("map() argument")
10485 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010486 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010487 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010488
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010490 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010491 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010492 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010493 return;
10494 }
10495 else if (argvars[0].v_type == VAR_DICT)
10496 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010497 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010498 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010499 return;
10500 }
10501 else
10502 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010503 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 return;
10505 }
10506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 expr = get_tv_string_buf_chk(&argvars[1], buf);
10508 /* On type errors, the preceding call has already displayed an error
10509 * message. Avoid a misleading error message for an empty string that
10510 * was not passed as argument. */
10511 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 prepare_vimvar(VV_VAL, &save_val);
10514 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010515
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010516 /* We reset "did_emsg" to be able to detect whether an error
10517 * occurred during evaluation of the expression. */
10518 save_did_emsg = did_emsg;
10519 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010520
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010521 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 vimvars[VV_KEY].vv_type = VAR_STRING;
10525
10526 ht = &d->dv_hashtab;
10527 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010528 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 if (!HASHITEM_EMPTY(hi))
10532 {
10533 --todo;
10534 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010535 if (tv_check_lock(di->di_tv.v_lock,
10536 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 break;
10538 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010539 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010540 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 break;
10542 if (!map && rem)
10543 dictitem_remove(d, di);
10544 clear_tv(&vimvars[VV_KEY].vv_tv);
10545 }
10546 }
10547 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548 }
10549 else
10550 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010551 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 for (li = l->lv_first; li != NULL; li = nli)
10554 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010555 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010556 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010558 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010559 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010560 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010561 break;
10562 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010564 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010565 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010566 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010567
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010568 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010570
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010571 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573
10574 copy_tv(&argvars[0], rettv);
10575}
10576
10577 static int
10578filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010579 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 char_u *expr;
10581 int map;
10582 int *remp;
10583{
Bram Moolenaar33570922005-01-25 22:26:29 +000010584 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010586 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587
Bram Moolenaar33570922005-01-25 22:26:29 +000010588 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010589 s = expr;
10590 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010591 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 if (*s != NUL) /* check for trailing chars after expr */
10593 {
10594 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010595 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 }
10597 if (map)
10598 {
10599 /* map(): replace the list item value */
10600 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010601 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010602 *tv = rettv;
10603 }
10604 else
10605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 int error = FALSE;
10607
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 /* On type error, nothing has been removed; return FAIL to stop the
10612 * loop. The error message was given by get_tv_number_chk(). */
10613 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010614 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010616 retval = OK;
10617theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010618 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010619 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010620}
10621
10622/*
10623 * "filter()" function
10624 */
10625 static void
10626f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010627 typval_T *argvars;
10628 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629{
10630 filter_map(argvars, rettv, FALSE);
10631}
10632
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010634 * "finddir({fname}[, {path}[, {count}]])" function
10635 */
10636 static void
10637f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010638 typval_T *argvars;
10639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010640{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010641 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010642}
10643
10644/*
10645 * "findfile({fname}[, {path}[, {count}]])" function
10646 */
10647 static void
10648f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *argvars;
10650 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010651{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010652 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010653}
10654
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010655#ifdef FEAT_FLOAT
10656/*
10657 * "float2nr({float})" function
10658 */
10659 static void
10660f_float2nr(argvars, rettv)
10661 typval_T *argvars;
10662 typval_T *rettv;
10663{
10664 float_T f;
10665
10666 if (get_float_arg(argvars, &f) == OK)
10667 {
10668 if (f < -0x7fffffff)
10669 rettv->vval.v_number = -0x7fffffff;
10670 else if (f > 0x7fffffff)
10671 rettv->vval.v_number = 0x7fffffff;
10672 else
10673 rettv->vval.v_number = (varnumber_T)f;
10674 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010675}
10676
10677/*
10678 * "floor({float})" function
10679 */
10680 static void
10681f_floor(argvars, rettv)
10682 typval_T *argvars;
10683 typval_T *rettv;
10684{
10685 float_T f;
10686
10687 rettv->v_type = VAR_FLOAT;
10688 if (get_float_arg(argvars, &f) == OK)
10689 rettv->vval.v_float = floor(f);
10690 else
10691 rettv->vval.v_float = 0.0;
10692}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010693
10694/*
10695 * "fmod()" function
10696 */
10697 static void
10698f_fmod(argvars, rettv)
10699 typval_T *argvars;
10700 typval_T *rettv;
10701{
10702 float_T fx, fy;
10703
10704 rettv->v_type = VAR_FLOAT;
10705 if (get_float_arg(argvars, &fx) == OK
10706 && get_float_arg(&argvars[1], &fy) == OK)
10707 rettv->vval.v_float = fmod(fx, fy);
10708 else
10709 rettv->vval.v_float = 0.0;
10710}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010711#endif
10712
Bram Moolenaar0d660222005-01-07 21:51:51 +000010713/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010714 * "fnameescape({string})" function
10715 */
10716 static void
10717f_fnameescape(argvars, rettv)
10718 typval_T *argvars;
10719 typval_T *rettv;
10720{
10721 rettv->vval.v_string = vim_strsave_fnameescape(
10722 get_tv_string(&argvars[0]), FALSE);
10723 rettv->v_type = VAR_STRING;
10724}
10725
10726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 * "fnamemodify({fname}, {mods})" function
10728 */
10729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *argvars;
10732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
10734 char_u *fname;
10735 char_u *mods;
10736 int usedlen = 0;
10737 int len;
10738 char_u *fbuf = NULL;
10739 char_u buf[NUMBUFLEN];
10740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 fname = get_tv_string_chk(&argvars[0]);
10742 mods = get_tv_string_buf_chk(&argvars[1], buf);
10743 if (fname == NULL || mods == NULL)
10744 fname = NULL;
10745 else
10746 {
10747 len = (int)STRLEN(fname);
10748 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 vim_free(fbuf);
10757}
10758
Bram Moolenaar33570922005-01-25 22:26:29 +000010759static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760
10761/*
10762 * "foldclosed()" function
10763 */
10764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010767 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010768 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770#ifdef FEAT_FOLDING
10771 linenr_T lnum;
10772 linenr_T first, last;
10773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10776 {
10777 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10778 {
10779 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 return;
10784 }
10785 }
10786#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788}
10789
10790/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010791 * "foldclosed()" function
10792 */
10793 static void
10794f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010795 typval_T *argvars;
10796 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010797{
10798 foldclosed_both(argvars, rettv, FALSE);
10799}
10800
10801/*
10802 * "foldclosedend()" function
10803 */
10804 static void
10805f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010806 typval_T *argvars;
10807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808{
10809 foldclosed_both(argvars, rettv, TRUE);
10810}
10811
10812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 * "foldlevel()" function
10814 */
10815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010817 typval_T *argvars UNUSED;
10818 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819{
10820#ifdef FEAT_FOLDING
10821 linenr_T lnum;
10822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827}
10828
10829/*
10830 * "foldtext()" function
10831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010834 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837#ifdef FEAT_FOLDING
10838 linenr_T lnum;
10839 char_u *s;
10840 char_u *r;
10841 int len;
10842 char *txt;
10843#endif
10844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->v_type = VAR_STRING;
10846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10849 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10850 <= curbuf->b_ml.ml_line_count
10851 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 {
10853 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10855 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 {
10857 if (!linewhite(lnum))
10858 break;
10859 ++lnum;
10860 }
10861
10862 /* Find interesting text in this line. */
10863 s = skipwhite(ml_get(lnum));
10864 /* skip C comment-start */
10865 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010868 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010870 {
10871 s = skipwhite(ml_get(lnum + 1));
10872 if (*s == '*')
10873 s = skipwhite(s + 1);
10874 }
10875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 txt = _("+-%s%3ld lines: ");
10877 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 + 20 /* for %3ld */
10880 + STRLEN(s))); /* concatenated */
10881 if (r != NULL)
10882 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010883 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10884 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10885 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 len = (int)STRLEN(r);
10887 STRCAT(r, s);
10888 /* remove 'foldmarker' and 'commentstring' */
10889 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 }
10892 }
10893#endif
10894}
10895
10896/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010897 * "foldtextresult(lnum)" function
10898 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010901 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010902 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010903{
10904#ifdef FEAT_FOLDING
10905 linenr_T lnum;
10906 char_u *text;
10907 char_u buf[51];
10908 foldinfo_T foldinfo;
10909 int fold_count;
10910#endif
10911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->v_type = VAR_STRING;
10913 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010914#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 /* treat illegal types and illegal string values for {lnum} the same */
10917 if (lnum < 0)
10918 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010919 fold_count = foldedCount(curwin, lnum, &foldinfo);
10920 if (fold_count > 0)
10921 {
10922 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10923 &foldinfo, buf);
10924 if (text == buf)
10925 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010927 }
10928#endif
10929}
10930
10931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 * "foreground()" function
10933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010936 typval_T *argvars UNUSED;
10937 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939#ifdef FEAT_GUI
10940 if (gui.in_use)
10941 gui_mch_set_foreground();
10942#else
10943# ifdef WIN32
10944 win32_set_foreground();
10945# endif
10946#endif
10947}
10948
10949/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010950 * "function()" function
10951 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010954 typval_T *argvars;
10955 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956{
10957 char_u *s;
10958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010960 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010961 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010962 /* Don't check an autoload name for existence here. */
10963 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010964 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 else
10966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 rettv->vval.v_string = vim_strsave(s);
10968 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010969 }
10970}
10971
10972/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010973 * "garbagecollect()" function
10974 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010975 static void
10976f_garbagecollect(argvars, rettv)
10977 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010978 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010979{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010980 /* This is postponed until we are back at the toplevel, because we may be
10981 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10982 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010983
10984 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10985 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010986}
10987
10988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010989 * "get()" function
10990 */
10991 static void
10992f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010993 typval_T *argvars;
10994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010995{
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 listitem_T *li;
10997 list_T *l;
10998 dictitem_T *di;
10999 dict_T *d;
11000 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001
Bram Moolenaare9a41262005-01-15 22:18:47 +000011002 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011004 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 int error = FALSE;
11007
11008 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11009 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011011 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011012 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013 else if (argvars[0].v_type == VAR_DICT)
11014 {
11015 if ((d = argvars[0].vval.v_dict) != NULL)
11016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011017 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011018 if (di != NULL)
11019 tv = &di->di_tv;
11020 }
11021 }
11022 else
11023 EMSG2(_(e_listdictarg), "get()");
11024
11025 if (tv == NULL)
11026 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011027 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011028 copy_tv(&argvars[2], rettv);
11029 }
11030 else
11031 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011032}
11033
Bram Moolenaar342337a2005-07-21 21:11:17 +000011034static 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 +000011035
11036/*
11037 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011038 * Return a range (from start to end) of lines in rettv from the specified
11039 * buffer.
11040 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011041 */
11042 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011043get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011044 buf_T *buf;
11045 linenr_T start;
11046 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011047 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011048 typval_T *rettv;
11049{
11050 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011051
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011052 if (retlist && rettv_list_alloc(rettv) == FAIL)
11053 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011054
11055 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11056 return;
11057
11058 if (!retlist)
11059 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011060 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11061 p = ml_get_buf(buf, start, FALSE);
11062 else
11063 p = (char_u *)"";
11064
11065 rettv->v_type = VAR_STRING;
11066 rettv->vval.v_string = vim_strsave(p);
11067 }
11068 else
11069 {
11070 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011071 return;
11072
11073 if (start < 1)
11074 start = 1;
11075 if (end > buf->b_ml.ml_line_count)
11076 end = buf->b_ml.ml_line_count;
11077 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011078 if (list_append_string(rettv->vval.v_list,
11079 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011080 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011081 }
11082}
11083
11084/*
11085 * "getbufline()" function
11086 */
11087 static void
11088f_getbufline(argvars, rettv)
11089 typval_T *argvars;
11090 typval_T *rettv;
11091{
11092 linenr_T lnum;
11093 linenr_T end;
11094 buf_T *buf;
11095
11096 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11097 ++emsg_off;
11098 buf = get_buf_tv(&argvars[0]);
11099 --emsg_off;
11100
Bram Moolenaar661b1822005-07-28 22:36:45 +000011101 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011102 if (argvars[2].v_type == VAR_UNKNOWN)
11103 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011104 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011105 end = get_tv_lnum_buf(&argvars[2], buf);
11106
Bram Moolenaar342337a2005-07-21 21:11:17 +000011107 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011108}
11109
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110/*
11111 * "getbufvar()" function
11112 */
11113 static void
11114f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *argvars;
11116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117{
11118 buf_T *buf;
11119 buf_T *save_curbuf;
11120 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011121 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11124 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 ++emsg_off;
11126 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011128 if (argvars[2].v_type != VAR_UNKNOWN)
11129 /* set the default value */
11130 copy_tv(&argvars[2], rettv);
11131 else
11132 {
11133 rettv->v_type = VAR_STRING;
11134 rettv->vval.v_string = NULL;
11135 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011136
11137 if (buf != NULL && varname != NULL)
11138 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011139 /* set curbuf to be our buf, temporarily */
11140 save_curbuf = curbuf;
11141 curbuf = buf;
11142
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011145 else if (STRCMP(varname, "changedtick") == 0)
11146 {
11147 rettv->v_type = VAR_NUMBER;
11148 rettv->vval.v_number = curbuf->b_changedtick;
11149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 else
11151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 if (*varname == NUL)
11153 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11154 * scope prefix before the NUL byte is required by
11155 * find_var_in_ht(). */
11156 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011157 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011158 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011162
11163 /* restore previous notion of curbuf */
11164 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 }
11166
11167 --emsg_off;
11168}
11169
11170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 * "getchar()" function
11172 */
11173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011175 typval_T *argvars;
11176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177{
11178 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011181 /* Position the cursor. Needed after a message that ends in a space. */
11182 windgoto(msg_row, msg_col);
11183
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 ++no_mapping;
11185 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011186 for (;;)
11187 {
11188 if (argvars[0].v_type == VAR_UNKNOWN)
11189 /* getchar(): blocking wait. */
11190 n = safe_vgetc();
11191 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11192 /* getchar(1): only check if char avail */
11193 n = vpeekc();
11194 else if (error || vpeekc() == NUL)
11195 /* illegal argument or getchar(0) and no char avail: return zero */
11196 n = 0;
11197 else
11198 /* getchar(0) and char avail: return char */
11199 n = safe_vgetc();
11200 if (n == K_IGNORE)
11201 continue;
11202 break;
11203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 --no_mapping;
11205 --allow_keys;
11206
Bram Moolenaar219b8702006-11-01 14:32:36 +000011207 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11208 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11209 vimvars[VV_MOUSE_COL].vv_nr = 0;
11210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 if (IS_SPECIAL(n) || mod_mask != 0)
11213 {
11214 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11215 int i = 0;
11216
11217 /* Turn a special key into three bytes, plus modifier. */
11218 if (mod_mask != 0)
11219 {
11220 temp[i++] = K_SPECIAL;
11221 temp[i++] = KS_MODIFIER;
11222 temp[i++] = mod_mask;
11223 }
11224 if (IS_SPECIAL(n))
11225 {
11226 temp[i++] = K_SPECIAL;
11227 temp[i++] = K_SECOND(n);
11228 temp[i++] = K_THIRD(n);
11229 }
11230#ifdef FEAT_MBYTE
11231 else if (has_mbyte)
11232 i += (*mb_char2bytes)(n, temp + i);
11233#endif
11234 else
11235 temp[i++] = n;
11236 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011239
11240#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011241 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011242 {
11243 int row = mouse_row;
11244 int col = mouse_col;
11245 win_T *win;
11246 linenr_T lnum;
11247# ifdef FEAT_WINDOWS
11248 win_T *wp;
11249# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011250 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011251
11252 if (row >= 0 && col >= 0)
11253 {
11254 /* Find the window at the mouse coordinates and compute the
11255 * text position. */
11256 win = mouse_find_win(&row, &col);
11257 (void)mouse_comp_pos(win, &row, &col, &lnum);
11258# ifdef FEAT_WINDOWS
11259 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011260 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011261# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011262 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011263 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11264 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11265 }
11266 }
11267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 }
11269}
11270
11271/*
11272 * "getcharmod()" function
11273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011276 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280}
11281
11282/*
11283 * "getcmdline()" function
11284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011287 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 rettv->v_type = VAR_STRING;
11291 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292}
11293
11294/*
11295 * "getcmdpos()" function
11296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303}
11304
11305/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011306 * "getcmdtype()" function
11307 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011308 static void
11309f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011310 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011311 typval_T *rettv;
11312{
11313 rettv->v_type = VAR_STRING;
11314 rettv->vval.v_string = alloc(2);
11315 if (rettv->vval.v_string != NULL)
11316 {
11317 rettv->vval.v_string[0] = get_cmdline_type();
11318 rettv->vval.v_string[1] = NUL;
11319 }
11320}
11321
11322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 * "getcwd()" function
11324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011327 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011330 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011333 rettv->vval.v_string = NULL;
11334 cwd = alloc(MAXPATHL);
11335 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011337 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11338 {
11339 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011341 if (rettv->vval.v_string != NULL)
11342 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011344 }
11345 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 }
11347}
11348
11349/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011350 * "getfontname()" function
11351 */
11352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011354 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011359#ifdef FEAT_GUI
11360 if (gui.in_use)
11361 {
11362 GuiFont font;
11363 char_u *name = NULL;
11364
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011365 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011366 {
11367 /* Get the "Normal" font. Either the name saved by
11368 * hl_set_font_name() or from the font ID. */
11369 font = gui.norm_font;
11370 name = hl_get_font_name();
11371 }
11372 else
11373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011375 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11376 return;
11377 font = gui_mch_get_font(name, FALSE);
11378 if (font == NOFONT)
11379 return; /* Invalid font name, return empty string. */
11380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011382 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011383 gui_mch_free_font(font);
11384 }
11385#endif
11386}
11387
11388/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011389 * "getfperm({fname})" function
11390 */
11391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011393 typval_T *argvars;
11394 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011395{
11396 char_u *fname;
11397 struct stat st;
11398 char_u *perm = NULL;
11399 char_u flags[] = "rwx";
11400 int i;
11401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011405 if (mch_stat((char *)fname, &st) >= 0)
11406 {
11407 perm = vim_strsave((char_u *)"---------");
11408 if (perm != NULL)
11409 {
11410 for (i = 0; i < 9; i++)
11411 {
11412 if (st.st_mode & (1 << (8 - i)))
11413 perm[i] = flags[i % 3];
11414 }
11415 }
11416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011418}
11419
11420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 * "getfsize({fname})" function
11422 */
11423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011425 typval_T *argvars;
11426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
11428 char_u *fname;
11429 struct stat st;
11430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434
11435 if (mch_stat((char *)fname, &st) >= 0)
11436 {
11437 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011442
11443 /* non-perfect check for overflow */
11444 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11445 rettv->vval.v_number = -2;
11446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 }
11448 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450}
11451
11452/*
11453 * "getftime({fname})" function
11454 */
11455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *argvars;
11458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460 char_u *fname;
11461 struct stat st;
11462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464
11465 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469}
11470
11471/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011472 * "getftype({fname})" function
11473 */
11474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *argvars;
11477 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011478{
11479 char_u *fname;
11480 struct stat st;
11481 char_u *type = NULL;
11482 char *t;
11483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011487 if (mch_lstat((char *)fname, &st) >= 0)
11488 {
11489#ifdef S_ISREG
11490 if (S_ISREG(st.st_mode))
11491 t = "file";
11492 else if (S_ISDIR(st.st_mode))
11493 t = "dir";
11494# ifdef S_ISLNK
11495 else if (S_ISLNK(st.st_mode))
11496 t = "link";
11497# endif
11498# ifdef S_ISBLK
11499 else if (S_ISBLK(st.st_mode))
11500 t = "bdev";
11501# endif
11502# ifdef S_ISCHR
11503 else if (S_ISCHR(st.st_mode))
11504 t = "cdev";
11505# endif
11506# ifdef S_ISFIFO
11507 else if (S_ISFIFO(st.st_mode))
11508 t = "fifo";
11509# endif
11510# ifdef S_ISSOCK
11511 else if (S_ISSOCK(st.st_mode))
11512 t = "fifo";
11513# endif
11514 else
11515 t = "other";
11516#else
11517# ifdef S_IFMT
11518 switch (st.st_mode & S_IFMT)
11519 {
11520 case S_IFREG: t = "file"; break;
11521 case S_IFDIR: t = "dir"; break;
11522# ifdef S_IFLNK
11523 case S_IFLNK: t = "link"; break;
11524# endif
11525# ifdef S_IFBLK
11526 case S_IFBLK: t = "bdev"; break;
11527# endif
11528# ifdef S_IFCHR
11529 case S_IFCHR: t = "cdev"; break;
11530# endif
11531# ifdef S_IFIFO
11532 case S_IFIFO: t = "fifo"; break;
11533# endif
11534# ifdef S_IFSOCK
11535 case S_IFSOCK: t = "socket"; break;
11536# endif
11537 default: t = "other";
11538 }
11539# else
11540 if (mch_isdir(fname))
11541 t = "dir";
11542 else
11543 t = "file";
11544# endif
11545#endif
11546 type = vim_strsave((char_u *)t);
11547 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011549}
11550
11551/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011552 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011553 */
11554 static void
11555f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011556 typval_T *argvars;
11557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558{
11559 linenr_T lnum;
11560 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011561 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011562
11563 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011564 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011565 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011566 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011567 retlist = FALSE;
11568 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011570 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011571 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011572 retlist = TRUE;
11573 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011574
Bram Moolenaar342337a2005-07-21 21:11:17 +000011575 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011576}
11577
11578/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011579 * "getmatches()" function
11580 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011581 static void
11582f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011583 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011584 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011585{
11586#ifdef FEAT_SEARCH_EXTRA
11587 dict_T *dict;
11588 matchitem_T *cur = curwin->w_match_head;
11589
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011590 if (rettv_list_alloc(rettv) == OK)
11591 {
11592 while (cur != NULL)
11593 {
11594 dict = dict_alloc();
11595 if (dict == NULL)
11596 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011597 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11598 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11599 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11600 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11601 list_append_dict(rettv->vval.v_list, dict);
11602 cur = cur->next;
11603 }
11604 }
11605#endif
11606}
11607
11608/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011609 * "getpid()" function
11610 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011611 static void
11612f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011613 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011614 typval_T *rettv;
11615{
11616 rettv->vval.v_number = mch_get_pid();
11617}
11618
11619/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011620 * "getpos(string)" function
11621 */
11622 static void
11623f_getpos(argvars, rettv)
11624 typval_T *argvars;
11625 typval_T *rettv;
11626{
11627 pos_T *fp;
11628 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011629 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011630
11631 if (rettv_list_alloc(rettv) == OK)
11632 {
11633 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011634 fp = var2fpos(&argvars[0], TRUE, &fnum);
11635 if (fnum != -1)
11636 list_append_number(l, (varnumber_T)fnum);
11637 else
11638 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011639 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11640 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011641 list_append_number(l, (fp != NULL)
11642 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011643 : (varnumber_T)0);
11644 list_append_number(l,
11645#ifdef FEAT_VIRTUALEDIT
11646 (fp != NULL) ? (varnumber_T)fp->coladd :
11647#endif
11648 (varnumber_T)0);
11649 }
11650 else
11651 rettv->vval.v_number = FALSE;
11652}
11653
11654/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011655 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011656 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011657 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011658f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011659 typval_T *argvars UNUSED;
11660 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011661{
11662#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011663 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664#endif
11665
Bram Moolenaar2641f772005-03-25 21:58:17 +000011666#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011667 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011668 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011669 wp = NULL;
11670 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11671 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011672 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011673 if (wp == NULL)
11674 return;
11675 }
11676
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011677 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011678 }
11679#endif
11680}
11681
11682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 * "getreg()" function
11684 */
11685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011687 typval_T *argvars;
11688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689{
11690 char_u *strregname;
11691 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011692 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011695 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 strregname = get_tv_string_chk(&argvars[0]);
11698 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011699 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011700 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 regname = (strregname == NULL ? '"' : *strregname);
11705 if (regname == 0)
11706 regname = '"';
11707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 rettv->vval.v_string = error ? NULL :
11710 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711}
11712
11713/*
11714 * "getregtype()" function
11715 */
11716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720{
11721 char_u *strregname;
11722 int regname;
11723 char_u buf[NUMBUFLEN + 2];
11724 long reglen = 0;
11725
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011726 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 {
11728 strregname = get_tv_string_chk(&argvars[0]);
11729 if (strregname == NULL) /* type error; errmsg already given */
11730 {
11731 rettv->v_type = VAR_STRING;
11732 rettv->vval.v_string = NULL;
11733 return;
11734 }
11735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 else
11737 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739
11740 regname = (strregname == NULL ? '"' : *strregname);
11741 if (regname == 0)
11742 regname = '"';
11743
11744 buf[0] = NUL;
11745 buf[1] = NUL;
11746 switch (get_reg_type(regname, &reglen))
11747 {
11748 case MLINE: buf[0] = 'V'; break;
11749 case MCHAR: buf[0] = 'v'; break;
11750#ifdef FEAT_VISUAL
11751 case MBLOCK:
11752 buf[0] = Ctrl_V;
11753 sprintf((char *)buf + 1, "%ld", reglen + 1);
11754 break;
11755#endif
11756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->v_type = VAR_STRING;
11758 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759}
11760
11761/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011762 * "gettabvar()" function
11763 */
11764 static void
11765f_gettabvar(argvars, rettv)
11766 typval_T *argvars;
11767 typval_T *rettv;
11768{
11769 tabpage_T *tp;
11770 dictitem_T *v;
11771 char_u *varname;
11772
11773 rettv->v_type = VAR_STRING;
11774 rettv->vval.v_string = NULL;
11775
11776 varname = get_tv_string_chk(&argvars[1]);
11777 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11778 if (tp != NULL && varname != NULL)
11779 {
11780 /* look up the variable */
11781 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11782 if (v != NULL)
11783 copy_tv(&v->di_tv, rettv);
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011784 else if (argvars[2].v_type != VAR_UNKNOWN)
11785 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011786 }
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011787 else if (argvars[2].v_type != VAR_UNKNOWN)
11788 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011789}
11790
11791/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011792 * "gettabwinvar()" function
11793 */
11794 static void
11795f_gettabwinvar(argvars, rettv)
11796 typval_T *argvars;
11797 typval_T *rettv;
11798{
11799 getwinvar(argvars, rettv, 1);
11800}
11801
11802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 * "getwinposx()" function
11804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011807 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811#ifdef FEAT_GUI
11812 if (gui.in_use)
11813 {
11814 int x, y;
11815
11816 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 }
11819#endif
11820}
11821
11822/*
11823 * "getwinposy()" function
11824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011827 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831#ifdef FEAT_GUI
11832 if (gui.in_use)
11833 {
11834 int x, y;
11835
11836 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 }
11839#endif
11840}
11841
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011842/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011843 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011844 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011845 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011846find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011847 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011848 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011849{
11850#ifdef FEAT_WINDOWS
11851 win_T *wp;
11852#endif
11853 int nr;
11854
11855 nr = get_tv_number_chk(vp, NULL);
11856
11857#ifdef FEAT_WINDOWS
11858 if (nr < 0)
11859 return NULL;
11860 if (nr == 0)
11861 return curwin;
11862
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011863 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11864 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011865 if (--nr <= 0)
11866 break;
11867 return wp;
11868#else
11869 if (nr == 0 || nr == 1)
11870 return curwin;
11871 return NULL;
11872#endif
11873}
11874
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875/*
11876 * "getwinvar()" function
11877 */
11878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 typval_T *argvars;
11881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011883 getwinvar(argvars, rettv, 0);
11884}
11885
11886/*
11887 * getwinvar() and gettabwinvar()
11888 */
11889 static void
11890getwinvar(argvars, rettv, off)
11891 typval_T *argvars;
11892 typval_T *rettv;
11893 int off; /* 1 for gettabwinvar() */
11894{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 win_T *win, *oldcurwin;
11896 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011898 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011900#ifdef FEAT_WINDOWS
11901 if (off == 1)
11902 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11903 else
11904 tp = curtab;
11905#endif
11906 win = find_win_by_nr(&argvars[off], tp);
11907 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011910 if (argvars[off + 2].v_type != VAR_UNKNOWN)
11911 /* set the default return value */
11912 copy_tv(&argvars[off + 2], rettv);
11913 else
11914 {
11915 rettv->v_type = VAR_STRING;
11916 rettv->vval.v_string = NULL;
11917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918
11919 if (win != NULL && varname != NULL)
11920 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011921 /* Set curwin to be our win, temporarily. Also set curbuf, so
11922 * that we can get buffer-local options. */
11923 oldcurwin = curwin;
11924 curwin = win;
11925 curbuf = win->w_buffer;
11926
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 else
11930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 if (*varname == NUL)
11932 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11933 * scope prefix before the NUL byte is required by
11934 * find_var_in_ht(). */
11935 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011937 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011939 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011941
11942 /* restore previous notion of curwin */
11943 curwin = oldcurwin;
11944 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 }
11946
11947 --emsg_off;
11948}
11949
11950/*
11951 * "glob()" function
11952 */
11953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011954f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011955 typval_T *argvars;
11956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011958 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011960 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011962 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011963 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011965 if (argvars[1].v_type != VAR_UNKNOWN)
11966 {
11967 if (get_tv_number_chk(&argvars[1], &error))
11968 options |= WILD_KEEP_ALL;
11969 if (argvars[2].v_type != VAR_UNKNOWN
11970 && get_tv_number_chk(&argvars[2], &error))
11971 {
11972 rettv->v_type = VAR_LIST;
11973 rettv->vval.v_list = NULL;
11974 }
11975 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011976 if (!error)
11977 {
11978 ExpandInit(&xpc);
11979 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011980 if (p_wic)
11981 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011982 if (rettv->v_type == VAR_STRING)
11983 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011984 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011985 else if (rettv_list_alloc(rettv) != FAIL)
11986 {
11987 int i;
11988
11989 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11990 NULL, options, WILD_ALL_KEEP);
11991 for (i = 0; i < xpc.xp_numfiles; i++)
11992 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11993
11994 ExpandCleanup(&xpc);
11995 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011996 }
11997 else
11998 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999}
12000
12001/*
12002 * "globpath()" function
12003 */
12004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012006 typval_T *argvars;
12007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012012 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012014 /* When the optional second argument is non-zero, don't remove matches
12015 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12016 if (argvars[2].v_type != VAR_UNKNOWN
12017 && get_tv_number_chk(&argvars[2], &error))
12018 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012020 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 rettv->vval.v_string = NULL;
12022 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012023 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12024 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025}
12026
12027/*
12028 * "has()" function
12029 */
12030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 typval_T *argvars;
12033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034{
12035 int i;
12036 char_u *name;
12037 int n = FALSE;
12038 static char *(has_list[]) =
12039 {
12040#ifdef AMIGA
12041 "amiga",
12042# ifdef FEAT_ARP
12043 "arp",
12044# endif
12045#endif
12046#ifdef __BEOS__
12047 "beos",
12048#endif
12049#ifdef MSDOS
12050# ifdef DJGPP
12051 "dos32",
12052# else
12053 "dos16",
12054# endif
12055#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012056#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 "mac",
12058#endif
12059#if defined(MACOS_X_UNIX)
12060 "macunix",
12061#endif
12062#ifdef OS2
12063 "os2",
12064#endif
12065#ifdef __QNX__
12066 "qnx",
12067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068#ifdef UNIX
12069 "unix",
12070#endif
12071#ifdef VMS
12072 "vms",
12073#endif
12074#ifdef WIN16
12075 "win16",
12076#endif
12077#ifdef WIN32
12078 "win32",
12079#endif
12080#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12081 "win32unix",
12082#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012083#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 "win64",
12085#endif
12086#ifdef EBCDIC
12087 "ebcdic",
12088#endif
12089#ifndef CASE_INSENSITIVE_FILENAME
12090 "fname_case",
12091#endif
12092#ifdef FEAT_ARABIC
12093 "arabic",
12094#endif
12095#ifdef FEAT_AUTOCMD
12096 "autocmd",
12097#endif
12098#ifdef FEAT_BEVAL
12099 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012100# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12101 "balloon_multiline",
12102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#endif
12104#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12105 "builtin_terms",
12106# ifdef ALL_BUILTIN_TCAPS
12107 "all_builtin_terms",
12108# endif
12109#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012110#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12111 || defined(FEAT_GUI_W32) \
12112 || defined(FEAT_GUI_MOTIF))
12113 "browsefilter",
12114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef FEAT_BYTEOFF
12116 "byte_offset",
12117#endif
12118#ifdef FEAT_CINDENT
12119 "cindent",
12120#endif
12121#ifdef FEAT_CLIENTSERVER
12122 "clientserver",
12123#endif
12124#ifdef FEAT_CLIPBOARD
12125 "clipboard",
12126#endif
12127#ifdef FEAT_CMDL_COMPL
12128 "cmdline_compl",
12129#endif
12130#ifdef FEAT_CMDHIST
12131 "cmdline_hist",
12132#endif
12133#ifdef FEAT_COMMENTS
12134 "comments",
12135#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012136#ifdef FEAT_CONCEAL
12137 "conceal",
12138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139#ifdef FEAT_CRYPT
12140 "cryptv",
12141#endif
12142#ifdef FEAT_CSCOPE
12143 "cscope",
12144#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012145#ifdef FEAT_CURSORBIND
12146 "cursorbind",
12147#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012148#ifdef CURSOR_SHAPE
12149 "cursorshape",
12150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151#ifdef DEBUG
12152 "debug",
12153#endif
12154#ifdef FEAT_CON_DIALOG
12155 "dialog_con",
12156#endif
12157#ifdef FEAT_GUI_DIALOG
12158 "dialog_gui",
12159#endif
12160#ifdef FEAT_DIFF
12161 "diff",
12162#endif
12163#ifdef FEAT_DIGRAPHS
12164 "digraphs",
12165#endif
12166#ifdef FEAT_DND
12167 "dnd",
12168#endif
12169#ifdef FEAT_EMACS_TAGS
12170 "emacs_tags",
12171#endif
12172 "eval", /* always present, of course! */
12173#ifdef FEAT_EX_EXTRA
12174 "ex_extra",
12175#endif
12176#ifdef FEAT_SEARCH_EXTRA
12177 "extra_search",
12178#endif
12179#ifdef FEAT_FKMAP
12180 "farsi",
12181#endif
12182#ifdef FEAT_SEARCHPATH
12183 "file_in_path",
12184#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012185#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012186 "filterpipe",
12187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#ifdef FEAT_FIND_ID
12189 "find_in_path",
12190#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012191#ifdef FEAT_FLOAT
12192 "float",
12193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#ifdef FEAT_FOLDING
12195 "folding",
12196#endif
12197#ifdef FEAT_FOOTER
12198 "footer",
12199#endif
12200#if !defined(USE_SYSTEM) && defined(UNIX)
12201 "fork",
12202#endif
12203#ifdef FEAT_GETTEXT
12204 "gettext",
12205#endif
12206#ifdef FEAT_GUI
12207 "gui",
12208#endif
12209#ifdef FEAT_GUI_ATHENA
12210# ifdef FEAT_GUI_NEXTAW
12211 "gui_neXtaw",
12212# else
12213 "gui_athena",
12214# endif
12215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216#ifdef FEAT_GUI_GTK
12217 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012220#ifdef FEAT_GUI_GNOME
12221 "gui_gnome",
12222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223#ifdef FEAT_GUI_MAC
12224 "gui_mac",
12225#endif
12226#ifdef FEAT_GUI_MOTIF
12227 "gui_motif",
12228#endif
12229#ifdef FEAT_GUI_PHOTON
12230 "gui_photon",
12231#endif
12232#ifdef FEAT_GUI_W16
12233 "gui_win16",
12234#endif
12235#ifdef FEAT_GUI_W32
12236 "gui_win32",
12237#endif
12238#ifdef FEAT_HANGULIN
12239 "hangul_input",
12240#endif
12241#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12242 "iconv",
12243#endif
12244#ifdef FEAT_INS_EXPAND
12245 "insert_expand",
12246#endif
12247#ifdef FEAT_JUMPLIST
12248 "jumplist",
12249#endif
12250#ifdef FEAT_KEYMAP
12251 "keymap",
12252#endif
12253#ifdef FEAT_LANGMAP
12254 "langmap",
12255#endif
12256#ifdef FEAT_LIBCALL
12257 "libcall",
12258#endif
12259#ifdef FEAT_LINEBREAK
12260 "linebreak",
12261#endif
12262#ifdef FEAT_LISP
12263 "lispindent",
12264#endif
12265#ifdef FEAT_LISTCMDS
12266 "listcmds",
12267#endif
12268#ifdef FEAT_LOCALMAP
12269 "localmap",
12270#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012271#ifdef FEAT_LUA
12272# ifndef DYNAMIC_LUA
12273 "lua",
12274# endif
12275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276#ifdef FEAT_MENU
12277 "menu",
12278#endif
12279#ifdef FEAT_SESSION
12280 "mksession",
12281#endif
12282#ifdef FEAT_MODIFY_FNAME
12283 "modify_fname",
12284#endif
12285#ifdef FEAT_MOUSE
12286 "mouse",
12287#endif
12288#ifdef FEAT_MOUSESHAPE
12289 "mouseshape",
12290#endif
12291#if defined(UNIX) || defined(VMS)
12292# ifdef FEAT_MOUSE_DEC
12293 "mouse_dec",
12294# endif
12295# ifdef FEAT_MOUSE_GPM
12296 "mouse_gpm",
12297# endif
12298# ifdef FEAT_MOUSE_JSB
12299 "mouse_jsbterm",
12300# endif
12301# ifdef FEAT_MOUSE_NET
12302 "mouse_netterm",
12303# endif
12304# ifdef FEAT_MOUSE_PTERM
12305 "mouse_pterm",
12306# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012307# ifdef FEAT_MOUSE_SGR
12308 "mouse_sgr",
12309# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012310# ifdef FEAT_SYSMOUSE
12311 "mouse_sysmouse",
12312# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012313# ifdef FEAT_MOUSE_URXVT
12314 "mouse_urxvt",
12315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316# ifdef FEAT_MOUSE_XTERM
12317 "mouse_xterm",
12318# endif
12319#endif
12320#ifdef FEAT_MBYTE
12321 "multi_byte",
12322#endif
12323#ifdef FEAT_MBYTE_IME
12324 "multi_byte_ime",
12325#endif
12326#ifdef FEAT_MULTI_LANG
12327 "multi_lang",
12328#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012329#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012330#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012331 "mzscheme",
12332#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_OLE
12335 "ole",
12336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337#ifdef FEAT_PATH_EXTRA
12338 "path_extra",
12339#endif
12340#ifdef FEAT_PERL
12341#ifndef DYNAMIC_PERL
12342 "perl",
12343#endif
12344#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012345#ifdef FEAT_PERSISTENT_UNDO
12346 "persistent_undo",
12347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348#ifdef FEAT_PYTHON
12349#ifndef DYNAMIC_PYTHON
12350 "python",
12351#endif
12352#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012353#ifdef FEAT_PYTHON3
12354#ifndef DYNAMIC_PYTHON3
12355 "python3",
12356#endif
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_POSTSCRIPT
12359 "postscript",
12360#endif
12361#ifdef FEAT_PRINTER
12362 "printer",
12363#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012364#ifdef FEAT_PROFILE
12365 "profile",
12366#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012367#ifdef FEAT_RELTIME
12368 "reltime",
12369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370#ifdef FEAT_QUICKFIX
12371 "quickfix",
12372#endif
12373#ifdef FEAT_RIGHTLEFT
12374 "rightleft",
12375#endif
12376#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12377 "ruby",
12378#endif
12379#ifdef FEAT_SCROLLBIND
12380 "scrollbind",
12381#endif
12382#ifdef FEAT_CMDL_INFO
12383 "showcmd",
12384 "cmdline_info",
12385#endif
12386#ifdef FEAT_SIGNS
12387 "signs",
12388#endif
12389#ifdef FEAT_SMARTINDENT
12390 "smartindent",
12391#endif
12392#ifdef FEAT_SNIFF
12393 "sniff",
12394#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012395#ifdef STARTUPTIME
12396 "startuptime",
12397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#ifdef FEAT_STL_OPT
12399 "statusline",
12400#endif
12401#ifdef FEAT_SUN_WORKSHOP
12402 "sun_workshop",
12403#endif
12404#ifdef FEAT_NETBEANS_INTG
12405 "netbeans_intg",
12406#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012407#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012408 "spell",
12409#endif
12410#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411 "syntax",
12412#endif
12413#if defined(USE_SYSTEM) || !defined(UNIX)
12414 "system",
12415#endif
12416#ifdef FEAT_TAG_BINS
12417 "tag_binary",
12418#endif
12419#ifdef FEAT_TAG_OLDSTATIC
12420 "tag_old_static",
12421#endif
12422#ifdef FEAT_TAG_ANYWHITE
12423 "tag_any_white",
12424#endif
12425#ifdef FEAT_TCL
12426# ifndef DYNAMIC_TCL
12427 "tcl",
12428# endif
12429#endif
12430#ifdef TERMINFO
12431 "terminfo",
12432#endif
12433#ifdef FEAT_TERMRESPONSE
12434 "termresponse",
12435#endif
12436#ifdef FEAT_TEXTOBJ
12437 "textobjects",
12438#endif
12439#ifdef HAVE_TGETENT
12440 "tgetent",
12441#endif
12442#ifdef FEAT_TITLE
12443 "title",
12444#endif
12445#ifdef FEAT_TOOLBAR
12446 "toolbar",
12447#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012448#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12449 "unnamedplus",
12450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451#ifdef FEAT_USR_CMDS
12452 "user-commands", /* was accidentally included in 5.4 */
12453 "user_commands",
12454#endif
12455#ifdef FEAT_VIMINFO
12456 "viminfo",
12457#endif
12458#ifdef FEAT_VERTSPLIT
12459 "vertsplit",
12460#endif
12461#ifdef FEAT_VIRTUALEDIT
12462 "virtualedit",
12463#endif
12464#ifdef FEAT_VISUAL
12465 "visual",
12466#endif
12467#ifdef FEAT_VISUALEXTRA
12468 "visualextra",
12469#endif
12470#ifdef FEAT_VREPLACE
12471 "vreplace",
12472#endif
12473#ifdef FEAT_WILDIGN
12474 "wildignore",
12475#endif
12476#ifdef FEAT_WILDMENU
12477 "wildmenu",
12478#endif
12479#ifdef FEAT_WINDOWS
12480 "windows",
12481#endif
12482#ifdef FEAT_WAK
12483 "winaltkeys",
12484#endif
12485#ifdef FEAT_WRITEBACKUP
12486 "writebackup",
12487#endif
12488#ifdef FEAT_XIM
12489 "xim",
12490#endif
12491#ifdef FEAT_XFONTSET
12492 "xfontset",
12493#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012494#ifdef FEAT_XPM_W32
12495 "xpm_w32",
12496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497#ifdef USE_XSMP
12498 "xsmp",
12499#endif
12500#ifdef USE_XSMP_INTERACT
12501 "xsmp_interact",
12502#endif
12503#ifdef FEAT_XCLIPBOARD
12504 "xterm_clipboard",
12505#endif
12506#ifdef FEAT_XTERM_SAVE
12507 "xterm_save",
12508#endif
12509#if defined(UNIX) && defined(FEAT_X11)
12510 "X11",
12511#endif
12512 NULL
12513 };
12514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 for (i = 0; has_list[i] != NULL; ++i)
12517 if (STRICMP(name, has_list[i]) == 0)
12518 {
12519 n = TRUE;
12520 break;
12521 }
12522
12523 if (n == FALSE)
12524 {
12525 if (STRNICMP(name, "patch", 5) == 0)
12526 n = has_patch(atoi((char *)name + 5));
12527 else if (STRICMP(name, "vim_starting") == 0)
12528 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012529#ifdef FEAT_MBYTE
12530 else if (STRICMP(name, "multi_byte_encoding") == 0)
12531 n = has_mbyte;
12532#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012533#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12534 else if (STRICMP(name, "balloon_multiline") == 0)
12535 n = multiline_balloon_available();
12536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537#ifdef DYNAMIC_TCL
12538 else if (STRICMP(name, "tcl") == 0)
12539 n = tcl_enabled(FALSE);
12540#endif
12541#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12542 else if (STRICMP(name, "iconv") == 0)
12543 n = iconv_enabled(FALSE);
12544#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012545#ifdef DYNAMIC_LUA
12546 else if (STRICMP(name, "lua") == 0)
12547 n = lua_enabled(FALSE);
12548#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012549#ifdef DYNAMIC_MZSCHEME
12550 else if (STRICMP(name, "mzscheme") == 0)
12551 n = mzscheme_enabled(FALSE);
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553#ifdef DYNAMIC_RUBY
12554 else if (STRICMP(name, "ruby") == 0)
12555 n = ruby_enabled(FALSE);
12556#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012557#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558#ifdef DYNAMIC_PYTHON
12559 else if (STRICMP(name, "python") == 0)
12560 n = python_enabled(FALSE);
12561#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012562#endif
12563#ifdef FEAT_PYTHON3
12564#ifdef DYNAMIC_PYTHON3
12565 else if (STRICMP(name, "python3") == 0)
12566 n = python3_enabled(FALSE);
12567#endif
12568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569#ifdef DYNAMIC_PERL
12570 else if (STRICMP(name, "perl") == 0)
12571 n = perl_enabled(FALSE);
12572#endif
12573#ifdef FEAT_GUI
12574 else if (STRICMP(name, "gui_running") == 0)
12575 n = (gui.in_use || gui.starting);
12576# ifdef FEAT_GUI_W32
12577 else if (STRICMP(name, "gui_win32s") == 0)
12578 n = gui_is_win32s();
12579# endif
12580# ifdef FEAT_BROWSE
12581 else if (STRICMP(name, "browse") == 0)
12582 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12583# endif
12584#endif
12585#ifdef FEAT_SYN_HL
12586 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012587 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588#endif
12589#if defined(WIN3264)
12590 else if (STRICMP(name, "win95") == 0)
12591 n = mch_windows95();
12592#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012593#ifdef FEAT_NETBEANS_INTG
12594 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012595 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 }
12598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600}
12601
12602/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012603 * "has_key()" function
12604 */
12605 static void
12606f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 typval_T *argvars;
12608 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012609{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012610 if (argvars[0].v_type != VAR_DICT)
12611 {
12612 EMSG(_(e_dictreq));
12613 return;
12614 }
12615 if (argvars[0].vval.v_dict == NULL)
12616 return;
12617
12618 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012619 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012620}
12621
12622/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012623 * "haslocaldir()" function
12624 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012625 static void
12626f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012627 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012628 typval_T *rettv;
12629{
12630 rettv->vval.v_number = (curwin->w_localdir != NULL);
12631}
12632
12633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 * "hasmapto()" function
12635 */
12636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *argvars;
12639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640{
12641 char_u *name;
12642 char_u *mode;
12643 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012644 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012647 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 mode = (char_u *)"nvo";
12649 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012652 if (argvars[2].v_type != VAR_UNKNOWN)
12653 abbr = get_tv_number(&argvars[2]);
12654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655
Bram Moolenaar2c932302006-03-18 21:42:09 +000012656 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660}
12661
12662/*
12663 * "histadd()" function
12664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669{
12670#ifdef FEAT_CMDHIST
12671 int histype;
12672 char_u *str;
12673 char_u buf[NUMBUFLEN];
12674#endif
12675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 if (check_restricted() || check_secure())
12678 return;
12679#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012680 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12681 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 if (histype >= 0)
12683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 if (*str != NUL)
12686 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012687 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 return;
12691 }
12692 }
12693#endif
12694}
12695
12696/*
12697 * "histdel()" function
12698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012701 typval_T *argvars UNUSED;
12702 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703{
12704#ifdef FEAT_CMDHIST
12705 int n;
12706 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12710 if (str == NULL)
12711 n = 0;
12712 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012715 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 else
12720 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 get_tv_string_buf(&argvars[1], buf));
12723 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724#endif
12725}
12726
12727/*
12728 * "histget()" function
12729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734{
12735#ifdef FEAT_CMDHIST
12736 int type;
12737 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12741 if (str == NULL)
12742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 {
12745 type = get_histtype(str);
12746 if (argvars[1].v_type == VAR_UNKNOWN)
12747 idx = get_history_idx(type);
12748 else
12749 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12750 /* -1 on type error */
12751 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757}
12758
12759/*
12760 * "histnr()" function
12761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012764 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766{
12767 int i;
12768
12769#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 char_u *history = get_tv_string_chk(&argvars[0]);
12771
12772 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 if (i >= HIST_CMD && i < HIST_COUNT)
12774 i = get_history_idx(i);
12775 else
12776#endif
12777 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779}
12780
12781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 * "highlightID(name)" function
12783 */
12784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790}
12791
12792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012793 * "highlight_exists()" function
12794 */
12795 static void
12796f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 typval_T *argvars;
12798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012799{
12800 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12801}
12802
12803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 * "hostname()" function
12805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
12811 char_u hostname[256];
12812
12813 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->v_type = VAR_STRING;
12815 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816}
12817
12818/*
12819 * iconv() function
12820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
12826#ifdef FEAT_MBYTE
12827 char_u buf1[NUMBUFLEN];
12828 char_u buf2[NUMBUFLEN];
12829 char_u *from, *to, *str;
12830 vimconv_T vimconv;
12831#endif
12832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->v_type = VAR_STRING;
12834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835
12836#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 str = get_tv_string(&argvars[0]);
12838 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12839 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 vimconv.vc_type = CONV_NONE;
12841 convert_setup(&vimconv, from, to);
12842
12843 /* If the encodings are equal, no conversion needed. */
12844 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
12849 convert_setup(&vimconv, NULL, NULL);
12850 vim_free(from);
12851 vim_free(to);
12852#endif
12853}
12854
12855/*
12856 * "indent()" function
12857 */
12858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *argvars;
12861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
12863 linenr_T lnum;
12864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012872/*
12873 * "index()" function
12874 */
12875 static void
12876f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012879{
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 list_T *l;
12881 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012882 long idx = 0;
12883 int ic = FALSE;
12884
12885 rettv->vval.v_number = -1;
12886 if (argvars[0].v_type != VAR_LIST)
12887 {
12888 EMSG(_(e_listreq));
12889 return;
12890 }
12891 l = argvars[0].vval.v_list;
12892 if (l != NULL)
12893 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012894 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 int error = FALSE;
12898
Bram Moolenaar758711c2005-02-02 23:11:38 +000012899 /* Start at specified item. Use the cached index that list_find()
12900 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012902 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012903 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904 ic = get_tv_number_chk(&argvars[3], &error);
12905 if (error)
12906 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012907 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012908
Bram Moolenaar758711c2005-02-02 23:11:38 +000012909 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012910 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012911 {
12912 rettv->vval.v_number = idx;
12913 break;
12914 }
12915 }
12916}
12917
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918static int inputsecret_flag = 0;
12919
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012920static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12921
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012923 * This function is used by f_input() and f_inputdialog() functions. The third
12924 * argument to f_input() specifies the type of completion to use at the
12925 * prompt. The third argument to f_inputdialog() specifies the value to return
12926 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 */
12928 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012929get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012932 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 char_u *p = NULL;
12936 int c;
12937 char_u buf[NUMBUFLEN];
12938 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012939 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012940 int xp_type = EXPAND_NOTHING;
12941 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
12946#ifdef NO_CONSOLE_INPUT
12947 /* While starting up, there is no place to enter text. */
12948 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950#endif
12951
12952 cmd_silent = FALSE; /* Want to see the prompt. */
12953 if (prompt != NULL)
12954 {
12955 /* Only the part of the message after the last NL is considered as
12956 * prompt for the command line */
12957 p = vim_strrchr(prompt, '\n');
12958 if (p == NULL)
12959 p = prompt;
12960 else
12961 {
12962 ++p;
12963 c = *p;
12964 *p = NUL;
12965 msg_start();
12966 msg_clr_eos();
12967 msg_puts_attr(prompt, echo_attr);
12968 msg_didout = FALSE;
12969 msg_starthere();
12970 *p = c;
12971 }
12972 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012974 if (argvars[1].v_type != VAR_UNKNOWN)
12975 {
12976 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12977 if (defstr != NULL)
12978 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012980 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012981 {
12982 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012983 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012984 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012985
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012986 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012987 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012988
Bram Moolenaar4463f292005-09-25 22:20:24 +000012989 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12990 if (xp_name == NULL)
12991 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012992
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012993 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012994
Bram Moolenaar4463f292005-09-25 22:20:24 +000012995 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12996 &xp_arg) == FAIL)
12997 return;
12998 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012999 }
13000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013001 if (defstr != NULL)
13002 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013003 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13004 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013005 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013006 && argvars[1].v_type != VAR_UNKNOWN
13007 && argvars[2].v_type != VAR_UNKNOWN)
13008 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13009 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013010
13011 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 /* since the user typed this, no need to wait for return */
13014 need_wait_return = FALSE;
13015 msg_didout = FALSE;
13016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 cmd_silent = cmd_silent_save;
13018}
13019
13020/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013021 * "input()" function
13022 * Also handles inputsecret() when inputsecret is set.
13023 */
13024 static void
13025f_input(argvars, rettv)
13026 typval_T *argvars;
13027 typval_T *rettv;
13028{
13029 get_user_input(argvars, rettv, FALSE);
13030}
13031
13032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 * "inputdialog()" function
13034 */
13035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 typval_T *argvars;
13038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039{
13040#if defined(FEAT_GUI_TEXTDIALOG)
13041 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13042 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13043 {
13044 char_u *message;
13045 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013048 message = get_tv_string_chk(&argvars[0]);
13049 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013050 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013051 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 else
13053 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 if (message != NULL && defstr != NULL
13055 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013056 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 else
13059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 if (message != NULL && defstr != NULL
13061 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013062 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 rettv->vval.v_string = vim_strsave(
13064 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 }
13070 else
13071#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013072 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073}
13074
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013075/*
13076 * "inputlist()" function
13077 */
13078 static void
13079f_inputlist(argvars, rettv)
13080 typval_T *argvars;
13081 typval_T *rettv;
13082{
13083 listitem_T *li;
13084 int selected;
13085 int mouse_used;
13086
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013087#ifdef NO_CONSOLE_INPUT
13088 /* While starting up, there is no place to enter text. */
13089 if (no_console_input())
13090 return;
13091#endif
13092 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13093 {
13094 EMSG2(_(e_listarg), "inputlist()");
13095 return;
13096 }
13097
13098 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013099 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013100 lines_left = Rows; /* avoid more prompt */
13101 msg_scroll = TRUE;
13102 msg_clr_eos();
13103
13104 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13105 {
13106 msg_puts(get_tv_string(&li->li_tv));
13107 msg_putchar('\n');
13108 }
13109
13110 /* Ask for choice. */
13111 selected = prompt_for_number(&mouse_used);
13112 if (mouse_used)
13113 selected -= lines_left;
13114
13115 rettv->vval.v_number = selected;
13116}
13117
13118
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13120
13121/*
13122 * "inputrestore()" function
13123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013126 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128{
13129 if (ga_userinput.ga_len > 0)
13130 {
13131 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13133 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013134 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 }
13136 else if (p_verbose > 1)
13137 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013138 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 }
13141}
13142
13143/*
13144 * "inputsave()" function
13145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013148 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013151 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 if (ga_grow(&ga_userinput, 1) == OK)
13153 {
13154 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13155 + ga_userinput.ga_len);
13156 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013157 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 }
13159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161}
13162
13163/*
13164 * "inputsecret()" function
13165 */
13166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013168 typval_T *argvars;
13169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
13171 ++cmdline_star;
13172 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 --cmdline_star;
13175 --inputsecret_flag;
13176}
13177
13178/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013179 * "insert()" function
13180 */
13181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 typval_T *argvars;
13184 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013185{
13186 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 listitem_T *item;
13188 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190
13191 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013193 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013194 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 {
13196 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 before = get_tv_number_chk(&argvars[2], &error);
13198 if (error)
13199 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200
Bram Moolenaar758711c2005-02-02 23:11:38 +000013201 if (before == l->lv_len)
13202 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 else
13204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013205 item = list_find(l, before);
13206 if (item == NULL)
13207 {
13208 EMSGN(_(e_listidx), before);
13209 l = NULL;
13210 }
13211 }
13212 if (l != NULL)
13213 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013214 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013215 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216 }
13217 }
13218}
13219
13220/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013221 * "invert(expr)" function
13222 */
13223 static void
13224f_invert(argvars, rettv)
13225 typval_T *argvars;
13226 typval_T *rettv;
13227{
13228 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13229}
13230
13231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 * "isdirectory()" function
13233 */
13234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013236 typval_T *argvars;
13237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240}
13241
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013242/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013243 * "islocked()" function
13244 */
13245 static void
13246f_islocked(argvars, rettv)
13247 typval_T *argvars;
13248 typval_T *rettv;
13249{
13250 lval_T lv;
13251 char_u *end;
13252 dictitem_T *di;
13253
13254 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013255 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13256 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013257 if (end != NULL && lv.ll_name != NULL)
13258 {
13259 if (*end != NUL)
13260 EMSG(_(e_trailing));
13261 else
13262 {
13263 if (lv.ll_tv == NULL)
13264 {
13265 if (check_changedtick(lv.ll_name))
13266 rettv->vval.v_number = 1; /* always locked */
13267 else
13268 {
13269 di = find_var(lv.ll_name, NULL);
13270 if (di != NULL)
13271 {
13272 /* Consider a variable locked when:
13273 * 1. the variable itself is locked
13274 * 2. the value of the variable is locked.
13275 * 3. the List or Dict value is locked.
13276 */
13277 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13278 || tv_islocked(&di->di_tv));
13279 }
13280 }
13281 }
13282 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013283 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013284 else if (lv.ll_newkey != NULL)
13285 EMSG2(_(e_dictkey), lv.ll_newkey);
13286 else if (lv.ll_list != NULL)
13287 /* List item. */
13288 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13289 else
13290 /* Dictionary item. */
13291 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13292 }
13293 }
13294
13295 clear_lval(&lv);
13296}
13297
Bram Moolenaar33570922005-01-25 22:26:29 +000013298static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013299
13300/*
13301 * Turn a dict into a list:
13302 * "what" == 0: list of keys
13303 * "what" == 1: list of values
13304 * "what" == 2: list of items
13305 */
13306 static void
13307dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 typval_T *argvars;
13309 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013310 int what;
13311{
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 list_T *l2;
13313 dictitem_T *di;
13314 hashitem_T *hi;
13315 listitem_T *li;
13316 listitem_T *li2;
13317 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013318 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319
Bram Moolenaar8c711452005-01-14 21:53:12 +000013320 if (argvars[0].v_type != VAR_DICT)
13321 {
13322 EMSG(_(e_dictreq));
13323 return;
13324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013325 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013326 return;
13327
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013328 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013329 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013331 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013336 --todo;
13337 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013339 li = listitem_alloc();
13340 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013341 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013342 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013344 if (what == 0)
13345 {
13346 /* keys() */
13347 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013348 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013349 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13350 }
13351 else if (what == 1)
13352 {
13353 /* values() */
13354 copy_tv(&di->di_tv, &li->li_tv);
13355 }
13356 else
13357 {
13358 /* items() */
13359 l2 = list_alloc();
13360 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013361 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013362 li->li_tv.vval.v_list = l2;
13363 if (l2 == NULL)
13364 break;
13365 ++l2->lv_refcount;
13366
13367 li2 = listitem_alloc();
13368 if (li2 == NULL)
13369 break;
13370 list_append(l2, li2);
13371 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013372 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013373 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13374
13375 li2 = listitem_alloc();
13376 if (li2 == NULL)
13377 break;
13378 list_append(l2, li2);
13379 copy_tv(&di->di_tv, &li2->li_tv);
13380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381 }
13382 }
13383}
13384
13385/*
13386 * "items(dict)" function
13387 */
13388 static void
13389f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013390 typval_T *argvars;
13391 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013392{
13393 dict_list(argvars, rettv, 2);
13394}
13395
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013397 * "join()" function
13398 */
13399 static void
13400f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013401 typval_T *argvars;
13402 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013403{
13404 garray_T ga;
13405 char_u *sep;
13406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013407 if (argvars[0].v_type != VAR_LIST)
13408 {
13409 EMSG(_(e_listreq));
13410 return;
13411 }
13412 if (argvars[0].vval.v_list == NULL)
13413 return;
13414 if (argvars[1].v_type == VAR_UNKNOWN)
13415 sep = (char_u *)" ";
13416 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013418
13419 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420
13421 if (sep != NULL)
13422 {
13423 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013424 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 ga_append(&ga, NUL);
13426 rettv->vval.v_string = (char_u *)ga.ga_data;
13427 }
13428 else
13429 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013430}
13431
13432/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433 * "keys()" function
13434 */
13435 static void
13436f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *argvars;
13438 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013439{
13440 dict_list(argvars, rettv, 0);
13441}
13442
13443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 * "last_buffer_nr()" function.
13445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450{
13451 int n = 0;
13452 buf_T *buf;
13453
13454 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13455 if (n < buf->b_fnum)
13456 n = buf->b_fnum;
13457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013459}
13460
13461/*
13462 * "len()" function
13463 */
13464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013466 typval_T *argvars;
13467 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013468{
13469 switch (argvars[0].v_type)
13470 {
13471 case VAR_STRING:
13472 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_number = (varnumber_T)STRLEN(
13474 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475 break;
13476 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013478 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013479 case VAR_DICT:
13480 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13481 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013483 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484 break;
13485 }
13486}
13487
Bram Moolenaar33570922005-01-25 22:26:29 +000013488static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013489
13490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 typval_T *argvars;
13493 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 int type;
13495{
13496#ifdef FEAT_LIBCALL
13497 char_u *string_in;
13498 char_u **string_result;
13499 int nr_result;
13500#endif
13501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013503 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013505
13506 if (check_restricted() || check_secure())
13507 return;
13508
13509#ifdef FEAT_LIBCALL
13510 /* The first two args must be strings, otherwise its meaningless */
13511 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013513 string_in = NULL;
13514 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515 string_in = argvars[2].vval.v_string;
13516 if (type == VAR_NUMBER)
13517 string_result = NULL;
13518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520 if (mch_libcall(argvars[0].vval.v_string,
13521 argvars[1].vval.v_string,
13522 string_in,
13523 argvars[2].vval.v_number,
13524 string_result,
13525 &nr_result) == OK
13526 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013528 }
13529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530}
13531
13532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013533 * "libcall()" function
13534 */
13535 static void
13536f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *argvars;
13538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013539{
13540 libcall_common(argvars, rettv, VAR_STRING);
13541}
13542
13543/*
13544 * "libcallnr()" function
13545 */
13546 static void
13547f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 typval_T *argvars;
13549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013550{
13551 libcall_common(argvars, rettv, VAR_NUMBER);
13552}
13553
13554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 * "line(string)" function
13556 */
13557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 typval_T *argvars;
13560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561{
13562 linenr_T lnum = 0;
13563 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013564 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013566 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 if (fp != NULL)
13568 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570}
13571
13572/*
13573 * "line2byte(lnum)" function
13574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013577 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
13580#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#else
13583 linenr_T lnum;
13584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13590 if (rettv->vval.v_number >= 0)
13591 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592#endif
13593}
13594
13595/*
13596 * "lispindent(lnum)" function
13597 */
13598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013600 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602{
13603#ifdef FEAT_LISP
13604 pos_T pos;
13605 linenr_T lnum;
13606
13607 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13610 {
13611 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 curwin->w_cursor = pos;
13614 }
13615 else
13616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
13620/*
13621 * "localtime()" function
13622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013625 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629}
13630
Bram Moolenaar33570922005-01-25 22:26:29 +000013631static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632
13633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 int exact;
13638{
13639 char_u *keys;
13640 char_u *which;
13641 char_u buf[NUMBUFLEN];
13642 char_u *keys_buf = NULL;
13643 char_u *rhs;
13644 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013645 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013646 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013647 mapblock_T *mp;
13648 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
13650 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->v_type = VAR_STRING;
13652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 if (*keys == NUL)
13656 return;
13657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013660 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013662 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013663 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013664 if (argvars[3].v_type != VAR_UNKNOWN)
13665 get_dict = get_tv_number(&argvars[3]);
13666 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 else
13669 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 if (which == NULL)
13671 return;
13672
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 mode = get_map_mode(&which, 0);
13674
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013675 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013676 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678
13679 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013681 /* Return a string. */
13682 if (rhs != NULL)
13683 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
Bram Moolenaarbd743252010-10-20 21:23:33 +020013685 }
13686 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13687 {
13688 /* Return a dictionary. */
13689 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13690 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13691 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692
Bram Moolenaarbd743252010-10-20 21:23:33 +020013693 dict_add_nr_str(dict, "lhs", 0L, lhs);
13694 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13695 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13696 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13697 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13698 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13699 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13700 dict_add_nr_str(dict, "mode", 0L, mapmode);
13701
13702 vim_free(lhs);
13703 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 }
13705}
13706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013707#ifdef FEAT_FLOAT
13708/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013709 * "log()" function
13710 */
13711 static void
13712f_log(argvars, rettv)
13713 typval_T *argvars;
13714 typval_T *rettv;
13715{
13716 float_T f;
13717
13718 rettv->v_type = VAR_FLOAT;
13719 if (get_float_arg(argvars, &f) == OK)
13720 rettv->vval.v_float = log(f);
13721 else
13722 rettv->vval.v_float = 0.0;
13723}
13724
13725/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013726 * "log10()" function
13727 */
13728 static void
13729f_log10(argvars, rettv)
13730 typval_T *argvars;
13731 typval_T *rettv;
13732{
13733 float_T f;
13734
13735 rettv->v_type = VAR_FLOAT;
13736 if (get_float_arg(argvars, &f) == OK)
13737 rettv->vval.v_float = log10(f);
13738 else
13739 rettv->vval.v_float = 0.0;
13740}
13741#endif
13742
Bram Moolenaar1dced572012-04-05 16:54:08 +020013743#ifdef FEAT_LUA
13744/*
13745 * "luaeval()" function
13746 */
13747 static void
13748f_luaeval(argvars, rettv)
13749 typval_T *argvars;
13750 typval_T *rettv;
13751{
13752 char_u *str;
13753 char_u buf[NUMBUFLEN];
13754
13755 str = get_tv_string_buf(&argvars[0], buf);
13756 do_luaeval(str, argvars + 1, rettv);
13757}
13758#endif
13759
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013761 * "map()" function
13762 */
13763 static void
13764f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 typval_T *argvars;
13766 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013767{
13768 filter_map(argvars, rettv, TRUE);
13769}
13770
13771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 */
13774 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 typval_T *argvars;
13777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013779 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780}
13781
13782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 */
13785 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 typval_T *argvars;
13788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013790 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791}
13792
Bram Moolenaar33570922005-01-25 22:26:29 +000013793static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794
13795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 int type;
13800{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013801 char_u *str = NULL;
13802 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 char_u *pat;
13804 regmatch_T regmatch;
13805 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013806 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 char_u *save_cpo;
13808 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013809 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013810 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013811 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 list_T *l = NULL;
13813 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013814 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013815 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
13817 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13818 save_cpo = p_cpo;
13819 p_cpo = (char_u *)"";
13820
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013821 rettv->vval.v_number = -1;
13822 if (type == 3)
13823 {
13824 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013825 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013826 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013827 }
13828 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->v_type = VAR_STRING;
13831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013834 if (argvars[0].v_type == VAR_LIST)
13835 {
13836 if ((l = argvars[0].vval.v_list) == NULL)
13837 goto theend;
13838 li = l->lv_first;
13839 }
13840 else
13841 expr = str = get_tv_string(&argvars[0]);
13842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013843 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13844 if (pat == NULL)
13845 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 int error = FALSE;
13850
13851 start = get_tv_number_chk(&argvars[2], &error);
13852 if (error)
13853 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854 if (l != NULL)
13855 {
13856 li = list_find(l, start);
13857 if (li == NULL)
13858 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013859 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860 }
13861 else
13862 {
13863 if (start < 0)
13864 start = 0;
13865 if (start > (long)STRLEN(str))
13866 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013867 /* When "count" argument is there ignore matches before "start",
13868 * otherwise skip part of the string. Differs when pattern is "^"
13869 * or "\<". */
13870 if (argvars[3].v_type != VAR_UNKNOWN)
13871 startcol = start;
13872 else
13873 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013874 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013875
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013876 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 nth = get_tv_number_chk(&argvars[3], &error);
13878 if (error)
13879 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
13881
13882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13883 if (regmatch.regprog != NULL)
13884 {
13885 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013886
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013887 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013888 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013889 if (l != NULL)
13890 {
13891 if (li == NULL)
13892 {
13893 match = FALSE;
13894 break;
13895 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013896 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013897 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013898 if (str == NULL)
13899 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013900 }
13901
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013902 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013904 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013906 if (l == NULL && !match)
13907 break;
13908
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013909 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013910 if (l != NULL)
13911 {
13912 li = li->li_next;
13913 ++idx;
13914 }
13915 else
13916 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013917#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013918 startcol = (colnr_T)(regmatch.startp[0]
13919 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013920#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013921 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013922#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013924 }
13925
13926 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013928 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013930 int i;
13931
13932 /* return list with matched string and submatches */
13933 for (i = 0; i < NSUBEXP; ++i)
13934 {
13935 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013936 {
13937 if (list_append_string(rettv->vval.v_list,
13938 (char_u *)"", 0) == FAIL)
13939 break;
13940 }
13941 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013942 regmatch.startp[i],
13943 (int)(regmatch.endp[i] - regmatch.startp[i]))
13944 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013945 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013946 }
13947 }
13948 else if (type == 2)
13949 {
13950 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 if (l != NULL)
13952 copy_tv(&li->li_tv, rettv);
13953 else
13954 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013956 }
13957 else if (l != NULL)
13958 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 else
13960 {
13961 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 (varnumber_T)(regmatch.startp[0] - str);
13964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013967 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 }
13969 }
13970 vim_free(regmatch.regprog);
13971 }
13972
13973theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013974 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 p_cpo = save_cpo;
13976}
13977
13978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 * "match()" function
13980 */
13981 static void
13982f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013983 typval_T *argvars;
13984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013985{
13986 find_some_match(argvars, rettv, 1);
13987}
13988
13989/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013990 * "matchadd()" function
13991 */
13992 static void
13993f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013994 typval_T *argvars UNUSED;
13995 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013996{
13997#ifdef FEAT_SEARCH_EXTRA
13998 char_u buf[NUMBUFLEN];
13999 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14000 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14001 int prio = 10; /* default priority */
14002 int id = -1;
14003 int error = FALSE;
14004
14005 rettv->vval.v_number = -1;
14006
14007 if (grp == NULL || pat == NULL)
14008 return;
14009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014010 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014011 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014012 if (argvars[3].v_type != VAR_UNKNOWN)
14013 id = get_tv_number_chk(&argvars[3], &error);
14014 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014015 if (error == TRUE)
14016 return;
14017 if (id >= 1 && id <= 3)
14018 {
14019 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14020 return;
14021 }
14022
14023 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14024#endif
14025}
14026
14027/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014028 * "matcharg()" function
14029 */
14030 static void
14031f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014032 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014033 typval_T *rettv;
14034{
14035 if (rettv_list_alloc(rettv) == OK)
14036 {
14037#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014038 int id = get_tv_number(&argvars[0]);
14039 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014040
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014041 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014042 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014043 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14044 {
14045 list_append_string(rettv->vval.v_list,
14046 syn_id2name(m->hlg_id), -1);
14047 list_append_string(rettv->vval.v_list, m->pattern, -1);
14048 }
14049 else
14050 {
14051 list_append_string(rettv->vval.v_list, NUL, -1);
14052 list_append_string(rettv->vval.v_list, NUL, -1);
14053 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014054 }
14055#endif
14056 }
14057}
14058
14059/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014060 * "matchdelete()" function
14061 */
14062 static void
14063f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014064 typval_T *argvars UNUSED;
14065 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014066{
14067#ifdef FEAT_SEARCH_EXTRA
14068 rettv->vval.v_number = match_delete(curwin,
14069 (int)get_tv_number(&argvars[0]), TRUE);
14070#endif
14071}
14072
14073/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074 * "matchend()" function
14075 */
14076 static void
14077f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 typval_T *argvars;
14079 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014080{
14081 find_some_match(argvars, rettv, 0);
14082}
14083
14084/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014085 * "matchlist()" function
14086 */
14087 static void
14088f_matchlist(argvars, rettv)
14089 typval_T *argvars;
14090 typval_T *rettv;
14091{
14092 find_some_match(argvars, rettv, 3);
14093}
14094
14095/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096 * "matchstr()" function
14097 */
14098 static void
14099f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014100 typval_T *argvars;
14101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014102{
14103 find_some_match(argvars, rettv, 2);
14104}
14105
Bram Moolenaar33570922005-01-25 22:26:29 +000014106static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014107
14108 static void
14109max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *argvars;
14111 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014112 int domax;
14113{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014114 long n = 0;
14115 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014117
14118 if (argvars[0].v_type == VAR_LIST)
14119 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 list_T *l;
14121 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014122
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014123 l = argvars[0].vval.v_list;
14124 if (l != NULL)
14125 {
14126 li = l->lv_first;
14127 if (li != NULL)
14128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014129 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014130 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014131 {
14132 li = li->li_next;
14133 if (li == NULL)
14134 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014136 if (domax ? i > n : i < n)
14137 n = i;
14138 }
14139 }
14140 }
14141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014142 else if (argvars[0].v_type == VAR_DICT)
14143 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014145 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014147 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014148
14149 d = argvars[0].vval.v_dict;
14150 if (d != NULL)
14151 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014152 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014153 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014155 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014159 if (first)
14160 {
14161 n = i;
14162 first = FALSE;
14163 }
14164 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014165 n = i;
14166 }
14167 }
14168 }
14169 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014171 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014173}
14174
14175/*
14176 * "max()" function
14177 */
14178 static void
14179f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014180 typval_T *argvars;
14181 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014182{
14183 max_min(argvars, rettv, TRUE);
14184}
14185
14186/*
14187 * "min()" function
14188 */
14189 static void
14190f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014193{
14194 max_min(argvars, rettv, FALSE);
14195}
14196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014197static int mkdir_recurse __ARGS((char_u *dir, int prot));
14198
14199/*
14200 * Create the directory in which "dir" is located, and higher levels when
14201 * needed.
14202 */
14203 static int
14204mkdir_recurse(dir, prot)
14205 char_u *dir;
14206 int prot;
14207{
14208 char_u *p;
14209 char_u *updir;
14210 int r = FAIL;
14211
14212 /* Get end of directory name in "dir".
14213 * We're done when it's "/" or "c:/". */
14214 p = gettail_sep(dir);
14215 if (p <= get_past_head(dir))
14216 return OK;
14217
14218 /* If the directory exists we're done. Otherwise: create it.*/
14219 updir = vim_strnsave(dir, (int)(p - dir));
14220 if (updir == NULL)
14221 return FAIL;
14222 if (mch_isdir(updir))
14223 r = OK;
14224 else if (mkdir_recurse(updir, prot) == OK)
14225 r = vim_mkdir_emsg(updir, prot);
14226 vim_free(updir);
14227 return r;
14228}
14229
14230#ifdef vim_mkdir
14231/*
14232 * "mkdir()" function
14233 */
14234 static void
14235f_mkdir(argvars, rettv)
14236 typval_T *argvars;
14237 typval_T *rettv;
14238{
14239 char_u *dir;
14240 char_u buf[NUMBUFLEN];
14241 int prot = 0755;
14242
14243 rettv->vval.v_number = FAIL;
14244 if (check_restricted() || check_secure())
14245 return;
14246
14247 dir = get_tv_string_buf(&argvars[0], buf);
14248 if (argvars[1].v_type != VAR_UNKNOWN)
14249 {
14250 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 prot = get_tv_number_chk(&argvars[2], NULL);
14252 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014253 mkdir_recurse(dir, prot);
14254 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014256}
14257#endif
14258
Bram Moolenaar0d660222005-01-07 21:51:51 +000014259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 * "mode()" function
14261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014267 char_u buf[3];
14268
14269 buf[1] = NUL;
14270 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271
14272#ifdef FEAT_VISUAL
14273 if (VIsual_active)
14274 {
14275 if (VIsual_select)
14276 buf[0] = VIsual_mode + 's' - 'v';
14277 else
14278 buf[0] = VIsual_mode;
14279 }
14280 else
14281#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014282 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14283 || State == CONFIRM)
14284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014286 if (State == ASKMORE)
14287 buf[1] = 'm';
14288 else if (State == CONFIRM)
14289 buf[1] = '?';
14290 }
14291 else if (State == EXTERNCMD)
14292 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 else if (State & INSERT)
14294 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014295#ifdef FEAT_VREPLACE
14296 if (State & VREPLACE_FLAG)
14297 {
14298 buf[0] = 'R';
14299 buf[1] = 'v';
14300 }
14301 else
14302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 if (State & REPLACE_FLAG)
14304 buf[0] = 'R';
14305 else
14306 buf[0] = 'i';
14307 }
14308 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014311 if (exmode_active)
14312 buf[1] = 'v';
14313 }
14314 else if (exmode_active)
14315 {
14316 buf[0] = 'c';
14317 buf[1] = 'e';
14318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014322 if (finish_op)
14323 buf[1] = 'o';
14324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014326 /* Clear out the minor mode when the argument is not a non-zero number or
14327 * non-empty string. */
14328 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329 buf[1] = NUL;
14330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 rettv->vval.v_string = vim_strsave(buf);
14332 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333}
14334
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014335#ifdef FEAT_MZSCHEME
14336/*
14337 * "mzeval()" function
14338 */
14339 static void
14340f_mzeval(argvars, rettv)
14341 typval_T *argvars;
14342 typval_T *rettv;
14343{
14344 char_u *str;
14345 char_u buf[NUMBUFLEN];
14346
14347 str = get_tv_string_buf(&argvars[0], buf);
14348 do_mzeval(str, rettv);
14349}
Bram Moolenaar75676462013-01-30 14:55:42 +010014350
14351 void
14352mzscheme_call_vim(name, args, rettv)
14353 char_u *name;
14354 typval_T *args;
14355 typval_T *rettv;
14356{
14357 typval_T argvars[3];
14358
14359 argvars[0].v_type = VAR_STRING;
14360 argvars[0].vval.v_string = name;
14361 copy_tv(args, &argvars[1]);
14362 argvars[2].v_type = VAR_UNKNOWN;
14363 f_call(argvars, rettv);
14364 clear_tv(&argvars[1]);
14365}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014366#endif
14367
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014369 * "nextnonblank()" function
14370 */
14371 static void
14372f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014373 typval_T *argvars;
14374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375{
14376 linenr_T lnum;
14377
14378 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381 {
14382 lnum = 0;
14383 break;
14384 }
14385 if (*skipwhite(ml_get(lnum)) != NUL)
14386 break;
14387 }
14388 rettv->vval.v_number = lnum;
14389}
14390
14391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 * "nr2char()" function
14393 */
14394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014396 typval_T *argvars;
14397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398{
14399 char_u buf[NUMBUFLEN];
14400
14401#ifdef FEAT_MBYTE
14402 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014403 {
14404 int utf8 = 0;
14405
14406 if (argvars[1].v_type != VAR_UNKNOWN)
14407 utf8 = get_tv_number_chk(&argvars[1], NULL);
14408 if (utf8)
14409 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14410 else
14411 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413 else
14414#endif
14415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 buf[1] = NUL;
14418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->v_type = VAR_STRING;
14420 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014421}
14422
14423/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014424 * "or(expr, expr)" function
14425 */
14426 static void
14427f_or(argvars, rettv)
14428 typval_T *argvars;
14429 typval_T *rettv;
14430{
14431 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14432 | get_tv_number_chk(&argvars[1], NULL);
14433}
14434
14435/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014436 * "pathshorten()" function
14437 */
14438 static void
14439f_pathshorten(argvars, rettv)
14440 typval_T *argvars;
14441 typval_T *rettv;
14442{
14443 char_u *p;
14444
14445 rettv->v_type = VAR_STRING;
14446 p = get_tv_string_chk(&argvars[0]);
14447 if (p == NULL)
14448 rettv->vval.v_string = NULL;
14449 else
14450 {
14451 p = vim_strsave(p);
14452 rettv->vval.v_string = p;
14453 if (p != NULL)
14454 shorten_dir(p);
14455 }
14456}
14457
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014458#ifdef FEAT_FLOAT
14459/*
14460 * "pow()" function
14461 */
14462 static void
14463f_pow(argvars, rettv)
14464 typval_T *argvars;
14465 typval_T *rettv;
14466{
14467 float_T fx, fy;
14468
14469 rettv->v_type = VAR_FLOAT;
14470 if (get_float_arg(argvars, &fx) == OK
14471 && get_float_arg(&argvars[1], &fy) == OK)
14472 rettv->vval.v_float = pow(fx, fy);
14473 else
14474 rettv->vval.v_float = 0.0;
14475}
14476#endif
14477
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014478/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479 * "prevnonblank()" function
14480 */
14481 static void
14482f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 typval_T *argvars;
14484 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014485{
14486 linenr_T lnum;
14487
14488 lnum = get_tv_lnum(argvars);
14489 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14490 lnum = 0;
14491 else
14492 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14493 --lnum;
14494 rettv->vval.v_number = lnum;
14495}
14496
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014497#ifdef HAVE_STDARG_H
14498/* This dummy va_list is here because:
14499 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14500 * - locally in the function results in a "used before set" warning
14501 * - using va_start() to initialize it gives "function with fixed args" error */
14502static va_list ap;
14503#endif
14504
Bram Moolenaar8c711452005-01-14 21:53:12 +000014505/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014506 * "printf()" function
14507 */
14508 static void
14509f_printf(argvars, rettv)
14510 typval_T *argvars;
14511 typval_T *rettv;
14512{
14513 rettv->v_type = VAR_STRING;
14514 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014515#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014516 {
14517 char_u buf[NUMBUFLEN];
14518 int len;
14519 char_u *s;
14520 int saved_did_emsg = did_emsg;
14521 char *fmt;
14522
14523 /* Get the required length, allocate the buffer and do it for real. */
14524 did_emsg = FALSE;
14525 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014526 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014527 if (!did_emsg)
14528 {
14529 s = alloc(len + 1);
14530 if (s != NULL)
14531 {
14532 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014533 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014534 }
14535 }
14536 did_emsg |= saved_did_emsg;
14537 }
14538#endif
14539}
14540
14541/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014542 * "pumvisible()" function
14543 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014544 static void
14545f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014546 typval_T *argvars UNUSED;
14547 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014548{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014549#ifdef FEAT_INS_EXPAND
14550 if (pum_visible())
14551 rettv->vval.v_number = 1;
14552#endif
14553}
14554
Bram Moolenaardb913952012-06-29 12:54:53 +020014555#ifdef FEAT_PYTHON3
14556/*
14557 * "py3eval()" function
14558 */
14559 static void
14560f_py3eval(argvars, rettv)
14561 typval_T *argvars;
14562 typval_T *rettv;
14563{
14564 char_u *str;
14565 char_u buf[NUMBUFLEN];
14566
14567 str = get_tv_string_buf(&argvars[0], buf);
14568 do_py3eval(str, rettv);
14569}
14570#endif
14571
14572#ifdef FEAT_PYTHON
14573/*
14574 * "pyeval()" function
14575 */
14576 static void
14577f_pyeval(argvars, rettv)
14578 typval_T *argvars;
14579 typval_T *rettv;
14580{
14581 char_u *str;
14582 char_u buf[NUMBUFLEN];
14583
14584 str = get_tv_string_buf(&argvars[0], buf);
14585 do_pyeval(str, rettv);
14586}
14587#endif
14588
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014589/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014590 * "range()" function
14591 */
14592 static void
14593f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 typval_T *argvars;
14595 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014596{
14597 long start;
14598 long end;
14599 long stride = 1;
14600 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604 if (argvars[1].v_type == VAR_UNKNOWN)
14605 {
14606 end = start - 1;
14607 start = 0;
14608 }
14609 else
14610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 }
14615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 if (error)
14617 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014619 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014620 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014621 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 else
14623 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014624 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014625 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014626 if (list_append_number(rettv->vval.v_list,
14627 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014629 }
14630}
14631
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014632/*
14633 * "readfile()" function
14634 */
14635 static void
14636f_readfile(argvars, rettv)
14637 typval_T *argvars;
14638 typval_T *rettv;
14639{
14640 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014641 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014642 char_u *fname;
14643 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014644 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14645 int io_size = sizeof(buf);
14646 int readlen; /* size of last fread() */
14647 char_u *prev = NULL; /* previously read bytes, if any */
14648 long prevlen = 0; /* length of data in prev */
14649 long prevsize = 0; /* size of prev buffer */
14650 long maxline = MAXLNUM;
14651 long cnt = 0;
14652 char_u *p; /* position in buf */
14653 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014654
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014655 if (argvars[1].v_type != VAR_UNKNOWN)
14656 {
14657 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14658 binary = TRUE;
14659 if (argvars[2].v_type != VAR_UNKNOWN)
14660 maxline = get_tv_number(&argvars[2]);
14661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014663 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014665
14666 /* Always open the file in binary mode, library functions have a mind of
14667 * their own about CR-LF conversion. */
14668 fname = get_tv_string(&argvars[0]);
14669 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14670 {
14671 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14672 return;
14673 }
14674
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014675 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014677 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014679 /* This for loop processes what was read, but is also entered at end
14680 * of file so that either:
14681 * - an incomplete line gets written
14682 * - a "binary" file gets an empty line at the end if it ends in a
14683 * newline. */
14684 for (p = buf, start = buf;
14685 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14686 ++p)
14687 {
14688 if (*p == '\n' || readlen <= 0)
14689 {
14690 listitem_T *li;
14691 char_u *s = NULL;
14692 long_u len = p - start;
14693
14694 /* Finished a line. Remove CRs before NL. */
14695 if (readlen > 0 && !binary)
14696 {
14697 while (len > 0 && start[len - 1] == '\r')
14698 --len;
14699 /* removal may cross back to the "prev" string */
14700 if (len == 0)
14701 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14702 --prevlen;
14703 }
14704 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014705 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706 else
14707 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014708 /* Change "prev" buffer to be the right size. This way
14709 * the bytes are only copied once, and very long lines are
14710 * allocated only once. */
14711 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014713 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715 prev = NULL; /* the list will own the string */
14716 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014717 }
14718 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014719 if (s == NULL)
14720 {
14721 do_outofmem_msg((long_u) prevlen + len + 1);
14722 failed = TRUE;
14723 break;
14724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014726 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 {
14728 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014730 break;
14731 }
14732 li->li_tv.v_type = VAR_STRING;
14733 li->li_tv.v_lock = 0;
14734 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014735 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014736
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014737 start = p + 1; /* step over newline */
14738 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739 break;
14740 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 else if (*p == NUL)
14742 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014743#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014744 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14745 * when finding the BF and check the previous two bytes. */
14746 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014747 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014748 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14749 * + 1, these may be in the "prev" string. */
14750 char_u back1 = p >= buf + 1 ? p[-1]
14751 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14752 char_u back2 = p >= buf + 2 ? p[-2]
14753 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14754 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014756 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 char_u *dest = p - 2;
14759
14760 /* Usually a BOM is at the beginning of a file, and so at
14761 * the beginning of a line; then we can just step over it.
14762 */
14763 if (start == dest)
14764 start = p + 1;
14765 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014766 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014767 /* have to shuffle buf to close gap */
14768 int adjust_prevlen = 0;
14769
14770 if (dest < buf)
14771 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014772 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014773 dest = buf;
14774 }
14775 if (readlen > p - buf + 1)
14776 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14777 readlen -= 3 - adjust_prevlen;
14778 prevlen -= adjust_prevlen;
14779 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014780 }
14781 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014782 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783#endif
14784 } /* for */
14785
14786 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14787 break;
14788 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014789 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014790 /* There's part of a line in buf, store it in "prev". */
14791 if (p - start + prevlen >= prevsize)
14792 {
14793 /* need bigger "prev" buffer */
14794 char_u *newprev;
14795
14796 /* A common use case is ordinary text files and "prev" gets a
14797 * fragment of a line, so the first allocation is made
14798 * small, to avoid repeatedly 'allocing' large and
14799 * 'reallocing' small. */
14800 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014801 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014802 else
14803 {
14804 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014805 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 prevsize = grow50pc > growmin ? grow50pc : growmin;
14807 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014808 newprev = prev == NULL ? alloc(prevsize)
14809 : vim_realloc(prev, prevsize);
14810 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014811 {
14812 do_outofmem_msg((long_u)prevsize);
14813 failed = TRUE;
14814 break;
14815 }
14816 prev = newprev;
14817 }
14818 /* Add the line part to end of "prev". */
14819 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014820 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014823
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014824 /*
14825 * For a negative line count use only the lines at the end of the file,
14826 * free the rest.
14827 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014828 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014829 while (cnt > -maxline)
14830 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014831 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014832 --cnt;
14833 }
14834
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014835 if (failed)
14836 {
14837 list_free(rettv->vval.v_list, TRUE);
14838 /* readfile doc says an empty list is returned on error */
14839 rettv->vval.v_list = list_alloc();
14840 }
14841
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014842 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014843 fclose(fd);
14844}
14845
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014846#if defined(FEAT_RELTIME)
14847static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14848
14849/*
14850 * Convert a List to proftime_T.
14851 * Return FAIL when there is something wrong.
14852 */
14853 static int
14854list2proftime(arg, tm)
14855 typval_T *arg;
14856 proftime_T *tm;
14857{
14858 long n1, n2;
14859 int error = FALSE;
14860
14861 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14862 || arg->vval.v_list->lv_len != 2)
14863 return FAIL;
14864 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14865 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14866# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014867 tm->HighPart = n1;
14868 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014869# else
14870 tm->tv_sec = n1;
14871 tm->tv_usec = n2;
14872# endif
14873 return error ? FAIL : OK;
14874}
14875#endif /* FEAT_RELTIME */
14876
14877/*
14878 * "reltime()" function
14879 */
14880 static void
14881f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014882 typval_T *argvars UNUSED;
14883 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014884{
14885#ifdef FEAT_RELTIME
14886 proftime_T res;
14887 proftime_T start;
14888
14889 if (argvars[0].v_type == VAR_UNKNOWN)
14890 {
14891 /* No arguments: get current time. */
14892 profile_start(&res);
14893 }
14894 else if (argvars[1].v_type == VAR_UNKNOWN)
14895 {
14896 if (list2proftime(&argvars[0], &res) == FAIL)
14897 return;
14898 profile_end(&res);
14899 }
14900 else
14901 {
14902 /* Two arguments: compute the difference. */
14903 if (list2proftime(&argvars[0], &start) == FAIL
14904 || list2proftime(&argvars[1], &res) == FAIL)
14905 return;
14906 profile_sub(&res, &start);
14907 }
14908
14909 if (rettv_list_alloc(rettv) == OK)
14910 {
14911 long n1, n2;
14912
14913# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014914 n1 = res.HighPart;
14915 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014916# else
14917 n1 = res.tv_sec;
14918 n2 = res.tv_usec;
14919# endif
14920 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14921 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14922 }
14923#endif
14924}
14925
14926/*
14927 * "reltimestr()" function
14928 */
14929 static void
14930f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014931 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014932 typval_T *rettv;
14933{
14934#ifdef FEAT_RELTIME
14935 proftime_T tm;
14936#endif
14937
14938 rettv->v_type = VAR_STRING;
14939 rettv->vval.v_string = NULL;
14940#ifdef FEAT_RELTIME
14941 if (list2proftime(&argvars[0], &tm) == OK)
14942 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14943#endif
14944}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14947static void make_connection __ARGS((void));
14948static int check_connection __ARGS((void));
14949
14950 static void
14951make_connection()
14952{
14953 if (X_DISPLAY == NULL
14954# ifdef FEAT_GUI
14955 && !gui.in_use
14956# endif
14957 )
14958 {
14959 x_force_connect = TRUE;
14960 setup_term_clip();
14961 x_force_connect = FALSE;
14962 }
14963}
14964
14965 static int
14966check_connection()
14967{
14968 make_connection();
14969 if (X_DISPLAY == NULL)
14970 {
14971 EMSG(_("E240: No connection to Vim server"));
14972 return FAIL;
14973 }
14974 return OK;
14975}
14976#endif
14977
14978#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014979static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980
14981 static void
14982remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 typval_T *argvars;
14984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 int expr;
14986{
14987 char_u *server_name;
14988 char_u *keys;
14989 char_u *r = NULL;
14990 char_u buf[NUMBUFLEN];
14991# ifdef WIN32
14992 HWND w;
14993# else
14994 Window w;
14995# endif
14996
14997 if (check_restricted() || check_secure())
14998 return;
14999
15000# ifdef FEAT_X11
15001 if (check_connection() == FAIL)
15002 return;
15003# endif
15004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015005 server_name = get_tv_string_chk(&argvars[0]);
15006 if (server_name == NULL)
15007 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008 keys = get_tv_string_buf(&argvars[1], buf);
15009# ifdef WIN32
15010 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15011# else
15012 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15013 < 0)
15014# endif
15015 {
15016 if (r != NULL)
15017 EMSG(r); /* sending worked but evaluation failed */
15018 else
15019 EMSG2(_("E241: Unable to send to %s"), server_name);
15020 return;
15021 }
15022
15023 rettv->vval.v_string = r;
15024
15025 if (argvars[2].v_type != VAR_UNKNOWN)
15026 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015028 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015029 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015031 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 v.di_tv.v_type = VAR_STRING;
15033 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 idvar = get_tv_string_chk(&argvars[2]);
15035 if (idvar != NULL)
15036 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038 }
15039}
15040#endif
15041
15042/*
15043 * "remote_expr()" function
15044 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045 static void
15046f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015049{
15050 rettv->v_type = VAR_STRING;
15051 rettv->vval.v_string = NULL;
15052#ifdef FEAT_CLIENTSERVER
15053 remote_common(argvars, rettv, TRUE);
15054#endif
15055}
15056
15057/*
15058 * "remote_foreground()" function
15059 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060 static void
15061f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015062 typval_T *argvars UNUSED;
15063 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015064{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065#ifdef FEAT_CLIENTSERVER
15066# ifdef WIN32
15067 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 {
15069 char_u *server_name = get_tv_string_chk(&argvars[0]);
15070
15071 if (server_name != NULL)
15072 serverForeground(server_name);
15073 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074# else
15075 /* Send a foreground() expression to the server. */
15076 argvars[1].v_type = VAR_STRING;
15077 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15078 argvars[2].v_type = VAR_UNKNOWN;
15079 remote_common(argvars, rettv, TRUE);
15080 vim_free(argvars[1].vval.v_string);
15081# endif
15082#endif
15083}
15084
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 static void
15086f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089{
15090#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 char_u *s = NULL;
15093# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015094 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097
15098 if (check_restricted() || check_secure())
15099 {
15100 rettv->vval.v_number = -1;
15101 return;
15102 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 serverid = get_tv_string_chk(&argvars[0]);
15104 if (serverid == NULL)
15105 {
15106 rettv->vval.v_number = -1;
15107 return; /* type error; errmsg already given */
15108 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015110 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 if (n == 0)
15112 rettv->vval.v_number = -1;
15113 else
15114 {
15115 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15116 rettv->vval.v_number = (s != NULL);
15117 }
15118# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 if (check_connection() == FAIL)
15120 return;
15121
15122 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124# endif
15125
15126 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128 char_u *retvar;
15129
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 v.di_tv.v_type = VAR_STRING;
15131 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015132 retvar = get_tv_string_chk(&argvars[1]);
15133 if (retvar != NULL)
15134 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136 }
15137#else
15138 rettv->vval.v_number = -1;
15139#endif
15140}
15141
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 static void
15143f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146{
15147 char_u *r = NULL;
15148
15149#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 char_u *serverid = get_tv_string_chk(&argvars[0]);
15151
15152 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 {
15154# ifdef WIN32
15155 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015156 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015158 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 if (n != 0)
15160 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15161 if (r == NULL)
15162# else
15163 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165# endif
15166 EMSG(_("E277: Unable to read a server reply"));
15167 }
15168#endif
15169 rettv->v_type = VAR_STRING;
15170 rettv->vval.v_string = r;
15171}
15172
15173/*
15174 * "remote_send()" function
15175 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 static void
15177f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180{
15181 rettv->v_type = VAR_STRING;
15182 rettv->vval.v_string = NULL;
15183#ifdef FEAT_CLIENTSERVER
15184 remote_common(argvars, rettv, FALSE);
15185#endif
15186}
15187
15188/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015189 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 */
15191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *argvars;
15194 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015195{
Bram Moolenaar33570922005-01-25 22:26:29 +000015196 list_T *l;
15197 listitem_T *item, *item2;
15198 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015199 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015200 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 dict_T *d;
15203 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015204 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015205
Bram Moolenaar8c711452005-01-14 21:53:12 +000015206 if (argvars[0].v_type == VAR_DICT)
15207 {
15208 if (argvars[2].v_type != VAR_UNKNOWN)
15209 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015210 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015211 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 key = get_tv_string_chk(&argvars[1]);
15214 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 di = dict_find(d, key, -1);
15217 if (di == NULL)
15218 EMSG2(_(e_dictkey), key);
15219 else
15220 {
15221 *rettv = di->di_tv;
15222 init_tv(&di->di_tv);
15223 dictitem_remove(d, di);
15224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226 }
15227 }
15228 else if (argvars[0].v_type != VAR_LIST)
15229 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015230 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015231 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 int error = FALSE;
15234
15235 idx = get_tv_number_chk(&argvars[1], &error);
15236 if (error)
15237 ; /* type error: do nothing, errmsg already given */
15238 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239 EMSGN(_(e_listidx), idx);
15240 else
15241 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015242 if (argvars[2].v_type == VAR_UNKNOWN)
15243 {
15244 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015245 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015246 *rettv = item->li_tv;
15247 vim_free(item);
15248 }
15249 else
15250 {
15251 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 end = get_tv_number_chk(&argvars[2], &error);
15253 if (error)
15254 ; /* type error: do nothing */
15255 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015256 EMSGN(_(e_listidx), end);
15257 else
15258 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015259 int cnt = 0;
15260
15261 for (li = item; li != NULL; li = li->li_next)
15262 {
15263 ++cnt;
15264 if (li == item2)
15265 break;
15266 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015267 if (li == NULL) /* didn't find "item2" after "item" */
15268 EMSG(_(e_invrange));
15269 else
15270 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015271 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015272 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015274 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 l->lv_first = item;
15276 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015277 item->li_prev = NULL;
15278 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015279 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015280 }
15281 }
15282 }
15283 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015284 }
15285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286}
15287
15288/*
15289 * "rename({from}, {to})" function
15290 */
15291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *argvars;
15294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295{
15296 char_u buf[NUMBUFLEN];
15297
15298 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15302 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303}
15304
15305/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015306 * "repeat()" function
15307 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015308 static void
15309f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 typval_T *argvars;
15311 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015312{
15313 char_u *p;
15314 int n;
15315 int slen;
15316 int len;
15317 char_u *r;
15318 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015319
15320 n = get_tv_number(&argvars[1]);
15321 if (argvars[0].v_type == VAR_LIST)
15322 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015323 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015325 if (list_extend(rettv->vval.v_list,
15326 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015327 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015328 }
15329 else
15330 {
15331 p = get_tv_string(&argvars[0]);
15332 rettv->v_type = VAR_STRING;
15333 rettv->vval.v_string = NULL;
15334
15335 slen = (int)STRLEN(p);
15336 len = slen * n;
15337 if (len <= 0)
15338 return;
15339
15340 r = alloc(len + 1);
15341 if (r != NULL)
15342 {
15343 for (i = 0; i < n; i++)
15344 mch_memmove(r + i * slen, p, (size_t)slen);
15345 r[len] = NUL;
15346 }
15347
15348 rettv->vval.v_string = r;
15349 }
15350}
15351
15352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 * "resolve()" function
15354 */
15355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 typval_T *argvars;
15358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359{
15360 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015361#ifdef HAVE_READLINK
15362 char_u *buf = NULL;
15363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366#ifdef FEAT_SHORTCUT
15367 {
15368 char_u *v = NULL;
15369
15370 v = mch_resolve_shortcut(p);
15371 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 }
15376#else
15377# ifdef HAVE_READLINK
15378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 char_u *cpy;
15380 int len;
15381 char_u *remain = NULL;
15382 char_u *q;
15383 int is_relative_to_current = FALSE;
15384 int has_trailing_pathsep = FALSE;
15385 int limit = 100;
15386
15387 p = vim_strsave(p);
15388
15389 if (p[0] == '.' && (vim_ispathsep(p[1])
15390 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15391 is_relative_to_current = TRUE;
15392
15393 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015394 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015397 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399
15400 q = getnextcomp(p);
15401 if (*q != NUL)
15402 {
15403 /* Separate the first path component in "p", and keep the
15404 * remainder (beginning with the path separator). */
15405 remain = vim_strsave(q - 1);
15406 q[-1] = NUL;
15407 }
15408
Bram Moolenaard9462e32011-04-11 21:35:11 +020015409 buf = alloc(MAXPATHL + 1);
15410 if (buf == NULL)
15411 goto fail;
15412
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 for (;;)
15414 {
15415 for (;;)
15416 {
15417 len = readlink((char *)p, (char *)buf, MAXPATHL);
15418 if (len <= 0)
15419 break;
15420 buf[len] = NUL;
15421
15422 if (limit-- == 0)
15423 {
15424 vim_free(p);
15425 vim_free(remain);
15426 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 goto fail;
15429 }
15430
15431 /* Ensure that the result will have a trailing path separator
15432 * if the argument has one. */
15433 if (remain == NULL && has_trailing_pathsep)
15434 add_pathsep(buf);
15435
15436 /* Separate the first path component in the link value and
15437 * concatenate the remainders. */
15438 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15439 if (*q != NUL)
15440 {
15441 if (remain == NULL)
15442 remain = vim_strsave(q - 1);
15443 else
15444 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015445 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 if (cpy != NULL)
15447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 vim_free(remain);
15449 remain = cpy;
15450 }
15451 }
15452 q[-1] = NUL;
15453 }
15454
15455 q = gettail(p);
15456 if (q > p && *q == NUL)
15457 {
15458 /* Ignore trailing path separator. */
15459 q[-1] = NUL;
15460 q = gettail(p);
15461 }
15462 if (q > p && !mch_isFullName(buf))
15463 {
15464 /* symlink is relative to directory of argument */
15465 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15466 if (cpy != NULL)
15467 {
15468 STRCPY(cpy, p);
15469 STRCPY(gettail(cpy), buf);
15470 vim_free(p);
15471 p = cpy;
15472 }
15473 }
15474 else
15475 {
15476 vim_free(p);
15477 p = vim_strsave(buf);
15478 }
15479 }
15480
15481 if (remain == NULL)
15482 break;
15483
15484 /* Append the first path component of "remain" to "p". */
15485 q = getnextcomp(remain + 1);
15486 len = q - remain - (*q != NUL);
15487 cpy = vim_strnsave(p, STRLEN(p) + len);
15488 if (cpy != NULL)
15489 {
15490 STRNCAT(cpy, remain, len);
15491 vim_free(p);
15492 p = cpy;
15493 }
15494 /* Shorten "remain". */
15495 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015496 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 else
15498 {
15499 vim_free(remain);
15500 remain = NULL;
15501 }
15502 }
15503
15504 /* If the result is a relative path name, make it explicitly relative to
15505 * the current directory if and only if the argument had this form. */
15506 if (!vim_ispathsep(*p))
15507 {
15508 if (is_relative_to_current
15509 && *p != NUL
15510 && !(p[0] == '.'
15511 && (p[1] == NUL
15512 || vim_ispathsep(p[1])
15513 || (p[1] == '.'
15514 && (p[2] == NUL
15515 || vim_ispathsep(p[2]))))))
15516 {
15517 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015518 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 if (cpy != NULL)
15520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 vim_free(p);
15522 p = cpy;
15523 }
15524 }
15525 else if (!is_relative_to_current)
15526 {
15527 /* Strip leading "./". */
15528 q = p;
15529 while (q[0] == '.' && vim_ispathsep(q[1]))
15530 q += 2;
15531 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015532 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 }
15534 }
15535
15536 /* Ensure that the result will have no trailing path separator
15537 * if the argument had none. But keep "/" or "//". */
15538 if (!has_trailing_pathsep)
15539 {
15540 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015541 if (after_pathsep(p, q))
15542 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 }
15544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 }
15547# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549# endif
15550#endif
15551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553
15554#ifdef HAVE_READLINK
15555fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015556 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 */
15564 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *argvars;
15567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568{
Bram Moolenaar33570922005-01-25 22:26:29 +000015569 list_T *l;
15570 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 if (argvars[0].v_type != VAR_LIST)
15573 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015574 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015575 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 {
15577 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015578 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015579 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580 while (li != NULL)
15581 {
15582 ni = li->li_prev;
15583 list_append(l, li);
15584 li = ni;
15585 }
15586 rettv->vval.v_list = l;
15587 rettv->v_type = VAR_LIST;
15588 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015589 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591}
15592
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015593#define SP_NOMOVE 0x01 /* don't move cursor */
15594#define SP_REPEAT 0x02 /* repeat to find outer pair */
15595#define SP_RETCOUNT 0x04 /* return matchcount */
15596#define SP_SETPCMARK 0x08 /* set previous context mark */
15597#define SP_START 0x10 /* accept match at start position */
15598#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15599#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015600
Bram Moolenaar33570922005-01-25 22:26:29 +000015601static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602
15603/*
15604 * Get flags for a search function.
15605 * Possibly sets "p_ws".
15606 * Returns BACKWARD, FORWARD or zero (for an error).
15607 */
15608 static int
15609get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 int *flagsp;
15612{
15613 int dir = FORWARD;
15614 char_u *flags;
15615 char_u nbuf[NUMBUFLEN];
15616 int mask;
15617
15618 if (varp->v_type != VAR_UNKNOWN)
15619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 flags = get_tv_string_buf_chk(varp, nbuf);
15621 if (flags == NULL)
15622 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623 while (*flags != NUL)
15624 {
15625 switch (*flags)
15626 {
15627 case 'b': dir = BACKWARD; break;
15628 case 'w': p_ws = TRUE; break;
15629 case 'W': p_ws = FALSE; break;
15630 default: mask = 0;
15631 if (flagsp != NULL)
15632 switch (*flags)
15633 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015634 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015635 case 'e': mask = SP_END; break;
15636 case 'm': mask = SP_RETCOUNT; break;
15637 case 'n': mask = SP_NOMOVE; break;
15638 case 'p': mask = SP_SUBPAT; break;
15639 case 'r': mask = SP_REPEAT; break;
15640 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641 }
15642 if (mask == 0)
15643 {
15644 EMSG2(_(e_invarg2), flags);
15645 dir = 0;
15646 }
15647 else
15648 *flagsp |= mask;
15649 }
15650 if (dir == 0)
15651 break;
15652 ++flags;
15653 }
15654 }
15655 return dir;
15656}
15657
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015659 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015661 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015662search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015663 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015664 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015665 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015667 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 char_u *pat;
15669 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015670 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 int save_p_ws = p_ws;
15672 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015674 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015675 proftime_T tm;
15676#ifdef FEAT_RELTIME
15677 long time_limit = 0;
15678#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015679 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015680 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015682 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015684 if (dir == 0)
15685 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015686 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015687 if (flags & SP_START)
15688 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015689 if (flags & SP_END)
15690 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015691
Bram Moolenaar76929292008-01-06 19:07:36 +000015692 /* Optional arguments: line number to stop searching and timeout. */
15693 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015694 {
15695 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15696 if (lnum_stop < 0)
15697 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015698#ifdef FEAT_RELTIME
15699 if (argvars[3].v_type != VAR_UNKNOWN)
15700 {
15701 time_limit = get_tv_number_chk(&argvars[3], NULL);
15702 if (time_limit < 0)
15703 goto theend;
15704 }
15705#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 }
15707
Bram Moolenaar76929292008-01-06 19:07:36 +000015708#ifdef FEAT_RELTIME
15709 /* Set the time limit, if there is one. */
15710 profile_setlimit(time_limit, &tm);
15711#endif
15712
Bram Moolenaar231334e2005-07-25 20:46:57 +000015713 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015714 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015715 * Check to make sure only those flags are set.
15716 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15717 * flags cannot be set. Check for that condition also.
15718 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015719 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015720 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015723 goto theend;
15724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015726 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015727 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015728 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015729 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015731 if (flags & SP_SUBPAT)
15732 retval = subpatnum;
15733 else
15734 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015735 if (flags & SP_SETPCMARK)
15736 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015738 if (match_pos != NULL)
15739 {
15740 /* Store the match cursor position */
15741 match_pos->lnum = pos.lnum;
15742 match_pos->col = pos.col + 1;
15743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 /* "/$" will put the cursor after the end of the line, may need to
15745 * correct that here */
15746 check_cursor();
15747 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015748
15749 /* If 'n' flag is used: restore cursor position. */
15750 if (flags & SP_NOMOVE)
15751 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015752 else
15753 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015754theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015756
15757 return retval;
15758}
15759
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015760#ifdef FEAT_FLOAT
15761/*
15762 * "round({float})" function
15763 */
15764 static void
15765f_round(argvars, rettv)
15766 typval_T *argvars;
15767 typval_T *rettv;
15768{
15769 float_T f;
15770
15771 rettv->v_type = VAR_FLOAT;
15772 if (get_float_arg(argvars, &f) == OK)
15773 /* round() is not in C90, use ceil() or floor() instead. */
15774 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15775 else
15776 rettv->vval.v_float = 0.0;
15777}
15778#endif
15779
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015780/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015781 * "screencol()" function
15782 *
15783 * First column is 1 to be consistent with virtcol().
15784 */
15785 static void
15786f_screencol(argvars, rettv)
15787 typval_T *argvars UNUSED;
15788 typval_T *rettv;
15789{
15790 rettv->vval.v_number = screen_screencol() + 1;
15791}
15792
15793/*
15794 * "screenrow()" function
15795 */
15796 static void
15797f_screenrow(argvars, rettv)
15798 typval_T *argvars UNUSED;
15799 typval_T *rettv;
15800{
15801 rettv->vval.v_number = screen_screenrow() + 1;
15802}
15803
15804/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015805 * "search()" function
15806 */
15807 static void
15808f_search(argvars, rettv)
15809 typval_T *argvars;
15810 typval_T *rettv;
15811{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015812 int flags = 0;
15813
15814 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815}
15816
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015818 * "searchdecl()" function
15819 */
15820 static void
15821f_searchdecl(argvars, rettv)
15822 typval_T *argvars;
15823 typval_T *rettv;
15824{
15825 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015826 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015827 int error = FALSE;
15828 char_u *name;
15829
15830 rettv->vval.v_number = 1; /* default: FAIL */
15831
15832 name = get_tv_string_chk(&argvars[0]);
15833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015834 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015835 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015836 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15837 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15838 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015839 if (!error && name != NULL)
15840 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015841 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015842}
15843
15844/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015845 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 static int
15848searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015850 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851{
15852 char_u *spat, *mpat, *epat;
15853 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 int dir;
15856 int flags = 0;
15857 char_u nbuf1[NUMBUFLEN];
15858 char_u nbuf2[NUMBUFLEN];
15859 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015860 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015861 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015862 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015865 spat = get_tv_string_chk(&argvars[0]);
15866 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15867 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15868 if (spat == NULL || mpat == NULL || epat == NULL)
15869 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 /* Handle the optional fourth argument: flags */
15872 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015873 if (dir == 0)
15874 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015875
15876 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015877 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15878 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015879 if ((flags & (SP_END | SP_SUBPAT)) != 0
15880 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015881 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015882 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015883 goto theend;
15884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015886 /* Using 'r' implies 'W', otherwise it doesn't work. */
15887 if (flags & SP_REPEAT)
15888 p_ws = FALSE;
15889
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015890 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015891 if (argvars[3].v_type == VAR_UNKNOWN
15892 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 skip = (char_u *)"";
15894 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015896 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015897 if (argvars[5].v_type != VAR_UNKNOWN)
15898 {
15899 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15900 if (lnum_stop < 0)
15901 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015902#ifdef FEAT_RELTIME
15903 if (argvars[6].v_type != VAR_UNKNOWN)
15904 {
15905 time_limit = get_tv_number_chk(&argvars[6], NULL);
15906 if (time_limit < 0)
15907 goto theend;
15908 }
15909#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015910 }
15911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 if (skip == NULL)
15913 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015915 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015916 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015917
15918theend:
15919 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015920
15921 return retval;
15922}
15923
15924/*
15925 * "searchpair()" function
15926 */
15927 static void
15928f_searchpair(argvars, rettv)
15929 typval_T *argvars;
15930 typval_T *rettv;
15931{
15932 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15933}
15934
15935/*
15936 * "searchpairpos()" function
15937 */
15938 static void
15939f_searchpairpos(argvars, rettv)
15940 typval_T *argvars;
15941 typval_T *rettv;
15942{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943 pos_T match_pos;
15944 int lnum = 0;
15945 int col = 0;
15946
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015947 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015948 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015949
15950 if (searchpair_cmn(argvars, &match_pos) > 0)
15951 {
15952 lnum = match_pos.lnum;
15953 col = match_pos.col;
15954 }
15955
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015956 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15957 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015958}
15959
15960/*
15961 * Search for a start/middle/end thing.
15962 * Used by searchpair(), see its documentation for the details.
15963 * Returns 0 or -1 for no match,
15964 */
15965 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015966do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15967 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015968 char_u *spat; /* start pattern */
15969 char_u *mpat; /* middle pattern */
15970 char_u *epat; /* end pattern */
15971 int dir; /* BACKWARD or FORWARD */
15972 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015973 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015974 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015975 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015976 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015977{
15978 char_u *save_cpo;
15979 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15980 long retval = 0;
15981 pos_T pos;
15982 pos_T firstpos;
15983 pos_T foundpos;
15984 pos_T save_cursor;
15985 pos_T save_pos;
15986 int n;
15987 int r;
15988 int nest = 1;
15989 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015990 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015991 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015992
15993 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15994 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015995 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015996
Bram Moolenaar76929292008-01-06 19:07:36 +000015997#ifdef FEAT_RELTIME
15998 /* Set the time limit, if there is one. */
15999 profile_setlimit(time_limit, &tm);
16000#endif
16001
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016002 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16003 * start/middle/end (pat3, for the top pair). */
16004 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16005 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16006 if (pat2 == NULL || pat3 == NULL)
16007 goto theend;
16008 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16009 if (*mpat == NUL)
16010 STRCPY(pat3, pat2);
16011 else
16012 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16013 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016014 if (flags & SP_START)
16015 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016016
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 save_cursor = curwin->w_cursor;
16018 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016019 clearpos(&firstpos);
16020 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 pat = pat3;
16022 for (;;)
16023 {
16024 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016025 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16027 /* didn't find it or found the first match again: FAIL */
16028 break;
16029
16030 if (firstpos.lnum == 0)
16031 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016032 if (equalpos(pos, foundpos))
16033 {
16034 /* Found the same position again. Can happen with a pattern that
16035 * has "\zs" at the end and searching backwards. Advance one
16036 * character and try again. */
16037 if (dir == BACKWARD)
16038 decl(&pos);
16039 else
16040 incl(&pos);
16041 }
16042 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016044 /* clear the start flag to avoid getting stuck here */
16045 options &= ~SEARCH_START;
16046
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 /* If the skip pattern matches, ignore this match. */
16048 if (*skip != NUL)
16049 {
16050 save_pos = curwin->w_cursor;
16051 curwin->w_cursor = pos;
16052 r = eval_to_bool(skip, &err, NULL, FALSE);
16053 curwin->w_cursor = save_pos;
16054 if (err)
16055 {
16056 /* Evaluating {skip} caused an error, break here. */
16057 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016058 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 break;
16060 }
16061 if (r)
16062 continue;
16063 }
16064
16065 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16066 {
16067 /* Found end when searching backwards or start when searching
16068 * forward: nested pair. */
16069 ++nest;
16070 pat = pat2; /* nested, don't search for middle */
16071 }
16072 else
16073 {
16074 /* Found end when searching forward or start when searching
16075 * backward: end of (nested) pair; or found middle in outer pair. */
16076 if (--nest == 1)
16077 pat = pat3; /* outer level, search for middle */
16078 }
16079
16080 if (nest == 0)
16081 {
16082 /* Found the match: return matchcount or line number. */
16083 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016084 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016086 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016087 if (flags & SP_SETPCMARK)
16088 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 curwin->w_cursor = pos;
16090 if (!(flags & SP_REPEAT))
16091 break;
16092 nest = 1; /* search for next unmatched */
16093 }
16094 }
16095
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016096 if (match_pos != NULL)
16097 {
16098 /* Store the match cursor position */
16099 match_pos->lnum = curwin->w_cursor.lnum;
16100 match_pos->col = curwin->w_cursor.col + 1;
16101 }
16102
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016104 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 curwin->w_cursor = save_cursor;
16106
16107theend:
16108 vim_free(pat2);
16109 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016110 if (p_cpo == empty_option)
16111 p_cpo = save_cpo;
16112 else
16113 /* Darn, evaluating the {skip} expression changed the value. */
16114 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016115
16116 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117}
16118
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016119/*
16120 * "searchpos()" function
16121 */
16122 static void
16123f_searchpos(argvars, rettv)
16124 typval_T *argvars;
16125 typval_T *rettv;
16126{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127 pos_T match_pos;
16128 int lnum = 0;
16129 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016130 int n;
16131 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016132
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016133 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016134 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016136 n = search_cmn(argvars, &match_pos, &flags);
16137 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016138 {
16139 lnum = match_pos.lnum;
16140 col = match_pos.col;
16141 }
16142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16144 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016145 if (flags & SP_SUBPAT)
16146 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016147}
16148
16149
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150 static void
16151f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016155#ifdef FEAT_CLIENTSERVER
16156 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 char_u *server = get_tv_string_chk(&argvars[0]);
16158 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016161 if (server == NULL || reply == NULL)
16162 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 if (check_restricted() || check_secure())
16164 return;
16165# ifdef FEAT_X11
16166 if (check_connection() == FAIL)
16167 return;
16168# endif
16169
16170 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 EMSG(_("E258: Unable to send to client"));
16173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175 rettv->vval.v_number = 0;
16176#else
16177 rettv->vval.v_number = -1;
16178#endif
16179}
16180
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181 static void
16182f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185{
16186 char_u *r = NULL;
16187
16188#ifdef FEAT_CLIENTSERVER
16189# ifdef WIN32
16190 r = serverGetVimNames();
16191# else
16192 make_connection();
16193 if (X_DISPLAY != NULL)
16194 r = serverGetVimNames(X_DISPLAY);
16195# endif
16196#endif
16197 rettv->v_type = VAR_STRING;
16198 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199}
16200
16201/*
16202 * "setbufvar()" function
16203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016207 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208{
16209 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 char_u nbuf[NUMBUFLEN];
16214
16215 if (check_restricted() || check_secure())
16216 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16218 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 varp = &argvars[2];
16221
16222 if (buf != NULL && varname != NULL && varp != NULL)
16223 {
16224 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226
16227 if (*varname == '&')
16228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 long numval;
16230 char_u *strval;
16231 int error = FALSE;
16232
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 numval = get_tv_number_chk(varp, &error);
16235 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 if (!error && strval != NULL)
16237 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 }
16239 else
16240 {
16241 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16242 if (bufvarname != NULL)
16243 {
16244 STRCPY(bufvarname, "b:");
16245 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016246 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 vim_free(bufvarname);
16248 }
16249 }
16250
16251 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
16256/*
16257 * "setcmdpos()" function
16258 */
16259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016261 typval_T *argvars;
16262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 int pos = (int)get_tv_number(&argvars[0]) - 1;
16265
16266 if (pos >= 0)
16267 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268}
16269
16270/*
16271 * "setline()" function
16272 */
16273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 typval_T *argvars;
16276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277{
16278 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016279 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016280 list_T *l = NULL;
16281 listitem_T *li = NULL;
16282 long added = 0;
16283 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016285 lnum = get_tv_lnum(&argvars[0]);
16286 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016288 l = argvars[1].vval.v_list;
16289 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016291 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016293
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016294 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016295 for (;;)
16296 {
16297 if (l != NULL)
16298 {
16299 /* list argument, get next string */
16300 if (li == NULL)
16301 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016302 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303 li = li->li_next;
16304 }
16305
16306 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016307 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016308 break;
16309 if (lnum <= curbuf->b_ml.ml_line_count)
16310 {
16311 /* existing line, replace it */
16312 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16313 {
16314 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016315 if (lnum == curwin->w_cursor.lnum)
16316 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016317 rettv->vval.v_number = 0; /* OK */
16318 }
16319 }
16320 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16321 {
16322 /* lnum is one past the last line, append the line */
16323 ++added;
16324 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16325 rettv->vval.v_number = 0; /* OK */
16326 }
16327
16328 if (l == NULL) /* only one string argument */
16329 break;
16330 ++lnum;
16331 }
16332
16333 if (added > 0)
16334 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016337static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16338
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016340 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016341 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016342 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016343set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016344 win_T *wp UNUSED;
16345 typval_T *list_arg UNUSED;
16346 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016347 typval_T *rettv;
16348{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016349#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016350 char_u *act;
16351 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016352#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016353
Bram Moolenaar2641f772005-03-25 21:58:17 +000016354 rettv->vval.v_number = -1;
16355
16356#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016357 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016358 EMSG(_(e_listreq));
16359 else
16360 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016361 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016362
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016363 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016364 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016365 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 if (act == NULL)
16367 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016368 if (*act == 'a' || *act == 'r')
16369 action = *act;
16370 }
16371
Bram Moolenaar81484f42012-12-05 15:16:47 +010016372 if (l != NULL && set_errorlist(wp, l, action,
16373 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016374 rettv->vval.v_number = 0;
16375 }
16376#endif
16377}
16378
16379/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016380 * "setloclist()" function
16381 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016382 static void
16383f_setloclist(argvars, rettv)
16384 typval_T *argvars;
16385 typval_T *rettv;
16386{
16387 win_T *win;
16388
16389 rettv->vval.v_number = -1;
16390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016391 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016392 if (win != NULL)
16393 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16394}
16395
16396/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016397 * "setmatches()" function
16398 */
16399 static void
16400f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016401 typval_T *argvars UNUSED;
16402 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016403{
16404#ifdef FEAT_SEARCH_EXTRA
16405 list_T *l;
16406 listitem_T *li;
16407 dict_T *d;
16408
16409 rettv->vval.v_number = -1;
16410 if (argvars[0].v_type != VAR_LIST)
16411 {
16412 EMSG(_(e_listreq));
16413 return;
16414 }
16415 if ((l = argvars[0].vval.v_list) != NULL)
16416 {
16417
16418 /* To some extent make sure that we are dealing with a list from
16419 * "getmatches()". */
16420 li = l->lv_first;
16421 while (li != NULL)
16422 {
16423 if (li->li_tv.v_type != VAR_DICT
16424 || (d = li->li_tv.vval.v_dict) == NULL)
16425 {
16426 EMSG(_(e_invarg));
16427 return;
16428 }
16429 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16430 && dict_find(d, (char_u *)"pattern", -1) != NULL
16431 && dict_find(d, (char_u *)"priority", -1) != NULL
16432 && dict_find(d, (char_u *)"id", -1) != NULL))
16433 {
16434 EMSG(_(e_invarg));
16435 return;
16436 }
16437 li = li->li_next;
16438 }
16439
16440 clear_matches(curwin);
16441 li = l->lv_first;
16442 while (li != NULL)
16443 {
16444 d = li->li_tv.vval.v_dict;
16445 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16446 get_dict_string(d, (char_u *)"pattern", FALSE),
16447 (int)get_dict_number(d, (char_u *)"priority"),
16448 (int)get_dict_number(d, (char_u *)"id"));
16449 li = li->li_next;
16450 }
16451 rettv->vval.v_number = 0;
16452 }
16453#endif
16454}
16455
16456/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016457 * "setpos()" function
16458 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016459 static void
16460f_setpos(argvars, rettv)
16461 typval_T *argvars;
16462 typval_T *rettv;
16463{
16464 pos_T pos;
16465 int fnum;
16466 char_u *name;
16467
Bram Moolenaar08250432008-02-13 11:42:46 +000016468 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016469 name = get_tv_string_chk(argvars);
16470 if (name != NULL)
16471 {
16472 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16473 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016474 if (--pos.col < 0)
16475 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016476 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016477 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016478 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016479 if (fnum == curbuf->b_fnum)
16480 {
16481 curwin->w_cursor = pos;
16482 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016483 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016484 }
16485 else
16486 EMSG(_(e_invarg));
16487 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016488 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16489 {
16490 /* set mark */
16491 if (setmark_pos(name[1], &pos, fnum) == OK)
16492 rettv->vval.v_number = 0;
16493 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016494 else
16495 EMSG(_(e_invarg));
16496 }
16497 }
16498}
16499
16500/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016501 * "setqflist()" function
16502 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016503 static void
16504f_setqflist(argvars, rettv)
16505 typval_T *argvars;
16506 typval_T *rettv;
16507{
16508 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16509}
16510
16511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 * "setreg()" function
16513 */
16514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016516 typval_T *argvars;
16517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518{
16519 int regname;
16520 char_u *strregname;
16521 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 int append;
16524 char_u yank_type;
16525 long block_len;
16526
16527 block_len = -1;
16528 yank_type = MAUTO;
16529 append = FALSE;
16530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016532 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 if (strregname == NULL)
16535 return; /* type error; errmsg already given */
16536 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 if (regname == 0 || regname == '@')
16538 regname = '"';
16539 else if (regname == '=')
16540 return;
16541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016544 stropt = get_tv_string_chk(&argvars[2]);
16545 if (stropt == NULL)
16546 return; /* type error */
16547 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 switch (*stropt)
16549 {
16550 case 'a': case 'A': /* append */
16551 append = TRUE;
16552 break;
16553 case 'v': case 'c': /* character-wise selection */
16554 yank_type = MCHAR;
16555 break;
16556 case 'V': case 'l': /* line-wise selection */
16557 yank_type = MLINE;
16558 break;
16559#ifdef FEAT_VISUAL
16560 case 'b': case Ctrl_V: /* block-wise selection */
16561 yank_type = MBLOCK;
16562 if (VIM_ISDIGIT(stropt[1]))
16563 {
16564 ++stropt;
16565 block_len = getdigits(&stropt) - 1;
16566 --stropt;
16567 }
16568 break;
16569#endif
16570 }
16571 }
16572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 strval = get_tv_string_chk(&argvars[1]);
16574 if (strval != NULL)
16575 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578}
16579
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016580/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016581 * "settabvar()" function
16582 */
16583 static void
16584f_settabvar(argvars, rettv)
16585 typval_T *argvars;
16586 typval_T *rettv;
16587{
16588 tabpage_T *save_curtab;
16589 char_u *varname, *tabvarname;
16590 typval_T *varp;
16591 tabpage_T *tp;
16592
16593 rettv->vval.v_number = 0;
16594
16595 if (check_restricted() || check_secure())
16596 return;
16597
16598 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16599 varname = get_tv_string_chk(&argvars[1]);
16600 varp = &argvars[2];
16601
16602 if (tp != NULL && varname != NULL && varp != NULL)
16603 {
16604 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016605 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016606
16607 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16608 if (tabvarname != NULL)
16609 {
16610 STRCPY(tabvarname, "t:");
16611 STRCPY(tabvarname + 2, varname);
16612 set_var(tabvarname, varp, TRUE);
16613 vim_free(tabvarname);
16614 }
16615
16616 /* Restore current tabpage */
16617 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016618 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016619 }
16620}
16621
16622/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016623 * "settabwinvar()" function
16624 */
16625 static void
16626f_settabwinvar(argvars, rettv)
16627 typval_T *argvars;
16628 typval_T *rettv;
16629{
16630 setwinvar(argvars, rettv, 1);
16631}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632
16633/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016634 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016638 typval_T *argvars;
16639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016641 setwinvar(argvars, rettv, 0);
16642}
16643
16644/*
16645 * "setwinvar()" and "settabwinvar()" functions
16646 */
16647 static void
16648setwinvar(argvars, rettv, off)
16649 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016650 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016651 int off;
16652{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 win_T *win;
16654#ifdef FEAT_WINDOWS
16655 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016656 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657#endif
16658 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016659 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016661 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662
16663 if (check_restricted() || check_secure())
16664 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016665
16666#ifdef FEAT_WINDOWS
16667 if (off == 1)
16668 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16669 else
16670 tp = curtab;
16671#endif
16672 win = find_win_by_nr(&argvars[off], tp);
16673 varname = get_tv_string_chk(&argvars[off + 1]);
16674 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675
16676 if (win != NULL && varname != NULL && varp != NULL)
16677 {
16678#ifdef FEAT_WINDOWS
16679 /* set curwin to be our win, temporarily */
16680 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016681 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016682 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016683 if (!win_valid(win))
16684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 curwin = win;
16686 curbuf = curwin->w_buffer;
16687#endif
16688
16689 if (*varname == '&')
16690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016691 long numval;
16692 char_u *strval;
16693 int error = FALSE;
16694
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 numval = get_tv_number_chk(varp, &error);
16697 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016698 if (!error && strval != NULL)
16699 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 }
16701 else
16702 {
16703 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16704 if (winvarname != NULL)
16705 {
16706 STRCPY(winvarname, "w:");
16707 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016708 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 vim_free(winvarname);
16710 }
16711 }
16712
16713#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016714 /* Restore current tabpage and window, if still valid (autocomands can
16715 * make them invalid). */
16716 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016717 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 if (win_valid(save_curwin))
16719 {
16720 curwin = save_curwin;
16721 curbuf = curwin->w_buffer;
16722 }
16723#endif
16724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725}
16726
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016727#ifdef FEAT_CRYPT
16728/*
16729 * "sha256({string})" function
16730 */
16731 static void
16732f_sha256(argvars, rettv)
16733 typval_T *argvars;
16734 typval_T *rettv;
16735{
16736 char_u *p;
16737
16738 p = get_tv_string(&argvars[0]);
16739 rettv->vval.v_string = vim_strsave(
16740 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16741 rettv->v_type = VAR_STRING;
16742}
16743#endif /* FEAT_CRYPT */
16744
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016746 * "shellescape({string})" function
16747 */
16748 static void
16749f_shellescape(argvars, rettv)
16750 typval_T *argvars;
16751 typval_T *rettv;
16752{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016753 rettv->vval.v_string = vim_strsave_shellescape(
16754 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016755 rettv->v_type = VAR_STRING;
16756}
16757
16758/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016759 * shiftwidth() function
16760 */
16761 static void
16762f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016763 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016764 typval_T *rettv;
16765{
16766 rettv->vval.v_number = get_sw_value();
16767}
16768
16769/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 */
16772 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016773f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 typval_T *argvars;
16775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778
Bram Moolenaar0d660222005-01-07 21:51:51 +000016779 p = get_tv_string(&argvars[0]);
16780 rettv->vval.v_string = vim_strsave(p);
16781 simplify_filename(rettv->vval.v_string); /* simplify in place */
16782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783}
16784
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016785#ifdef FEAT_FLOAT
16786/*
16787 * "sin()" function
16788 */
16789 static void
16790f_sin(argvars, rettv)
16791 typval_T *argvars;
16792 typval_T *rettv;
16793{
16794 float_T f;
16795
16796 rettv->v_type = VAR_FLOAT;
16797 if (get_float_arg(argvars, &f) == OK)
16798 rettv->vval.v_float = sin(f);
16799 else
16800 rettv->vval.v_float = 0.0;
16801}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016802
16803/*
16804 * "sinh()" function
16805 */
16806 static void
16807f_sinh(argvars, rettv)
16808 typval_T *argvars;
16809 typval_T *rettv;
16810{
16811 float_T f;
16812
16813 rettv->v_type = VAR_FLOAT;
16814 if (get_float_arg(argvars, &f) == OK)
16815 rettv->vval.v_float = sinh(f);
16816 else
16817 rettv->vval.v_float = 0.0;
16818}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016819#endif
16820
Bram Moolenaar0d660222005-01-07 21:51:51 +000016821static int
16822#ifdef __BORLANDC__
16823 _RTLENTRYF
16824#endif
16825 item_compare __ARGS((const void *s1, const void *s2));
16826static int
16827#ifdef __BORLANDC__
16828 _RTLENTRYF
16829#endif
16830 item_compare2 __ARGS((const void *s1, const void *s2));
16831
16832static int item_compare_ic;
16833static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016834static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016835static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836#define ITEM_COMPARE_FAIL 999
16837
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 static int
16842#ifdef __BORLANDC__
16843_RTLENTRYF
16844#endif
16845item_compare(s1, s2)
16846 const void *s1;
16847 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849 char_u *p1, *p2;
16850 char_u *tofree1, *tofree2;
16851 int res;
16852 char_u numbuf1[NUMBUFLEN];
16853 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016855 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16856 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016857 if (p1 == NULL)
16858 p1 = (char_u *)"";
16859 if (p2 == NULL)
16860 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861 if (item_compare_ic)
16862 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 res = STRCMP(p1, p2);
16865 vim_free(tofree1);
16866 vim_free(tofree2);
16867 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868}
16869
16870 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871#ifdef __BORLANDC__
16872_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874item_compare2(s1, s2)
16875 const void *s1;
16876 const void *s2;
16877{
16878 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016880 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016883 /* shortcut after failure in previous call; compare all items equal */
16884 if (item_compare_func_err)
16885 return 0;
16886
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016887 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16888 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016889 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16890 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891
16892 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016893 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016894 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16895 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016896 clear_tv(&argv[0]);
16897 clear_tv(&argv[1]);
16898
16899 if (res == FAIL)
16900 res = ITEM_COMPARE_FAIL;
16901 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016902 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16903 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016904 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 clear_tv(&rettv);
16906 return res;
16907}
16908
16909/*
16910 * "sort({list})" function
16911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 typval_T *argvars;
16915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916{
Bram Moolenaar33570922005-01-25 22:26:29 +000016917 list_T *l;
16918 listitem_T *li;
16919 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920 long len;
16921 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 if (argvars[0].v_type != VAR_LIST)
16924 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 else
16926 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016928 if (l == NULL || tv_check_lock(l->lv_lock,
16929 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 return;
16931 rettv->vval.v_list = l;
16932 rettv->v_type = VAR_LIST;
16933 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 len = list_len(l);
16936 if (len <= 1)
16937 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 item_compare_ic = FALSE;
16940 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016941 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 if (argvars[1].v_type != VAR_UNKNOWN)
16943 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016944 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947 else
16948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016949 int error = FALSE;
16950
16951 i = get_tv_number_chk(&argvars[1], &error);
16952 if (error)
16953 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 if (i == 1)
16955 item_compare_ic = TRUE;
16956 else
16957 item_compare_func = get_tv_string(&argvars[1]);
16958 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016959
16960 if (argvars[2].v_type != VAR_UNKNOWN)
16961 {
16962 /* optional third argument: {dict} */
16963 if (argvars[2].v_type != VAR_DICT)
16964 {
16965 EMSG(_(e_dictreq));
16966 return;
16967 }
16968 item_compare_selfdict = argvars[2].vval.v_dict;
16969 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 if (ptrs == NULL)
16975 return;
16976 i = 0;
16977 for (li = l->lv_first; li != NULL; li = li->li_next)
16978 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981 /* test the compare function */
16982 if (item_compare_func != NULL
16983 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16984 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016985 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 {
16988 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990 item_compare_func == NULL ? item_compare : item_compare2);
16991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016992 if (!item_compare_func_err)
16993 {
16994 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016995 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 l->lv_len = 0;
16997 for (i = 0; i < len; ++i)
16998 list_append(l, ptrs[i]);
16999 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 }
17001
17002 vim_free(ptrs);
17003 }
17004}
17005
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017006/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017007 * "soundfold({word})" function
17008 */
17009 static void
17010f_soundfold(argvars, rettv)
17011 typval_T *argvars;
17012 typval_T *rettv;
17013{
17014 char_u *s;
17015
17016 rettv->v_type = VAR_STRING;
17017 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017018#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017019 rettv->vval.v_string = eval_soundfold(s);
17020#else
17021 rettv->vval.v_string = vim_strsave(s);
17022#endif
17023}
17024
17025/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017026 * "spellbadword()" function
17027 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017028 static void
17029f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017030 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017031 typval_T *rettv;
17032{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017033 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017034 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017035 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017036
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017037 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017038 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017039
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017040#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017041 if (argvars[0].v_type == VAR_UNKNOWN)
17042 {
17043 /* Find the start and length of the badly spelled word. */
17044 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17045 if (len != 0)
17046 word = ml_get_cursor();
17047 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017048 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017049 {
17050 char_u *str = get_tv_string_chk(&argvars[0]);
17051 int capcol = -1;
17052
17053 if (str != NULL)
17054 {
17055 /* Check the argument for spelling. */
17056 while (*str != NUL)
17057 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017058 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017059 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017060 {
17061 word = str;
17062 break;
17063 }
17064 str += len;
17065 }
17066 }
17067 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017068#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017069
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017070 list_append_string(rettv->vval.v_list, word, len);
17071 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017072 attr == HLF_SPB ? "bad" :
17073 attr == HLF_SPR ? "rare" :
17074 attr == HLF_SPL ? "local" :
17075 attr == HLF_SPC ? "caps" :
17076 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017077}
17078
17079/*
17080 * "spellsuggest()" function
17081 */
17082 static void
17083f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017084 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017085 typval_T *rettv;
17086{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017087#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017088 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017089 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017090 int maxcount;
17091 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017092 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017093 listitem_T *li;
17094 int need_capital = FALSE;
17095#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017096
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017097 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017098 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017099
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017100#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017101 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102 {
17103 str = get_tv_string(&argvars[0]);
17104 if (argvars[1].v_type != VAR_UNKNOWN)
17105 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017106 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017107 if (maxcount <= 0)
17108 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017109 if (argvars[2].v_type != VAR_UNKNOWN)
17110 {
17111 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17112 if (typeerr)
17113 return;
17114 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017115 }
17116 else
17117 maxcount = 25;
17118
Bram Moolenaar4770d092006-01-12 23:22:24 +000017119 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017120
17121 for (i = 0; i < ga.ga_len; ++i)
17122 {
17123 str = ((char_u **)ga.ga_data)[i];
17124
17125 li = listitem_alloc();
17126 if (li == NULL)
17127 vim_free(str);
17128 else
17129 {
17130 li->li_tv.v_type = VAR_STRING;
17131 li->li_tv.v_lock = 0;
17132 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017133 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017134 }
17135 }
17136 ga_clear(&ga);
17137 }
17138#endif
17139}
17140
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017142f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 typval_T *argvars;
17144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145{
17146 char_u *str;
17147 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017148 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 regmatch_T regmatch;
17150 char_u patbuf[NUMBUFLEN];
17151 char_u *save_cpo;
17152 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017154 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017155 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156
17157 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17158 save_cpo = p_cpo;
17159 p_cpo = (char_u *)"";
17160
17161 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017162 if (argvars[1].v_type != VAR_UNKNOWN)
17163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017164 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17165 if (pat == NULL)
17166 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017167 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017169 }
17170 if (pat == NULL || *pat == NUL)
17171 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017173 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017175 if (typeerr)
17176 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17179 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017182 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017184 if (*str == NUL)
17185 match = FALSE; /* empty item at the end */
17186 else
17187 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188 if (match)
17189 end = regmatch.startp[0];
17190 else
17191 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017192 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17193 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017194 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017195 if (list_append_string(rettv->vval.v_list, str,
17196 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 }
17199 if (!match)
17200 break;
17201 /* Advance to just after the match. */
17202 if (regmatch.endp[0] > str)
17203 col = 0;
17204 else
17205 {
17206 /* Don't get stuck at the same match. */
17207#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017208 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017209#else
17210 col = 1;
17211#endif
17212 }
17213 str = regmatch.endp[0];
17214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215
Bram Moolenaar0d660222005-01-07 21:51:51 +000017216 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220}
17221
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017222#ifdef FEAT_FLOAT
17223/*
17224 * "sqrt()" function
17225 */
17226 static void
17227f_sqrt(argvars, rettv)
17228 typval_T *argvars;
17229 typval_T *rettv;
17230{
17231 float_T f;
17232
17233 rettv->v_type = VAR_FLOAT;
17234 if (get_float_arg(argvars, &f) == OK)
17235 rettv->vval.v_float = sqrt(f);
17236 else
17237 rettv->vval.v_float = 0.0;
17238}
17239
17240/*
17241 * "str2float()" function
17242 */
17243 static void
17244f_str2float(argvars, rettv)
17245 typval_T *argvars;
17246 typval_T *rettv;
17247{
17248 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17249
17250 if (*p == '+')
17251 p = skipwhite(p + 1);
17252 (void)string2float(p, &rettv->vval.v_float);
17253 rettv->v_type = VAR_FLOAT;
17254}
17255#endif
17256
Bram Moolenaar2c932302006-03-18 21:42:09 +000017257/*
17258 * "str2nr()" function
17259 */
17260 static void
17261f_str2nr(argvars, rettv)
17262 typval_T *argvars;
17263 typval_T *rettv;
17264{
17265 int base = 10;
17266 char_u *p;
17267 long n;
17268
17269 if (argvars[1].v_type != VAR_UNKNOWN)
17270 {
17271 base = get_tv_number(&argvars[1]);
17272 if (base != 8 && base != 10 && base != 16)
17273 {
17274 EMSG(_(e_invarg));
17275 return;
17276 }
17277 }
17278
17279 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017280 if (*p == '+')
17281 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017282 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17283 rettv->vval.v_number = n;
17284}
17285
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286#ifdef HAVE_STRFTIME
17287/*
17288 * "strftime({format}[, {time}])" function
17289 */
17290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017292 typval_T *argvars;
17293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294{
17295 char_u result_buf[256];
17296 struct tm *curtime;
17297 time_t seconds;
17298 char_u *p;
17299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017303 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 seconds = time(NULL);
17305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 curtime = localtime(&seconds);
17308 /* MSVC returns NULL for an invalid value of seconds. */
17309 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 else
17312 {
17313# ifdef FEAT_MBYTE
17314 vimconv_T conv;
17315 char_u *enc;
17316
17317 conv.vc_type = CONV_NONE;
17318 enc = enc_locale();
17319 convert_setup(&conv, p_enc, enc);
17320 if (conv.vc_type != CONV_NONE)
17321 p = string_convert(&conv, p, NULL);
17322# endif
17323 if (p != NULL)
17324 (void)strftime((char *)result_buf, sizeof(result_buf),
17325 (char *)p, curtime);
17326 else
17327 result_buf[0] = NUL;
17328
17329# ifdef FEAT_MBYTE
17330 if (conv.vc_type != CONV_NONE)
17331 vim_free(p);
17332 convert_setup(&conv, enc, p_enc);
17333 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017334 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 else
17336# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017337 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338
17339# ifdef FEAT_MBYTE
17340 /* Release conversion descriptors */
17341 convert_setup(&conv, NULL, NULL);
17342 vim_free(enc);
17343# endif
17344 }
17345}
17346#endif
17347
17348/*
17349 * "stridx()" function
17350 */
17351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017352f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017353 typval_T *argvars;
17354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355{
17356 char_u buf[NUMBUFLEN];
17357 char_u *needle;
17358 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 needle = get_tv_string_chk(&argvars[1]);
17364 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 if (needle == NULL || haystack == NULL)
17367 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 if (argvars[2].v_type != VAR_UNKNOWN)
17370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 int error = FALSE;
17372
17373 start_idx = get_tv_number_chk(&argvars[2], &error);
17374 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017375 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017376 if (start_idx >= 0)
17377 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 }
17379
17380 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17381 if (pos != NULL)
17382 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383}
17384
17385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017386 * "string()" function
17387 */
17388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 typval_T *argvars;
17391 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017392{
17393 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017396 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017397 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017398 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017399 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401}
17402
17403/*
17404 * "strlen()" function
17405 */
17406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 typval_T *argvars;
17409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411 rettv->vval.v_number = (varnumber_T)(STRLEN(
17412 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413}
17414
17415/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017416 * "strchars()" function
17417 */
17418 static void
17419f_strchars(argvars, rettv)
17420 typval_T *argvars;
17421 typval_T *rettv;
17422{
17423 char_u *s = get_tv_string(&argvars[0]);
17424#ifdef FEAT_MBYTE
17425 varnumber_T len = 0;
17426
17427 while (*s != NUL)
17428 {
17429 mb_cptr2char_adv(&s);
17430 ++len;
17431 }
17432 rettv->vval.v_number = len;
17433#else
17434 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17435#endif
17436}
17437
17438/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017439 * "strdisplaywidth()" function
17440 */
17441 static void
17442f_strdisplaywidth(argvars, rettv)
17443 typval_T *argvars;
17444 typval_T *rettv;
17445{
17446 char_u *s = get_tv_string(&argvars[0]);
17447 int col = 0;
17448
17449 if (argvars[1].v_type != VAR_UNKNOWN)
17450 col = get_tv_number(&argvars[1]);
17451
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017452 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017453}
17454
17455/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017456 * "strwidth()" function
17457 */
17458 static void
17459f_strwidth(argvars, rettv)
17460 typval_T *argvars;
17461 typval_T *rettv;
17462{
17463 char_u *s = get_tv_string(&argvars[0]);
17464
17465 rettv->vval.v_number = (varnumber_T)(
17466#ifdef FEAT_MBYTE
17467 mb_string2cells(s, -1)
17468#else
17469 STRLEN(s)
17470#endif
17471 );
17472}
17473
17474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 * "strpart()" function
17476 */
17477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017478f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 typval_T *argvars;
17480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481{
17482 char_u *p;
17483 int n;
17484 int len;
17485 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 slen = (int)STRLEN(p);
17490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017491 n = get_tv_number_chk(&argvars[1], &error);
17492 if (error)
17493 len = 0;
17494 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 else
17497 len = slen - n; /* default len: all bytes that are available. */
17498
17499 /*
17500 * Only return the overlap between the specified part and the actual
17501 * string.
17502 */
17503 if (n < 0)
17504 {
17505 len += n;
17506 n = 0;
17507 }
17508 else if (n > slen)
17509 n = slen;
17510 if (len < 0)
17511 len = 0;
17512 else if (n + len > slen)
17513 len = slen - n;
17514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 rettv->v_type = VAR_STRING;
17516 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517}
17518
17519/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017520 * "strridx()" function
17521 */
17522 static void
17523f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017524 typval_T *argvars;
17525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526{
17527 char_u buf[NUMBUFLEN];
17528 char_u *needle;
17529 char_u *haystack;
17530 char_u *rest;
17531 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017532 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017534 needle = get_tv_string_chk(&argvars[1]);
17535 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017536
17537 rettv->vval.v_number = -1;
17538 if (needle == NULL || haystack == NULL)
17539 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017540
17541 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017542 if (argvars[2].v_type != VAR_UNKNOWN)
17543 {
17544 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017545 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017546 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017547 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017548 }
17549 else
17550 end_idx = haystack_len;
17551
Bram Moolenaar0d660222005-01-07 21:51:51 +000017552 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017553 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017555 lastmatch = haystack + end_idx;
17556 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017557 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017558 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 for (rest = haystack; *rest != '\0'; ++rest)
17560 {
17561 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017562 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563 break;
17564 lastmatch = rest;
17565 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567
17568 if (lastmatch == NULL)
17569 rettv->vval.v_number = -1;
17570 else
17571 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17572}
17573
17574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 * "strtrans()" function
17576 */
17577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017578f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017579 typval_T *argvars;
17580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 rettv->v_type = VAR_STRING;
17583 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584}
17585
17586/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017587 * "submatch()" function
17588 */
17589 static void
17590f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 typval_T *argvars;
17592 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017593{
17594 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595 rettv->vval.v_string =
17596 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597}
17598
17599/*
17600 * "substitute()" function
17601 */
17602 static void
17603f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 typval_T *argvars;
17605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606{
17607 char_u patbuf[NUMBUFLEN];
17608 char_u subbuf[NUMBUFLEN];
17609 char_u flagsbuf[NUMBUFLEN];
17610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017611 char_u *str = get_tv_string_chk(&argvars[0]);
17612 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17613 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17614 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17615
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017617 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17618 rettv->vval.v_string = NULL;
17619 else
17620 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621}
17622
17623/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017624 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017628 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630{
17631 int id = 0;
17632#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017633 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 long col;
17635 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017636 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017638 lnum = get_tv_lnum(argvars); /* -1 on type error */
17639 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17640 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017642 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017643 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017644 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645#endif
17646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017647 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648}
17649
17650/*
17651 * "synIDattr(id, what [, mode])" function
17652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017655 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657{
17658 char_u *p = NULL;
17659#ifdef FEAT_SYN_HL
17660 int id;
17661 char_u *what;
17662 char_u *mode;
17663 char_u modebuf[NUMBUFLEN];
17664 int modec;
17665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666 id = get_tv_number(&argvars[0]);
17667 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017668 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017672 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 modec = 0; /* replace invalid with current */
17674 }
17675 else
17676 {
17677#ifdef FEAT_GUI
17678 if (gui.in_use)
17679 modec = 'g';
17680 else
17681#endif
17682 if (t_colors > 1)
17683 modec = 'c';
17684 else
17685 modec = 't';
17686 }
17687
17688
17689 switch (TOLOWER_ASC(what[0]))
17690 {
17691 case 'b':
17692 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17693 p = highlight_color(id, what, modec);
17694 else /* bold */
17695 p = highlight_has_attr(id, HL_BOLD, modec);
17696 break;
17697
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017698 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 p = highlight_color(id, what, modec);
17700 break;
17701
17702 case 'i':
17703 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17704 p = highlight_has_attr(id, HL_INVERSE, modec);
17705 else /* italic */
17706 p = highlight_has_attr(id, HL_ITALIC, modec);
17707 break;
17708
17709 case 'n': /* name */
17710 p = get_highlight_name(NULL, id - 1);
17711 break;
17712
17713 case 'r': /* reverse */
17714 p = highlight_has_attr(id, HL_INVERSE, modec);
17715 break;
17716
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017717 case 's':
17718 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17719 p = highlight_color(id, what, modec);
17720 else /* standout */
17721 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 break;
17723
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017724 case 'u':
17725 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17726 /* underline */
17727 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17728 else
17729 /* undercurl */
17730 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 break;
17732 }
17733
17734 if (p != NULL)
17735 p = vim_strsave(p);
17736#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737 rettv->v_type = VAR_STRING;
17738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739}
17740
17741/*
17742 * "synIDtrans(id)" function
17743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017745f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748{
17749 int id;
17750
17751#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017752 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753
17754 if (id > 0)
17755 id = syn_get_final_id(id);
17756 else
17757#endif
17758 id = 0;
17759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017760 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761}
17762
17763/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017764 * "synconcealed(lnum, col)" function
17765 */
17766 static void
17767f_synconcealed(argvars, rettv)
17768 typval_T *argvars UNUSED;
17769 typval_T *rettv;
17770{
17771#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17772 long lnum;
17773 long col;
17774 int syntax_flags = 0;
17775 int cchar;
17776 int matchid = 0;
17777 char_u str[NUMBUFLEN];
17778#endif
17779
17780 rettv->v_type = VAR_LIST;
17781 rettv->vval.v_list = NULL;
17782
17783#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17784 lnum = get_tv_lnum(argvars); /* -1 on type error */
17785 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17786
17787 vim_memset(str, NUL, sizeof(str));
17788
17789 if (rettv_list_alloc(rettv) != FAIL)
17790 {
17791 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17792 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17793 && curwin->w_p_cole > 0)
17794 {
17795 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17796 syntax_flags = get_syntax_info(&matchid);
17797
17798 /* get the conceal character */
17799 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17800 {
17801 cchar = syn_get_sub_char();
17802 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17803 cchar = lcs_conceal;
17804 if (cchar != NUL)
17805 {
17806# ifdef FEAT_MBYTE
17807 if (has_mbyte)
17808 (*mb_char2bytes)(cchar, str);
17809 else
17810# endif
17811 str[0] = cchar;
17812 }
17813 }
17814 }
17815
17816 list_append_number(rettv->vval.v_list,
17817 (syntax_flags & HL_CONCEAL) != 0);
17818 /* -1 to auto-determine strlen */
17819 list_append_string(rettv->vval.v_list, str, -1);
17820 list_append_number(rettv->vval.v_list, matchid);
17821 }
17822#endif
17823}
17824
17825/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017826 * "synstack(lnum, col)" function
17827 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017828 static void
17829f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017830 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017831 typval_T *rettv;
17832{
17833#ifdef FEAT_SYN_HL
17834 long lnum;
17835 long col;
17836 int i;
17837 int id;
17838#endif
17839
17840 rettv->v_type = VAR_LIST;
17841 rettv->vval.v_list = NULL;
17842
17843#ifdef FEAT_SYN_HL
17844 lnum = get_tv_lnum(argvars); /* -1 on type error */
17845 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17846
17847 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017848 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017849 && rettv_list_alloc(rettv) != FAIL)
17850 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017851 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017852 for (i = 0; ; ++i)
17853 {
17854 id = syn_get_stack_item(i);
17855 if (id < 0)
17856 break;
17857 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17858 break;
17859 }
17860 }
17861#endif
17862}
17863
17864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 * "system()" function
17866 */
17867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017868f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017869 typval_T *argvars;
17870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017872 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017874 char_u *infile = NULL;
17875 char_u buf[NUMBUFLEN];
17876 int err = FALSE;
17877 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017879 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017880 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017881
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017882 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017883 {
17884 /*
17885 * Write the string to a temp file, to be used for input of the shell
17886 * command.
17887 */
17888 if ((infile = vim_tempname('i')) == NULL)
17889 {
17890 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017891 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017892 }
17893
17894 fd = mch_fopen((char *)infile, WRITEBIN);
17895 if (fd == NULL)
17896 {
17897 EMSG2(_(e_notopen), infile);
17898 goto done;
17899 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017900 p = get_tv_string_buf_chk(&argvars[1], buf);
17901 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017902 {
17903 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017904 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017905 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017906 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17907 err = TRUE;
17908 if (fclose(fd) != 0)
17909 err = TRUE;
17910 if (err)
17911 {
17912 EMSG(_("E677: Error writing temp file"));
17913 goto done;
17914 }
17915 }
17916
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017917 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17918 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017919
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920#ifdef USE_CR
17921 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017922 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
17924 char_u *s;
17925
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017926 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 {
17928 if (*s == CAR)
17929 *s = NL;
17930 }
17931 }
17932#else
17933# ifdef USE_CRNL
17934 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017935 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 {
17937 char_u *s, *d;
17938
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017939 d = res;
17940 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941 {
17942 if (s[0] == CAR && s[1] == NL)
17943 ++s;
17944 *d++ = *s;
17945 }
17946 *d = NUL;
17947 }
17948# endif
17949#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017950
17951done:
17952 if (infile != NULL)
17953 {
17954 mch_remove(infile);
17955 vim_free(infile);
17956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017957 rettv->v_type = VAR_STRING;
17958 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959}
17960
17961/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017962 * "tabpagebuflist()" function
17963 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017964 static void
17965f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017966 typval_T *argvars UNUSED;
17967 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017968{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017969#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017970 tabpage_T *tp;
17971 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017972
17973 if (argvars[0].v_type == VAR_UNKNOWN)
17974 wp = firstwin;
17975 else
17976 {
17977 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17978 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017979 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017980 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017981 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017982 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017983 for (; wp != NULL; wp = wp->w_next)
17984 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017985 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017986 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017987 }
17988#endif
17989}
17990
17991
17992/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017993 * "tabpagenr()" function
17994 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017995 static void
17996f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017997 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017998 typval_T *rettv;
17999{
18000 int nr = 1;
18001#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018002 char_u *arg;
18003
18004 if (argvars[0].v_type != VAR_UNKNOWN)
18005 {
18006 arg = get_tv_string_chk(&argvars[0]);
18007 nr = 0;
18008 if (arg != NULL)
18009 {
18010 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018011 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018012 else
18013 EMSG2(_(e_invexpr2), arg);
18014 }
18015 }
18016 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018017 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018018#endif
18019 rettv->vval.v_number = nr;
18020}
18021
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018022
18023#ifdef FEAT_WINDOWS
18024static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18025
18026/*
18027 * Common code for tabpagewinnr() and winnr().
18028 */
18029 static int
18030get_winnr(tp, argvar)
18031 tabpage_T *tp;
18032 typval_T *argvar;
18033{
18034 win_T *twin;
18035 int nr = 1;
18036 win_T *wp;
18037 char_u *arg;
18038
18039 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18040 if (argvar->v_type != VAR_UNKNOWN)
18041 {
18042 arg = get_tv_string_chk(argvar);
18043 if (arg == NULL)
18044 nr = 0; /* type error; errmsg already given */
18045 else if (STRCMP(arg, "$") == 0)
18046 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18047 else if (STRCMP(arg, "#") == 0)
18048 {
18049 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18050 if (twin == NULL)
18051 nr = 0;
18052 }
18053 else
18054 {
18055 EMSG2(_(e_invexpr2), arg);
18056 nr = 0;
18057 }
18058 }
18059
18060 if (nr > 0)
18061 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18062 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018063 {
18064 if (wp == NULL)
18065 {
18066 /* didn't find it in this tabpage */
18067 nr = 0;
18068 break;
18069 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018070 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018071 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018072 return nr;
18073}
18074#endif
18075
18076/*
18077 * "tabpagewinnr()" function
18078 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018079 static void
18080f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018081 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018082 typval_T *rettv;
18083{
18084 int nr = 1;
18085#ifdef FEAT_WINDOWS
18086 tabpage_T *tp;
18087
18088 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18089 if (tp == NULL)
18090 nr = 0;
18091 else
18092 nr = get_winnr(tp, &argvars[1]);
18093#endif
18094 rettv->vval.v_number = nr;
18095}
18096
18097
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018098/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018099 * "tagfiles()" function
18100 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018101 static void
18102f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018103 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018104 typval_T *rettv;
18105{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018106 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018107 tagname_T tn;
18108 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018111 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018112 fname = alloc(MAXPATHL);
18113 if (fname == NULL)
18114 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018115
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018116 for (first = TRUE; ; first = FALSE)
18117 if (get_tagfname(&tn, first, fname) == FAIL
18118 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018119 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018120 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018121 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018122}
18123
18124/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018125 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018126 */
18127 static void
18128f_taglist(argvars, rettv)
18129 typval_T *argvars;
18130 typval_T *rettv;
18131{
18132 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018133
18134 tag_pattern = get_tv_string(&argvars[0]);
18135
18136 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018137 if (*tag_pattern == NUL)
18138 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018140 if (rettv_list_alloc(rettv) == OK)
18141 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018142}
18143
18144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 * "tempname()" function
18146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018149 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151{
18152 static int x = 'A';
18153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018154 rettv->v_type = VAR_STRING;
18155 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156
18157 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18158 * names. Skip 'I' and 'O', they are used for shell redirection. */
18159 do
18160 {
18161 if (x == 'Z')
18162 x = '0';
18163 else if (x == '9')
18164 x = 'A';
18165 else
18166 {
18167#ifdef EBCDIC
18168 if (x == 'I')
18169 x = 'J';
18170 else if (x == 'R')
18171 x = 'S';
18172 else
18173#endif
18174 ++x;
18175 }
18176 } while (x == 'I' || x == 'O');
18177}
18178
18179/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018180 * "test(list)" function: Just checking the walls...
18181 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018182 static void
18183f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018184 typval_T *argvars UNUSED;
18185 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018186{
18187 /* Used for unit testing. Change the code below to your liking. */
18188#if 0
18189 listitem_T *li;
18190 list_T *l;
18191 char_u *bad, *good;
18192
18193 if (argvars[0].v_type != VAR_LIST)
18194 return;
18195 l = argvars[0].vval.v_list;
18196 if (l == NULL)
18197 return;
18198 li = l->lv_first;
18199 if (li == NULL)
18200 return;
18201 bad = get_tv_string(&li->li_tv);
18202 li = li->li_next;
18203 if (li == NULL)
18204 return;
18205 good = get_tv_string(&li->li_tv);
18206 rettv->vval.v_number = test_edit_score(bad, good);
18207#endif
18208}
18209
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018210#ifdef FEAT_FLOAT
18211/*
18212 * "tan()" function
18213 */
18214 static void
18215f_tan(argvars, rettv)
18216 typval_T *argvars;
18217 typval_T *rettv;
18218{
18219 float_T f;
18220
18221 rettv->v_type = VAR_FLOAT;
18222 if (get_float_arg(argvars, &f) == OK)
18223 rettv->vval.v_float = tan(f);
18224 else
18225 rettv->vval.v_float = 0.0;
18226}
18227
18228/*
18229 * "tanh()" function
18230 */
18231 static void
18232f_tanh(argvars, rettv)
18233 typval_T *argvars;
18234 typval_T *rettv;
18235{
18236 float_T f;
18237
18238 rettv->v_type = VAR_FLOAT;
18239 if (get_float_arg(argvars, &f) == OK)
18240 rettv->vval.v_float = tanh(f);
18241 else
18242 rettv->vval.v_float = 0.0;
18243}
18244#endif
18245
Bram Moolenaard52d9742005-08-21 22:20:28 +000018246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 * "tolower(string)" function
18248 */
18249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018250f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 typval_T *argvars;
18252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253{
18254 char_u *p;
18255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018256 p = vim_strsave(get_tv_string(&argvars[0]));
18257 rettv->v_type = VAR_STRING;
18258 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259
18260 if (p != NULL)
18261 while (*p != NUL)
18262 {
18263#ifdef FEAT_MBYTE
18264 int l;
18265
18266 if (enc_utf8)
18267 {
18268 int c, lc;
18269
18270 c = utf_ptr2char(p);
18271 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018272 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 /* TODO: reallocate string when byte count changes. */
18274 if (utf_char2len(lc) == l)
18275 utf_char2bytes(lc, p);
18276 p += l;
18277 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018278 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 p += l; /* skip multi-byte character */
18280 else
18281#endif
18282 {
18283 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18284 ++p;
18285 }
18286 }
18287}
18288
18289/*
18290 * "toupper(string)" function
18291 */
18292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018294 typval_T *argvars;
18295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018298 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299}
18300
18301/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018302 * "tr(string, fromstr, tostr)" function
18303 */
18304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018306 typval_T *argvars;
18307 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018308{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018309 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018310 char_u *fromstr;
18311 char_u *tostr;
18312 char_u *p;
18313#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018314 int inlen;
18315 int fromlen;
18316 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018317 int idx;
18318 char_u *cpstr;
18319 int cplen;
18320 int first = TRUE;
18321#endif
18322 char_u buf[NUMBUFLEN];
18323 char_u buf2[NUMBUFLEN];
18324 garray_T ga;
18325
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018326 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018327 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18328 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018329
18330 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018331 rettv->v_type = VAR_STRING;
18332 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018333 if (fromstr == NULL || tostr == NULL)
18334 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018335 ga_init2(&ga, (int)sizeof(char), 80);
18336
18337#ifdef FEAT_MBYTE
18338 if (!has_mbyte)
18339#endif
18340 /* not multi-byte: fromstr and tostr must be the same length */
18341 if (STRLEN(fromstr) != STRLEN(tostr))
18342 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018343#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018344error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018345#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018346 EMSG2(_(e_invarg2), fromstr);
18347 ga_clear(&ga);
18348 return;
18349 }
18350
18351 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018352 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018353 {
18354#ifdef FEAT_MBYTE
18355 if (has_mbyte)
18356 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018357 inlen = (*mb_ptr2len)(in_str);
18358 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018359 cplen = inlen;
18360 idx = 0;
18361 for (p = fromstr; *p != NUL; p += fromlen)
18362 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018363 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018364 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018365 {
18366 for (p = tostr; *p != NUL; p += tolen)
18367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018368 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018369 if (idx-- == 0)
18370 {
18371 cplen = tolen;
18372 cpstr = p;
18373 break;
18374 }
18375 }
18376 if (*p == NUL) /* tostr is shorter than fromstr */
18377 goto error;
18378 break;
18379 }
18380 ++idx;
18381 }
18382
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018383 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018384 {
18385 /* Check that fromstr and tostr have the same number of
18386 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018387 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018388 first = FALSE;
18389 for (p = tostr; *p != NUL; p += tolen)
18390 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018391 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018392 --idx;
18393 }
18394 if (idx != 0)
18395 goto error;
18396 }
18397
18398 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018399 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018400 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018401
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018402 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018403 }
18404 else
18405#endif
18406 {
18407 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018408 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018409 if (p != NULL)
18410 ga_append(&ga, tostr[p - fromstr]);
18411 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018412 ga_append(&ga, *in_str);
18413 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018414 }
18415 }
18416
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018417 /* add a terminating NUL */
18418 ga_grow(&ga, 1);
18419 ga_append(&ga, NUL);
18420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018422}
18423
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018424#ifdef FEAT_FLOAT
18425/*
18426 * "trunc({float})" function
18427 */
18428 static void
18429f_trunc(argvars, rettv)
18430 typval_T *argvars;
18431 typval_T *rettv;
18432{
18433 float_T f;
18434
18435 rettv->v_type = VAR_FLOAT;
18436 if (get_float_arg(argvars, &f) == OK)
18437 /* trunc() is not in C90, use floor() or ceil() instead. */
18438 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18439 else
18440 rettv->vval.v_float = 0.0;
18441}
18442#endif
18443
Bram Moolenaar8299df92004-07-10 09:47:34 +000018444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445 * "type(expr)" function
18446 */
18447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018448f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018449 typval_T *argvars;
18450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018452 int n;
18453
18454 switch (argvars[0].v_type)
18455 {
18456 case VAR_NUMBER: n = 0; break;
18457 case VAR_STRING: n = 1; break;
18458 case VAR_FUNC: n = 2; break;
18459 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018460 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018461#ifdef FEAT_FLOAT
18462 case VAR_FLOAT: n = 5; break;
18463#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018464 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18465 }
18466 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467}
18468
18469/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018470 * "undofile(name)" function
18471 */
18472 static void
18473f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018474 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018475 typval_T *rettv;
18476{
18477 rettv->v_type = VAR_STRING;
18478#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018479 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018480 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018481
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018482 if (*fname == NUL)
18483 {
18484 /* If there is no file name there will be no undo file. */
18485 rettv->vval.v_string = NULL;
18486 }
18487 else
18488 {
18489 char_u *ffname = FullName_save(fname, FALSE);
18490
18491 if (ffname != NULL)
18492 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18493 vim_free(ffname);
18494 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018495 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018496#else
18497 rettv->vval.v_string = NULL;
18498#endif
18499}
18500
18501/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018502 * "undotree()" function
18503 */
18504 static void
18505f_undotree(argvars, rettv)
18506 typval_T *argvars UNUSED;
18507 typval_T *rettv;
18508{
18509 if (rettv_dict_alloc(rettv) == OK)
18510 {
18511 dict_T *dict = rettv->vval.v_dict;
18512 list_T *list;
18513
Bram Moolenaar730cde92010-06-27 05:18:54 +020018514 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018515 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018516 dict_add_nr_str(dict, "save_last",
18517 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018518 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18519 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018520 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018521
18522 list = list_alloc();
18523 if (list != NULL)
18524 {
18525 u_eval_tree(curbuf->b_u_oldhead, list);
18526 dict_add_list(dict, "entries", list);
18527 }
18528 }
18529}
18530
18531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018532 * "values(dict)" function
18533 */
18534 static void
18535f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018536 typval_T *argvars;
18537 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018538{
18539 dict_list(argvars, rettv, 1);
18540}
18541
18542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 * "virtcol(string)" function
18544 */
18545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018546f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018547 typval_T *argvars;
18548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549{
18550 colnr_T vcol = 0;
18551 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018552 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018554 fp = var2fpos(&argvars[0], FALSE, &fnum);
18555 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18556 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 {
18558 getvvcol(curwin, fp, NULL, NULL, &vcol);
18559 ++vcol;
18560 }
18561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018562 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563}
18564
18565/*
18566 * "visualmode()" function
18567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018570 typval_T *argvars UNUSED;
18571 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572{
18573#ifdef FEAT_VISUAL
18574 char_u str[2];
18575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018576 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 str[0] = curbuf->b_visual_mode_eval;
18578 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580
18581 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018582 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584#endif
18585}
18586
18587/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018588 * "wildmenumode()" function
18589 */
18590 static void
18591f_wildmenumode(argvars, rettv)
18592 typval_T *argvars UNUSED;
18593 typval_T *rettv UNUSED;
18594{
18595#ifdef FEAT_WILDMENU
18596 if (wild_menu_showing)
18597 rettv->vval.v_number = 1;
18598#endif
18599}
18600
18601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 * "winbufnr(nr)" function
18603 */
18604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018606 typval_T *argvars;
18607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608{
18609 win_T *wp;
18610
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018611 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616}
18617
18618/*
18619 * "wincol()" function
18620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018622f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625{
18626 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628}
18629
18630/*
18631 * "winheight(nr)" function
18632 */
18633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 typval_T *argvars;
18636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
18638 win_T *wp;
18639
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018640 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645}
18646
18647/*
18648 * "winline()" function
18649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018652 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654{
18655 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657}
18658
18659/*
18660 * "winnr()" function
18661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666{
18667 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018668
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018670 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673}
18674
18675/*
18676 * "winrestcmd()" function
18677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018679f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018680 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682{
18683#ifdef FEAT_WINDOWS
18684 win_T *wp;
18685 int winnr = 1;
18686 garray_T ga;
18687 char_u buf[50];
18688
18689 ga_init2(&ga, (int)sizeof(char), 70);
18690 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18691 {
18692 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18693 ga_concat(&ga, buf);
18694# ifdef FEAT_VERTSPLIT
18695 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18696 ga_concat(&ga, buf);
18697# endif
18698 ++winnr;
18699 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018700 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707}
18708
18709/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018710 * "winrestview()" function
18711 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018712 static void
18713f_winrestview(argvars, rettv)
18714 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018715 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018716{
18717 dict_T *dict;
18718
18719 if (argvars[0].v_type != VAR_DICT
18720 || (dict = argvars[0].vval.v_dict) == NULL)
18721 EMSG(_(e_invarg));
18722 else
18723 {
18724 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18725 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18726#ifdef FEAT_VIRTUALEDIT
18727 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18728#endif
18729 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018730 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018731
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018732 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018733#ifdef FEAT_DIFF
18734 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18735#endif
18736 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18737 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18738
18739 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018740 win_new_height(curwin, curwin->w_height);
18741# ifdef FEAT_VERTSPLIT
18742 win_new_width(curwin, W_WIDTH(curwin));
18743# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018744 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018745
18746 if (curwin->w_topline == 0)
18747 curwin->w_topline = 1;
18748 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18749 curwin->w_topline = curbuf->b_ml.ml_line_count;
18750#ifdef FEAT_DIFF
18751 check_topfill(curwin, TRUE);
18752#endif
18753 }
18754}
18755
18756/*
18757 * "winsaveview()" function
18758 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018759 static void
18760f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018761 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018762 typval_T *rettv;
18763{
18764 dict_T *dict;
18765
Bram Moolenaara800b422010-06-27 01:15:55 +020018766 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018767 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018768 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018769
18770 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18771 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18772#ifdef FEAT_VIRTUALEDIT
18773 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18774#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018775 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018776 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18777
18778 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18779#ifdef FEAT_DIFF
18780 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18781#endif
18782 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18783 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18784}
18785
18786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 * "winwidth(nr)" function
18788 */
18789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T *argvars;
18792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793{
18794 win_T *wp;
18795
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018796 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 else
18800#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804#endif
18805}
18806
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018808 * "writefile()" function
18809 */
18810 static void
18811f_writefile(argvars, rettv)
18812 typval_T *argvars;
18813 typval_T *rettv;
18814{
18815 int binary = FALSE;
18816 char_u *fname;
18817 FILE *fd;
18818 listitem_T *li;
18819 char_u *s;
18820 int ret = 0;
18821 int c;
18822
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018823 if (check_restricted() || check_secure())
18824 return;
18825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018826 if (argvars[0].v_type != VAR_LIST)
18827 {
18828 EMSG2(_(e_listarg), "writefile()");
18829 return;
18830 }
18831 if (argvars[0].vval.v_list == NULL)
18832 return;
18833
18834 if (argvars[2].v_type != VAR_UNKNOWN
18835 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18836 binary = TRUE;
18837
18838 /* Always open the file in binary mode, library functions have a mind of
18839 * their own about CR-LF conversion. */
18840 fname = get_tv_string(&argvars[1]);
18841 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18842 {
18843 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18844 ret = -1;
18845 }
18846 else
18847 {
18848 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18849 li = li->li_next)
18850 {
18851 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18852 {
18853 if (*s == '\n')
18854 c = putc(NUL, fd);
18855 else
18856 c = putc(*s, fd);
18857 if (c == EOF)
18858 {
18859 ret = -1;
18860 break;
18861 }
18862 }
18863 if (!binary || li->li_next != NULL)
18864 if (putc('\n', fd) == EOF)
18865 {
18866 ret = -1;
18867 break;
18868 }
18869 if (ret < 0)
18870 {
18871 EMSG(_(e_write));
18872 break;
18873 }
18874 }
18875 fclose(fd);
18876 }
18877
18878 rettv->vval.v_number = ret;
18879}
18880
18881/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018882 * "xor(expr, expr)" function
18883 */
18884 static void
18885f_xor(argvars, rettv)
18886 typval_T *argvars;
18887 typval_T *rettv;
18888{
18889 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18890 ^ get_tv_number_chk(&argvars[1], NULL);
18891}
18892
18893
18894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018896 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 */
18898 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018899var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018900 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018901 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018902 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018904 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018906 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907
Bram Moolenaara5525202006-03-02 22:52:09 +000018908 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018909 if (varp->v_type == VAR_LIST)
18910 {
18911 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018912 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018913 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018914 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018915
18916 l = varp->vval.v_list;
18917 if (l == NULL)
18918 return NULL;
18919
18920 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018921 pos.lnum = list_find_nr(l, 0L, &error);
18922 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018923 return NULL; /* invalid line number */
18924
18925 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018926 pos.col = list_find_nr(l, 1L, &error);
18927 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018928 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018929 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018930
18931 /* We accept "$" for the column number: last column. */
18932 li = list_find(l, 1L);
18933 if (li != NULL && li->li_tv.v_type == VAR_STRING
18934 && li->li_tv.vval.v_string != NULL
18935 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18936 pos.col = len + 1;
18937
Bram Moolenaara5525202006-03-02 22:52:09 +000018938 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018939 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018940 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018941 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018942
Bram Moolenaara5525202006-03-02 22:52:09 +000018943#ifdef FEAT_VIRTUALEDIT
18944 /* Get the virtual offset. Defaults to zero. */
18945 pos.coladd = list_find_nr(l, 2L, &error);
18946 if (error)
18947 pos.coladd = 0;
18948#endif
18949
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018950 return &pos;
18951 }
18952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018953 name = get_tv_string_chk(varp);
18954 if (name == NULL)
18955 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018956 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018958#ifdef FEAT_VISUAL
18959 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18960 {
18961 if (VIsual_active)
18962 return &VIsual;
18963 return &curwin->w_cursor;
18964 }
18965#endif
18966 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018968 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18970 return NULL;
18971 return pp;
18972 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018973
18974#ifdef FEAT_VIRTUALEDIT
18975 pos.coladd = 0;
18976#endif
18977
Bram Moolenaar477933c2007-07-17 14:32:23 +000018978 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018979 {
18980 pos.col = 0;
18981 if (name[1] == '0') /* "w0": first visible line */
18982 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018983 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018984 pos.lnum = curwin->w_topline;
18985 return &pos;
18986 }
18987 else if (name[1] == '$') /* "w$": last visible line */
18988 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018989 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018990 pos.lnum = curwin->w_botline - 1;
18991 return &pos;
18992 }
18993 }
18994 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018996 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 {
18998 pos.lnum = curbuf->b_ml.ml_line_count;
18999 pos.col = 0;
19000 }
19001 else
19002 {
19003 pos.lnum = curwin->w_cursor.lnum;
19004 pos.col = (colnr_T)STRLEN(ml_get_curline());
19005 }
19006 return &pos;
19007 }
19008 return NULL;
19009}
19010
19011/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019012 * Convert list in "arg" into a position and optional file number.
19013 * When "fnump" is NULL there is no file number, only 3 items.
19014 * Note that the column is passed on as-is, the caller may want to decrement
19015 * it to use 1 for the first column.
19016 * Return FAIL when conversion is not possible, doesn't check the position for
19017 * validity.
19018 */
19019 static int
19020list2fpos(arg, posp, fnump)
19021 typval_T *arg;
19022 pos_T *posp;
19023 int *fnump;
19024{
19025 list_T *l = arg->vval.v_list;
19026 long i = 0;
19027 long n;
19028
Bram Moolenaarbde35262006-07-23 20:12:24 +000019029 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19030 * when "fnump" isn't NULL and "coladd" is optional. */
19031 if (arg->v_type != VAR_LIST
19032 || l == NULL
19033 || l->lv_len < (fnump == NULL ? 2 : 3)
19034 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019035 return FAIL;
19036
19037 if (fnump != NULL)
19038 {
19039 n = list_find_nr(l, i++, NULL); /* fnum */
19040 if (n < 0)
19041 return FAIL;
19042 if (n == 0)
19043 n = curbuf->b_fnum; /* current buffer */
19044 *fnump = n;
19045 }
19046
19047 n = list_find_nr(l, i++, NULL); /* lnum */
19048 if (n < 0)
19049 return FAIL;
19050 posp->lnum = n;
19051
19052 n = list_find_nr(l, i++, NULL); /* col */
19053 if (n < 0)
19054 return FAIL;
19055 posp->col = n;
19056
19057#ifdef FEAT_VIRTUALEDIT
19058 n = list_find_nr(l, i, NULL);
19059 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019060 posp->coladd = 0;
19061 else
19062 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019063#endif
19064
19065 return OK;
19066}
19067
19068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 * Get the length of an environment variable name.
19070 * Advance "arg" to the first character after the name.
19071 * Return 0 for error.
19072 */
19073 static int
19074get_env_len(arg)
19075 char_u **arg;
19076{
19077 char_u *p;
19078 int len;
19079
19080 for (p = *arg; vim_isIDc(*p); ++p)
19081 ;
19082 if (p == *arg) /* no name found */
19083 return 0;
19084
19085 len = (int)(p - *arg);
19086 *arg = p;
19087 return len;
19088}
19089
19090/*
19091 * Get the length of the name of a function or internal variable.
19092 * "arg" is advanced to the first non-white character after the name.
19093 * Return 0 if something is wrong.
19094 */
19095 static int
19096get_id_len(arg)
19097 char_u **arg;
19098{
19099 char_u *p;
19100 int len;
19101
19102 /* Find the end of the name. */
19103 for (p = *arg; eval_isnamec(*p); ++p)
19104 ;
19105 if (p == *arg) /* no name found */
19106 return 0;
19107
19108 len = (int)(p - *arg);
19109 *arg = skipwhite(p);
19110
19111 return len;
19112}
19113
19114/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019115 * Get the length of the name of a variable or function.
19116 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019118 * Return -1 if curly braces expansion failed.
19119 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 * If the name contains 'magic' {}'s, expand them and return the
19121 * expanded name in an allocated string via 'alias' - caller must free.
19122 */
19123 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019124get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 char_u **arg;
19126 char_u **alias;
19127 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019128 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129{
19130 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 char_u *p;
19132 char_u *expr_start;
19133 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134
19135 *alias = NULL; /* default to no alias */
19136
19137 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19138 && (*arg)[2] == (int)KE_SNR)
19139 {
19140 /* hard coded <SNR>, already translated */
19141 *arg += 3;
19142 return get_id_len(arg) + 3;
19143 }
19144 len = eval_fname_script(*arg);
19145 if (len > 0)
19146 {
19147 /* literal "<SID>", "s:" or "<SNR>" */
19148 *arg += len;
19149 }
19150
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019154 p = find_name_end(*arg, &expr_start, &expr_end,
19155 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 if (expr_start != NULL)
19157 {
19158 char_u *temp_string;
19159
19160 if (!evaluate)
19161 {
19162 len += (int)(p - *arg);
19163 *arg = skipwhite(p);
19164 return len;
19165 }
19166
19167 /*
19168 * Include any <SID> etc in the expanded string:
19169 * Thus the -len here.
19170 */
19171 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19172 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019173 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 *alias = temp_string;
19175 *arg = skipwhite(p);
19176 return (int)STRLEN(temp_string);
19177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178
19179 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019180 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 EMSG2(_(e_invexpr2), *arg);
19182
19183 return len;
19184}
19185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186/*
19187 * Find the end of a variable or function name, taking care of magic braces.
19188 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19189 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019190 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019191 * Return a pointer to just after the name. Equal to "arg" if there is no
19192 * valid name.
19193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019195find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 char_u *arg;
19197 char_u **expr_start;
19198 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019199 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 int mb_nest = 0;
19202 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 char_u *p;
19204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019207 *expr_start = NULL;
19208 *expr_end = NULL;
19209 }
19210
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019211 /* Quick check for valid starting character. */
19212 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19213 return arg;
19214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 for (p = arg; *p != NUL
19216 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019217 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019218 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019220 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019221 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019222 if (*p == '\'')
19223 {
19224 /* skip over 'string' to avoid counting [ and ] inside it. */
19225 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19226 ;
19227 if (*p == NUL)
19228 break;
19229 }
19230 else if (*p == '"')
19231 {
19232 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19233 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19234 if (*p == '\\' && p[1] != NUL)
19235 ++p;
19236 if (*p == NUL)
19237 break;
19238 }
19239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019240 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019242 if (*p == '[')
19243 ++br_nest;
19244 else if (*p == ']')
19245 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019248 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250 if (*p == '{')
19251 {
19252 mb_nest++;
19253 if (expr_start != NULL && *expr_start == NULL)
19254 *expr_start = p;
19255 }
19256 else if (*p == '}')
19257 {
19258 mb_nest--;
19259 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19260 *expr_end = p;
19261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 }
19264
19265 return p;
19266}
19267
19268/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019269 * Expands out the 'magic' {}'s in a variable/function name.
19270 * Note that this can call itself recursively, to deal with
19271 * constructs like foo{bar}{baz}{bam}
19272 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19273 * "in_start" ^
19274 * "expr_start" ^
19275 * "expr_end" ^
19276 * "in_end" ^
19277 *
19278 * Returns a new allocated string, which the caller must free.
19279 * Returns NULL for failure.
19280 */
19281 static char_u *
19282make_expanded_name(in_start, expr_start, expr_end, in_end)
19283 char_u *in_start;
19284 char_u *expr_start;
19285 char_u *expr_end;
19286 char_u *in_end;
19287{
19288 char_u c1;
19289 char_u *retval = NULL;
19290 char_u *temp_result;
19291 char_u *nextcmd = NULL;
19292
19293 if (expr_end == NULL || in_end == NULL)
19294 return NULL;
19295 *expr_start = NUL;
19296 *expr_end = NUL;
19297 c1 = *in_end;
19298 *in_end = NUL;
19299
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019300 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019301 if (temp_result != NULL && nextcmd == NULL)
19302 {
19303 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19304 + (in_end - expr_end) + 1));
19305 if (retval != NULL)
19306 {
19307 STRCPY(retval, in_start);
19308 STRCAT(retval, temp_result);
19309 STRCAT(retval, expr_end + 1);
19310 }
19311 }
19312 vim_free(temp_result);
19313
19314 *in_end = c1; /* put char back for error messages */
19315 *expr_start = '{';
19316 *expr_end = '}';
19317
19318 if (retval != NULL)
19319 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019320 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019321 if (expr_start != NULL)
19322 {
19323 /* Further expansion! */
19324 temp_result = make_expanded_name(retval, expr_start,
19325 expr_end, temp_result);
19326 vim_free(retval);
19327 retval = temp_result;
19328 }
19329 }
19330
19331 return retval;
19332}
19333
19334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019336 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 */
19338 static int
19339eval_isnamec(c)
19340 int c;
19341{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019342 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19343}
19344
19345/*
19346 * Return TRUE if character "c" can be used as the first character in a
19347 * variable or function name (excluding '{' and '}').
19348 */
19349 static int
19350eval_isnamec1(c)
19351 int c;
19352{
19353 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354}
19355
19356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 * Set number v: variable to "val".
19358 */
19359 void
19360set_vim_var_nr(idx, val)
19361 int idx;
19362 long val;
19363{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019364 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365}
19366
19367/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019368 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 */
19370 long
19371get_vim_var_nr(idx)
19372 int idx;
19373{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019374 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375}
19376
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019377/*
19378 * Get string v: variable value. Uses a static buffer, can only be used once.
19379 */
19380 char_u *
19381get_vim_var_str(idx)
19382 int idx;
19383{
19384 return get_tv_string(&vimvars[idx].vv_tv);
19385}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019386
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019388 * Get List v: variable value. Caller must take care of reference count when
19389 * needed.
19390 */
19391 list_T *
19392get_vim_var_list(idx)
19393 int idx;
19394{
19395 return vimvars[idx].vv_list;
19396}
19397
19398/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019399 * Set v:char to character "c".
19400 */
19401 void
19402set_vim_var_char(c)
19403 int c;
19404{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019405 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019406
19407#ifdef FEAT_MBYTE
19408 if (has_mbyte)
19409 buf[(*mb_char2bytes)(c, buf)] = NUL;
19410 else
19411#endif
19412 {
19413 buf[0] = c;
19414 buf[1] = NUL;
19415 }
19416 set_vim_var_string(VV_CHAR, buf, -1);
19417}
19418
19419/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019420 * Set v:count to "count" and v:count1 to "count1".
19421 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 */
19423 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019424set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 long count;
19426 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019427 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019429 if (set_prevcount)
19430 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019431 vimvars[VV_COUNT].vv_nr = count;
19432 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433}
19434
19435/*
19436 * Set string v: variable to a copy of "val".
19437 */
19438 void
19439set_vim_var_string(idx, val, len)
19440 int idx;
19441 char_u *val;
19442 int len; /* length of "val" to use or -1 (whole string) */
19443{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019444 /* Need to do this (at least) once, since we can't initialize a union.
19445 * Will always be invoked when "v:progname" is set. */
19446 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19447
Bram Moolenaare9a41262005-01-15 22:18:47 +000019448 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019450 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019452 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019454 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455}
19456
19457/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019458 * Set List v: variable to "val".
19459 */
19460 void
19461set_vim_var_list(idx, val)
19462 int idx;
19463 list_T *val;
19464{
19465 list_unref(vimvars[idx].vv_list);
19466 vimvars[idx].vv_list = val;
19467 if (val != NULL)
19468 ++val->lv_refcount;
19469}
19470
19471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 * Set v:register if needed.
19473 */
19474 void
19475set_reg_var(c)
19476 int c;
19477{
19478 char_u regname;
19479
19480 if (c == 0 || c == ' ')
19481 regname = '"';
19482 else
19483 regname = c;
19484 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019485 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 set_vim_var_string(VV_REG, &regname, 1);
19487}
19488
19489/*
19490 * Get or set v:exception. If "oldval" == NULL, return the current value.
19491 * Otherwise, restore the value to "oldval" and return NULL.
19492 * Must always be called in pairs to save and restore v:exception! Does not
19493 * take care of memory allocations.
19494 */
19495 char_u *
19496v_exception(oldval)
19497 char_u *oldval;
19498{
19499 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019500 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501
Bram Moolenaare9a41262005-01-15 22:18:47 +000019502 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 return NULL;
19504}
19505
19506/*
19507 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19508 * Otherwise, restore the value to "oldval" and return NULL.
19509 * Must always be called in pairs to save and restore v:throwpoint! Does not
19510 * take care of memory allocations.
19511 */
19512 char_u *
19513v_throwpoint(oldval)
19514 char_u *oldval;
19515{
19516 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019517 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518
Bram Moolenaare9a41262005-01-15 22:18:47 +000019519 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 return NULL;
19521}
19522
19523#if defined(FEAT_AUTOCMD) || defined(PROTO)
19524/*
19525 * Set v:cmdarg.
19526 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19527 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19528 * Must always be called in pairs!
19529 */
19530 char_u *
19531set_cmdarg(eap, oldarg)
19532 exarg_T *eap;
19533 char_u *oldarg;
19534{
19535 char_u *oldval;
19536 char_u *newval;
19537 unsigned len;
19538
Bram Moolenaare9a41262005-01-15 22:18:47 +000019539 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019540 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019542 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019543 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019544 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 }
19546
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019547 if (eap->force_bin == FORCE_BIN)
19548 len = 6;
19549 else if (eap->force_bin == FORCE_NOBIN)
19550 len = 8;
19551 else
19552 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019553
19554 if (eap->read_edit)
19555 len += 7;
19556
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019557 if (eap->force_ff != 0)
19558 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19559# ifdef FEAT_MBYTE
19560 if (eap->force_enc != 0)
19561 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019562 if (eap->bad_char != 0)
19563 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019564# endif
19565
19566 newval = alloc(len + 1);
19567 if (newval == NULL)
19568 return NULL;
19569
19570 if (eap->force_bin == FORCE_BIN)
19571 sprintf((char *)newval, " ++bin");
19572 else if (eap->force_bin == FORCE_NOBIN)
19573 sprintf((char *)newval, " ++nobin");
19574 else
19575 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019576
19577 if (eap->read_edit)
19578 STRCAT(newval, " ++edit");
19579
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019580 if (eap->force_ff != 0)
19581 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19582 eap->cmd + eap->force_ff);
19583# ifdef FEAT_MBYTE
19584 if (eap->force_enc != 0)
19585 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19586 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019587 if (eap->bad_char == BAD_KEEP)
19588 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19589 else if (eap->bad_char == BAD_DROP)
19590 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19591 else if (eap->bad_char != 0)
19592 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019593# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019594 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019595 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596}
19597#endif
19598
19599/*
19600 * Get the value of internal variable "name".
19601 * Return OK or FAIL.
19602 */
19603 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019604get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 char_u *name;
19606 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019608 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609{
19610 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 typval_T *tv = NULL;
19612 typval_T atv;
19613 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615
19616 /* truncate the name, so that we can use strcmp() */
19617 cc = name[len];
19618 name[len] = NUL;
19619
19620 /*
19621 * Check for "b:changedtick".
19622 */
19623 if (STRCMP(name, "b:changedtick") == 0)
19624 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019625 atv.v_type = VAR_NUMBER;
19626 atv.vval.v_number = curbuf->b_changedtick;
19627 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 }
19629
19630 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 * Check for user-defined variables.
19632 */
19633 else
19634 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019635 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 }
19639
Bram Moolenaare9a41262005-01-15 22:18:47 +000019640 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019642 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 ret = FAIL;
19645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019647 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648
19649 name[len] = cc;
19650
19651 return ret;
19652}
19653
19654/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019655 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19656 * Also handle function call with Funcref variable: func(expr)
19657 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19658 */
19659 static int
19660handle_subscript(arg, rettv, evaluate, verbose)
19661 char_u **arg;
19662 typval_T *rettv;
19663 int evaluate; /* do more than finding the end */
19664 int verbose; /* give error messages */
19665{
19666 int ret = OK;
19667 dict_T *selfdict = NULL;
19668 char_u *s;
19669 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019670 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019671
19672 while (ret == OK
19673 && (**arg == '['
19674 || (**arg == '.' && rettv->v_type == VAR_DICT)
19675 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19676 && !vim_iswhite(*(*arg - 1)))
19677 {
19678 if (**arg == '(')
19679 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019680 /* need to copy the funcref so that we can clear rettv */
19681 functv = *rettv;
19682 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019683
19684 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019685 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019686 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019687 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19688 &len, evaluate, selfdict);
19689
19690 /* Clear the funcref afterwards, so that deleting it while
19691 * evaluating the arguments is possible (see test55). */
19692 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019693
19694 /* Stop the expression evaluation when immediately aborting on
19695 * error, or when an interrupt occurred or an exception was thrown
19696 * but not caught. */
19697 if (aborting())
19698 {
19699 if (ret == OK)
19700 clear_tv(rettv);
19701 ret = FAIL;
19702 }
19703 dict_unref(selfdict);
19704 selfdict = NULL;
19705 }
19706 else /* **arg == '[' || **arg == '.' */
19707 {
19708 dict_unref(selfdict);
19709 if (rettv->v_type == VAR_DICT)
19710 {
19711 selfdict = rettv->vval.v_dict;
19712 if (selfdict != NULL)
19713 ++selfdict->dv_refcount;
19714 }
19715 else
19716 selfdict = NULL;
19717 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19718 {
19719 clear_tv(rettv);
19720 ret = FAIL;
19721 }
19722 }
19723 }
19724 dict_unref(selfdict);
19725 return ret;
19726}
19727
19728/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019729 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019730 * value).
19731 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019734{
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019736}
19737
19738/*
19739 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 * The string "s" must have been allocated, it is consumed.
19741 * Return NULL for out of memory, the variable otherwise.
19742 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 char_u *s;
19746{
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019749 rettv = alloc_tv();
19750 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019752 rettv->v_type = VAR_STRING;
19753 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 }
19755 else
19756 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758}
19759
19760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019761 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019763 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766{
19767 if (varp != NULL)
19768 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019769 switch (varp->v_type)
19770 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019771 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019772 func_unref(varp->vval.v_string);
19773 /*FALLTHROUGH*/
19774 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019775 vim_free(varp->vval.v_string);
19776 break;
19777 case VAR_LIST:
19778 list_unref(varp->vval.v_list);
19779 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019780 case VAR_DICT:
19781 dict_unref(varp->vval.v_dict);
19782 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019783 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019784#ifdef FEAT_FLOAT
19785 case VAR_FLOAT:
19786#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019787 case VAR_UNKNOWN:
19788 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019789 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019790 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019791 break;
19792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 vim_free(varp);
19794 }
19795}
19796
19797/*
19798 * Free the memory for a variable value and set the value to NULL or 0.
19799 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019800 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803{
19804 if (varp != NULL)
19805 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019806 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019809 func_unref(varp->vval.v_string);
19810 /*FALLTHROUGH*/
19811 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019812 vim_free(varp->vval.v_string);
19813 varp->vval.v_string = NULL;
19814 break;
19815 case VAR_LIST:
19816 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019817 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019818 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019819 case VAR_DICT:
19820 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019821 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019822 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824 varp->vval.v_number = 0;
19825 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019826#ifdef FEAT_FLOAT
19827 case VAR_FLOAT:
19828 varp->vval.v_float = 0.0;
19829 break;
19830#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 case VAR_UNKNOWN:
19832 break;
19833 default:
19834 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019836 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 }
19838}
19839
19840/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 * Set the value of a variable to NULL without freeing items.
19842 */
19843 static void
19844init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019845 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019846{
19847 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019848 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849}
19850
19851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 * Get the number value of a variable.
19853 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019854 * For incompatible types, return 0.
19855 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19856 * caller of incompatible types: it sets *denote to TRUE if "denote"
19857 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 */
19859 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019860get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019863 int error = FALSE;
19864
19865 return get_tv_number_chk(varp, &error); /* return 0L on error */
19866}
19867
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019868 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019869get_tv_number_chk(varp, denote)
19870 typval_T *varp;
19871 int *denote;
19872{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019873 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019877 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019878 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019879#ifdef FEAT_FLOAT
19880 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019881 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019882 break;
19883#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019884 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019885 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019886 break;
19887 case VAR_STRING:
19888 if (varp->vval.v_string != NULL)
19889 vim_str2nr(varp->vval.v_string, NULL, NULL,
19890 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019891 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019892 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019893 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019894 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019895 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019896 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019897 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019898 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019899 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019902 if (denote == NULL) /* useful for values that must be unsigned */
19903 n = -1;
19904 else
19905 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907}
19908
19909/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019910 * Get the lnum from the first argument.
19911 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019912 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 */
19914 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917{
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 linenr_T lnum;
19920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019921 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 if (lnum == 0) /* no valid number, try using line() */
19923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 rettv.v_type = VAR_NUMBER;
19925 f_line(argvars, &rettv);
19926 lnum = rettv.vval.v_number;
19927 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 }
19929 return lnum;
19930}
19931
19932/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019933 * Get the lnum from the first argument.
19934 * Also accepts "$", then "buf" is used.
19935 * Returns 0 on error.
19936 */
19937 static linenr_T
19938get_tv_lnum_buf(argvars, buf)
19939 typval_T *argvars;
19940 buf_T *buf;
19941{
19942 if (argvars[0].v_type == VAR_STRING
19943 && argvars[0].vval.v_string != NULL
19944 && argvars[0].vval.v_string[0] == '$'
19945 && buf != NULL)
19946 return buf->b_ml.ml_line_count;
19947 return get_tv_number_chk(&argvars[0], NULL);
19948}
19949
19950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 * Get the string value of a variable.
19952 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019953 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19954 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 * If the String variable has never been set, return an empty string.
19956 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019957 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19958 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 */
19960 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019962 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963{
19964 static char_u mybuf[NUMBUFLEN];
19965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019966 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967}
19968
19969 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019970get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019972 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019974 char_u *res = get_tv_string_buf_chk(varp, buf);
19975
19976 return res != NULL ? res : (char_u *)"";
19977}
19978
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019979 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019980get_tv_string_chk(varp)
19981 typval_T *varp;
19982{
19983 static char_u mybuf[NUMBUFLEN];
19984
19985 return get_tv_string_buf_chk(varp, mybuf);
19986}
19987
19988 static char_u *
19989get_tv_string_buf_chk(varp, buf)
19990 typval_T *varp;
19991 char_u *buf;
19992{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019993 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 case VAR_NUMBER:
19996 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19997 return buf;
19998 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019999 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020000 break;
20001 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020002 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020003 break;
20004 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020006 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020007#ifdef FEAT_FLOAT
20008 case VAR_FLOAT:
20009 EMSG(_("E806: using Float as a String"));
20010 break;
20011#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020012 case VAR_STRING:
20013 if (varp->vval.v_string != NULL)
20014 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020015 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020017 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020020 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021}
20022
20023/*
20024 * Find variable "name" in the list of variables.
20025 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020026 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020027 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020028 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020030 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020031find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020033 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037
Bram Moolenaara7043832005-01-21 11:56:39 +000020038 ht = find_var_ht(name, &varname);
20039 if (htp != NULL)
20040 *htp = ht;
20041 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020043 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044}
20045
20046/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000020048 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020050 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020051find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000020053 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020054 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020055{
Bram Moolenaar33570922005-01-25 22:26:29 +000020056 hashitem_T *hi;
20057
20058 if (*varname == NUL)
20059 {
20060 /* Must be something like "s:", otherwise "ht" would be NULL. */
20061 switch (varname[-2])
20062 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020063 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 case 'g': return &globvars_var;
20065 case 'v': return &vimvars_var;
20066 case 'b': return &curbuf->b_bufvar;
20067 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020068#ifdef FEAT_WINDOWS
20069 case 't': return &curtab->tp_winvar;
20070#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020071 case 'l': return current_funccal == NULL
20072 ? NULL : &current_funccal->l_vars_var;
20073 case 'a': return current_funccal == NULL
20074 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 }
20076 return NULL;
20077 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020078
20079 hi = hash_find(ht, varname);
20080 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020081 {
20082 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020083 * worked find the variable again. Don't auto-load a script if it was
20084 * loaded already, otherwise it would be loaded every time when
20085 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020086 if (ht == &globvarht && !writing)
20087 {
20088 /* Note: script_autoload() may make "hi" invalid. It must either
20089 * be obtained again or not used. */
20090 if (!script_autoload(varname, FALSE) || aborting())
20091 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020092 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020094 if (HASHITEM_EMPTY(hi))
20095 return NULL;
20096 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020098}
20099
20100/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020101 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020102 * Set "varname" to the start of name without ':'.
20103 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020105find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 char_u *name;
20107 char_u **varname;
20108{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020109 hashitem_T *hi;
20110
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 if (name[1] != ':')
20112 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020113 /* The name must not start with a colon or #. */
20114 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 return NULL;
20116 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020117
20118 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020119 hi = hash_find(&compat_hashtab, name);
20120 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020121 return &compat_hashtab;
20122
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 return &globvarht; /* global variable */
20125 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 }
20127 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020128 if (*name == 'g') /* global variable */
20129 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020130 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20131 */
20132 if (vim_strchr(name + 2, ':') != NULL
20133 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020134 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020138 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020139#ifdef FEAT_WINDOWS
20140 if (*name == 't') /* tab page variable */
20141 return &curtab->tp_vars.dv_hashtab;
20142#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020143 if (*name == 'v') /* v: variable */
20144 return &vimvarht;
20145 if (*name == 'a' && current_funccal != NULL) /* function argument */
20146 return &current_funccal->l_avars.dv_hashtab;
20147 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20148 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 if (*name == 's' /* script variable */
20150 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20151 return &SCRIPT_VARS(current_SID);
20152 return NULL;
20153}
20154
20155/*
20156 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020157 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 * Returns NULL when it doesn't exist.
20159 */
20160 char_u *
20161get_var_value(name)
20162 char_u *name;
20163{
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165
Bram Moolenaara7043832005-01-21 11:56:39 +000020166 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 if (v == NULL)
20168 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170}
20171
20172/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 * sourcing this script and when executing functions defined in the script.
20175 */
20176 void
20177new_script_vars(id)
20178 scid_T id;
20179{
Bram Moolenaara7043832005-01-21 11:56:39 +000020180 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 hashtab_T *ht;
20182 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020183
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20185 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020186 /* Re-allocating ga_data means that an ht_array pointing to
20187 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020188 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020189 for (i = 1; i <= ga_scripts.ga_len; ++i)
20190 {
20191 ht = &SCRIPT_VARS(i);
20192 if (ht->ht_mask == HT_INIT_SIZE - 1)
20193 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020194 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020195 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020196 }
20197
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 while (ga_scripts.ga_len < id)
20199 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020200 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020201 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020202 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 }
20205 }
20206}
20207
20208/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20210 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 */
20212 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020213init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020214 dict_T *dict;
20215 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020216 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217{
Bram Moolenaar33570922005-01-25 22:26:29 +000020218 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020219 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020220 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020221 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020222 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 dict_var->di_tv.vval.v_dict = dict;
20224 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020225 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20227 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
20231 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 * Frees all allocated variables and the value they contain.
20233 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 */
20235 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020236vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020237 hashtab_T *ht;
20238{
20239 vars_clear_ext(ht, TRUE);
20240}
20241
20242/*
20243 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20244 */
20245 static void
20246vars_clear_ext(ht, free_val)
20247 hashtab_T *ht;
20248 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249{
Bram Moolenaara7043832005-01-21 11:56:39 +000020250 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020251 hashitem_T *hi;
20252 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020255 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020256 for (hi = ht->ht_array; todo > 0; ++hi)
20257 {
20258 if (!HASHITEM_EMPTY(hi))
20259 {
20260 --todo;
20261
Bram Moolenaar33570922005-01-25 22:26:29 +000020262 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020263 * ht_array might change then. hash_clear() takes care of it
20264 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 v = HI2DI(hi);
20266 if (free_val)
20267 clear_tv(&v->di_tv);
20268 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20269 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020270 }
20271 }
20272 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020273 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274}
20275
Bram Moolenaara7043832005-01-21 11:56:39 +000020276/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020277 * Delete a variable from hashtab "ht" at item "hi".
20278 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020281delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 hashtab_T *ht;
20283 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284{
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020286
20287 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020288 clear_tv(&di->di_tv);
20289 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290}
20291
20292/*
20293 * List the value of one internal variable.
20294 */
20295 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020296list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020297 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020299 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020301 char_u *tofree;
20302 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020303 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020304
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020305 current_copyID += COPYID_INC;
20306 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020307 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020308 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020309 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310}
20311
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020313list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 char_u *prefix;
20315 char_u *name;
20316 int type;
20317 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020318 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319{
Bram Moolenaar31859182007-08-14 20:41:13 +000020320 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20321 msg_start();
20322 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 if (name != NULL) /* "a:" vars don't have a name stored */
20324 msg_puts(name);
20325 msg_putchar(' ');
20326 msg_advance(22);
20327 if (type == VAR_NUMBER)
20328 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020329 else if (type == VAR_FUNC)
20330 msg_putchar('*');
20331 else if (type == VAR_LIST)
20332 {
20333 msg_putchar('[');
20334 if (*string == '[')
20335 ++string;
20336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020337 else if (type == VAR_DICT)
20338 {
20339 msg_putchar('{');
20340 if (*string == '{')
20341 ++string;
20342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 else
20344 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020345
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020347
20348 if (type == VAR_FUNC)
20349 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020350 if (*first)
20351 {
20352 msg_clr_eos();
20353 *first = FALSE;
20354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355}
20356
20357/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 * If the variable already exists, the value is updated.
20360 * Otherwise the variable is created.
20361 */
20362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020363set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020365 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020366 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367{
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020370 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020372 ht = find_var_ht(name, &varname);
20373 if (ht == NULL || *varname == NUL)
20374 {
20375 EMSG2(_(e_illvar), name);
20376 return;
20377 }
20378 v = find_var_in_ht(ht, varname, TRUE);
20379
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020380 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20381 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020382
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020385 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020386 if (var_check_ro(v->di_flags, name)
20387 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020388 return;
20389 if (v->di_tv.v_type != tv->v_type
20390 && !((v->di_tv.v_type == VAR_STRING
20391 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020393 || tv->v_type == VAR_NUMBER))
20394#ifdef FEAT_FLOAT
20395 && !((v->di_tv.v_type == VAR_NUMBER
20396 || v->di_tv.v_type == VAR_FLOAT)
20397 && (tv->v_type == VAR_NUMBER
20398 || tv->v_type == VAR_FLOAT))
20399#endif
20400 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020401 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020402 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020403 return;
20404 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020405
20406 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020407 * Handle setting internal v: variables separately: we don't change
20408 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 */
20410 if (ht == &vimvarht)
20411 {
20412 if (v->di_tv.v_type == VAR_STRING)
20413 {
20414 vim_free(v->di_tv.vval.v_string);
20415 if (copy || tv->v_type != VAR_STRING)
20416 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20417 else
20418 {
20419 /* Take over the string to avoid an extra alloc/free. */
20420 v->di_tv.vval.v_string = tv->vval.v_string;
20421 tv->vval.v_string = NULL;
20422 }
20423 }
20424 else if (v->di_tv.v_type != VAR_NUMBER)
20425 EMSG2(_(e_intern2), "set_var()");
20426 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020427 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020428 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020429 if (STRCMP(varname, "searchforward") == 0)
20430 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20431 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020432 return;
20433 }
20434
20435 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 }
20437 else /* add a new variable */
20438 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020439 /* Can't add "v:" variable. */
20440 if (ht == &vimvarht)
20441 {
20442 EMSG2(_(e_illvar), name);
20443 return;
20444 }
20445
Bram Moolenaar92124a32005-06-17 22:03:40 +000020446 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020447 if (!valid_varname(varname))
20448 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020449
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020450 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20451 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020452 if (v == NULL)
20453 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020455 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020457 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 return;
20459 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020460 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020462
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020463 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020464 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020465 else
20466 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020468 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020469 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471}
20472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020473/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020474 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020475 * Also give an error message.
20476 */
20477 static int
20478var_check_ro(flags, name)
20479 int flags;
20480 char_u *name;
20481{
20482 if (flags & DI_FLAGS_RO)
20483 {
20484 EMSG2(_(e_readonlyvar), name);
20485 return TRUE;
20486 }
20487 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20488 {
20489 EMSG2(_(e_readonlysbx), name);
20490 return TRUE;
20491 }
20492 return FALSE;
20493}
20494
20495/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020496 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20497 * Also give an error message.
20498 */
20499 static int
20500var_check_fixed(flags, name)
20501 int flags;
20502 char_u *name;
20503{
20504 if (flags & DI_FLAGS_FIX)
20505 {
20506 EMSG2(_("E795: Cannot delete variable %s"), name);
20507 return TRUE;
20508 }
20509 return FALSE;
20510}
20511
20512/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020513 * Check if a funcref is assigned to a valid variable name.
20514 * Return TRUE and give an error if not.
20515 */
20516 static int
20517var_check_func_name(name, new_var)
20518 char_u *name; /* points to start of variable name */
20519 int new_var; /* TRUE when creating the variable */
20520{
20521 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20522 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20523 ? name[2] : name[0]))
20524 {
20525 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20526 name);
20527 return TRUE;
20528 }
20529 /* Don't allow hiding a function. When "v" is not NULL we might be
20530 * assigning another function to the same var, the type is checked
20531 * below. */
20532 if (new_var && function_exists(name))
20533 {
20534 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20535 name);
20536 return TRUE;
20537 }
20538 return FALSE;
20539}
20540
20541/*
20542 * Check if a variable name is valid.
20543 * Return FALSE and give an error if not.
20544 */
20545 static int
20546valid_varname(varname)
20547 char_u *varname;
20548{
20549 char_u *p;
20550
20551 for (p = varname; *p != NUL; ++p)
20552 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20553 && *p != AUTOLOAD_CHAR)
20554 {
20555 EMSG2(_(e_illvar), varname);
20556 return FALSE;
20557 }
20558 return TRUE;
20559}
20560
20561/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020562 * Return TRUE if typeval "tv" is set to be locked (immutable).
20563 * Also give an error message, using "name".
20564 */
20565 static int
20566tv_check_lock(lock, name)
20567 int lock;
20568 char_u *name;
20569{
20570 if (lock & VAR_LOCKED)
20571 {
20572 EMSG2(_("E741: Value is locked: %s"),
20573 name == NULL ? (char_u *)_("Unknown") : name);
20574 return TRUE;
20575 }
20576 if (lock & VAR_FIXED)
20577 {
20578 EMSG2(_("E742: Cannot change value of %s"),
20579 name == NULL ? (char_u *)_("Unknown") : name);
20580 return TRUE;
20581 }
20582 return FALSE;
20583}
20584
20585/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020586 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020587 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020588 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020589 * It is OK for "from" and "to" to point to the same item. This is used to
20590 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020591 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020592 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020594 typval_T *from;
20595 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020598 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020599 switch (from->v_type)
20600 {
20601 case VAR_NUMBER:
20602 to->vval.v_number = from->vval.v_number;
20603 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020604#ifdef FEAT_FLOAT
20605 case VAR_FLOAT:
20606 to->vval.v_float = from->vval.v_float;
20607 break;
20608#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 case VAR_STRING:
20610 case VAR_FUNC:
20611 if (from->vval.v_string == NULL)
20612 to->vval.v_string = NULL;
20613 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020615 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020616 if (from->v_type == VAR_FUNC)
20617 func_ref(to->vval.v_string);
20618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020619 break;
20620 case VAR_LIST:
20621 if (from->vval.v_list == NULL)
20622 to->vval.v_list = NULL;
20623 else
20624 {
20625 to->vval.v_list = from->vval.v_list;
20626 ++to->vval.v_list->lv_refcount;
20627 }
20628 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020629 case VAR_DICT:
20630 if (from->vval.v_dict == NULL)
20631 to->vval.v_dict = NULL;
20632 else
20633 {
20634 to->vval.v_dict = from->vval.v_dict;
20635 ++to->vval.v_dict->dv_refcount;
20636 }
20637 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020638 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020640 break;
20641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642}
20643
20644/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020645 * Make a copy of an item.
20646 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020647 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20648 * reference to an already copied list/dict can be used.
20649 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020650 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020651 static int
20652item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020653 typval_T *from;
20654 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020655 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020656 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020657{
20658 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020659 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020660
Bram Moolenaar33570922005-01-25 22:26:29 +000020661 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020662 {
20663 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020664 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020665 }
20666 ++recurse;
20667
20668 switch (from->v_type)
20669 {
20670 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020671#ifdef FEAT_FLOAT
20672 case VAR_FLOAT:
20673#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020674 case VAR_STRING:
20675 case VAR_FUNC:
20676 copy_tv(from, to);
20677 break;
20678 case VAR_LIST:
20679 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020680 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020681 if (from->vval.v_list == NULL)
20682 to->vval.v_list = NULL;
20683 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20684 {
20685 /* use the copy made earlier */
20686 to->vval.v_list = from->vval.v_list->lv_copylist;
20687 ++to->vval.v_list->lv_refcount;
20688 }
20689 else
20690 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20691 if (to->vval.v_list == NULL)
20692 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020693 break;
20694 case VAR_DICT:
20695 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020696 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020697 if (from->vval.v_dict == NULL)
20698 to->vval.v_dict = NULL;
20699 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20700 {
20701 /* use the copy made earlier */
20702 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20703 ++to->vval.v_dict->dv_refcount;
20704 }
20705 else
20706 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20707 if (to->vval.v_dict == NULL)
20708 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020709 break;
20710 default:
20711 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020712 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020713 }
20714 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020715 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020716}
20717
20718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 * ":echo expr1 ..." print each argument separated with a space, add a
20720 * newline at the end.
20721 * ":echon expr1 ..." print each argument plain.
20722 */
20723 void
20724ex_echo(eap)
20725 exarg_T *eap;
20726{
20727 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020728 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020729 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 char_u *p;
20731 int needclr = TRUE;
20732 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020733 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734
20735 if (eap->skip)
20736 ++emsg_skip;
20737 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20738 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020739 /* If eval1() causes an error message the text from the command may
20740 * still need to be cleared. E.g., "echo 22,44". */
20741 need_clr_eos = needclr;
20742
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020744 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 {
20746 /*
20747 * Report the invalid expression unless the expression evaluation
20748 * has been cancelled due to an aborting error, an interrupt, or an
20749 * exception.
20750 */
20751 if (!aborting())
20752 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020753 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 break;
20755 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020756 need_clr_eos = FALSE;
20757
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 if (!eap->skip)
20759 {
20760 if (atstart)
20761 {
20762 atstart = FALSE;
20763 /* Call msg_start() after eval1(), evaluating the expression
20764 * may cause a message to appear. */
20765 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020766 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020767 /* Mark the saved text as finishing the line, so that what
20768 * follows is displayed on a new line when scrolling back
20769 * at the more prompt. */
20770 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 }
20774 else if (eap->cmdidx == CMD_echo)
20775 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020776 current_copyID += COPYID_INC;
20777 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020778 if (p != NULL)
20779 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020781 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020783 if (*p != TAB && needclr)
20784 {
20785 /* remove any text still there from the command */
20786 msg_clr_eos();
20787 needclr = FALSE;
20788 }
20789 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 }
20791 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020792 {
20793#ifdef FEAT_MBYTE
20794 if (has_mbyte)
20795 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020796 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020797
20798 (void)msg_outtrans_len_attr(p, i, echo_attr);
20799 p += i - 1;
20800 }
20801 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020803 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020806 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020808 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 arg = skipwhite(arg);
20810 }
20811 eap->nextcmd = check_nextcmd(arg);
20812
20813 if (eap->skip)
20814 --emsg_skip;
20815 else
20816 {
20817 /* remove text that may still be there from the command */
20818 if (needclr)
20819 msg_clr_eos();
20820 if (eap->cmdidx == CMD_echo)
20821 msg_end();
20822 }
20823}
20824
20825/*
20826 * ":echohl {name}".
20827 */
20828 void
20829ex_echohl(eap)
20830 exarg_T *eap;
20831{
20832 int id;
20833
20834 id = syn_name2id(eap->arg);
20835 if (id == 0)
20836 echo_attr = 0;
20837 else
20838 echo_attr = syn_id2attr(id);
20839}
20840
20841/*
20842 * ":execute expr1 ..." execute the result of an expression.
20843 * ":echomsg expr1 ..." Print a message
20844 * ":echoerr expr1 ..." Print an error
20845 * Each gets spaces around each argument and a newline at the end for
20846 * echo commands
20847 */
20848 void
20849ex_execute(eap)
20850 exarg_T *eap;
20851{
20852 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020853 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 int ret = OK;
20855 char_u *p;
20856 garray_T ga;
20857 int len;
20858 int save_did_emsg;
20859
20860 ga_init2(&ga, 1, 80);
20861
20862 if (eap->skip)
20863 ++emsg_skip;
20864 while (*arg != NUL && *arg != '|' && *arg != '\n')
20865 {
20866 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020867 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 {
20869 /*
20870 * Report the invalid expression unless the expression evaluation
20871 * has been cancelled due to an aborting error, an interrupt, or an
20872 * exception.
20873 */
20874 if (!aborting())
20875 EMSG2(_(e_invexpr2), p);
20876 ret = FAIL;
20877 break;
20878 }
20879
20880 if (!eap->skip)
20881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020882 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 len = (int)STRLEN(p);
20884 if (ga_grow(&ga, len + 2) == FAIL)
20885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020886 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 ret = FAIL;
20888 break;
20889 }
20890 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 ga.ga_len += len;
20894 }
20895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 arg = skipwhite(arg);
20898 }
20899
20900 if (ret != FAIL && ga.ga_data != NULL)
20901 {
20902 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020905 out_flush();
20906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 else if (eap->cmdidx == CMD_echoerr)
20908 {
20909 /* We don't want to abort following commands, restore did_emsg. */
20910 save_did_emsg = did_emsg;
20911 EMSG((char_u *)ga.ga_data);
20912 if (!force_abort)
20913 did_emsg = save_did_emsg;
20914 }
20915 else if (eap->cmdidx == CMD_execute)
20916 do_cmdline((char_u *)ga.ga_data,
20917 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20918 }
20919
20920 ga_clear(&ga);
20921
20922 if (eap->skip)
20923 --emsg_skip;
20924
20925 eap->nextcmd = check_nextcmd(arg);
20926}
20927
20928/*
20929 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20930 * "arg" points to the "&" or '+' when called, to "option" when returning.
20931 * Returns NULL when no option name found. Otherwise pointer to the char
20932 * after the option name.
20933 */
20934 static char_u *
20935find_option_end(arg, opt_flags)
20936 char_u **arg;
20937 int *opt_flags;
20938{
20939 char_u *p = *arg;
20940
20941 ++p;
20942 if (*p == 'g' && p[1] == ':')
20943 {
20944 *opt_flags = OPT_GLOBAL;
20945 p += 2;
20946 }
20947 else if (*p == 'l' && p[1] == ':')
20948 {
20949 *opt_flags = OPT_LOCAL;
20950 p += 2;
20951 }
20952 else
20953 *opt_flags = 0;
20954
20955 if (!ASCII_ISALPHA(*p))
20956 return NULL;
20957 *arg = p;
20958
20959 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20960 p += 4; /* termcap option */
20961 else
20962 while (ASCII_ISALPHA(*p))
20963 ++p;
20964 return p;
20965}
20966
20967/*
20968 * ":function"
20969 */
20970 void
20971ex_function(eap)
20972 exarg_T *eap;
20973{
20974 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020975 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 int j;
20977 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 char_u *name = NULL;
20980 char_u *p;
20981 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020982 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 garray_T newargs;
20984 garray_T newlines;
20985 int varargs = FALSE;
20986 int mustend = FALSE;
20987 int flags = 0;
20988 ufunc_T *fp;
20989 int indent;
20990 int nesting;
20991 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020992 dictitem_T *v;
20993 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020994 static int func_nr = 0; /* number for nameless function */
20995 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020996 hashtab_T *ht;
20997 int todo;
20998 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020999 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000
21001 /*
21002 * ":function" without argument: list functions.
21003 */
21004 if (ends_excmd(*eap->arg))
21005 {
21006 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021007 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021008 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021009 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021010 {
21011 if (!HASHITEM_EMPTY(hi))
21012 {
21013 --todo;
21014 fp = HI2UF(hi);
21015 if (!isdigit(*fp->uf_name))
21016 list_func_head(fp, FALSE);
21017 }
21018 }
21019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 eap->nextcmd = check_nextcmd(eap->arg);
21021 return;
21022 }
21023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021024 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021025 * ":function /pat": list functions matching pattern.
21026 */
21027 if (*eap->arg == '/')
21028 {
21029 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21030 if (!eap->skip)
21031 {
21032 regmatch_T regmatch;
21033
21034 c = *p;
21035 *p = NUL;
21036 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21037 *p = c;
21038 if (regmatch.regprog != NULL)
21039 {
21040 regmatch.rm_ic = p_ic;
21041
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021042 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021043 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21044 {
21045 if (!HASHITEM_EMPTY(hi))
21046 {
21047 --todo;
21048 fp = HI2UF(hi);
21049 if (!isdigit(*fp->uf_name)
21050 && vim_regexec(&regmatch, fp->uf_name, 0))
21051 list_func_head(fp, FALSE);
21052 }
21053 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021054 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021055 }
21056 }
21057 if (*p == '/')
21058 ++p;
21059 eap->nextcmd = check_nextcmd(p);
21060 return;
21061 }
21062
21063 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021064 * Get the function name. There are these situations:
21065 * func normal function name
21066 * "name" == func, "fudi.fd_dict" == NULL
21067 * dict.func new dictionary entry
21068 * "name" == NULL, "fudi.fd_dict" set,
21069 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21070 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021071 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021072 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21073 * dict.func existing dict entry that's not a Funcref
21074 * "name" == NULL, "fudi.fd_dict" set,
21075 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021078 name = trans_function_name(&p, eap->skip, 0, &fudi);
21079 paren = (vim_strchr(p, '(') != NULL);
21080 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 {
21082 /*
21083 * Return on an invalid expression in braces, unless the expression
21084 * evaluation has been cancelled due to an aborting error, an
21085 * interrupt, or an exception.
21086 */
21087 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021089 if (!eap->skip && fudi.fd_newkey != NULL)
21090 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021091 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 else
21095 eap->skip = TRUE;
21096 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021097
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 /* An error in a function call during evaluation of an expression in magic
21099 * braces should not cause the function not to be defined. */
21100 saved_did_emsg = did_emsg;
21101 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102
21103 /*
21104 * ":function func" with only function name: list function.
21105 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021106 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 {
21108 if (!ends_excmd(*skipwhite(p)))
21109 {
21110 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021111 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 }
21113 eap->nextcmd = check_nextcmd(p);
21114 if (eap->nextcmd != NULL)
21115 *p = NUL;
21116 if (!eap->skip && !got_int)
21117 {
21118 fp = find_func(name);
21119 if (fp != NULL)
21120 {
21121 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021122 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021124 if (FUNCLINE(fp, j) == NULL)
21125 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 msg_putchar('\n');
21127 msg_outnum((long)(j + 1));
21128 if (j < 9)
21129 msg_putchar(' ');
21130 if (j < 99)
21131 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021132 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 out_flush(); /* show a line at a time */
21134 ui_breakcheck();
21135 }
21136 if (!got_int)
21137 {
21138 msg_putchar('\n');
21139 msg_puts((char_u *)" endfunction");
21140 }
21141 }
21142 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021143 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021145 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 }
21147
21148 /*
21149 * ":function name(arg1, arg2)" Define function.
21150 */
21151 p = skipwhite(p);
21152 if (*p != '(')
21153 {
21154 if (!eap->skip)
21155 {
21156 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 }
21159 /* attempt to continue by skipping some text */
21160 if (vim_strchr(p, '(') != NULL)
21161 p = vim_strchr(p, '(');
21162 }
21163 p = skipwhite(p + 1);
21164
21165 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21166 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21167
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021168 if (!eap->skip)
21169 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021170 /* Check the name of the function. Unless it's a dictionary function
21171 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021172 if (name != NULL)
21173 arg = name;
21174 else
21175 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021176 if (arg != NULL && (fudi.fd_di == NULL
21177 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021178 {
21179 if (*arg == K_SPECIAL)
21180 j = 3;
21181 else
21182 j = 0;
21183 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21184 : eval_isnamec(arg[j])))
21185 ++j;
21186 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021187 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021188 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021189 /* Disallow using the g: dict. */
21190 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21191 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021192 }
21193
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 /*
21195 * Isolate the arguments: "arg1, arg2, ...)"
21196 */
21197 while (*p != ')')
21198 {
21199 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21200 {
21201 varargs = TRUE;
21202 p += 3;
21203 mustend = TRUE;
21204 }
21205 else
21206 {
21207 arg = p;
21208 while (ASCII_ISALNUM(*p) || *p == '_')
21209 ++p;
21210 if (arg == p || isdigit(*arg)
21211 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21212 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21213 {
21214 if (!eap->skip)
21215 EMSG2(_("E125: Illegal argument: %s"), arg);
21216 break;
21217 }
21218 if (ga_grow(&newargs, 1) == FAIL)
21219 goto erret;
21220 c = *p;
21221 *p = NUL;
21222 arg = vim_strsave(arg);
21223 if (arg == NULL)
21224 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021225
21226 /* Check for duplicate argument name. */
21227 for (i = 0; i < newargs.ga_len; ++i)
21228 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21229 {
21230 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21231 goto erret;
21232 }
21233
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21235 *p = c;
21236 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 if (*p == ',')
21238 ++p;
21239 else
21240 mustend = TRUE;
21241 }
21242 p = skipwhite(p);
21243 if (mustend && *p != ')')
21244 {
21245 if (!eap->skip)
21246 EMSG2(_(e_invarg2), eap->arg);
21247 break;
21248 }
21249 }
21250 ++p; /* skip the ')' */
21251
Bram Moolenaare9a41262005-01-15 22:18:47 +000021252 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 for (;;)
21254 {
21255 p = skipwhite(p);
21256 if (STRNCMP(p, "range", 5) == 0)
21257 {
21258 flags |= FC_RANGE;
21259 p += 5;
21260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021261 else if (STRNCMP(p, "dict", 4) == 0)
21262 {
21263 flags |= FC_DICT;
21264 p += 4;
21265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 else if (STRNCMP(p, "abort", 5) == 0)
21267 {
21268 flags |= FC_ABORT;
21269 p += 5;
21270 }
21271 else
21272 break;
21273 }
21274
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021275 /* When there is a line break use what follows for the function body.
21276 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21277 if (*p == '\n')
21278 line_arg = p + 1;
21279 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 EMSG(_(e_trailing));
21281
21282 /*
21283 * Read the body of the function, until ":endfunction" is found.
21284 */
21285 if (KeyTyped)
21286 {
21287 /* Check if the function already exists, don't let the user type the
21288 * whole function before telling him it doesn't work! For a script we
21289 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021290 if (!eap->skip && !eap->forceit)
21291 {
21292 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21293 EMSG(_(e_funcdict));
21294 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021295 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021298 if (!eap->skip && did_emsg)
21299 goto erret;
21300
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 msg_putchar('\n'); /* don't overwrite the function name */
21302 cmdline_row = msg_row;
21303 }
21304
21305 indent = 2;
21306 nesting = 0;
21307 for (;;)
21308 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021309 if (KeyTyped)
21310 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021312 sourcing_lnum_off = sourcing_lnum;
21313
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021314 if (line_arg != NULL)
21315 {
21316 /* Use eap->arg, split up in parts by line breaks. */
21317 theline = line_arg;
21318 p = vim_strchr(theline, '\n');
21319 if (p == NULL)
21320 line_arg += STRLEN(line_arg);
21321 else
21322 {
21323 *p = NUL;
21324 line_arg = p + 1;
21325 }
21326 }
21327 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 theline = getcmdline(':', 0L, indent);
21329 else
21330 theline = eap->getline(':', eap->cookie, indent);
21331 if (KeyTyped)
21332 lines_left = Rows - 1;
21333 if (theline == NULL)
21334 {
21335 EMSG(_("E126: Missing :endfunction"));
21336 goto erret;
21337 }
21338
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021339 /* Detect line continuation: sourcing_lnum increased more than one. */
21340 if (sourcing_lnum > sourcing_lnum_off + 1)
21341 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21342 else
21343 sourcing_lnum_off = 0;
21344
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 if (skip_until != NULL)
21346 {
21347 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21348 * don't check for ":endfunc". */
21349 if (STRCMP(theline, skip_until) == 0)
21350 {
21351 vim_free(skip_until);
21352 skip_until = NULL;
21353 }
21354 }
21355 else
21356 {
21357 /* skip ':' and blanks*/
21358 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21359 ;
21360
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021361 /* Check for "endfunction". */
21362 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021364 if (line_arg == NULL)
21365 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 break;
21367 }
21368
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021369 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 * at "end". */
21371 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21372 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021373 else if (STRNCMP(p, "if", 2) == 0
21374 || STRNCMP(p, "wh", 2) == 0
21375 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 || STRNCMP(p, "try", 3) == 0)
21377 indent += 2;
21378
21379 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021380 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021382 if (*p == '!')
21383 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 p += eval_fname_script(p);
21385 if (ASCII_ISALPHA(*p))
21386 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021387 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 if (*skipwhite(p) == '(')
21389 {
21390 ++nesting;
21391 indent += 2;
21392 }
21393 }
21394 }
21395
21396 /* Check for ":append" or ":insert". */
21397 p = skip_range(p, NULL);
21398 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21399 || (p[0] == 'i'
21400 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21401 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21402 skip_until = vim_strsave((char_u *)".");
21403
21404 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21405 arg = skipwhite(skiptowhite(p));
21406 if (arg[0] == '<' && arg[1] =='<'
21407 && ((p[0] == 'p' && p[1] == 'y'
21408 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21409 || (p[0] == 'p' && p[1] == 'e'
21410 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21411 || (p[0] == 't' && p[1] == 'c'
21412 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021413 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21414 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21416 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021417 || (p[0] == 'm' && p[1] == 'z'
21418 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 ))
21420 {
21421 /* ":python <<" continues until a dot, like ":append" */
21422 p = skipwhite(arg + 2);
21423 if (*p == NUL)
21424 skip_until = vim_strsave((char_u *)".");
21425 else
21426 skip_until = vim_strsave(p);
21427 }
21428 }
21429
21430 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021431 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021432 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021433 if (line_arg == NULL)
21434 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021436 }
21437
21438 /* Copy the line to newly allocated memory. get_one_sourceline()
21439 * allocates 250 bytes per line, this saves 80% on average. The cost
21440 * is an extra alloc/free. */
21441 p = vim_strsave(theline);
21442 if (p != NULL)
21443 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021444 if (line_arg == NULL)
21445 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021446 theline = p;
21447 }
21448
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021449 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21450
21451 /* Add NULL lines for continuation lines, so that the line count is
21452 * equal to the index in the growarray. */
21453 while (sourcing_lnum_off-- > 0)
21454 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021455
21456 /* Check for end of eap->arg. */
21457 if (line_arg != NULL && *line_arg == NUL)
21458 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 }
21460
21461 /* Don't define the function when skipping commands or when an error was
21462 * detected. */
21463 if (eap->skip || did_emsg)
21464 goto erret;
21465
21466 /*
21467 * If there are no errors, add the function
21468 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021469 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021470 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021471 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021473 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021474 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021475 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021476 goto erret;
21477 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021479 fp = find_func(name);
21480 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021482 if (!eap->forceit)
21483 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021484 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021485 goto erret;
21486 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021487 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021488 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021489 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021490 name);
21491 goto erret;
21492 }
21493 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021494 ga_clear_strings(&(fp->uf_args));
21495 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021496 vim_free(name);
21497 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 }
21500 else
21501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 char numbuf[20];
21503
21504 fp = NULL;
21505 if (fudi.fd_newkey == NULL && !eap->forceit)
21506 {
21507 EMSG(_(e_funcdict));
21508 goto erret;
21509 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021510 if (fudi.fd_di == NULL)
21511 {
21512 /* Can't add a function to a locked dictionary */
21513 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21514 goto erret;
21515 }
21516 /* Can't change an existing function if it is locked */
21517 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21518 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021519
21520 /* Give the function a sequential number. Can only be used with a
21521 * Funcref! */
21522 vim_free(name);
21523 sprintf(numbuf, "%d", ++func_nr);
21524 name = vim_strsave((char_u *)numbuf);
21525 if (name == NULL)
21526 goto erret;
21527 }
21528
21529 if (fp == NULL)
21530 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021531 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021532 {
21533 int slen, plen;
21534 char_u *scriptname;
21535
21536 /* Check that the autoload name matches the script name. */
21537 j = FAIL;
21538 if (sourcing_name != NULL)
21539 {
21540 scriptname = autoload_name(name);
21541 if (scriptname != NULL)
21542 {
21543 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021544 plen = (int)STRLEN(p);
21545 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021546 if (slen > plen && fnamecmp(p,
21547 sourcing_name + slen - plen) == 0)
21548 j = OK;
21549 vim_free(scriptname);
21550 }
21551 }
21552 if (j == FAIL)
21553 {
21554 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21555 goto erret;
21556 }
21557 }
21558
21559 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 if (fp == NULL)
21561 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021562
21563 if (fudi.fd_dict != NULL)
21564 {
21565 if (fudi.fd_di == NULL)
21566 {
21567 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021568 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021569 if (fudi.fd_di == NULL)
21570 {
21571 vim_free(fp);
21572 goto erret;
21573 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021574 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21575 {
21576 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021577 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021578 goto erret;
21579 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580 }
21581 else
21582 /* overwrite existing dict entry */
21583 clear_tv(&fudi.fd_di->di_tv);
21584 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021585 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021586 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021587 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021588
21589 /* behave like "dict" was used */
21590 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591 }
21592
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021594 STRCPY(fp->uf_name, name);
21595 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021597 fp->uf_args = newargs;
21598 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021599#ifdef FEAT_PROFILE
21600 fp->uf_tml_count = NULL;
21601 fp->uf_tml_total = NULL;
21602 fp->uf_tml_self = NULL;
21603 fp->uf_profiling = FALSE;
21604 if (prof_def_func())
21605 func_do_profile(fp);
21606#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 fp->uf_varargs = varargs;
21608 fp->uf_flags = flags;
21609 fp->uf_calls = 0;
21610 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612
21613erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 ga_clear_strings(&newargs);
21615 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616ret_free:
21617 vim_free(skip_until);
21618 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621}
21622
21623/*
21624 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021625 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021627 * flags:
21628 * TFN_INT: internal function name OK
21629 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 * Advances "pp" to just after the function name (if no error).
21631 */
21632 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 char_u **pp;
21635 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021636 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021637 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021639 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 char_u *start;
21641 char_u *end;
21642 int lead;
21643 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021646
21647 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021650
21651 /* Check for hard coded <SNR>: already translated function ID (from a user
21652 * command). */
21653 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21654 && (*pp)[2] == (int)KE_SNR)
21655 {
21656 *pp += 3;
21657 len = get_id_len(pp) + 3;
21658 return vim_strnsave(start, len);
21659 }
21660
21661 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21662 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021664 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021666
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021667 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21668 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021669 if (end == start)
21670 {
21671 if (!skip)
21672 EMSG(_("E129: Function name required"));
21673 goto theend;
21674 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021675 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021676 {
21677 /*
21678 * Report an invalid expression in braces, unless the expression
21679 * evaluation has been cancelled due to an aborting error, an
21680 * interrupt, or an exception.
21681 */
21682 if (!aborting())
21683 {
21684 if (end != NULL)
21685 EMSG2(_(e_invarg2), start);
21686 }
21687 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021688 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021689 goto theend;
21690 }
21691
21692 if (lv.ll_tv != NULL)
21693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 if (fdp != NULL)
21695 {
21696 fdp->fd_dict = lv.ll_dict;
21697 fdp->fd_newkey = lv.ll_newkey;
21698 lv.ll_newkey = NULL;
21699 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021701 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21702 {
21703 name = vim_strsave(lv.ll_tv->vval.v_string);
21704 *pp = end;
21705 }
21706 else
21707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021708 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21709 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021710 EMSG(_(e_funcref));
21711 else
21712 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021713 name = NULL;
21714 }
21715 goto theend;
21716 }
21717
21718 if (lv.ll_name == NULL)
21719 {
21720 /* Error found, but continue after the function name. */
21721 *pp = end;
21722 goto theend;
21723 }
21724
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021725 /* Check if the name is a Funcref. If so, use the value. */
21726 if (lv.ll_exp_name != NULL)
21727 {
21728 len = (int)STRLEN(lv.ll_exp_name);
21729 name = deref_func_name(lv.ll_exp_name, &len);
21730 if (name == lv.ll_exp_name)
21731 name = NULL;
21732 }
21733 else
21734 {
21735 len = (int)(end - *pp);
21736 name = deref_func_name(*pp, &len);
21737 if (name == *pp)
21738 name = NULL;
21739 }
21740 if (name != NULL)
21741 {
21742 name = vim_strsave(name);
21743 *pp = end;
21744 goto theend;
21745 }
21746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021747 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021748 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021749 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021750 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21751 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21752 {
21753 /* When there was "s:" already or the name expanded to get a
21754 * leading "s:" then remove it. */
21755 lv.ll_name += 2;
21756 len -= 2;
21757 lead = 2;
21758 }
21759 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021760 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021761 {
21762 if (lead == 2) /* skip over "s:" */
21763 lv.ll_name += 2;
21764 len = (int)(end - lv.ll_name);
21765 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021766
21767 /*
21768 * Copy the function name to allocated memory.
21769 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21770 * Accept <SNR>123_name() outside a script.
21771 */
21772 if (skip)
21773 lead = 0; /* do nothing */
21774 else if (lead > 0)
21775 {
21776 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021777 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21778 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021779 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021780 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021781 if (current_SID <= 0)
21782 {
21783 EMSG(_(e_usingsid));
21784 goto theend;
21785 }
21786 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21787 lead += (int)STRLEN(sid_buf);
21788 }
21789 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021790 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021791 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021792 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021793 goto theend;
21794 }
21795 name = alloc((unsigned)(len + lead + 1));
21796 if (name != NULL)
21797 {
21798 if (lead > 0)
21799 {
21800 name[0] = K_SPECIAL;
21801 name[1] = KS_EXTRA;
21802 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021803 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021804 STRCPY(name + 3, sid_buf);
21805 }
21806 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21807 name[len + lead] = NUL;
21808 }
21809 *pp = end;
21810
21811theend:
21812 clear_lval(&lv);
21813 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814}
21815
21816/*
21817 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21818 * Return 2 if "p" starts with "s:".
21819 * Return 0 otherwise.
21820 */
21821 static int
21822eval_fname_script(p)
21823 char_u *p;
21824{
21825 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21826 || STRNICMP(p + 1, "SNR>", 4) == 0))
21827 return 5;
21828 if (p[0] == 's' && p[1] == ':')
21829 return 2;
21830 return 0;
21831}
21832
21833/*
21834 * Return TRUE if "p" starts with "<SID>" or "s:".
21835 * Only works if eval_fname_script() returned non-zero for "p"!
21836 */
21837 static int
21838eval_fname_sid(p)
21839 char_u *p;
21840{
21841 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21842}
21843
21844/*
21845 * List the head of the function: "name(arg1, arg2)".
21846 */
21847 static void
21848list_func_head(fp, indent)
21849 ufunc_T *fp;
21850 int indent;
21851{
21852 int j;
21853
21854 msg_start();
21855 if (indent)
21856 MSG_PUTS(" ");
21857 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021858 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 {
21860 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021861 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 }
21863 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021864 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021866 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 {
21868 if (j)
21869 MSG_PUTS(", ");
21870 msg_puts(FUNCARG(fp, j));
21871 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021872 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 {
21874 if (j)
21875 MSG_PUTS(", ");
21876 MSG_PUTS("...");
21877 }
21878 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021879 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021880 if (p_verbose > 0)
21881 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882}
21883
21884/*
21885 * Find a function by name, return pointer to it in ufuncs.
21886 * Return NULL for unknown function.
21887 */
21888 static ufunc_T *
21889find_func(name)
21890 char_u *name;
21891{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021892 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021894 hi = hash_find(&func_hashtab, name);
21895 if (!HASHITEM_EMPTY(hi))
21896 return HI2UF(hi);
21897 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898}
21899
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021900#if defined(EXITFREE) || defined(PROTO)
21901 void
21902free_all_functions()
21903{
21904 hashitem_T *hi;
21905
21906 /* Need to start all over every time, because func_free() may change the
21907 * hash table. */
21908 while (func_hashtab.ht_used > 0)
21909 for (hi = func_hashtab.ht_array; ; ++hi)
21910 if (!HASHITEM_EMPTY(hi))
21911 {
21912 func_free(HI2UF(hi));
21913 break;
21914 }
21915}
21916#endif
21917
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918/*
21919 * Return TRUE if a function "name" exists.
21920 */
21921 static int
21922function_exists(name)
21923 char_u *name;
21924{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021925 char_u *nm = name;
21926 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021927 int n = FALSE;
21928
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021929 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021930 nm = skipwhite(nm);
21931
21932 /* Only accept "funcname", "funcname ", "funcname (..." and
21933 * "funcname(...", not "funcname!...". */
21934 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021935 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021936 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021937 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021938 else
21939 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021940 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021941 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021942 return n;
21943}
21944
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021945/*
21946 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021947 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021948 */
21949 static int
21950builtin_function(name)
21951 char_u *name;
21952{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021953 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21954 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021955}
21956
Bram Moolenaar05159a02005-02-26 23:04:13 +000021957#if defined(FEAT_PROFILE) || defined(PROTO)
21958/*
21959 * Start profiling function "fp".
21960 */
21961 static void
21962func_do_profile(fp)
21963 ufunc_T *fp;
21964{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021965 int len = fp->uf_lines.ga_len;
21966
21967 if (len == 0)
21968 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021969 fp->uf_tm_count = 0;
21970 profile_zero(&fp->uf_tm_self);
21971 profile_zero(&fp->uf_tm_total);
21972 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021973 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021974 if (fp->uf_tml_total == NULL)
21975 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021976 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021977 if (fp->uf_tml_self == NULL)
21978 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021979 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021980 fp->uf_tml_idx = -1;
21981 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21982 || fp->uf_tml_self == NULL)
21983 return; /* out of memory */
21984
21985 fp->uf_profiling = TRUE;
21986}
21987
21988/*
21989 * Dump the profiling results for all functions in file "fd".
21990 */
21991 void
21992func_dump_profile(fd)
21993 FILE *fd;
21994{
21995 hashitem_T *hi;
21996 int todo;
21997 ufunc_T *fp;
21998 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021999 ufunc_T **sorttab;
22000 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022001
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022002 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022003 if (todo == 0)
22004 return; /* nothing to dump */
22005
Bram Moolenaar73830342005-02-28 22:48:19 +000022006 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22007
Bram Moolenaar05159a02005-02-26 23:04:13 +000022008 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22009 {
22010 if (!HASHITEM_EMPTY(hi))
22011 {
22012 --todo;
22013 fp = HI2UF(hi);
22014 if (fp->uf_profiling)
22015 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022016 if (sorttab != NULL)
22017 sorttab[st_len++] = fp;
22018
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 if (fp->uf_name[0] == K_SPECIAL)
22020 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22021 else
22022 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22023 if (fp->uf_tm_count == 1)
22024 fprintf(fd, "Called 1 time\n");
22025 else
22026 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22027 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22028 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22029 fprintf(fd, "\n");
22030 fprintf(fd, "count total (s) self (s)\n");
22031
22032 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22033 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022034 if (FUNCLINE(fp, i) == NULL)
22035 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022036 prof_func_line(fd, fp->uf_tml_count[i],
22037 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022038 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22039 }
22040 fprintf(fd, "\n");
22041 }
22042 }
22043 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022044
22045 if (sorttab != NULL && st_len > 0)
22046 {
22047 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22048 prof_total_cmp);
22049 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22050 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22051 prof_self_cmp);
22052 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22053 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022054
22055 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022056}
Bram Moolenaar73830342005-02-28 22:48:19 +000022057
22058 static void
22059prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22060 FILE *fd;
22061 ufunc_T **sorttab;
22062 int st_len;
22063 char *title;
22064 int prefer_self; /* when equal print only self time */
22065{
22066 int i;
22067 ufunc_T *fp;
22068
22069 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22070 fprintf(fd, "count total (s) self (s) function\n");
22071 for (i = 0; i < 20 && i < st_len; ++i)
22072 {
22073 fp = sorttab[i];
22074 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22075 prefer_self);
22076 if (fp->uf_name[0] == K_SPECIAL)
22077 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22078 else
22079 fprintf(fd, " %s()\n", fp->uf_name);
22080 }
22081 fprintf(fd, "\n");
22082}
22083
22084/*
22085 * Print the count and times for one function or function line.
22086 */
22087 static void
22088prof_func_line(fd, count, total, self, prefer_self)
22089 FILE *fd;
22090 int count;
22091 proftime_T *total;
22092 proftime_T *self;
22093 int prefer_self; /* when equal print only self time */
22094{
22095 if (count > 0)
22096 {
22097 fprintf(fd, "%5d ", count);
22098 if (prefer_self && profile_equal(total, self))
22099 fprintf(fd, " ");
22100 else
22101 fprintf(fd, "%s ", profile_msg(total));
22102 if (!prefer_self && profile_equal(total, self))
22103 fprintf(fd, " ");
22104 else
22105 fprintf(fd, "%s ", profile_msg(self));
22106 }
22107 else
22108 fprintf(fd, " ");
22109}
22110
22111/*
22112 * Compare function for total time sorting.
22113 */
22114 static int
22115#ifdef __BORLANDC__
22116_RTLENTRYF
22117#endif
22118prof_total_cmp(s1, s2)
22119 const void *s1;
22120 const void *s2;
22121{
22122 ufunc_T *p1, *p2;
22123
22124 p1 = *(ufunc_T **)s1;
22125 p2 = *(ufunc_T **)s2;
22126 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22127}
22128
22129/*
22130 * Compare function for self time sorting.
22131 */
22132 static int
22133#ifdef __BORLANDC__
22134_RTLENTRYF
22135#endif
22136prof_self_cmp(s1, s2)
22137 const void *s1;
22138 const void *s2;
22139{
22140 ufunc_T *p1, *p2;
22141
22142 p1 = *(ufunc_T **)s1;
22143 p2 = *(ufunc_T **)s2;
22144 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22145}
22146
Bram Moolenaar05159a02005-02-26 23:04:13 +000022147#endif
22148
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022149/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022150 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022151 * Return TRUE if a package was loaded.
22152 */
22153 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022154script_autoload(name, reload)
22155 char_u *name;
22156 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022157{
22158 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022159 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022160 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022161 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022162
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022163 /* Return quickly when autoload disabled. */
22164 if (no_autoload)
22165 return FALSE;
22166
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022167 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022168 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022169 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022170 return FALSE;
22171
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022172 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022173
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022174 /* Find the name in the list of previously loaded package names. Skip
22175 * "autoload/", it's always the same. */
22176 for (i = 0; i < ga_loaded.ga_len; ++i)
22177 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22178 break;
22179 if (!reload && i < ga_loaded.ga_len)
22180 ret = FALSE; /* was loaded already */
22181 else
22182 {
22183 /* Remember the name if it wasn't loaded already. */
22184 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22185 {
22186 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22187 tofree = NULL;
22188 }
22189
22190 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022191 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022192 ret = TRUE;
22193 }
22194
22195 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022196 return ret;
22197}
22198
22199/*
22200 * Return the autoload script name for a function or variable name.
22201 * Returns NULL when out of memory.
22202 */
22203 static char_u *
22204autoload_name(name)
22205 char_u *name;
22206{
22207 char_u *p;
22208 char_u *scriptname;
22209
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022210 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022211 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22212 if (scriptname == NULL)
22213 return FALSE;
22214 STRCPY(scriptname, "autoload/");
22215 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022216 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022217 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022218 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022219 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022220 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022221}
22222
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22224
22225/*
22226 * Function given to ExpandGeneric() to obtain the list of user defined
22227 * function names.
22228 */
22229 char_u *
22230get_user_func_name(xp, idx)
22231 expand_T *xp;
22232 int idx;
22233{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022234 static long_u done;
22235 static hashitem_T *hi;
22236 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237
22238 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022240 done = 0;
22241 hi = func_hashtab.ht_array;
22242 }
22243 if (done < func_hashtab.ht_used)
22244 {
22245 if (done++ > 0)
22246 ++hi;
22247 while (HASHITEM_EMPTY(hi))
22248 ++hi;
22249 fp = HI2UF(hi);
22250
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022251 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022252 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022253
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022254 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22255 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256
22257 cat_func_name(IObuff, fp);
22258 if (xp->xp_context != EXPAND_USER_FUNC)
22259 {
22260 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022261 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 STRCAT(IObuff, ")");
22263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 return IObuff;
22265 }
22266 return NULL;
22267}
22268
22269#endif /* FEAT_CMDL_COMPL */
22270
22271/*
22272 * Copy the function name of "fp" to buffer "buf".
22273 * "buf" must be able to hold the function name plus three bytes.
22274 * Takes care of script-local function names.
22275 */
22276 static void
22277cat_func_name(buf, fp)
22278 char_u *buf;
22279 ufunc_T *fp;
22280{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022281 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 {
22283 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022284 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 }
22286 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022287 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288}
22289
22290/*
22291 * ":delfunction {name}"
22292 */
22293 void
22294ex_delfunction(eap)
22295 exarg_T *eap;
22296{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022297 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 char_u *p;
22299 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022300 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301
22302 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022303 name = trans_function_name(&p, eap->skip, 0, &fudi);
22304 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 {
22307 if (fudi.fd_dict != NULL && !eap->skip)
22308 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311 if (!ends_excmd(*skipwhite(p)))
22312 {
22313 vim_free(name);
22314 EMSG(_(e_trailing));
22315 return;
22316 }
22317 eap->nextcmd = check_nextcmd(p);
22318 if (eap->nextcmd != NULL)
22319 *p = NUL;
22320
22321 if (!eap->skip)
22322 fp = find_func(name);
22323 vim_free(name);
22324
22325 if (!eap->skip)
22326 {
22327 if (fp == NULL)
22328 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022329 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 return;
22331 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022332 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 {
22334 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22335 return;
22336 }
22337
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022338 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 /* Delete the dict item that refers to the function, it will
22341 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022342 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022344 else
22345 func_free(fp);
22346 }
22347}
22348
22349/*
22350 * Free a function and remove it from the list of functions.
22351 */
22352 static void
22353func_free(fp)
22354 ufunc_T *fp;
22355{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022356 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357
22358 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022359 ga_clear_strings(&(fp->uf_args));
22360 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022361#ifdef FEAT_PROFILE
22362 vim_free(fp->uf_tml_count);
22363 vim_free(fp->uf_tml_total);
22364 vim_free(fp->uf_tml_self);
22365#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022366
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022367 /* remove the function from the function hashtable */
22368 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22369 if (HASHITEM_EMPTY(hi))
22370 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022371 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022372 hash_remove(&func_hashtab, hi);
22373
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022374 vim_free(fp);
22375}
22376
22377/*
22378 * Unreference a Function: decrement the reference count and free it when it
22379 * becomes zero. Only for numbered functions.
22380 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022381 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022382func_unref(name)
22383 char_u *name;
22384{
22385 ufunc_T *fp;
22386
22387 if (name != NULL && isdigit(*name))
22388 {
22389 fp = find_func(name);
22390 if (fp == NULL)
22391 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022392 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022393 {
22394 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022395 * when "uf_calls" becomes zero. */
22396 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022397 func_free(fp);
22398 }
22399 }
22400}
22401
22402/*
22403 * Count a reference to a Function.
22404 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022405 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022406func_ref(name)
22407 char_u *name;
22408{
22409 ufunc_T *fp;
22410
22411 if (name != NULL && isdigit(*name))
22412 {
22413 fp = find_func(name);
22414 if (fp == NULL)
22415 EMSG2(_(e_intern2), "func_ref()");
22416 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022417 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 }
22419}
22420
22421/*
22422 * Call a user function.
22423 */
22424 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022425call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 ufunc_T *fp; /* pointer to function */
22427 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022428 typval_T *argvars; /* arguments */
22429 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 linenr_T firstline; /* first line of range */
22431 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433{
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 char_u *save_sourcing_name;
22435 linenr_T save_sourcing_lnum;
22436 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022437 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 int save_did_emsg;
22439 static int depth = 0;
22440 dictitem_T *v;
22441 int fixvar_idx = 0; /* index in fixvar[] */
22442 int i;
22443 int ai;
22444 char_u numbuf[NUMBUFLEN];
22445 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022446#ifdef FEAT_PROFILE
22447 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022448 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450
22451 /* If depth of calling is getting too high, don't execute the function */
22452 if (depth >= p_mfd)
22453 {
22454 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022455 rettv->v_type = VAR_NUMBER;
22456 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 return;
22458 }
22459 ++depth;
22460
22461 line_breakcheck(); /* check for CTRL-C hit */
22462
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022463 fc = (funccall_T *)alloc(sizeof(funccall_T));
22464 fc->caller = current_funccal;
22465 current_funccal = fc;
22466 fc->func = fp;
22467 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022468 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022469 fc->linenr = 0;
22470 fc->returned = FALSE;
22471 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022473 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22474 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022477 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022478 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22479 * each argument variable and saves a lot of time.
22480 */
22481 /*
22482 * Init l: variables.
22483 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022484 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022485 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022486 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022487 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22488 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022489 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022490 name = v->di_key;
22491 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022493 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022495 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 v->di_tv.vval.v_dict = selfdict;
22497 ++selfdict->dv_refcount;
22498 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022499
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 /*
22501 * Init a: variables.
22502 * Set a:0 to "argcount".
22503 * Set a:000 to a list with room for the "..." arguments.
22504 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022505 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022506 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022507 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022508 /* Use "name" to avoid a warning from some compiler that checks the
22509 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022511 name = v->di_key;
22512 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022513 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022514 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022516 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022517 v->di_tv.vval.v_list = &fc->l_varlist;
22518 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22519 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22520 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022521
22522 /*
22523 * Set a:firstline to "firstline" and a:lastline to "lastline".
22524 * Set a:name to named arguments.
22525 * Set a:N to the "..." arguments.
22526 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022527 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022528 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022529 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022530 (varnumber_T)lastline);
22531 for (i = 0; i < argcount; ++i)
22532 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022533 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022534 if (ai < 0)
22535 /* named argument a:name */
22536 name = FUNCARG(fp, i);
22537 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022538 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 /* "..." argument a:1, a:2, etc. */
22540 sprintf((char *)numbuf, "%d", ai + 1);
22541 name = numbuf;
22542 }
22543 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22544 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022545 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22547 }
22548 else
22549 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022550 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22551 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 if (v == NULL)
22553 break;
22554 v->di_flags = DI_FLAGS_RO;
22555 }
22556 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022557 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022558
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022559 /* Note: the values are copied directly to avoid alloc/free.
22560 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022561 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022562 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022563
22564 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22565 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022566 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22567 fc->l_listitems[ai].li_tv = argvars[i];
22568 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022569 }
22570 }
22571
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 /* Don't redraw while executing the function. */
22573 ++RedrawingDisabled;
22574 save_sourcing_name = sourcing_name;
22575 save_sourcing_lnum = sourcing_lnum;
22576 sourcing_lnum = 1;
22577 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 if (sourcing_name != NULL)
22580 {
22581 if (save_sourcing_name != NULL
22582 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22583 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22584 else
22585 STRCPY(sourcing_name, "function ");
22586 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22587
22588 if (p_verbose >= 12)
22589 {
22590 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022591 verbose_enter_scroll();
22592
Bram Moolenaar555b2802005-05-19 21:08:39 +000022593 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 if (p_verbose >= 14)
22595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022597 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022598 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022599 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600
22601 msg_puts((char_u *)"(");
22602 for (i = 0; i < argcount; ++i)
22603 {
22604 if (i > 0)
22605 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022606 if (argvars[i].v_type == VAR_NUMBER)
22607 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 else
22609 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022610 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22611 if (s != NULL)
22612 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022613 if (vim_strsize(s) > MSG_BUF_CLEN)
22614 {
22615 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22616 s = buf;
22617 }
22618 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022619 vim_free(tofree);
22620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 }
22622 }
22623 msg_puts((char_u *)")");
22624 }
22625 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022626
22627 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 --no_wait_return;
22629 }
22630 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022631#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022632 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022633 {
22634 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22635 func_do_profile(fp);
22636 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022637 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022638 {
22639 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022640 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022641 profile_zero(&fp->uf_tm_children);
22642 }
22643 script_prof_save(&wait_start);
22644 }
22645#endif
22646
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022648 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 save_did_emsg = did_emsg;
22650 did_emsg = FALSE;
22651
22652 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022653 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22655
22656 --RedrawingDisabled;
22657
22658 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022659 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022661 clear_tv(rettv);
22662 rettv->v_type = VAR_NUMBER;
22663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 }
22665
Bram Moolenaar05159a02005-02-26 23:04:13 +000022666#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022667 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022668 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022669 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022670 profile_end(&call_start);
22671 profile_sub_wait(&wait_start, &call_start);
22672 profile_add(&fp->uf_tm_total, &call_start);
22673 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022674 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022675 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022676 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22677 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022678 }
22679 }
22680#endif
22681
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 /* when being verbose, mention the return value */
22683 if (p_verbose >= 12)
22684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022686 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022689 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022690 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022691 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022692 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022695 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022696 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022697 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022698 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022699
Bram Moolenaar555b2802005-05-19 21:08:39 +000022700 /* The value may be very long. Skip the middle part, so that we
22701 * have some idea how it starts and ends. smsg() would always
22702 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022703 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022704 if (s != NULL)
22705 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022706 if (vim_strsize(s) > MSG_BUF_CLEN)
22707 {
22708 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22709 s = buf;
22710 }
22711 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022712 vim_free(tofree);
22713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 }
22715 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022716
22717 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 --no_wait_return;
22719 }
22720
22721 vim_free(sourcing_name);
22722 sourcing_name = save_sourcing_name;
22723 sourcing_lnum = save_sourcing_lnum;
22724 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022725#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022726 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022727 script_prof_restore(&wait_start);
22728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729
22730 if (p_verbose >= 12 && sourcing_name != NULL)
22731 {
22732 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022733 verbose_enter_scroll();
22734
Bram Moolenaar555b2802005-05-19 21:08:39 +000022735 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022737
22738 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 --no_wait_return;
22740 }
22741
22742 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022743 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022745
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022746 /* If the a:000 list and the l: and a: dicts are not referenced we can
22747 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022748 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22749 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22750 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22751 {
22752 free_funccal(fc, FALSE);
22753 }
22754 else
22755 {
22756 hashitem_T *hi;
22757 listitem_T *li;
22758 int todo;
22759
22760 /* "fc" is still in use. This can happen when returning "a:000" or
22761 * assigning "l:" to a global variable.
22762 * Link "fc" in the list for garbage collection later. */
22763 fc->caller = previous_funccal;
22764 previous_funccal = fc;
22765
22766 /* Make a copy of the a: variables, since we didn't do that above. */
22767 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22768 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22769 {
22770 if (!HASHITEM_EMPTY(hi))
22771 {
22772 --todo;
22773 v = HI2DI(hi);
22774 copy_tv(&v->di_tv, &v->di_tv);
22775 }
22776 }
22777
22778 /* Make a copy of the a:000 items, since we didn't do that above. */
22779 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22780 copy_tv(&li->li_tv, &li->li_tv);
22781 }
22782}
22783
22784/*
22785 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022786 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022787 */
22788 static int
22789can_free_funccal(fc, copyID)
22790 funccall_T *fc;
22791 int copyID;
22792{
22793 return (fc->l_varlist.lv_copyID != copyID
22794 && fc->l_vars.dv_copyID != copyID
22795 && fc->l_avars.dv_copyID != copyID);
22796}
22797
22798/*
22799 * Free "fc" and what it contains.
22800 */
22801 static void
22802free_funccal(fc, free_val)
22803 funccall_T *fc;
22804 int free_val; /* a: vars were allocated */
22805{
22806 listitem_T *li;
22807
22808 /* The a: variables typevals may not have been allocated, only free the
22809 * allocated variables. */
22810 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22811
22812 /* free all l: variables */
22813 vars_clear(&fc->l_vars.dv_hashtab);
22814
22815 /* Free the a:000 variables if they were allocated. */
22816 if (free_val)
22817 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22818 clear_tv(&li->li_tv);
22819
22820 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821}
22822
22823/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 * Add a number variable "name" to dict "dp" with value "nr".
22825 */
22826 static void
22827add_nr_var(dp, v, name, nr)
22828 dict_T *dp;
22829 dictitem_T *v;
22830 char *name;
22831 varnumber_T nr;
22832{
22833 STRCPY(v->di_key, name);
22834 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22835 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22836 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022837 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022838 v->di_tv.vval.v_number = nr;
22839}
22840
22841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 * ":return [expr]"
22843 */
22844 void
22845ex_return(eap)
22846 exarg_T *eap;
22847{
22848 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 int returning = FALSE;
22851
22852 if (current_funccal == NULL)
22853 {
22854 EMSG(_("E133: :return not inside a function"));
22855 return;
22856 }
22857
22858 if (eap->skip)
22859 ++emsg_skip;
22860
22861 eap->nextcmd = NULL;
22862 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022863 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 {
22865 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022866 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022868 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 }
22870 /* It's safer to return also on error. */
22871 else if (!eap->skip)
22872 {
22873 /*
22874 * Return unless the expression evaluation has been cancelled due to an
22875 * aborting error, an interrupt, or an exception.
22876 */
22877 if (!aborting())
22878 returning = do_return(eap, FALSE, TRUE, NULL);
22879 }
22880
22881 /* When skipping or the return gets pending, advance to the next command
22882 * in this line (!returning). Otherwise, ignore the rest of the line.
22883 * Following lines will be ignored by get_func_line(). */
22884 if (returning)
22885 eap->nextcmd = NULL;
22886 else if (eap->nextcmd == NULL) /* no argument */
22887 eap->nextcmd = check_nextcmd(arg);
22888
22889 if (eap->skip)
22890 --emsg_skip;
22891}
22892
22893/*
22894 * Return from a function. Possibly makes the return pending. Also called
22895 * for a pending return at the ":endtry" or after returning from an extra
22896 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022897 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022898 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 * FALSE when the return gets pending.
22900 */
22901 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022902do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 exarg_T *eap;
22904 int reanimate;
22905 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022906 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907{
22908 int idx;
22909 struct condstack *cstack = eap->cstack;
22910
22911 if (reanimate)
22912 /* Undo the return. */
22913 current_funccal->returned = FALSE;
22914
22915 /*
22916 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22917 * not in its finally clause (which then is to be executed next) is found.
22918 * In this case, make the ":return" pending for execution at the ":endtry".
22919 * Otherwise, return normally.
22920 */
22921 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22922 if (idx >= 0)
22923 {
22924 cstack->cs_pending[idx] = CSTP_RETURN;
22925
22926 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022927 /* A pending return again gets pending. "rettv" points to an
22928 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022930 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 else
22932 {
22933 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022934 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022936 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022938 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 {
22940 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022941 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022942 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 else
22944 EMSG(_(e_outofmem));
22945 }
22946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022947 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948
22949 if (reanimate)
22950 {
22951 /* The pending return value could be overwritten by a ":return"
22952 * without argument in a finally clause; reset the default
22953 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022954 current_funccal->rettv->v_type = VAR_NUMBER;
22955 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 }
22957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022958 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 }
22960 else
22961 {
22962 current_funccal->returned = TRUE;
22963
22964 /* If the return is carried out now, store the return value. For
22965 * a return immediately after reanimation, the value is already
22966 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022967 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022969 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022972 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 }
22974 }
22975
22976 return idx < 0;
22977}
22978
22979/*
22980 * Free the variable with a pending return value.
22981 */
22982 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022983discard_pending_return(rettv)
22984 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985{
Bram Moolenaar33570922005-01-25 22:26:29 +000022986 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987}
22988
22989/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022990 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 * is an allocated string. Used by report_pending() for verbose messages.
22992 */
22993 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022994get_return_cmd(rettv)
22995 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022997 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022998 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022999 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023001 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023002 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023003 if (s == NULL)
23004 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023005
23006 STRCPY(IObuff, ":return ");
23007 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23008 if (STRLEN(s) + 8 >= IOSIZE)
23009 STRCPY(IObuff + IOSIZE - 4, "...");
23010 vim_free(tofree);
23011 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012}
23013
23014/*
23015 * Get next function line.
23016 * Called by do_cmdline() to get the next line.
23017 * Returns allocated string, or NULL for end of function.
23018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 char_u *
23020get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023021 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023023 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024{
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023026 ufunc_T *fp = fcp->func;
23027 char_u *retval;
23028 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029
23030 /* If breakpoints have been added/deleted need to check for it. */
23031 if (fcp->dbg_tick != debug_tick)
23032 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023033 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 sourcing_lnum);
23035 fcp->dbg_tick = debug_tick;
23036 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023037#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023038 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023039 func_line_end(cookie);
23040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041
Bram Moolenaar05159a02005-02-26 23:04:13 +000023042 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023043 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23044 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 retval = NULL;
23046 else
23047 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023048 /* Skip NULL lines (continuation lines). */
23049 while (fcp->linenr < gap->ga_len
23050 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23051 ++fcp->linenr;
23052 if (fcp->linenr >= gap->ga_len)
23053 retval = NULL;
23054 else
23055 {
23056 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23057 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023058#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023059 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023060 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023061#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 }
23064
23065 /* Did we encounter a breakpoint? */
23066 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23067 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023068 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023070 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 sourcing_lnum);
23072 fcp->dbg_tick = debug_tick;
23073 }
23074
23075 return retval;
23076}
23077
Bram Moolenaar05159a02005-02-26 23:04:13 +000023078#if defined(FEAT_PROFILE) || defined(PROTO)
23079/*
23080 * Called when starting to read a function line.
23081 * "sourcing_lnum" must be correct!
23082 * When skipping lines it may not actually be executed, but we won't find out
23083 * until later and we need to store the time now.
23084 */
23085 void
23086func_line_start(cookie)
23087 void *cookie;
23088{
23089 funccall_T *fcp = (funccall_T *)cookie;
23090 ufunc_T *fp = fcp->func;
23091
23092 if (fp->uf_profiling && sourcing_lnum >= 1
23093 && sourcing_lnum <= fp->uf_lines.ga_len)
23094 {
23095 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023096 /* Skip continuation lines. */
23097 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23098 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023099 fp->uf_tml_execed = FALSE;
23100 profile_start(&fp->uf_tml_start);
23101 profile_zero(&fp->uf_tml_children);
23102 profile_get_wait(&fp->uf_tml_wait);
23103 }
23104}
23105
23106/*
23107 * Called when actually executing a function line.
23108 */
23109 void
23110func_line_exec(cookie)
23111 void *cookie;
23112{
23113 funccall_T *fcp = (funccall_T *)cookie;
23114 ufunc_T *fp = fcp->func;
23115
23116 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23117 fp->uf_tml_execed = TRUE;
23118}
23119
23120/*
23121 * Called when done with a function line.
23122 */
23123 void
23124func_line_end(cookie)
23125 void *cookie;
23126{
23127 funccall_T *fcp = (funccall_T *)cookie;
23128 ufunc_T *fp = fcp->func;
23129
23130 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23131 {
23132 if (fp->uf_tml_execed)
23133 {
23134 ++fp->uf_tml_count[fp->uf_tml_idx];
23135 profile_end(&fp->uf_tml_start);
23136 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023137 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023138 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23139 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023140 }
23141 fp->uf_tml_idx = -1;
23142 }
23143}
23144#endif
23145
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146/*
23147 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023148 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 */
23150 int
23151func_has_ended(cookie)
23152 void *cookie;
23153{
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155
23156 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23157 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023158 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 || fcp->returned);
23160}
23161
23162/*
23163 * return TRUE if cookie indicates a function which "abort"s on errors.
23164 */
23165 int
23166func_has_abort(cookie)
23167 void *cookie;
23168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023169 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170}
23171
23172#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23173typedef enum
23174{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023175 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23176 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23177 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178} var_flavour_T;
23179
23180static var_flavour_T var_flavour __ARGS((char_u *varname));
23181
23182 static var_flavour_T
23183var_flavour(varname)
23184 char_u *varname;
23185{
23186 char_u *p = varname;
23187
23188 if (ASCII_ISUPPER(*p))
23189 {
23190 while (*(++p))
23191 if (ASCII_ISLOWER(*p))
23192 return VAR_FLAVOUR_SESSION;
23193 return VAR_FLAVOUR_VIMINFO;
23194 }
23195 else
23196 return VAR_FLAVOUR_DEFAULT;
23197}
23198#endif
23199
23200#if defined(FEAT_VIMINFO) || defined(PROTO)
23201/*
23202 * Restore global vars that start with a capital from the viminfo file
23203 */
23204 int
23205read_viminfo_varlist(virp, writing)
23206 vir_T *virp;
23207 int writing;
23208{
23209 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023210 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212
23213 if (!writing && (find_viminfo_parameter('!') != NULL))
23214 {
23215 tab = vim_strchr(virp->vir_line + 1, '\t');
23216 if (tab != NULL)
23217 {
23218 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023219 switch (*tab)
23220 {
23221 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023222#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023223 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023224#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023225 case 'D': type = VAR_DICT; break;
23226 case 'L': type = VAR_LIST; break;
23227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228
23229 tab = vim_strchr(tab, '\t');
23230 if (tab != NULL)
23231 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023232 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023233 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023234 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023236#ifdef FEAT_FLOAT
23237 else if (type == VAR_FLOAT)
23238 (void)string2float(tab + 1, &tv.vval.v_float);
23239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023241 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023242 if (type == VAR_DICT || type == VAR_LIST)
23243 {
23244 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23245
23246 if (etv == NULL)
23247 /* Failed to parse back the dict or list, use it as a
23248 * string. */
23249 tv.v_type = VAR_STRING;
23250 else
23251 {
23252 vim_free(tv.vval.v_string);
23253 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023254 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023255 }
23256 }
23257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023258 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023259
23260 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023261 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023262 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23263 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 }
23265 }
23266 }
23267
23268 return viminfo_readline(virp);
23269}
23270
23271/*
23272 * Write global vars that start with a capital to the viminfo file
23273 */
23274 void
23275write_viminfo_varlist(fp)
23276 FILE *fp;
23277{
Bram Moolenaar33570922005-01-25 22:26:29 +000023278 hashitem_T *hi;
23279 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023280 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023281 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023282 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023283 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023284 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285
23286 if (find_viminfo_parameter('!') == NULL)
23287 return;
23288
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023289 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023290
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023291 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023292 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023294 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023296 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 this_var = HI2DI(hi);
23298 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023300 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023301 {
23302 case VAR_STRING: s = "STR"; break;
23303 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023304#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023305 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023306#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023307 case VAR_DICT: s = "DIC"; break;
23308 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023309 default: continue;
23310 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023312 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023313 if (p != NULL)
23314 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023315 vim_free(tofree);
23316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 }
23318 }
23319}
23320#endif
23321
23322#if defined(FEAT_SESSION) || defined(PROTO)
23323 int
23324store_session_globals(fd)
23325 FILE *fd;
23326{
Bram Moolenaar33570922005-01-25 22:26:29 +000023327 hashitem_T *hi;
23328 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023329 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 char_u *p, *t;
23331
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023332 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023333 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023335 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023337 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023338 this_var = HI2DI(hi);
23339 if ((this_var->di_tv.v_type == VAR_NUMBER
23340 || this_var->di_tv.v_type == VAR_STRING)
23341 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023342 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023343 /* Escape special characters with a backslash. Turn a LF and
23344 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023345 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023346 (char_u *)"\\\"\n\r");
23347 if (p == NULL) /* out of memory */
23348 break;
23349 for (t = p; *t != NUL; ++t)
23350 if (*t == '\n')
23351 *t = 'n';
23352 else if (*t == '\r')
23353 *t = 'r';
23354 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 this_var->di_key,
23356 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23357 : ' ',
23358 p,
23359 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23360 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023361 || put_eol(fd) == FAIL)
23362 {
23363 vim_free(p);
23364 return FAIL;
23365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 vim_free(p);
23367 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023368#ifdef FEAT_FLOAT
23369 else if (this_var->di_tv.v_type == VAR_FLOAT
23370 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23371 {
23372 float_T f = this_var->di_tv.vval.v_float;
23373 int sign = ' ';
23374
23375 if (f < 0)
23376 {
23377 f = -f;
23378 sign = '-';
23379 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023380 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023381 this_var->di_key, sign, f) < 0)
23382 || put_eol(fd) == FAIL)
23383 return FAIL;
23384 }
23385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 }
23387 }
23388 return OK;
23389}
23390#endif
23391
Bram Moolenaar661b1822005-07-28 22:36:45 +000023392/*
23393 * Display script name where an item was last set.
23394 * Should only be invoked when 'verbose' is non-zero.
23395 */
23396 void
23397last_set_msg(scriptID)
23398 scid_T scriptID;
23399{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023400 char_u *p;
23401
Bram Moolenaar661b1822005-07-28 22:36:45 +000023402 if (scriptID != 0)
23403 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023404 p = home_replace_save(NULL, get_scriptname(scriptID));
23405 if (p != NULL)
23406 {
23407 verbose_enter();
23408 MSG_PUTS(_("\n\tLast set from "));
23409 MSG_PUTS(p);
23410 vim_free(p);
23411 verbose_leave();
23412 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023413 }
23414}
23415
Bram Moolenaard812df62008-11-09 12:46:09 +000023416/*
23417 * List v:oldfiles in a nice way.
23418 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023419 void
23420ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023421 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023422{
23423 list_T *l = vimvars[VV_OLDFILES].vv_list;
23424 listitem_T *li;
23425 int nr = 0;
23426
23427 if (l == NULL)
23428 msg((char_u *)_("No old files"));
23429 else
23430 {
23431 msg_start();
23432 msg_scroll = TRUE;
23433 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23434 {
23435 msg_outnum((long)++nr);
23436 MSG_PUTS(": ");
23437 msg_outtrans(get_tv_string(&li->li_tv));
23438 msg_putchar('\n');
23439 out_flush(); /* output one line at a time */
23440 ui_breakcheck();
23441 }
23442 /* Assume "got_int" was set to truncate the listing. */
23443 got_int = FALSE;
23444
23445#ifdef FEAT_BROWSE_CMD
23446 if (cmdmod.browse)
23447 {
23448 quit_more = FALSE;
23449 nr = prompt_for_number(FALSE);
23450 msg_starthere();
23451 if (nr > 0)
23452 {
23453 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23454 (long)nr);
23455
23456 if (p != NULL)
23457 {
23458 p = expand_env_save(p);
23459 eap->arg = p;
23460 eap->cmdidx = CMD_edit;
23461 cmdmod.browse = FALSE;
23462 do_exedit(eap, NULL);
23463 vim_free(p);
23464 }
23465 }
23466 }
23467#endif
23468 }
23469}
23470
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471#endif /* FEAT_EVAL */
23472
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023474#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475
23476#ifdef WIN3264
23477/*
23478 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23479 */
23480static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23481static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23482static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23483
23484/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023485 * Get the short path (8.3) for the filename in "fnamep".
23486 * Only works for a valid file name.
23487 * When the path gets longer "fnamep" is changed and the allocated buffer
23488 * is put in "bufp".
23489 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23490 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 */
23492 static int
23493get_short_pathname(fnamep, bufp, fnamelen)
23494 char_u **fnamep;
23495 char_u **bufp;
23496 int *fnamelen;
23497{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023498 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 char_u *newbuf;
23500
23501 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 l = GetShortPathName(*fnamep, *fnamep, len);
23503 if (l > len - 1)
23504 {
23505 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023506 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 newbuf = vim_strnsave(*fnamep, l);
23508 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023509 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510
23511 vim_free(*bufp);
23512 *fnamep = *bufp = newbuf;
23513
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023514 /* Really should always succeed, as the buffer is big enough. */
23515 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 }
23517
23518 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023519 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520}
23521
23522/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023523 * Get the short path (8.3) for the filename in "fname". The converted
23524 * path is returned in "bufp".
23525 *
23526 * Some of the directories specified in "fname" may not exist. This function
23527 * will shorten the existing directories at the beginning of the path and then
23528 * append the remaining non-existing path.
23529 *
23530 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023531 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023532 * bufp - Pointer to an allocated buffer for the filename.
23533 * fnamelen - Length of the filename pointed to by fname
23534 *
23535 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 */
23537 static int
23538shortpath_for_invalid_fname(fname, bufp, fnamelen)
23539 char_u **fname;
23540 char_u **bufp;
23541 int *fnamelen;
23542{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023543 char_u *short_fname, *save_fname, *pbuf_unused;
23544 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023546 int old_len, len;
23547 int new_len, sfx_len;
23548 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549
23550 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023551 old_len = *fnamelen;
23552 save_fname = vim_strnsave(*fname, old_len);
23553 pbuf_unused = NULL;
23554 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023556 endp = save_fname + old_len - 1; /* Find the end of the copy */
23557 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023559 /*
23560 * Try shortening the supplied path till it succeeds by removing one
23561 * directory at a time from the tail of the path.
23562 */
23563 len = 0;
23564 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023566 /* go back one path-separator */
23567 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23568 --endp;
23569 if (endp <= save_fname)
23570 break; /* processed the complete path */
23571
23572 /*
23573 * Replace the path separator with a NUL and try to shorten the
23574 * resulting path.
23575 */
23576 ch = *endp;
23577 *endp = 0;
23578 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023579 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023580 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23581 {
23582 retval = FAIL;
23583 goto theend;
23584 }
23585 *endp = ch; /* preserve the string */
23586
23587 if (len > 0)
23588 break; /* successfully shortened the path */
23589
23590 /* failed to shorten the path. Skip the path separator */
23591 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 }
23593
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023594 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023596 /*
23597 * Succeeded in shortening the path. Now concatenate the shortened
23598 * path with the remaining path at the tail.
23599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023601 /* Compute the length of the new path. */
23602 sfx_len = (int)(save_endp - endp) + 1;
23603 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023605 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023607 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023609 /* There is not enough space in the currently allocated string,
23610 * copy it to a buffer big enough. */
23611 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023613 {
23614 retval = FAIL;
23615 goto theend;
23616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 }
23618 else
23619 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023620 /* Transfer short_fname to the main buffer (it's big enough),
23621 * unless get_short_pathname() did its work in-place. */
23622 *fname = *bufp = save_fname;
23623 if (short_fname != save_fname)
23624 vim_strncpy(save_fname, short_fname, len);
23625 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023627
23628 /* concat the not-shortened part of the path */
23629 vim_strncpy(*fname + len, endp, sfx_len);
23630 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023632
23633theend:
23634 vim_free(pbuf_unused);
23635 vim_free(save_fname);
23636
23637 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638}
23639
23640/*
23641 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023642 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 */
23644 static int
23645shortpath_for_partial(fnamep, bufp, fnamelen)
23646 char_u **fnamep;
23647 char_u **bufp;
23648 int *fnamelen;
23649{
23650 int sepcount, len, tflen;
23651 char_u *p;
23652 char_u *pbuf, *tfname;
23653 int hasTilde;
23654
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023655 /* Count up the path separators from the RHS.. so we know which part
23656 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023658 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 if (vim_ispathsep(*p))
23660 ++sepcount;
23661
23662 /* Need full path first (use expand_env() to remove a "~/") */
23663 hasTilde = (**fnamep == '~');
23664 if (hasTilde)
23665 pbuf = tfname = expand_env_save(*fnamep);
23666 else
23667 pbuf = tfname = FullName_save(*fnamep, FALSE);
23668
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023669 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023671 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23672 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673
23674 if (len == 0)
23675 {
23676 /* Don't have a valid filename, so shorten the rest of the
23677 * path if we can. This CAN give us invalid 8.3 filenames, but
23678 * there's not a lot of point in guessing what it might be.
23679 */
23680 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023681 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 }
23684
23685 /* Count the paths backward to find the beginning of the desired string. */
23686 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023687 {
23688#ifdef FEAT_MBYTE
23689 if (has_mbyte)
23690 p -= mb_head_off(tfname, p);
23691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 if (vim_ispathsep(*p))
23693 {
23694 if (sepcount == 0 || (hasTilde && sepcount == 1))
23695 break;
23696 else
23697 sepcount --;
23698 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700 if (hasTilde)
23701 {
23702 --p;
23703 if (p >= tfname)
23704 *p = '~';
23705 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 }
23708 else
23709 ++p;
23710
23711 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23712 vim_free(*bufp);
23713 *fnamelen = (int)STRLEN(p);
23714 *bufp = pbuf;
23715 *fnamep = p;
23716
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023717 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718}
23719#endif /* WIN3264 */
23720
23721/*
23722 * Adjust a filename, according to a string of modifiers.
23723 * *fnamep must be NUL terminated when called. When returning, the length is
23724 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023725 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 * When there is an error, *fnamep is set to NULL.
23727 */
23728 int
23729modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23730 char_u *src; /* string with modifiers */
23731 int *usedlen; /* characters after src that are used */
23732 char_u **fnamep; /* file name so far */
23733 char_u **bufp; /* buffer for allocated file name or NULL */
23734 int *fnamelen; /* length of fnamep */
23735{
23736 int valid = 0;
23737 char_u *tail;
23738 char_u *s, *p, *pbuf;
23739 char_u dirname[MAXPATHL];
23740 int c;
23741 int has_fullname = 0;
23742#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023743 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744 int has_shortname = 0;
23745#endif
23746
23747repeat:
23748 /* ":p" - full path/file_name */
23749 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23750 {
23751 has_fullname = 1;
23752
23753 valid |= VALID_PATH;
23754 *usedlen += 2;
23755
23756 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23757 if ((*fnamep)[0] == '~'
23758#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23759 && ((*fnamep)[1] == '/'
23760# ifdef BACKSLASH_IN_FILENAME
23761 || (*fnamep)[1] == '\\'
23762# endif
23763 || (*fnamep)[1] == NUL)
23764
23765#endif
23766 )
23767 {
23768 *fnamep = expand_env_save(*fnamep);
23769 vim_free(*bufp); /* free any allocated file name */
23770 *bufp = *fnamep;
23771 if (*fnamep == NULL)
23772 return -1;
23773 }
23774
23775 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023776 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 {
23778 if (vim_ispathsep(*p)
23779 && p[1] == '.'
23780 && (p[2] == NUL
23781 || vim_ispathsep(p[2])
23782 || (p[2] == '.'
23783 && (p[3] == NUL || vim_ispathsep(p[3])))))
23784 break;
23785 }
23786
23787 /* FullName_save() is slow, don't use it when not needed. */
23788 if (*p != NUL || !vim_isAbsName(*fnamep))
23789 {
23790 *fnamep = FullName_save(*fnamep, *p != NUL);
23791 vim_free(*bufp); /* free any allocated file name */
23792 *bufp = *fnamep;
23793 if (*fnamep == NULL)
23794 return -1;
23795 }
23796
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023797#ifdef WIN3264
23798# if _WIN32_WINNT >= 0x0500
23799 if (vim_strchr(*fnamep, '~') != NULL)
23800 {
23801 /* Expand 8.3 filename to full path. Needed to make sure the same
23802 * file does not have two different names.
23803 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23804 p = alloc(_MAX_PATH + 1);
23805 if (p != NULL)
23806 {
23807 if (GetLongPathName(*fnamep, p, MAXPATHL))
23808 {
23809 vim_free(*bufp);
23810 *bufp = *fnamep = p;
23811 }
23812 else
23813 vim_free(p);
23814 }
23815 }
23816# endif
23817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 /* Append a path separator to a directory. */
23819 if (mch_isdir(*fnamep))
23820 {
23821 /* Make room for one or two extra characters. */
23822 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23823 vim_free(*bufp); /* free any allocated file name */
23824 *bufp = *fnamep;
23825 if (*fnamep == NULL)
23826 return -1;
23827 add_pathsep(*fnamep);
23828 }
23829 }
23830
23831 /* ":." - path relative to the current directory */
23832 /* ":~" - path relative to the home directory */
23833 /* ":8" - shortname path - postponed till after */
23834 while (src[*usedlen] == ':'
23835 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23836 {
23837 *usedlen += 2;
23838 if (c == '8')
23839 {
23840#ifdef WIN3264
23841 has_shortname = 1; /* Postpone this. */
23842#endif
23843 continue;
23844 }
23845 pbuf = NULL;
23846 /* Need full path first (use expand_env() to remove a "~/") */
23847 if (!has_fullname)
23848 {
23849 if (c == '.' && **fnamep == '~')
23850 p = pbuf = expand_env_save(*fnamep);
23851 else
23852 p = pbuf = FullName_save(*fnamep, FALSE);
23853 }
23854 else
23855 p = *fnamep;
23856
23857 has_fullname = 0;
23858
23859 if (p != NULL)
23860 {
23861 if (c == '.')
23862 {
23863 mch_dirname(dirname, MAXPATHL);
23864 s = shorten_fname(p, dirname);
23865 if (s != NULL)
23866 {
23867 *fnamep = s;
23868 if (pbuf != NULL)
23869 {
23870 vim_free(*bufp); /* free any allocated file name */
23871 *bufp = pbuf;
23872 pbuf = NULL;
23873 }
23874 }
23875 }
23876 else
23877 {
23878 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23879 /* Only replace it when it starts with '~' */
23880 if (*dirname == '~')
23881 {
23882 s = vim_strsave(dirname);
23883 if (s != NULL)
23884 {
23885 *fnamep = s;
23886 vim_free(*bufp);
23887 *bufp = s;
23888 }
23889 }
23890 }
23891 vim_free(pbuf);
23892 }
23893 }
23894
23895 tail = gettail(*fnamep);
23896 *fnamelen = (int)STRLEN(*fnamep);
23897
23898 /* ":h" - head, remove "/file_name", can be repeated */
23899 /* Don't remove the first "/" or "c:\" */
23900 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23901 {
23902 valid |= VALID_HEAD;
23903 *usedlen += 2;
23904 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023905 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023906 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 *fnamelen = (int)(tail - *fnamep);
23908#ifdef VMS
23909 if (*fnamelen > 0)
23910 *fnamelen += 1; /* the path separator is part of the path */
23911#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023912 if (*fnamelen == 0)
23913 {
23914 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23915 p = vim_strsave((char_u *)".");
23916 if (p == NULL)
23917 return -1;
23918 vim_free(*bufp);
23919 *bufp = *fnamep = tail = p;
23920 *fnamelen = 1;
23921 }
23922 else
23923 {
23924 while (tail > s && !after_pathsep(s, tail))
23925 mb_ptr_back(*fnamep, tail);
23926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 }
23928
23929 /* ":8" - shortname */
23930 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23931 {
23932 *usedlen += 2;
23933#ifdef WIN3264
23934 has_shortname = 1;
23935#endif
23936 }
23937
23938#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023939 /*
23940 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 */
23942 if (has_shortname)
23943 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023944 /* Copy the string if it is shortened by :h and when it wasn't copied
23945 * yet, because we are going to change it in place. Avoids changing
23946 * the buffer name for "%:8". */
23947 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 {
23949 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023950 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 return -1;
23952 vim_free(*bufp);
23953 *bufp = *fnamep = p;
23954 }
23955
23956 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023957 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 if (!has_fullname && !vim_isAbsName(*fnamep))
23959 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023960 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 return -1;
23962 }
23963 else
23964 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023965 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966
Bram Moolenaardc935552011-08-17 15:23:23 +020023967 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023969 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 return -1;
23971
23972 if (l == 0)
23973 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023974 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023976 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 return -1;
23978 }
23979 *fnamelen = l;
23980 }
23981 }
23982#endif /* WIN3264 */
23983
23984 /* ":t" - tail, just the basename */
23985 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23986 {
23987 *usedlen += 2;
23988 *fnamelen -= (int)(tail - *fnamep);
23989 *fnamep = tail;
23990 }
23991
23992 /* ":e" - extension, can be repeated */
23993 /* ":r" - root, without extension, can be repeated */
23994 while (src[*usedlen] == ':'
23995 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23996 {
23997 /* find a '.' in the tail:
23998 * - for second :e: before the current fname
23999 * - otherwise: The last '.'
24000 */
24001 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24002 s = *fnamep - 2;
24003 else
24004 s = *fnamep + *fnamelen - 1;
24005 for ( ; s > tail; --s)
24006 if (s[0] == '.')
24007 break;
24008 if (src[*usedlen + 1] == 'e') /* :e */
24009 {
24010 if (s > tail)
24011 {
24012 *fnamelen += (int)(*fnamep - (s + 1));
24013 *fnamep = s + 1;
24014#ifdef VMS
24015 /* cut version from the extension */
24016 s = *fnamep + *fnamelen - 1;
24017 for ( ; s > *fnamep; --s)
24018 if (s[0] == ';')
24019 break;
24020 if (s > *fnamep)
24021 *fnamelen = s - *fnamep;
24022#endif
24023 }
24024 else if (*fnamep <= tail)
24025 *fnamelen = 0;
24026 }
24027 else /* :r */
24028 {
24029 if (s > tail) /* remove one extension */
24030 *fnamelen = (int)(s - *fnamep);
24031 }
24032 *usedlen += 2;
24033 }
24034
24035 /* ":s?pat?foo?" - substitute */
24036 /* ":gs?pat?foo?" - global substitute */
24037 if (src[*usedlen] == ':'
24038 && (src[*usedlen + 1] == 's'
24039 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24040 {
24041 char_u *str;
24042 char_u *pat;
24043 char_u *sub;
24044 int sep;
24045 char_u *flags;
24046 int didit = FALSE;
24047
24048 flags = (char_u *)"";
24049 s = src + *usedlen + 2;
24050 if (src[*usedlen + 1] == 'g')
24051 {
24052 flags = (char_u *)"g";
24053 ++s;
24054 }
24055
24056 sep = *s++;
24057 if (sep)
24058 {
24059 /* find end of pattern */
24060 p = vim_strchr(s, sep);
24061 if (p != NULL)
24062 {
24063 pat = vim_strnsave(s, (int)(p - s));
24064 if (pat != NULL)
24065 {
24066 s = p + 1;
24067 /* find end of substitution */
24068 p = vim_strchr(s, sep);
24069 if (p != NULL)
24070 {
24071 sub = vim_strnsave(s, (int)(p - s));
24072 str = vim_strnsave(*fnamep, *fnamelen);
24073 if (sub != NULL && str != NULL)
24074 {
24075 *usedlen = (int)(p + 1 - src);
24076 s = do_string_sub(str, pat, sub, flags);
24077 if (s != NULL)
24078 {
24079 *fnamep = s;
24080 *fnamelen = (int)STRLEN(s);
24081 vim_free(*bufp);
24082 *bufp = s;
24083 didit = TRUE;
24084 }
24085 }
24086 vim_free(sub);
24087 vim_free(str);
24088 }
24089 vim_free(pat);
24090 }
24091 }
24092 /* after using ":s", repeat all the modifiers */
24093 if (didit)
24094 goto repeat;
24095 }
24096 }
24097
24098 return valid;
24099}
24100
24101/*
24102 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24103 * "flags" can be "g" to do a global substitute.
24104 * Returns an allocated string, NULL for error.
24105 */
24106 char_u *
24107do_string_sub(str, pat, sub, flags)
24108 char_u *str;
24109 char_u *pat;
24110 char_u *sub;
24111 char_u *flags;
24112{
24113 int sublen;
24114 regmatch_T regmatch;
24115 int i;
24116 int do_all;
24117 char_u *tail;
24118 garray_T ga;
24119 char_u *ret;
24120 char_u *save_cpo;
24121
24122 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24123 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024124 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125
24126 ga_init2(&ga, 1, 200);
24127
24128 do_all = (flags[0] == 'g');
24129
24130 regmatch.rm_ic = p_ic;
24131 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24132 if (regmatch.regprog != NULL)
24133 {
24134 tail = str;
24135 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24136 {
24137 /*
24138 * Get some space for a temporary buffer to do the substitution
24139 * into. It will contain:
24140 * - The text up to where the match is.
24141 * - The substituted text.
24142 * - The text after the match.
24143 */
24144 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24145 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24146 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24147 {
24148 ga_clear(&ga);
24149 break;
24150 }
24151
24152 /* copy the text up to where the match is */
24153 i = (int)(regmatch.startp[0] - tail);
24154 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24155 /* add the substituted text */
24156 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24157 + ga.ga_len + i, TRUE, TRUE, FALSE);
24158 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 /* avoid getting stuck on a match with an empty string */
24160 if (tail == regmatch.endp[0])
24161 {
24162 if (*tail == NUL)
24163 break;
24164 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24165 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 }
24167 else
24168 {
24169 tail = regmatch.endp[0];
24170 if (*tail == NUL)
24171 break;
24172 }
24173 if (!do_all)
24174 break;
24175 }
24176
24177 if (ga.ga_data != NULL)
24178 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24179
24180 vim_free(regmatch.regprog);
24181 }
24182
24183 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24184 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024185 if (p_cpo == empty_option)
24186 p_cpo = save_cpo;
24187 else
24188 /* Darn, evaluating {sub} expression changed the value. */
24189 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190
24191 return ret;
24192}
24193
24194#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */