blob: b04f33034ae2cdc73be8e5cc89ef4ac6ddf6638c [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 Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100758static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000760static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000761static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static int get_env_len __ARGS((char_u **arg));
763static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000765static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
766#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
767#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
768 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000769static 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 +0000770static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
773static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static typval_T *alloc_tv __ARGS((void));
775static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void init_tv __ARGS((typval_T *varp));
777static long get_tv_number __ARGS((typval_T *varp));
778static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000779static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static char_u *get_tv_string __ARGS((typval_T *varp));
781static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
786static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
787static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000788static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
789static 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 +0000790static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
791static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000792static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200793static int var_check_func_name __ARGS((char_u *name, int new_var));
794static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static 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 +0000825static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
826static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000828static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
829static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000830static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000831static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000832static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200834
835#ifdef EBCDIC
836static int compare_func_name __ARGS((const void *s1, const void *s2));
837static void sortFunctions __ARGS(());
838#endif
839
840
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000841/* Character used as separated in autoload function/variable names. */
842#define AUTOLOAD_CHAR '#'
843
Bram Moolenaar33570922005-01-25 22:26:29 +0000844/*
845 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000846 */
847 void
848eval_init()
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 int i;
851 struct vimvar *p;
852
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200853 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
854 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200855 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000856 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
859 for (i = 0; i < VV_LEN; ++i)
860 {
861 p = &vimvars[i];
862 STRCPY(p->vv_di.di_key, p->vv_name);
863 if (p->vv_flags & VV_RO)
864 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 else if (p->vv_flags & VV_RO_SBX)
866 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
867 else
868 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000869
870 /* add to v: scope dict, unless the value is not always available */
871 if (p->vv_type != VAR_UNKNOWN)
872 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000874 /* add to compat scope dict */
875 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000877 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200914 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 /* script-local variables */
923 for (i = 1; i <= ga_scripts.ga_len; ++i)
924 {
925 vars_clear(&SCRIPT_VARS(i));
926 vim_free(SCRIPT_SV(i));
927 }
928 ga_clear(&ga_scripts);
929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000930 /* unreferenced lists and dicts */
931 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932
933 /* functions */
934 free_all_functions();
935 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936}
937#endif
938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Return the name of the executed function.
941 */
942 char_u *
943func_name(cookie)
944 void *cookie;
945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the next breakpoint line for a funccall cookie.
951 */
952 linenr_T *
953func_breakpoint(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the debug tick for a funccall cookie.
961 */
962 int *
963func_dbg_tick(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the nesting level for a funccall cookie.
971 */
972 int
973func_level(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000980funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000982/* pointer to list of previously used funccal, still around because some
983 * item in it is still being used. */
984funccall_T *previous_funccal = NULL;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Return TRUE when a function was ended by a ":return" command.
988 */
989 int
990current_func_returned()
991{
992 return current_funccal->returned;
993}
994
995
996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Set an internal variable to a string value. Creates the variable if it does
998 * not already exist.
999 */
1000 void
1001set_internal_string_var(name, value)
1002 char_u *name;
1003 char_u *value;
1004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 val = vim_strsave(value);
1009 if (val != NULL)
1010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018}
1019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001021static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022static char_u *redir_endp = NULL;
1023static char_u *redir_varname = NULL;
1024
1025/*
1026 * Start recording command output to a variable
1027 * Returns OK if successfully completed the setup. FAIL otherwise.
1028 */
1029 int
1030var_redir_start(name, append)
1031 char_u *name;
1032 int append; /* append to an existing variable */
1033{
1034 int save_emsg;
1035 int err;
1036 typval_T tv;
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001039 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 {
1041 EMSG(_(e_invarg));
1042 return FAIL;
1043 }
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 redir_varname = vim_strsave(name);
1047 if (redir_varname == NULL)
1048 return FAIL;
1049
1050 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1051 if (redir_lval == NULL)
1052 {
1053 var_redir_stop();
1054 return FAIL;
1055 }
1056
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 /* The output is stored in growarray "redir_ga" until redirection ends. */
1058 ga_init2(&redir_ga, (int)sizeof(char), 500);
1059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1062 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1064 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001065 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp != NULL && *redir_endp != NUL)
1067 /* Trailing characters are present after the variable name */
1068 EMSG(_(e_trailing));
1069 else
1070 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 var_redir_stop();
1073 return FAIL;
1074 }
1075
1076 /* check if we can write to the variable: set it to or append an empty
1077 * string */
1078 save_emsg = did_emsg;
1079 did_emsg = FALSE;
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = (char_u *)"";
1082 if (append)
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1084 else
1085 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001086 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001088 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 if (err)
1090 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 var_redir_stop();
1093 return FAIL;
1094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 * Append "value[value_len]" to the variable set by var_redir_start().
1101 * The actual appending is postponed until redirection ends, because the value
1102 * appended may in fact be the string we write to, changing it may cause freed
1103 * memory to be used:
1104 * :redir => foo
1105 * :let foo
1106 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 */
1108 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 if (redir_lval == NULL)
1116 return;
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 {
1125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 }
1128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130}
1131
1132/*
1133 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
1137var_redir_stop()
1138{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 typval_T tv;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_lval != NULL)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* If there was no error: assign the text to the variable. */
1144 if (redir_endp != NULL)
1145 {
1146 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 /* Call get_lval() again, if it's inside a Dict or List it may
1150 * have changed. */
1151 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1152 FALSE, FALSE, FALSE, FNE_CHECK_START);
1153 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1154 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1155 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* free the collected output */
1159 vim_free(redir_ga.ga_data);
1160 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 vim_free(redir_lval);
1163 redir_lval = NULL;
1164 }
1165 vim_free(redir_varname);
1166 redir_varname = NULL;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# if defined(FEAT_MBYTE) || defined(PROTO)
1170 int
1171eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1172 char_u *enc_from;
1173 char_u *enc_to;
1174 char_u *fname_from;
1175 char_u *fname_to;
1176{
1177 int err = FALSE;
1178
1179 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1180 set_vim_var_string(VV_CC_TO, enc_to, -1);
1181 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1182 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1183 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1184 err = TRUE;
1185 set_vim_var_string(VV_CC_FROM, NULL, -1);
1186 set_vim_var_string(VV_CC_TO, NULL, -1);
1187 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1188 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1189
1190 if (err)
1191 return FAIL;
1192 return OK;
1193}
1194# endif
1195
1196# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1197 int
1198eval_printexpr(fname, args)
1199 char_u *fname;
1200 char_u *args;
1201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_FNAME_IN, fname, -1);
1205 set_vim_var_string(VV_CMDARG, args, -1);
1206 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1207 err = TRUE;
1208 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1209 set_vim_var_string(VV_CMDARG, NULL, -1);
1210
1211 if (err)
1212 {
1213 mch_remove(fname);
1214 return FAIL;
1215 }
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_DIFF) || defined(PROTO)
1221 void
1222eval_diff(origfile, newfile, outfile)
1223 char_u *origfile;
1224 char_u *newfile;
1225 char_u *outfile;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1230 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1231 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1232 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1235 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1236}
1237
1238 void
1239eval_patch(origfile, difffile, outfile)
1240 char_u *origfile;
1241 char_u *difffile;
1242 char_u *outfile;
1243{
1244 int err;
1245
1246 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1248 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1249 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1252 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1253}
1254# endif
1255
1256/*
1257 * Top level evaluation function, returning a boolean.
1258 * Sets "error" to TRUE if there was an error.
1259 * Return TRUE or FALSE.
1260 */
1261 int
1262eval_to_bool(arg, error, nextcmd, skip)
1263 char_u *arg;
1264 int *error;
1265 char_u **nextcmd;
1266 int skip; /* only parse, don't execute */
1267{
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 int retval = FALSE;
1270
1271 if (skip)
1272 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
1276 {
1277 *error = FALSE;
1278 if (!skip)
1279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
1291 * Top level evaluation function, returning a string. If "skip" is TRUE,
1292 * only parsing to "nextcmd" is done, without reporting errors. Return
1293 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1294 */
1295 char_u *
1296eval_to_string_skip(arg, nextcmd, skip)
1297 char_u *arg;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *retval;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = NULL;
1308 else
1309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 retval = vim_strsave(get_tv_string(&tv));
1311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320 * Skip over an expression at "*pp".
1321 * Return FAIL for an error, OK otherwise.
1322 */
1323 int
1324skip_expr(pp)
1325 char_u **pp;
1326{
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328
1329 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331}
1332
1333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 * When "convert" is TRUE convert a List into a sequence of lines and convert
1336 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Return pointer to allocated memory, or NULL for failure.
1338 */
1339 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *arg;
1342 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 {
1358 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001359 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001360 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001361 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001362 if (tv.vval.v_list->lv_len > 0)
1363 ga_append(&ga, NL);
1364 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 ga_append(&ga, NUL);
1366 retval = (char_u *)ga.ga_data;
1367 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368#ifdef FEAT_FLOAT
1369 else if (convert && tv.v_type == VAR_FLOAT)
1370 {
1371 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1372 retval = vim_strsave(numbuf);
1373 }
1374#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 else
1376 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379
1380 return retval;
1381}
1382
1383/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 * Call eval_to_string() without using current local variables and using
1385 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 */
1387 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg;
1390 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 char_u *retval;
1394 void *save_funccalp;
1395
1396 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 ++sandbox;
1399 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 if (use_sandbox)
1402 --sandbox;
1403 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 restore_funccal(save_funccalp);
1405 return retval;
1406}
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/*
1409 * Top level evaluation function, returning a number.
1410 * Evaluates "expr" silently.
1411 * Returns -1 for an error.
1412 */
1413 int
1414eval_to_number(expr)
1415 char_u *expr;
1416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001419 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 ++emsg_off;
1422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 retval = -1;
1425 else
1426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001427 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430 --emsg_off;
1431
1432 return retval;
1433}
1434
Bram Moolenaara40058a2005-07-11 22:42:07 +00001435/*
1436 * Prepare v: variable "idx" to be used.
1437 * Save the current typeval in "save_tv".
1438 * When not used yet add the variable to the v: hashtable.
1439 */
1440 static void
1441prepare_vimvar(idx, save_tv)
1442 int idx;
1443 typval_T *save_tv;
1444{
1445 *save_tv = vimvars[idx].vv_tv;
1446 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1447 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1448}
1449
1450/*
1451 * Restore v: variable "idx" to typeval "save_tv".
1452 * When no longer defined, remove the variable from the v: hashtable.
1453 */
1454 static void
1455restore_vimvar(idx, save_tv)
1456 int idx;
1457 typval_T *save_tv;
1458{
1459 hashitem_T *hi;
1460
Bram Moolenaara40058a2005-07-11 22:42:07 +00001461 vimvars[idx].vv_tv = *save_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 {
1464 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1465 if (HASHITEM_EMPTY(hi))
1466 EMSG2(_(e_intern2), "restore_vimvar()");
1467 else
1468 hash_remove(&vimvarht, hi);
1469 }
1470}
1471
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001472#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473/*
1474 * Evaluate an expression to a list with suggestions.
1475 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001477 */
1478 list_T *
1479eval_spell_expr(badword, expr)
1480 char_u *badword;
1481 char_u *expr;
1482{
1483 typval_T save_val;
1484 typval_T rettv;
1485 list_T *list = NULL;
1486 char_u *p = skipwhite(expr);
1487
1488 /* Set "v:val" to the bad word. */
1489 prepare_vimvar(VV_VAL, &save_val);
1490 vimvars[VV_VAL].vv_type = VAR_STRING;
1491 vimvars[VV_VAL].vv_str = badword;
1492 if (p_verbose == 0)
1493 ++emsg_off;
1494
1495 if (eval1(&p, &rettv, TRUE) == OK)
1496 {
1497 if (rettv.v_type != VAR_LIST)
1498 clear_tv(&rettv);
1499 else
1500 list = rettv.vval.v_list;
1501 }
1502
1503 if (p_verbose == 0)
1504 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 restore_vimvar(VV_VAL, &save_val);
1506
1507 return list;
1508}
1509
1510/*
1511 * "list" is supposed to contain two items: a word and a number. Return the
1512 * word in "pp" and the number as the return value.
1513 * Return -1 if anything isn't right.
1514 * Used to get the good word and score from the eval_spell_expr() result.
1515 */
1516 int
1517get_spellword(list, pp)
1518 list_T *list;
1519 char_u **pp;
1520{
1521 listitem_T *li;
1522
1523 li = list->lv_first;
1524 if (li == NULL)
1525 return -1;
1526 *pp = get_tv_string(&li->li_tv);
1527
1528 li = li->li_next;
1529 if (li == NULL)
1530 return -1;
1531 return get_tv_number(&li->li_tv);
1532}
1533#endif
1534
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 * Top level evaluation function.
1537 * Returns an allocated typval_T with the result.
1538 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 */
1540 typval_T *
1541eval_expr(arg, nextcmd)
1542 char_u *arg;
1543 char_u **nextcmd;
1544{
1545 typval_T *tv;
1546
1547 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 {
1550 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 }
1553
1554 return tv;
1555}
1556
1557
Bram Moolenaar4f688582007-07-24 12:34:30 +00001558#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1559 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562 * Uses argv[argc] for the function arguments. Only Number and String
1563 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001566 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001567call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 char_u *func;
1569 int argc;
1570 char_u **argv;
1571 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001572 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 long n;
1577 int len;
1578 int i;
1579 int doesrange;
1580 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001583 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 for (i = 0; i < argc; i++)
1588 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 /* Pass a NULL or empty argument as an empty string */
1590 if (argv[i] == NULL || *argv[i] == NUL)
1591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001592 argvars[i].v_type = VAR_STRING;
1593 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001594 continue;
1595 }
1596
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001597 if (str_arg_only)
1598 len = 0;
1599 else
1600 /* Recognize a number argument, the others must be strings. */
1601 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (len != 0 && len == (int)STRLEN(argv[i]))
1603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001604 argvars[i].v_type = VAR_NUMBER;
1605 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 else
1608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001609 argvars[i].v_type = VAR_STRING;
1610 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 }
1613
1614 if (safe)
1615 {
1616 save_funccalp = save_funccal();
1617 ++sandbox;
1618 }
1619
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1621 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (safe)
1625 {
1626 --sandbox;
1627 restore_funccal(save_funccalp);
1628 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629 vim_free(argvars);
1630
1631 if (ret == FAIL)
1632 clear_tv(rettv);
1633
1634 return ret;
1635}
1636
Bram Moolenaar4f688582007-07-24 12:34:30 +00001637# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001639 * Call vimL function "func" and return the result as a string.
1640 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 * Uses argv[argc] for the function arguments.
1642 */
1643 void *
1644call_func_retstr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001651 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001653 /* All arguments are passed as strings, no conversion to number. */
1654 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 return NULL;
1656
1657 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 return retval;
1660}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001661# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662
Bram Moolenaar4f688582007-07-24 12:34:30 +00001663# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001664/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001665 * Call vimL function "func" and return the result as a number.
1666 * Returns -1 when calling the function fails.
1667 * Uses argv[argc] for the function arguments.
1668 */
1669 long
1670call_func_retnr(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
1677 long retval;
1678
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001679 /* All arguments are passed as strings, no conversion to number. */
1680 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001681 return -1;
1682
1683 retval = get_tv_number_chk(&rettv, NULL);
1684 clear_tv(&rettv);
1685 return retval;
1686}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001687# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001688
1689/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001690 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 */
1694 void *
1695call_func_retlist(func, argc, argv, safe)
1696 char_u *func;
1697 int argc;
1698 char_u **argv;
1699 int safe; /* use the sandbox */
1700{
1701 typval_T rettv;
1702
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001703 /* All arguments are passed as strings, no conversion to number. */
1704 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 return NULL;
1706
1707 if (rettv.v_type != VAR_LIST)
1708 {
1709 clear_tv(&rettv);
1710 return NULL;
1711 }
1712
1713 return rettv.vval.v_list;
1714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718/*
1719 * Save the current function call pointer, and set it to NULL.
1720 * Used when executing autocommands and for ":source".
1721 */
1722 void *
1723save_funccal()
1724{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 current_funccal = NULL;
1728 return (void *)fc;
1729}
1730
1731 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732restore_funccal(vfc)
1733 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735 funccall_T *fc = (funccall_T *)vfc;
1736
1737 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738}
1739
Bram Moolenaar05159a02005-02-26 23:04:13 +00001740#if defined(FEAT_PROFILE) || defined(PROTO)
1741/*
1742 * Prepare profiling for entering a child or something else that is not
1743 * counted for the script/function itself.
1744 * Should always be called in pair with prof_child_exit().
1745 */
1746 void
1747prof_child_enter(tm)
1748 proftime_T *tm; /* place to store waittime */
1749{
1750 funccall_T *fc = current_funccal;
1751
1752 if (fc != NULL && fc->func->uf_profiling)
1753 profile_start(&fc->prof_child);
1754 script_prof_save(tm);
1755}
1756
1757/*
1758 * Take care of time spent in a child.
1759 * Should always be called after prof_child_enter().
1760 */
1761 void
1762prof_child_exit(tm)
1763 proftime_T *tm; /* where waittime was stored */
1764{
1765 funccall_T *fc = current_funccal;
1766
1767 if (fc != NULL && fc->func->uf_profiling)
1768 {
1769 profile_end(&fc->prof_child);
1770 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1771 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1772 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1773 }
1774 script_prof_restore(tm);
1775}
1776#endif
1777
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#ifdef FEAT_FOLDING
1780/*
1781 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1782 * it in "*cp". Doesn't give error messages.
1783 */
1784 int
1785eval_foldexpr(arg, cp)
1786 char_u *arg;
1787 int *cp;
1788{
Bram Moolenaar33570922005-01-25 22:26:29 +00001789 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 int retval;
1791 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001792 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1793 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001796 if (use_sandbox)
1797 ++sandbox;
1798 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 retval = 0;
1802 else
1803 {
1804 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 if (tv.v_type == VAR_NUMBER)
1806 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001807 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a string, check if there is a non-digit before
1812 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (!VIM_ISDIGIT(*s) && *s != '-')
1815 *cp = *s++;
1816 retval = atol((char *)s);
1817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001821 if (use_sandbox)
1822 --sandbox;
1823 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 return retval;
1826}
1827#endif
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let" list all variable values
1831 * ":let var1 var2" list variable values
1832 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 * ":let var += expr" assignment command.
1834 * ":let var -= expr" assignment command.
1835 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 */
1838 void
1839ex_let(eap)
1840 exarg_T *eap;
1841{
1842 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 int var_count = 0;
1847 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001848 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001849 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001850 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 argend = skip_var_list(arg, &var_count, &semicolon);
1853 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001855 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1856 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001857 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001860 /*
1861 * ":let" without "=": list variables
1862 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 if (*arg == '[')
1864 EMSG(_(e_invarg));
1865 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001867 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_glob_vars(&first);
1872 list_buf_vars(&first);
1873 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001874#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001876#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001877 list_script_vars(&first);
1878 list_func_vars(&first);
1879 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 eap->nextcmd = check_nextcmd(arg);
1882 }
1883 else
1884 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001885 op[0] = '=';
1886 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001887 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 {
1889 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1890 op[0] = expr[-1]; /* +=, -= or .= */
1891 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (eap->skip)
1895 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (eap->skip)
1898 {
1899 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 --emsg_skip;
1902 }
1903 else if (i != FAIL)
1904 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 }
1910}
1911
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912/*
1913 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1914 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 * When "nextchars" is not NULL it points to a string with characters that
1916 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1917 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 * Returns OK or FAIL;
1919 */
1920 static int
1921ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1922 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 int copy; /* copy values from "tv", don't move */
1925 int semicolon; /* from skip_var_list() */
1926 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928{
1929 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 listitem_T *item;
1933 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934
1935 if (*arg != '[')
1936 {
1937 /*
1938 * ":let var = expr" or ":for var in list"
1939 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return FAIL;
1942 return OK;
1943 }
1944
1945 /*
1946 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1947 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001948 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 {
1950 EMSG(_(e_listreq));
1951 return FAIL;
1952 }
1953
1954 i = list_len(l);
1955 if (semicolon == 0 && var_count < i)
1956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001957 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 }
1960 if (var_count - semicolon > i)
1961 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001962 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 return FAIL;
1964 }
1965
1966 item = l->lv_first;
1967 while (*arg != ']')
1968 {
1969 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 item = item->li_next;
1972 if (arg == NULL)
1973 return FAIL;
1974
1975 arg = skipwhite(arg);
1976 if (*arg == ';')
1977 {
1978 /* Put the rest of the list (may be empty) in the var after ';'.
1979 * Create a new list for this. */
1980 l = list_alloc();
1981 if (l == NULL)
1982 return FAIL;
1983 while (item != NULL)
1984 {
1985 list_append_tv(l, &item->li_tv);
1986 item = item->li_next;
1987 }
1988
1989 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001990 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 ltv.vval.v_list = l;
1992 l->lv_refcount = 1;
1993
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1995 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 clear_tv(&ltv);
1997 if (arg == NULL)
1998 return FAIL;
1999 break;
2000 }
2001 else if (*arg != ',' && *arg != ']')
2002 {
2003 EMSG2(_(e_intern2), "ex_let_vars()");
2004 return FAIL;
2005 }
2006 }
2007
2008 return OK;
2009}
2010
2011/*
2012 * Skip over assignable variable "var" or list of variables "[var, var]".
2013 * Used for ":let varvar = expr" and ":for varvar in expr".
2014 * For "[var, var]" increment "*var_count" for each variable.
2015 * for "[var, var; var]" set "semicolon".
2016 * Return NULL for an error.
2017 */
2018 static char_u *
2019skip_var_list(arg, var_count, semicolon)
2020 char_u *arg;
2021 int *var_count;
2022 int *semicolon;
2023{
2024 char_u *p, *s;
2025
2026 if (*arg == '[')
2027 {
2028 /* "[var, var]": find the matching ']'. */
2029 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002030 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 {
2032 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2033 s = skip_var_one(p);
2034 if (s == p)
2035 {
2036 EMSG2(_(e_invarg2), p);
2037 return NULL;
2038 }
2039 ++*var_count;
2040
2041 p = skipwhite(s);
2042 if (*p == ']')
2043 break;
2044 else if (*p == ';')
2045 {
2046 if (*semicolon == 1)
2047 {
2048 EMSG(_("Double ; in list of variables"));
2049 return NULL;
2050 }
2051 *semicolon = 1;
2052 }
2053 else if (*p != ',')
2054 {
2055 EMSG2(_(e_invarg2), p);
2056 return NULL;
2057 }
2058 }
2059 return p + 1;
2060 }
2061 else
2062 return skip_var_one(arg);
2063}
2064
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002066 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 static char_u *
2070skip_var_one(arg)
2071 char_u *arg;
2072{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002073 if (*arg == '@' && arg[1] != NUL)
2074 return arg + 2;
2075 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2076 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077}
2078
Bram Moolenaara7043832005-01-21 11:56:39 +00002079/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 * List variables for hashtab "ht" with prefix "prefix".
2081 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002084list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089{
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 hashitem_T *hi;
2091 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 int todo;
2093
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002094 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002095 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2096 {
2097 if (!HASHITEM_EMPTY(hi))
2098 {
2099 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 di = HI2DI(hi);
2101 if (empty || di->di_tv.v_type != VAR_STRING
2102 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 }
2105 }
2106}
2107
2108/*
2109 * List global variables.
2110 */
2111 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112list_glob_vars(first)
2113 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002114{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002116}
2117
2118/*
2119 * List buffer variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_buf_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002125 char_u numbuf[NUMBUFLEN];
2126
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2128 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129
2130 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2132 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List window variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_win_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2143 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002144}
2145
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146#ifdef FEAT_WINDOWS
2147/*
2148 * List tab page variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_tab_vars(first)
2152 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2155 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156}
2157#endif
2158
Bram Moolenaara7043832005-01-21 11:56:39 +00002159/*
2160 * List Vim variables.
2161 */
2162 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163list_vim_vars(first)
2164 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167}
2168
2169/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170 * List script-local variables, if there is a script.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_script_vars(first)
2174 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002175{
2176 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2178 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179}
2180
2181/*
2182 * List function variables, if there is a function.
2183 */
2184 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_func_vars(first)
2186 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187{
2188 if (current_funccal != NULL)
2189 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002191}
2192
2193/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 * List variables in "arg".
2195 */
2196 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 exarg_T *eap;
2199 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201{
2202 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 char_u *name_start;
2206 char_u *arg_subsc;
2207 char_u *tofree;
2208 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209
2210 while (!ends_excmd(*arg) && !got_int)
2211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002214 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2216 {
2217 emsg_severe = TRUE;
2218 EMSG(_(e_trailing));
2219 break;
2220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224 /* get_name_len() takes care of expanding curly braces */
2225 name_start = name = arg;
2226 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2227 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 /* This is mainly to keep test 49 working: when expanding
2230 * curly braces fails overrule the exception error message. */
2231 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 emsg_severe = TRUE;
2234 EMSG2(_(e_invarg2), arg);
2235 break;
2236 }
2237 error = TRUE;
2238 }
2239 else
2240 {
2241 if (tofree != NULL)
2242 name = tofree;
2243 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
2246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 /* handle d.key, l[idx], f(expr) */
2248 arg_subsc = arg;
2249 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'g': list_glob_vars(first); break;
2258 case 'b': list_buf_vars(first); break;
2259 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002260#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002261 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002262#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 case 'v': list_vim_vars(first); break;
2264 case 's': list_script_vars(first); break;
2265 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 default:
2267 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002268 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
2270 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 {
2272 char_u numbuf[NUMBUFLEN];
2273 char_u *tf;
2274 int c;
2275 char_u *s;
2276
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002277 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 c = *arg;
2279 *arg = NUL;
2280 list_one_var_a((char_u *)"",
2281 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 tv.v_type,
2283 s == NULL ? (char_u *)"" : s,
2284 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 *arg = c;
2286 vim_free(tf);
2287 }
2288 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 }
2291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292
2293 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295
2296 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298
2299 return arg;
2300}
2301
2302/*
2303 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2304 * Returns a pointer to the char just after the var name.
2305 * Returns NULL if there is an error.
2306 */
2307 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002310 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002312 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002313 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314{
2315 int c1;
2316 char_u *name;
2317 char_u *p;
2318 char_u *arg_end = NULL;
2319 int len;
2320 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322
2323 /*
2324 * ":let $VAR = expr": Set environment variable.
2325 */
2326 if (*arg == '$')
2327 {
2328 /* Find the end of the name. */
2329 ++arg;
2330 name = arg;
2331 len = get_env_len(&arg);
2332 if (len == 0)
2333 EMSG2(_(e_invarg2), name - 1);
2334 else
2335 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 if (op != NULL && (*op == '+' || *op == '-'))
2337 EMSG2(_(e_letwrong), op);
2338 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002339 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002341 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 {
2343 c1 = name[len];
2344 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002345 p = get_tv_string_chk(tv);
2346 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 {
2348 int mustfree = FALSE;
2349 char_u *s = vim_getenv(name, &mustfree);
2350
2351 if (s != NULL)
2352 {
2353 p = tofree = concat_str(s, p);
2354 if (mustfree)
2355 vim_free(s);
2356 }
2357 }
2358 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002361 if (STRICMP(name, "HOME") == 0)
2362 init_homedir();
2363 else if (didset_vim && STRICMP(name, "VIM") == 0)
2364 didset_vim = FALSE;
2365 else if (didset_vimruntime
2366 && STRICMP(name, "VIMRUNTIME") == 0)
2367 didset_vimruntime = FALSE;
2368 arg_end = arg;
2369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 }
2373 }
2374 }
2375
2376 /*
2377 * ":let &option = expr": Set option value.
2378 * ":let &l:option = expr": Set local option value.
2379 * ":let &g:option = expr": Set global option value.
2380 */
2381 else if (*arg == '&')
2382 {
2383 /* Find the end of the name. */
2384 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002385 if (p == NULL || (endchars != NULL
2386 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 EMSG(_(e_letunexp));
2388 else
2389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 long n;
2391 int opt_type;
2392 long numval;
2393 char_u *stringval = NULL;
2394 char_u *s;
2395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 c1 = *p;
2397 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398
2399 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 s = get_tv_string_chk(tv); /* != NULL if number or string */
2401 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 {
2403 opt_type = get_option_value(arg, &numval,
2404 &stringval, opt_flags);
2405 if ((opt_type == 1 && *op == '.')
2406 || (opt_type == 0 && *op != '.'))
2407 EMSG2(_(e_letwrong), op);
2408 else
2409 {
2410 if (opt_type == 1) /* number */
2411 {
2412 if (*op == '+')
2413 n = numval + n;
2414 else
2415 n = numval - n;
2416 }
2417 else if (opt_type == 0 && stringval != NULL) /* string */
2418 {
2419 s = concat_str(stringval, s);
2420 vim_free(stringval);
2421 stringval = s;
2422 }
2423 }
2424 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 if (s != NULL)
2426 {
2427 set_option_value(arg, n, s, opt_flags);
2428 arg_end = p;
2429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 }
2433 }
2434
2435 /*
2436 * ":let @r = expr": Set register contents.
2437 */
2438 else if (*arg == '@')
2439 {
2440 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 if (op != NULL && (*op == '+' || *op == '-'))
2442 EMSG2(_(e_letwrong), op);
2443 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 EMSG(_(e_letunexp));
2446 else
2447 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002448 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 char_u *s;
2450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 p = get_tv_string_chk(tv);
2452 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002454 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 if (s != NULL)
2456 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002457 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 vim_free(s);
2459 }
2460 }
2461 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 arg_end = arg + 1;
2465 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002466 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
2468 }
2469
2470 /*
2471 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002474 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002478 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2482 EMSG(_(e_letunexp));
2483 else
2484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 arg_end = p;
2487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 }
2491
2492 else
2493 EMSG2(_(e_invarg2), arg);
2494
2495 return arg_end;
2496}
2497
2498/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2500 */
2501 static int
2502check_changedtick(arg)
2503 char_u *arg;
2504{
2505 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2506 {
2507 EMSG2(_(e_readonlyvar), arg);
2508 return TRUE;
2509 }
2510 return FALSE;
2511}
2512
2513/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Get an lval: variable, Dict item or List item that can be assigned a value
2515 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2516 * "name.key", "name.key[expr]" etc.
2517 * Indexing only works if "name" is an existing List or Dictionary.
2518 * "name" points to the start of the name.
2519 * If "rettv" is not NULL it points to the value to be assigned.
2520 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2521 * wrong; must end in space or cmd separator.
2522 *
2523 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002524 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 */
2527 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002528get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 typval_T *rettv;
2531 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int unlet;
2533 int skip;
2534 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002535 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 char_u *expr_start, *expr_end;
2539 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 dictitem_T *v;
2541 typval_T var1;
2542 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551
2552 if (skip)
2553 {
2554 /* When skipping just find the end of the name. */
2555 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 }
2558
2559 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002560 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (expr_start != NULL)
2562 {
2563 /* Don't expand the name when we already know there is an error. */
2564 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2565 && *p != '[' && *p != '.')
2566 {
2567 EMSG(_(e_trailing));
2568 return NULL;
2569 }
2570
2571 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2572 if (lp->ll_exp_name == NULL)
2573 {
2574 /* Report an invalid expression in braces, unless the
2575 * expression evaluation has been cancelled due to an
2576 * aborting error, an interrupt, or an exception. */
2577 if (!aborting() && !quiet)
2578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002579 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 EMSG2(_(e_invarg2), name);
2581 return NULL;
2582 }
2583 }
2584 lp->ll_name = lp->ll_exp_name;
2585 }
2586 else
2587 lp->ll_name = name;
2588
2589 /* Without [idx] or .key we are done. */
2590 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2591 return p;
2592
2593 cc = *p;
2594 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002595 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (v == NULL && !quiet)
2597 EMSG2(_(e_undefvar), lp->ll_name);
2598 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 if (v == NULL)
2600 return NULL;
2601
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 /*
2603 * Loop until no more [idx] or .key is following.
2604 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2609 && !(lp->ll_tv->v_type == VAR_DICT
2610 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E689: Can only index a List or Dictionary"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E708: [:] must come last"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 len = -1;
2624 if (*p == '.')
2625 {
2626 key = p + 1;
2627 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2628 ;
2629 if (len == 0)
2630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (!quiet)
2632 EMSG(_(e_emptykey));
2633 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 }
2635 p = key + len;
2636 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 if (*p == ':')
2642 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 empty1 = FALSE;
2646 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648 if (get_tv_string_chk(&var1) == NULL)
2649 {
2650 /* not a number or string */
2651 clear_tv(&var1);
2652 return NULL;
2653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 }
2655
2656 /* Optionally get the second index [ :expr]. */
2657 if (*p == ':')
2658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 if (!empty1)
2664 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (rettv != NULL && (rettv->v_type != VAR_LIST
2668 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (!quiet)
2671 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676 p = skipwhite(p + 1);
2677 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 else
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002688 if (get_tv_string_chk(&var2) == NULL)
2689 {
2690 /* not a number or string */
2691 if (!empty1)
2692 clear_tv(&var1);
2693 clear_tv(&var2);
2694 return NULL;
2695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
2705 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
2712
2713 /* Skip to past ']'. */
2714 ++p;
2715 }
2716
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 {
2719 if (len == -1)
2720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*key == NUL)
2724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002731 lp->ll_list = NULL;
2732 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002733 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002734
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002735 /* When assigning to a scope dictionary check that a function and
2736 * variable name is valid (only variable name unless it is l: or
2737 * g: dictionary). Disallow overwriting a builtin function. */
2738 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002739 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002740 int prevval;
2741 int wrong;
2742
2743 if (len != -1)
2744 {
2745 prevval = key[len];
2746 key[len] = NUL;
2747 }
2748 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2749 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002750 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002751 || !valid_varname(key);
2752 if (len != -1)
2753 key[len] = prevval;
2754 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755 return NULL;
2756 }
2757
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 /* Can't add "v:" variable. */
2761 if (lp->ll_dict == &vimvardict)
2762 {
2763 EMSG2(_(e_illvar), name);
2764 return NULL;
2765 }
2766
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002767 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002771 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (len == -1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (len == -1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 p = NULL;
2784 break;
2785 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 /* existing variable, need to check if it can be changed */
2787 else if (var_check_ro(lp->ll_di->di_flags, name))
2788 return NULL;
2789
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (len == -1)
2791 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 }
2794 else
2795 {
2796 /*
2797 * Get the number and item for the only or first index of the List.
2798 */
2799 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
2802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002803 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 clear_tv(&var1);
2805 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002806 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_list = lp->ll_tv->vval.v_list;
2808 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2809 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002811 if (lp->ll_n1 < 0)
2812 {
2813 lp->ll_n1 = 0;
2814 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2815 }
2816 }
2817 if (lp->ll_li == NULL)
2818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 if (!quiet)
2822 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 }
2825
2826 /*
2827 * May need to find the item or absolute index for the second
2828 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 * When no index given: "lp->ll_empty2" is TRUE.
2830 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002834 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002840 {
2841 if (!quiet)
2842 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 }
2847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2849 if (lp->ll_n1 < 0)
2850 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2851 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 {
2853 if (!quiet)
2854 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002856 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 }
2858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002861 }
2862
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return p;
2864}
2865
2866/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 */
2869 static void
2870clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872{
2873 vim_free(lp->ll_exp_name);
2874 vim_free(lp->ll_newkey);
2875}
2876
2877/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002878 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 */
2882 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002886 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889{
2890 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 listitem_T *ri;
2892 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893
2894 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002895 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 cc = *endp;
2899 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900 if (op != NULL && *op != '=')
2901 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002904 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002905 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002906 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 {
2908 if (tv_op(&tv, rettv, op) == OK)
2909 set_var(lp->ll_name, &tv, FALSE);
2910 clear_tv(&tv);
2911 }
2912 }
2913 else
2914 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 else if (tv_check_lock(lp->ll_newkey == NULL
2919 ? lp->ll_tv->v_lock
2920 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2921 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 else if (lp->ll_range)
2923 {
2924 /*
2925 * Assign the List values to the list items.
2926 */
2927 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 if (op != NULL && *op != '=')
2930 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2931 else
2932 {
2933 clear_tv(&lp->ll_li->li_tv);
2934 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2935 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 ri = ri->li_next;
2937 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2938 break;
2939 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002942 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 ri = NULL;
2945 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 lp->ll_li = lp->ll_li->li_next;
2949 ++lp->ll_n1;
2950 }
2951 if (ri != NULL)
2952 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 else if (lp->ll_empty2
2954 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 : lp->ll_n1 != lp->ll_n2)
2956 EMSG(_("E711: List value has not enough items"));
2957 }
2958 else
2959 {
2960 /*
2961 * Assign to a List or Dictionary item.
2962 */
2963 if (lp->ll_newkey != NULL)
2964 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965 if (op != NULL && *op != '=')
2966 {
2967 EMSG2(_(e_letwrong), op);
2968 return;
2969 }
2970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002972 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 if (di == NULL)
2974 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002975 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2976 {
2977 vim_free(di);
2978 return;
2979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002981 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 else if (op != NULL && *op != '=')
2983 {
2984 tv_op(lp->ll_tv, rettv, op);
2985 return;
2986 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002987 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 /*
2991 * Assign the value to the variable or list item.
2992 */
2993 if (copy)
2994 copy_tv(rettv, lp->ll_tv);
2995 else
2996 {
2997 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002998 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003000 }
3001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002}
3003
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3006 * Returns OK or FAIL.
3007 */
3008 static int
3009tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003010 typval_T *tv1;
3011 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 char_u *op;
3013{
3014 long n;
3015 char_u numbuf[NUMBUFLEN];
3016 char_u *s;
3017
3018 /* Can't do anything with a Funcref or a Dict on the right. */
3019 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3020 {
3021 switch (tv1->v_type)
3022 {
3023 case VAR_DICT:
3024 case VAR_FUNC:
3025 break;
3026
3027 case VAR_LIST:
3028 if (*op != '+' || tv2->v_type != VAR_LIST)
3029 break;
3030 /* List += List */
3031 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3032 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3033 return OK;
3034
3035 case VAR_NUMBER:
3036 case VAR_STRING:
3037 if (tv2->v_type == VAR_LIST)
3038 break;
3039 if (*op == '+' || *op == '-')
3040 {
3041 /* nr += nr or nr -= nr*/
3042 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043#ifdef FEAT_FLOAT
3044 if (tv2->v_type == VAR_FLOAT)
3045 {
3046 float_T f = n;
3047
3048 if (*op == '+')
3049 f += tv2->vval.v_float;
3050 else
3051 f -= tv2->vval.v_float;
3052 clear_tv(tv1);
3053 tv1->v_type = VAR_FLOAT;
3054 tv1->vval.v_float = f;
3055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057#endif
3058 {
3059 if (*op == '+')
3060 n += get_tv_number(tv2);
3061 else
3062 n -= get_tv_number(tv2);
3063 clear_tv(tv1);
3064 tv1->v_type = VAR_NUMBER;
3065 tv1->vval.v_number = n;
3066 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 }
3068 else
3069 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003070 if (tv2->v_type == VAR_FLOAT)
3071 break;
3072
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 /* str .= str */
3074 s = get_tv_string(tv1);
3075 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3076 clear_tv(tv1);
3077 tv1->v_type = VAR_STRING;
3078 tv1->vval.v_string = s;
3079 }
3080 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003081
3082#ifdef FEAT_FLOAT
3083 case VAR_FLOAT:
3084 {
3085 float_T f;
3086
3087 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3088 && tv2->v_type != VAR_NUMBER
3089 && tv2->v_type != VAR_STRING))
3090 break;
3091 if (tv2->v_type == VAR_FLOAT)
3092 f = tv2->vval.v_float;
3093 else
3094 f = get_tv_number(tv2);
3095 if (*op == '+')
3096 tv1->vval.v_float += f;
3097 else
3098 tv1->vval.v_float -= f;
3099 }
3100 return OK;
3101#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 }
3103 }
3104
3105 EMSG2(_(e_letwrong), op);
3106 return FAIL;
3107}
3108
3109/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110 * Add a watcher to a list.
3111 */
3112 static void
3113list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003114 list_T *l;
3115 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116{
3117 lw->lw_next = l->lv_watch;
3118 l->lv_watch = lw;
3119}
3120
3121/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003122 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123 * No warning when it isn't found...
3124 */
3125 static void
3126list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 list_T *l;
3128 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129{
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131
3132 lwp = &l->lv_watch;
3133 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3134 {
3135 if (lw == lwrem)
3136 {
3137 *lwp = lw->lw_next;
3138 break;
3139 }
3140 lwp = &lw->lw_next;
3141 }
3142}
3143
3144/*
3145 * Just before removing an item from a list: advance watchers to the next
3146 * item.
3147 */
3148 static void
3149list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 list_T *l;
3151 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152{
Bram Moolenaar33570922005-01-25 22:26:29 +00003153 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154
3155 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3156 if (lw->lw_item == item)
3157 lw->lw_item = item->li_next;
3158}
3159
3160/*
3161 * Evaluate the expression used in a ":for var in expr" command.
3162 * "arg" points to "var".
3163 * Set "*errp" to TRUE for an error, FALSE otherwise;
3164 * Return a pointer that holds the info. Null when there is an error.
3165 */
3166 void *
3167eval_for_line(arg, errp, nextcmdp, skip)
3168 char_u *arg;
3169 int *errp;
3170 char_u **nextcmdp;
3171 int skip;
3172{
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 typval_T tv;
3176 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177
3178 *errp = TRUE; /* default: there is an error */
3179
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 if (fi == NULL)
3182 return NULL;
3183
3184 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3185 if (expr == NULL)
3186 return fi;
3187
3188 expr = skipwhite(expr);
3189 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3190 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003191 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 return fi;
3193 }
3194
3195 if (skip)
3196 ++emsg_skip;
3197 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3198 {
3199 *errp = FALSE;
3200 if (!skip)
3201 {
3202 l = tv.vval.v_list;
3203 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003206 clear_tv(&tv);
3207 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 else
3209 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003210 /* No need to increment the refcount, it's already set for the
3211 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 fi->fi_list = l;
3213 list_add_watch(l, &fi->fi_lw);
3214 fi->fi_lw.lw_item = l->lv_first;
3215 }
3216 }
3217 }
3218 if (skip)
3219 --emsg_skip;
3220
3221 return fi;
3222}
3223
3224/*
3225 * Use the first item in a ":for" list. Advance to the next.
3226 * Assign the values to the variable (list). "arg" points to the first one.
3227 * Return TRUE when a valid item was found, FALSE when at end of list or
3228 * something wrong.
3229 */
3230 int
3231next_for_item(fi_void, arg)
3232 void *fi_void;
3233 char_u *arg;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238
3239 item = fi->fi_lw.lw_item;
3240 if (item == NULL)
3241 result = FALSE;
3242 else
3243 {
3244 fi->fi_lw.lw_item = item->li_next;
3245 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3246 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3247 }
3248 return result;
3249}
3250
3251/*
3252 * Free the structure used to store info used by ":for".
3253 */
3254 void
3255free_for_info(fi_void)
3256 void *fi_void;
3257{
Bram Moolenaar33570922005-01-25 22:26:29 +00003258 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003260 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003263 list_unref(fi->fi_list);
3264 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 vim_free(fi);
3266}
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3269
3270 void
3271set_context_for_expression(xp, arg, cmdidx)
3272 expand_T *xp;
3273 char_u *arg;
3274 cmdidx_T cmdidx;
3275{
3276 int got_eq = FALSE;
3277 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 if (cmdidx == CMD_let)
3281 {
3282 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003283 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 {
3285 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003286 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 {
3288 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 if (vim_iswhite(*p))
3291 break;
3292 }
3293 return;
3294 }
3295 }
3296 else
3297 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3298 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 while ((xp->xp_pattern = vim_strpbrk(arg,
3300 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3301 {
3302 c = *xp->xp_pattern;
3303 if (c == '&')
3304 {
3305 c = xp->xp_pattern[1];
3306 if (c == '&')
3307 {
3308 ++xp->xp_pattern;
3309 xp->xp_context = cmdidx != CMD_let || got_eq
3310 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3311 }
3312 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003315 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3316 xp->xp_pattern += 2;
3317
3318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 else if (c == '$')
3321 {
3322 /* environment variable */
3323 xp->xp_context = EXPAND_ENV_VARS;
3324 }
3325 else if (c == '=')
3326 {
3327 got_eq = TRUE;
3328 xp->xp_context = EXPAND_EXPRESSION;
3329 }
3330 else if (c == '<'
3331 && xp->xp_context == EXPAND_FUNCTIONS
3332 && vim_strchr(xp->xp_pattern, '(') == NULL)
3333 {
3334 /* Function name can start with "<SNR>" */
3335 break;
3336 }
3337 else if (cmdidx != CMD_let || got_eq)
3338 {
3339 if (c == '"') /* string */
3340 {
3341 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3342 if (c == '\\' && xp->xp_pattern[1] != NUL)
3343 ++xp->xp_pattern;
3344 xp->xp_context = EXPAND_NOTHING;
3345 }
3346 else if (c == '\'') /* literal string */
3347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3350 /* skip */ ;
3351 xp->xp_context = EXPAND_NOTHING;
3352 }
3353 else if (c == '|')
3354 {
3355 if (xp->xp_pattern[1] == '|')
3356 {
3357 ++xp->xp_pattern;
3358 xp->xp_context = EXPAND_EXPRESSION;
3359 }
3360 else
3361 xp->xp_context = EXPAND_COMMANDS;
3362 }
3363 else
3364 xp->xp_context = EXPAND_EXPRESSION;
3365 }
3366 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 /* Doesn't look like something valid, expand as an expression
3368 * anyway. */
3369 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 arg = xp->xp_pattern;
3371 if (*arg != NUL)
3372 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3373 /* skip */ ;
3374 }
3375 xp->xp_pattern = arg;
3376}
3377
3378#endif /* FEAT_CMDL_COMPL */
3379
3380/*
3381 * ":1,25call func(arg1, arg2)" function call.
3382 */
3383 void
3384ex_call(eap)
3385 exarg_T *eap;
3386{
3387 char_u *arg = eap->arg;
3388 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003390 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 linenr_T lnum;
3394 int doesrange;
3395 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003396 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003398 if (eap->skip)
3399 {
3400 /* trans_function_name() doesn't work well when skipping, use eval0()
3401 * instead to skip to any following command, e.g. for:
3402 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003403 ++emsg_skip;
3404 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3405 clear_tv(&rettv);
3406 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003407 return;
3408 }
3409
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003410 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003411 if (fudi.fd_newkey != NULL)
3412 {
3413 /* Still need to give an error message for missing key. */
3414 EMSG2(_(e_dictkey), fudi.fd_newkey);
3415 vim_free(fudi.fd_newkey);
3416 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003417 if (tofree == NULL)
3418 return;
3419
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003420 /* Increase refcount on dictionary, it could get deleted when evaluating
3421 * the arguments. */
3422 if (fudi.fd_dict != NULL)
3423 ++fudi.fd_dict->dv_refcount;
3424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003426 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003427 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar532c7802005-01-27 14:44:31 +00003429 /* Skip white space to allow ":call func ()". Not good, but required for
3430 * backward compatibility. */
3431 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003432 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 if (*startarg != '(')
3435 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003436 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 goto end;
3438 }
3439
3440 /*
3441 * When skipping, evaluate the function once, to find the end of the
3442 * arguments.
3443 * When the function takes a range, this is discovered after the first
3444 * call, and the loop is broken.
3445 */
3446 if (eap->skip)
3447 {
3448 ++emsg_skip;
3449 lnum = eap->line2; /* do it once, also with an invalid range */
3450 }
3451 else
3452 lnum = eap->line1;
3453 for ( ; lnum <= eap->line2; ++lnum)
3454 {
3455 if (!eap->skip && eap->addr_count > 0)
3456 {
3457 curwin->w_cursor.lnum = lnum;
3458 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003459#ifdef FEAT_VIRTUALEDIT
3460 curwin->w_cursor.coladd = 0;
3461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
3463 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003464 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003465 eap->line1, eap->line2, &doesrange,
3466 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 failed = TRUE;
3469 break;
3470 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003471
3472 /* Handle a function returning a Funcref, Dictionary or List. */
3473 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3474 {
3475 failed = TRUE;
3476 break;
3477 }
3478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003479 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (doesrange || eap->skip)
3481 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003484 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003486 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (aborting())
3488 break;
3489 }
3490 if (eap->skip)
3491 --emsg_skip;
3492
3493 if (!failed)
3494 {
3495 /* Check for trailing illegal characters and a following command. */
3496 if (!ends_excmd(*arg))
3497 {
3498 emsg_severe = TRUE;
3499 EMSG(_(e_trailing));
3500 }
3501 else
3502 eap->nextcmd = check_nextcmd(arg);
3503 }
3504
3505end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003506 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003507 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508}
3509
3510/*
3511 * ":unlet[!] var1 ... " command.
3512 */
3513 void
3514ex_unlet(eap)
3515 exarg_T *eap;
3516{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003517 ex_unletlock(eap, eap->arg, 0);
3518}
3519
3520/*
3521 * ":lockvar" and ":unlockvar" commands
3522 */
3523 void
3524ex_lockvar(eap)
3525 exarg_T *eap;
3526{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003528 int deep = 2;
3529
3530 if (eap->forceit)
3531 deep = -1;
3532 else if (vim_isdigit(*arg))
3533 {
3534 deep = getdigits(&arg);
3535 arg = skipwhite(arg);
3536 }
3537
3538 ex_unletlock(eap, arg, deep);
3539}
3540
3541/*
3542 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3543 */
3544 static void
3545ex_unletlock(eap, argstart, deep)
3546 exarg_T *eap;
3547 char_u *argstart;
3548 int deep;
3549{
3550 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003553 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
3555 do
3556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003558 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3559 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 if (lv.ll_name == NULL)
3561 error = TRUE; /* error but continue parsing */
3562 if (name_end == NULL || (!vim_iswhite(*name_end)
3563 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 if (name_end != NULL)
3566 {
3567 emsg_severe = TRUE;
3568 EMSG(_(e_trailing));
3569 }
3570 if (!(eap->skip || error))
3571 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 break;
3573 }
3574
3575 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 {
3577 if (eap->cmdidx == CMD_unlet)
3578 {
3579 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3580 error = TRUE;
3581 }
3582 else
3583 {
3584 if (do_lock_var(&lv, name_end, deep,
3585 eap->cmdidx == CMD_lockvar) == FAIL)
3586 error = TRUE;
3587 }
3588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003590 if (!eap->skip)
3591 clear_lval(&lv);
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 arg = skipwhite(name_end);
3594 } while (!ends_excmd(*arg));
3595
3596 eap->nextcmd = check_nextcmd(arg);
3597}
3598
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003601 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 int forceit;
3604{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003605 int ret = OK;
3606 int cc;
3607
3608 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 cc = *name_end;
3611 *name_end = NUL;
3612
3613 /* Normal name or expanded name. */
3614 if (check_changedtick(lp->ll_name))
3615 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003619 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3621 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 else if (lp->ll_range)
3623 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625
3626 /* Delete a range of List items. */
3627 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3628 {
3629 li = lp->ll_li->li_next;
3630 listitem_remove(lp->ll_list, lp->ll_li);
3631 lp->ll_li = li;
3632 ++lp->ll_n1;
3633 }
3634 }
3635 else
3636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 /* unlet a List item. */
3639 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003642 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 }
3644
3645 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648/*
3649 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
3652 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 hashtab_T *ht;
3658 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003659 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003660 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 ht = find_var_ht(name, &varname);
3663 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 hi = hash_find(ht, varname);
3666 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003668 di = HI2DI(hi);
3669 if (var_check_fixed(di->di_flags, name)
3670 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671 return FAIL;
3672 delete_var(ht, hi);
3673 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 if (forceit)
3677 return OK;
3678 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FAIL;
3680}
3681
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682/*
3683 * Lock or unlock variable indicated by "lp".
3684 * "deep" is the levels to go (-1 for unlimited);
3685 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3686 */
3687 static int
3688do_lock_var(lp, name_end, deep, lock)
3689 lval_T *lp;
3690 char_u *name_end;
3691 int deep;
3692 int lock;
3693{
3694 int ret = OK;
3695 int cc;
3696 dictitem_T *di;
3697
3698 if (deep == 0) /* nothing to do */
3699 return OK;
3700
3701 if (lp->ll_tv == NULL)
3702 {
3703 cc = *name_end;
3704 *name_end = NUL;
3705
3706 /* Normal name or expanded name. */
3707 if (check_changedtick(lp->ll_name))
3708 ret = FAIL;
3709 else
3710 {
3711 di = find_var(lp->ll_name, NULL);
3712 if (di == NULL)
3713 ret = FAIL;
3714 else
3715 {
3716 if (lock)
3717 di->di_flags |= DI_FLAGS_LOCK;
3718 else
3719 di->di_flags &= ~DI_FLAGS_LOCK;
3720 item_lock(&di->di_tv, deep, lock);
3721 }
3722 }
3723 *name_end = cc;
3724 }
3725 else if (lp->ll_range)
3726 {
3727 listitem_T *li = lp->ll_li;
3728
3729 /* (un)lock a range of List items. */
3730 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3731 {
3732 item_lock(&li->li_tv, deep, lock);
3733 li = li->li_next;
3734 ++lp->ll_n1;
3735 }
3736 }
3737 else if (lp->ll_list != NULL)
3738 /* (un)lock a List item. */
3739 item_lock(&lp->ll_li->li_tv, deep, lock);
3740 else
3741 /* un(lock) a Dictionary item. */
3742 item_lock(&lp->ll_di->di_tv, deep, lock);
3743
3744 return ret;
3745}
3746
3747/*
3748 * Lock or unlock an item. "deep" is nr of levels to go.
3749 */
3750 static void
3751item_lock(tv, deep, lock)
3752 typval_T *tv;
3753 int deep;
3754 int lock;
3755{
3756 static int recurse = 0;
3757 list_T *l;
3758 listitem_T *li;
3759 dict_T *d;
3760 hashitem_T *hi;
3761 int todo;
3762
3763 if (recurse >= DICT_MAXNEST)
3764 {
3765 EMSG(_("E743: variable nested too deep for (un)lock"));
3766 return;
3767 }
3768 if (deep == 0)
3769 return;
3770 ++recurse;
3771
3772 /* lock/unlock the item itself */
3773 if (lock)
3774 tv->v_lock |= VAR_LOCKED;
3775 else
3776 tv->v_lock &= ~VAR_LOCKED;
3777
3778 switch (tv->v_type)
3779 {
3780 case VAR_LIST:
3781 if ((l = tv->vval.v_list) != NULL)
3782 {
3783 if (lock)
3784 l->lv_lock |= VAR_LOCKED;
3785 else
3786 l->lv_lock &= ~VAR_LOCKED;
3787 if (deep < 0 || deep > 1)
3788 /* recursive: lock/unlock the items the List contains */
3789 for (li = l->lv_first; li != NULL; li = li->li_next)
3790 item_lock(&li->li_tv, deep - 1, lock);
3791 }
3792 break;
3793 case VAR_DICT:
3794 if ((d = tv->vval.v_dict) != NULL)
3795 {
3796 if (lock)
3797 d->dv_lock |= VAR_LOCKED;
3798 else
3799 d->dv_lock &= ~VAR_LOCKED;
3800 if (deep < 0 || deep > 1)
3801 {
3802 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003803 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3805 {
3806 if (!HASHITEM_EMPTY(hi))
3807 {
3808 --todo;
3809 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3810 }
3811 }
3812 }
3813 }
3814 }
3815 --recurse;
3816}
3817
Bram Moolenaara40058a2005-07-11 22:42:07 +00003818/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003819 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3820 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003821 */
3822 static int
3823tv_islocked(tv)
3824 typval_T *tv;
3825{
3826 return (tv->v_lock & VAR_LOCKED)
3827 || (tv->v_type == VAR_LIST
3828 && tv->vval.v_list != NULL
3829 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3830 || (tv->v_type == VAR_DICT
3831 && tv->vval.v_dict != NULL
3832 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3833}
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3836/*
3837 * Delete all "menutrans_" variables.
3838 */
3839 void
3840del_menutrans_vars()
3841{
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003843 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003846 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003848 {
3849 if (!HASHITEM_EMPTY(hi))
3850 {
3851 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3853 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 }
3855 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857}
3858#endif
3859
3860#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3861
3862/*
3863 * Local string buffer for the next two functions to store a variable name
3864 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3865 * get_user_var_name().
3866 */
3867
3868static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3869
3870static char_u *varnamebuf = NULL;
3871static int varnamebuflen = 0;
3872
3873/*
3874 * Function to concatenate a prefix and a variable name.
3875 */
3876 static char_u *
3877cat_prefix_varname(prefix, name)
3878 int prefix;
3879 char_u *name;
3880{
3881 int len;
3882
3883 len = (int)STRLEN(name) + 3;
3884 if (len > varnamebuflen)
3885 {
3886 vim_free(varnamebuf);
3887 len += 10; /* some additional space */
3888 varnamebuf = alloc(len);
3889 if (varnamebuf == NULL)
3890 {
3891 varnamebuflen = 0;
3892 return NULL;
3893 }
3894 varnamebuflen = len;
3895 }
3896 *varnamebuf = prefix;
3897 varnamebuf[1] = ':';
3898 STRCPY(varnamebuf + 2, name);
3899 return varnamebuf;
3900}
3901
3902/*
3903 * Function given to ExpandGeneric() to obtain the list of user defined
3904 * (global/buffer/window/built-in) variable names.
3905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 char_u *
3907get_user_var_name(xp, idx)
3908 expand_T *xp;
3909 int idx;
3910{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003911 static long_u gdone;
3912 static long_u bdone;
3913 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003914#ifdef FEAT_WINDOWS
3915 static long_u tdone;
3916#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003917 static int vidx;
3918 static hashitem_T *hi;
3919 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003922 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003924#ifdef FEAT_WINDOWS
3925 tdone = 0;
3926#endif
3927 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003928
3929 /* Global variables */
3930 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 else
3935 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003936 while (HASHITEM_EMPTY(hi))
3937 ++hi;
3938 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3939 return cat_prefix_varname('g', hi->hi_key);
3940 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942
3943 /* b: variables */
3944 ht = &curbuf->b_vars.dv_hashtab;
3945 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003949 else
3950 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 while (HASHITEM_EMPTY(hi))
3952 ++hi;
3953 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003957 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 return (char_u *)"b:changedtick";
3959 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003960
3961 /* w: variables */
3962 ht = &curwin->w_vars.dv_hashtab;
3963 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003965 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003967 else
3968 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003969 while (HASHITEM_EMPTY(hi))
3970 ++hi;
3971 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003973
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974#ifdef FEAT_WINDOWS
3975 /* t: variables */
3976 ht = &curtab->tp_vars.dv_hashtab;
3977 if (tdone < ht->ht_used)
3978 {
3979 if (tdone++ == 0)
3980 hi = ht->ht_array;
3981 else
3982 ++hi;
3983 while (HASHITEM_EMPTY(hi))
3984 ++hi;
3985 return cat_prefix_varname('t', hi->hi_key);
3986 }
3987#endif
3988
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 /* v: variables */
3990 if (vidx < VV_LEN)
3991 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 vim_free(varnamebuf);
3994 varnamebuf = NULL;
3995 varnamebuflen = 0;
3996 return NULL;
3997}
3998
3999#endif /* FEAT_CMDL_COMPL */
4000
4001/*
4002 * types for expressions.
4003 */
4004typedef enum
4005{
4006 TYPE_UNKNOWN = 0
4007 , TYPE_EQUAL /* == */
4008 , TYPE_NEQUAL /* != */
4009 , TYPE_GREATER /* > */
4010 , TYPE_GEQUAL /* >= */
4011 , TYPE_SMALLER /* < */
4012 , TYPE_SEQUAL /* <= */
4013 , TYPE_MATCH /* =~ */
4014 , TYPE_NOMATCH /* !~ */
4015} exptype_T;
4016
4017/*
4018 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4021 */
4022
4023/*
4024 * Handle zero level expression.
4025 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004027 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * Return OK or FAIL.
4029 */
4030 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 char_u **nextcmd;
4035 int evaluate;
4036{
4037 int ret;
4038 char_u *p;
4039
4040 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (ret == FAIL || !ends_excmd(*p))
4043 {
4044 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 /*
4047 * Report the invalid expression unless the expression evaluation has
4048 * been cancelled due to an aborting error, an interrupt, or an
4049 * exception.
4050 */
4051 if (!aborting())
4052 EMSG2(_(e_invexpr2), arg);
4053 ret = FAIL;
4054 }
4055 if (nextcmd != NULL)
4056 *nextcmd = check_nextcmd(p);
4057
4058 return ret;
4059}
4060
4061/*
4062 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004063 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 *
4065 * "arg" must point to the first non-white of the expression.
4066 * "arg" is advanced to the next non-white after the recognized expression.
4067 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004068 * Note: "rettv.v_lock" is not set.
4069 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * Return OK or FAIL.
4071 */
4072 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 int evaluate;
4077{
4078 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 /*
4082 * Get the first variable.
4083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 return FAIL;
4086
4087 if ((*arg)[0] == '?')
4088 {
4089 result = FALSE;
4090 if (evaluate)
4091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 int error = FALSE;
4093
4094 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004097 if (error)
4098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100
4101 /*
4102 * Get the second variable.
4103 */
4104 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 return FAIL;
4107
4108 /*
4109 * Check for the ":".
4110 */
4111 if ((*arg)[0] != ':')
4112 {
4113 EMSG(_("E109: Missing ':' after '?'"));
4114 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 return FAIL;
4117 }
4118
4119 /*
4120 * Get the third variable.
4121 */
4122 *arg = skipwhite(*arg + 1);
4123 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4124 {
4125 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 return FAIL;
4128 }
4129 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132
4133 return OK;
4134}
4135
4136/*
4137 * Handle first level expression:
4138 * expr2 || expr2 || expr2 logical OR
4139 *
4140 * "arg" must point to the first non-white of the expression.
4141 * "arg" is advanced to the next non-white after the recognized expression.
4142 *
4143 * Return OK or FAIL.
4144 */
4145 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int evaluate;
4150{
Bram Moolenaar33570922005-01-25 22:26:29 +00004151 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 long result;
4153 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /*
4157 * Get the first variable.
4158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return FAIL;
4161
4162 /*
4163 * Repeat until there is no following "||".
4164 */
4165 first = TRUE;
4166 result = FALSE;
4167 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4168 {
4169 if (evaluate && first)
4170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174 if (error)
4175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 first = FALSE;
4177 }
4178
4179 /*
4180 * Get the second variable.
4181 */
4182 *arg = skipwhite(*arg + 2);
4183 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4184 return FAIL;
4185
4186 /*
4187 * Compute the result.
4188 */
4189 if (evaluate && !result)
4190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 if (error)
4195 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 if (evaluate)
4198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 rettv->v_type = VAR_NUMBER;
4200 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 }
4203
4204 return OK;
4205}
4206
4207/*
4208 * Handle second level expression:
4209 * expr3 && expr3 && expr3 logical AND
4210 *
4211 * "arg" must point to the first non-white of the expression.
4212 * "arg" is advanced to the next non-white after the recognized expression.
4213 *
4214 * Return OK or FAIL.
4215 */
4216 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 int evaluate;
4221{
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 long result;
4224 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 /*
4228 * Get the first variable.
4229 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return FAIL;
4232
4233 /*
4234 * Repeat until there is no following "&&".
4235 */
4236 first = TRUE;
4237 result = TRUE;
4238 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4239 {
4240 if (evaluate && first)
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (error)
4246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 first = FALSE;
4248 }
4249
4250 /*
4251 * Get the second variable.
4252 */
4253 *arg = skipwhite(*arg + 2);
4254 if (eval4(arg, &var2, evaluate && result) == FAIL)
4255 return FAIL;
4256
4257 /*
4258 * Compute the result.
4259 */
4260 if (evaluate && result)
4261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (error)
4266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 if (evaluate)
4269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 rettv->v_type = VAR_NUMBER;
4271 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
4274
4275 return OK;
4276}
4277
4278/*
4279 * Handle third level expression:
4280 * var1 == var2
4281 * var1 =~ var2
4282 * var1 != var2
4283 * var1 !~ var2
4284 * var1 > var2
4285 * var1 >= var2
4286 * var1 < var2
4287 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004288 * var1 is var2
4289 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int evaluate;
4301{
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 char_u *p;
4304 int i;
4305 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004306 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int len = 2;
4308 long n1, n2;
4309 char_u *s1, *s2;
4310 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4311 regmatch_T regmatch;
4312 int ic;
4313 char_u *save_cpo;
4314
4315 /*
4316 * Get the first variable.
4317 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320
4321 p = *arg;
4322 switch (p[0])
4323 {
4324 case '=': if (p[1] == '=')
4325 type = TYPE_EQUAL;
4326 else if (p[1] == '~')
4327 type = TYPE_MATCH;
4328 break;
4329 case '!': if (p[1] == '=')
4330 type = TYPE_NEQUAL;
4331 else if (p[1] == '~')
4332 type = TYPE_NOMATCH;
4333 break;
4334 case '>': if (p[1] != '=')
4335 {
4336 type = TYPE_GREATER;
4337 len = 1;
4338 }
4339 else
4340 type = TYPE_GEQUAL;
4341 break;
4342 case '<': if (p[1] != '=')
4343 {
4344 type = TYPE_SMALLER;
4345 len = 1;
4346 }
4347 else
4348 type = TYPE_SEQUAL;
4349 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004350 case 'i': if (p[1] == 's')
4351 {
4352 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4353 len = 5;
4354 if (!vim_isIDc(p[len]))
4355 {
4356 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4357 type_is = TRUE;
4358 }
4359 }
4360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362
4363 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004364 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 */
4366 if (type != TYPE_UNKNOWN)
4367 {
4368 /* extra question mark appended: ignore case */
4369 if (p[len] == '?')
4370 {
4371 ic = TRUE;
4372 ++len;
4373 }
4374 /* extra '#' appended: match case */
4375 else if (p[len] == '#')
4376 {
4377 ic = FALSE;
4378 ++len;
4379 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004380 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 else
4382 ic = p_ic;
4383
4384 /*
4385 * Get the second variable.
4386 */
4387 *arg = skipwhite(p + len);
4388 if (eval5(arg, &var2, evaluate) == FAIL)
4389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return FAIL;
4392 }
4393
4394 if (evaluate)
4395 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 if (type_is && rettv->v_type != var2.v_type)
4397 {
4398 /* For "is" a different type always means FALSE, for "notis"
4399 * it means TRUE. */
4400 n1 = (type == TYPE_NEQUAL);
4401 }
4402 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4403 {
4404 if (type_is)
4405 {
4406 n1 = (rettv->v_type == var2.v_type
4407 && rettv->vval.v_list == var2.vval.v_list);
4408 if (type == TYPE_NEQUAL)
4409 n1 = !n1;
4410 }
4411 else if (rettv->v_type != var2.v_type
4412 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4413 {
4414 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004415 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004416 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004417 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 clear_tv(rettv);
4419 clear_tv(&var2);
4420 return FAIL;
4421 }
4422 else
4423 {
4424 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004425 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4426 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004427 if (type == TYPE_NEQUAL)
4428 n1 = !n1;
4429 }
4430 }
4431
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004432 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4433 {
4434 if (type_is)
4435 {
4436 n1 = (rettv->v_type == var2.v_type
4437 && rettv->vval.v_dict == var2.vval.v_dict);
4438 if (type == TYPE_NEQUAL)
4439 n1 = !n1;
4440 }
4441 else if (rettv->v_type != var2.v_type
4442 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4443 {
4444 if (rettv->v_type != var2.v_type)
4445 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4446 else
4447 EMSG(_("E736: Invalid operation for Dictionary"));
4448 clear_tv(rettv);
4449 clear_tv(&var2);
4450 return FAIL;
4451 }
4452 else
4453 {
4454 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004455 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4456 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004457 if (type == TYPE_NEQUAL)
4458 n1 = !n1;
4459 }
4460 }
4461
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4463 {
4464 if (rettv->v_type != var2.v_type
4465 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4466 {
4467 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004468 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004470 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 clear_tv(rettv);
4472 clear_tv(&var2);
4473 return FAIL;
4474 }
4475 else
4476 {
4477 /* Compare two Funcrefs for being equal or unequal. */
4478 if (rettv->vval.v_string == NULL
4479 || var2.vval.v_string == NULL)
4480 n1 = FALSE;
4481 else
4482 n1 = STRCMP(rettv->vval.v_string,
4483 var2.vval.v_string) == 0;
4484 if (type == TYPE_NEQUAL)
4485 n1 = !n1;
4486 }
4487 }
4488
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004489#ifdef FEAT_FLOAT
4490 /*
4491 * If one of the two variables is a float, compare as a float.
4492 * When using "=~" or "!~", always compare as string.
4493 */
4494 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4495 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4496 {
4497 float_T f1, f2;
4498
4499 if (rettv->v_type == VAR_FLOAT)
4500 f1 = rettv->vval.v_float;
4501 else
4502 f1 = get_tv_number(rettv);
4503 if (var2.v_type == VAR_FLOAT)
4504 f2 = var2.vval.v_float;
4505 else
4506 f2 = get_tv_number(&var2);
4507 n1 = FALSE;
4508 switch (type)
4509 {
4510 case TYPE_EQUAL: n1 = (f1 == f2); break;
4511 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4512 case TYPE_GREATER: n1 = (f1 > f2); break;
4513 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4514 case TYPE_SMALLER: n1 = (f1 < f2); break;
4515 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4516 case TYPE_UNKNOWN:
4517 case TYPE_MATCH:
4518 case TYPE_NOMATCH: break; /* avoid gcc warning */
4519 }
4520 }
4521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /*
4524 * If one of the two variables is a number, compare as a number.
4525 * When using "=~" or "!~", always compare as string.
4526 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004530 n1 = get_tv_number(rettv);
4531 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 switch (type)
4533 {
4534 case TYPE_EQUAL: n1 = (n1 == n2); break;
4535 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4536 case TYPE_GREATER: n1 = (n1 > n2); break;
4537 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4538 case TYPE_SMALLER: n1 = (n1 < n2); break;
4539 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4540 case TYPE_UNKNOWN:
4541 case TYPE_MATCH:
4542 case TYPE_NOMATCH: break; /* avoid gcc warning */
4543 }
4544 }
4545 else
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 s1 = get_tv_string_buf(rettv, buf1);
4548 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4550 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4551 else
4552 i = 0;
4553 n1 = FALSE;
4554 switch (type)
4555 {
4556 case TYPE_EQUAL: n1 = (i == 0); break;
4557 case TYPE_NEQUAL: n1 = (i != 0); break;
4558 case TYPE_GREATER: n1 = (i > 0); break;
4559 case TYPE_GEQUAL: n1 = (i >= 0); break;
4560 case TYPE_SMALLER: n1 = (i < 0); break;
4561 case TYPE_SEQUAL: n1 = (i <= 0); break;
4562
4563 case TYPE_MATCH:
4564 case TYPE_NOMATCH:
4565 /* avoid 'l' flag in 'cpoptions' */
4566 save_cpo = p_cpo;
4567 p_cpo = (char_u *)"";
4568 regmatch.regprog = vim_regcomp(s2,
4569 RE_MAGIC + RE_STRING);
4570 regmatch.rm_ic = ic;
4571 if (regmatch.regprog != NULL)
4572 {
4573 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4574 vim_free(regmatch.regprog);
4575 if (type == TYPE_NOMATCH)
4576 n1 = !n1;
4577 }
4578 p_cpo = save_cpo;
4579 break;
4580
4581 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4582 }
4583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 clear_tv(rettv);
4585 clear_tv(&var2);
4586 rettv->v_type = VAR_NUMBER;
4587 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589 }
4590
4591 return OK;
4592}
4593
4594/*
4595 * Handle fourth level expression:
4596 * + number addition
4597 * - number subtraction
4598 * . string concatenation
4599 *
4600 * "arg" must point to the first non-white of the expression.
4601 * "arg" is advanced to the next non-white after the recognized expression.
4602 *
4603 * Return OK or FAIL.
4604 */
4605 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004606eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 int evaluate;
4610{
Bram Moolenaar33570922005-01-25 22:26:29 +00004611 typval_T var2;
4612 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 int op;
4614 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004615#ifdef FEAT_FLOAT
4616 float_T f1 = 0, f2 = 0;
4617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 char_u *s1, *s2;
4619 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4620 char_u *p;
4621
4622 /*
4623 * Get the first variable.
4624 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004625 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 return FAIL;
4627
4628 /*
4629 * Repeat computing, until no '+', '-' or '.' is following.
4630 */
4631 for (;;)
4632 {
4633 op = **arg;
4634 if (op != '+' && op != '-' && op != '.')
4635 break;
4636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004637 if ((op != '+' || rettv->v_type != VAR_LIST)
4638#ifdef FEAT_FLOAT
4639 && (op == '.' || rettv->v_type != VAR_FLOAT)
4640#endif
4641 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004642 {
4643 /* For "list + ...", an illegal use of the first operand as
4644 * a number cannot be determined before evaluating the 2nd
4645 * operand: if this is also a list, all is ok.
4646 * For "something . ...", "something - ..." or "non-list + ...",
4647 * we know that the first operand needs to be a string or number
4648 * without evaluating the 2nd operand. So check before to avoid
4649 * side effects after an error. */
4650 if (evaluate && get_tv_string_chk(rettv) == NULL)
4651 {
4652 clear_tv(rettv);
4653 return FAIL;
4654 }
4655 }
4656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 /*
4658 * Get the second variable.
4659 */
4660 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004661 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 return FAIL;
4665 }
4666
4667 if (evaluate)
4668 {
4669 /*
4670 * Compute the result.
4671 */
4672 if (op == '.')
4673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4675 s2 = get_tv_string_buf_chk(&var2, buf2);
4676 if (s2 == NULL) /* type error ? */
4677 {
4678 clear_tv(rettv);
4679 clear_tv(&var2);
4680 return FAIL;
4681 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004682 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 clear_tv(rettv);
4684 rettv->v_type = VAR_STRING;
4685 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004687 else if (op == '+' && rettv->v_type == VAR_LIST
4688 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004689 {
4690 /* concatenate Lists */
4691 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4692 &var3) == FAIL)
4693 {
4694 clear_tv(rettv);
4695 clear_tv(&var2);
4696 return FAIL;
4697 }
4698 clear_tv(rettv);
4699 *rettv = var3;
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 else
4702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 int error = FALSE;
4704
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705#ifdef FEAT_FLOAT
4706 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708 f1 = rettv->vval.v_float;
4709 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 else
4712#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714 n1 = get_tv_number_chk(rettv, &error);
4715 if (error)
4716 {
4717 /* This can only happen for "list + non-list". For
4718 * "non-list + ..." or "something - ...", we returned
4719 * before evaluating the 2nd operand. */
4720 clear_tv(rettv);
4721 return FAIL;
4722 }
4723#ifdef FEAT_FLOAT
4724 if (var2.v_type == VAR_FLOAT)
4725 f1 = n1;
4726#endif
4727 }
4728#ifdef FEAT_FLOAT
4729 if (var2.v_type == VAR_FLOAT)
4730 {
4731 f2 = var2.vval.v_float;
4732 n2 = 0;
4733 }
4734 else
4735#endif
4736 {
4737 n2 = get_tv_number_chk(&var2, &error);
4738 if (error)
4739 {
4740 clear_tv(rettv);
4741 clear_tv(&var2);
4742 return FAIL;
4743 }
4744#ifdef FEAT_FLOAT
4745 if (rettv->v_type == VAR_FLOAT)
4746 f2 = n2;
4747#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004750
4751#ifdef FEAT_FLOAT
4752 /* If there is a float on either side the result is a float. */
4753 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4754 {
4755 if (op == '+')
4756 f1 = f1 + f2;
4757 else
4758 f1 = f1 - f2;
4759 rettv->v_type = VAR_FLOAT;
4760 rettv->vval.v_float = f1;
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004763#endif
4764 {
4765 if (op == '+')
4766 n1 = n1 + n2;
4767 else
4768 n1 = n1 - n2;
4769 rettv->v_type = VAR_NUMBER;
4770 rettv->vval.v_number = n1;
4771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775 }
4776 return OK;
4777}
4778
4779/*
4780 * Handle fifth level expression:
4781 * * number multiplication
4782 * / number division
4783 * % number modulo
4784 *
4785 * "arg" must point to the first non-white of the expression.
4786 * "arg" is advanced to the next non-white after the recognized expression.
4787 *
4788 * Return OK or FAIL.
4789 */
4790 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004795 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
Bram Moolenaar33570922005-01-25 22:26:29 +00004797 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 int op;
4799 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800#ifdef FEAT_FLOAT
4801 int use_float = FALSE;
4802 float_T f1 = 0, f2;
4803#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806 /*
4807 * Get the first variable.
4808 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004809 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 return FAIL;
4811
4812 /*
4813 * Repeat computing, until no '*', '/' or '%' is following.
4814 */
4815 for (;;)
4816 {
4817 op = **arg;
4818 if (op != '*' && op != '/' && op != '%')
4819 break;
4820
4821 if (evaluate)
4822 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823#ifdef FEAT_FLOAT
4824 if (rettv->v_type == VAR_FLOAT)
4825 {
4826 f1 = rettv->vval.v_float;
4827 use_float = TRUE;
4828 n1 = 0;
4829 }
4830 else
4831#endif
4832 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 if (error)
4835 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 else
4838 n1 = 0;
4839
4840 /*
4841 * Get the second variable.
4842 */
4843 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004844 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 return FAIL;
4846
4847 if (evaluate)
4848 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004849#ifdef FEAT_FLOAT
4850 if (var2.v_type == VAR_FLOAT)
4851 {
4852 if (!use_float)
4853 {
4854 f1 = n1;
4855 use_float = TRUE;
4856 }
4857 f2 = var2.vval.v_float;
4858 n2 = 0;
4859 }
4860 else
4861#endif
4862 {
4863 n2 = get_tv_number_chk(&var2, &error);
4864 clear_tv(&var2);
4865 if (error)
4866 return FAIL;
4867#ifdef FEAT_FLOAT
4868 if (use_float)
4869 f2 = n2;
4870#endif
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 /*
4874 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877#ifdef FEAT_FLOAT
4878 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 if (op == '*')
4881 f1 = f1 * f2;
4882 else if (op == '/')
4883 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004884# ifdef VMS
4885 /* VMS crashes on divide by zero, work around it */
4886 if (f2 == 0.0)
4887 {
4888 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004889 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004891 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004893 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004894 }
4895 else
4896 f1 = f1 / f2;
4897# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 /* We rely on the floating point library to handle divide
4899 * by zero to result in "inf" and not a crash. */
4900 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004905 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 return FAIL;
4907 }
4908 rettv->v_type = VAR_FLOAT;
4909 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914 if (op == '*')
4915 n1 = n1 * n2;
4916 else if (op == '/')
4917 {
4918 if (n2 == 0) /* give an error message? */
4919 {
4920 if (n1 == 0)
4921 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4922 else if (n1 < 0)
4923 n1 = -0x7fffffffL;
4924 else
4925 n1 = 0x7fffffffL;
4926 }
4927 else
4928 n1 = n1 / n2;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 {
4932 if (n2 == 0) /* give an error message? */
4933 n1 = 0;
4934 else
4935 n1 = n1 % n2;
4936 }
4937 rettv->v_type = VAR_NUMBER;
4938 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 }
4942
4943 return OK;
4944}
4945
4946/*
4947 * Handle sixth level expression:
4948 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004949 * "string" string constant
4950 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 * &option-name option value
4952 * @r register contents
4953 * identifier variable value
4954 * function() function call
4955 * $VAR environment variable
4956 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004957 * [expr, expr] List
4958 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 *
4960 * Also handle:
4961 * ! in front logical NOT
4962 * - in front unary minus
4963 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 * trailing [] subscript in String or List
4965 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 *
4967 * "arg" must point to the first non-white of the expression.
4968 * "arg" is advanced to the next non-white after the recognized expression.
4969 *
4970 * Return OK or FAIL.
4971 */
4972 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004973eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004977 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 long n;
4980 int len;
4981 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_u *start_leader, *end_leader;
4983 int ret = OK;
4984 char_u *alias;
4985
4986 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 /*
4993 * Skip '!' and '-' characters. They are handled later.
4994 */
4995 start_leader = *arg;
4996 while (**arg == '!' || **arg == '-' || **arg == '+')
4997 *arg = skipwhite(*arg + 1);
4998 end_leader = *arg;
4999
5000 switch (**arg)
5001 {
5002 /*
5003 * Number constant.
5004 */
5005 case '0':
5006 case '1':
5007 case '2':
5008 case '3':
5009 case '4':
5010 case '5':
5011 case '6':
5012 case '7':
5013 case '8':
5014 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 {
5016#ifdef FEAT_FLOAT
5017 char_u *p = skipdigits(*arg + 1);
5018 int get_float = FALSE;
5019
5020 /* We accept a float when the format matches
5021 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005022 * strict to avoid backwards compatibility problems.
5023 * Don't look for a float after the "." operator, so that
5024 * ":let vers = 1.2.3" doesn't fail. */
5025 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005027 get_float = TRUE;
5028 p = skipdigits(p + 2);
5029 if (*p == 'e' || *p == 'E')
5030 {
5031 ++p;
5032 if (*p == '-' || *p == '+')
5033 ++p;
5034 if (!vim_isdigit(*p))
5035 get_float = FALSE;
5036 else
5037 p = skipdigits(p + 1);
5038 }
5039 if (ASCII_ISALPHA(*p) || *p == '.')
5040 get_float = FALSE;
5041 }
5042 if (get_float)
5043 {
5044 float_T f;
5045
5046 *arg += string2float(*arg, &f);
5047 if (evaluate)
5048 {
5049 rettv->v_type = VAR_FLOAT;
5050 rettv->vval.v_float = f;
5051 }
5052 }
5053 else
5054#endif
5055 {
5056 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5057 *arg += len;
5058 if (evaluate)
5059 {
5060 rettv->v_type = VAR_NUMBER;
5061 rettv->vval.v_number = n;
5062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 /*
5068 * String constant: "string".
5069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 break;
5072
5073 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005077 break;
5078
5079 /*
5080 * List: [expr, expr]
5081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084
5085 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * Dictionary: {key: val, key: val}
5087 */
5088 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5089 break;
5090
5091 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005092 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005094 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096
5097 /*
5098 * Environment variable: $VAR.
5099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 break;
5102
5103 /*
5104 * Register contents: @r.
5105 */
5106 case '@': ++*arg;
5107 if (evaluate)
5108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005110 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112 if (**arg != NUL)
5113 ++*arg;
5114 break;
5115
5116 /*
5117 * nested expression: (expression).
5118 */
5119 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (**arg == ')')
5122 ++*arg;
5123 else if (ret == OK)
5124 {
5125 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 ret = FAIL;
5128 }
5129 break;
5130
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 break;
5133 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134
5135 if (ret == NOTDONE)
5136 {
5137 /*
5138 * Must be a variable or function name.
5139 * Can also be a curly-braces kind of name: {expr}.
5140 */
5141 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 if (alias != NULL)
5144 s = alias;
5145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005146 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 ret = FAIL;
5148 else
5149 {
5150 if (**arg == '(') /* recursive! */
5151 {
5152 /* If "s" is the name of a variable of type VAR_FUNC
5153 * use its contents. */
5154 s = deref_func_name(s, &len);
5155
5156 /* Invoke the function. */
5157 ret = get_func_tv(s, len, rettv, arg,
5158 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 /* Stop the expression evaluation when immediately
5161 * aborting on error, or when an interrupt occurred or
5162 * an exception was thrown but not caught. */
5163 if (aborting())
5164 {
5165 if (ret == OK)
5166 clear_tv(rettv);
5167 ret = FAIL;
5168 }
5169 }
5170 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005171 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005172 else
5173 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005175 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 }
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 *arg = skipwhite(*arg);
5179
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005180 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5181 * expr(expr). */
5182 if (ret == OK)
5183 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184
5185 /*
5186 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5187 */
5188 if (ret == OK && evaluate && end_leader > start_leader)
5189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005190 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005191 int val = 0;
5192#ifdef FEAT_FLOAT
5193 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005194
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005195 if (rettv->v_type == VAR_FLOAT)
5196 f = rettv->vval.v_float;
5197 else
5198#endif
5199 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 clear_tv(rettv);
5203 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 else
5206 {
5207 while (end_leader > start_leader)
5208 {
5209 --end_leader;
5210 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005211 {
5212#ifdef FEAT_FLOAT
5213 if (rettv->v_type == VAR_FLOAT)
5214 f = !f;
5215 else
5216#endif
5217 val = !val;
5218 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005220 {
5221#ifdef FEAT_FLOAT
5222 if (rettv->v_type == VAR_FLOAT)
5223 f = -f;
5224 else
5225#endif
5226 val = -val;
5227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229#ifdef FEAT_FLOAT
5230 if (rettv->v_type == VAR_FLOAT)
5231 {
5232 clear_tv(rettv);
5233 rettv->vval.v_float = f;
5234 }
5235 else
5236#endif
5237 {
5238 clear_tv(rettv);
5239 rettv->v_type = VAR_NUMBER;
5240 rettv->vval.v_number = val;
5241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244
5245 return ret;
5246}
5247
5248/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005249 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5250 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5252 */
5253 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005254eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005256 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259{
5260 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 long len = -1;
5264 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005268 if (rettv->v_type == VAR_FUNC
5269#ifdef FEAT_FLOAT
5270 || rettv->v_type == VAR_FLOAT
5271#endif
5272 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 if (verbose)
5275 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 return FAIL;
5277 }
5278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 /*
5282 * dict.name
5283 */
5284 key = *arg + 1;
5285 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5286 ;
5287 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 }
5291 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 /*
5294 * something[idx]
5295 *
5296 * Get the (first) variable from inside the [].
5297 */
5298 *arg = skipwhite(*arg + 1);
5299 if (**arg == ':')
5300 empty1 = TRUE;
5301 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5302 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5304 {
5305 /* not a number or string */
5306 clear_tv(&var1);
5307 return FAIL;
5308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309
5310 /*
5311 * Get the second variable from inside the [:].
5312 */
5313 if (**arg == ':')
5314 {
5315 range = TRUE;
5316 *arg = skipwhite(*arg + 1);
5317 if (**arg == ']')
5318 empty2 = TRUE;
5319 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 if (!empty1)
5322 clear_tv(&var1);
5323 return FAIL;
5324 }
5325 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5326 {
5327 /* not a number or string */
5328 if (!empty1)
5329 clear_tv(&var1);
5330 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 return FAIL;
5332 }
5333 }
5334
5335 /* Check for the ']'. */
5336 if (**arg != ']')
5337 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005338 if (verbose)
5339 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 clear_tv(&var1);
5341 if (range)
5342 clear_tv(&var2);
5343 return FAIL;
5344 }
5345 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 }
5347
5348 if (evaluate)
5349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 n1 = 0;
5351 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 n1 = get_tv_number(&var1);
5354 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 }
5356 if (range)
5357 {
5358 if (empty2)
5359 n2 = -1;
5360 else
5361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005362 n2 = get_tv_number(&var2);
5363 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 }
5365 }
5366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 {
5369 case VAR_NUMBER:
5370 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005371 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 len = (long)STRLEN(s);
5373 if (range)
5374 {
5375 /* The resulting variable is a substring. If the indexes
5376 * are out of range the result is empty. */
5377 if (n1 < 0)
5378 {
5379 n1 = len + n1;
5380 if (n1 < 0)
5381 n1 = 0;
5382 }
5383 if (n2 < 0)
5384 n2 = len + n2;
5385 else if (n2 >= len)
5386 n2 = len;
5387 if (n1 >= len || n2 < 0 || n1 > n2)
5388 s = NULL;
5389 else
5390 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5391 }
5392 else
5393 {
5394 /* The resulting variable is a string of a single
5395 * character. If the index is too big or negative the
5396 * result is empty. */
5397 if (n1 >= len || n1 < 0)
5398 s = NULL;
5399 else
5400 s = vim_strnsave(s + n1, 1);
5401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005402 clear_tv(rettv);
5403 rettv->v_type = VAR_STRING;
5404 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 break;
5406
5407 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 if (n1 < 0)
5410 n1 = len + n1;
5411 if (!empty1 && (n1 < 0 || n1 >= len))
5412 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005413 /* For a range we allow invalid values and return an empty
5414 * list. A list index out of range is an error. */
5415 if (!range)
5416 {
5417 if (verbose)
5418 EMSGN(_(e_listidx), n1);
5419 return FAIL;
5420 }
5421 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 if (range)
5424 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 list_T *l;
5426 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427
5428 if (n2 < 0)
5429 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005430 else if (n2 >= len)
5431 n2 = len - 1;
5432 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005433 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 l = list_alloc();
5435 if (l == NULL)
5436 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 n1 <= n2; ++n1)
5439 {
5440 if (list_append_tv(l, &item->li_tv) == FAIL)
5441 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005442 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 return FAIL;
5444 }
5445 item = item->li_next;
5446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 clear_tv(rettv);
5448 rettv->v_type = VAR_LIST;
5449 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005450 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 }
5452 else
5453 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005454 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 clear_tv(rettv);
5456 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459
5460 case VAR_DICT:
5461 if (range)
5462 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005463 if (verbose)
5464 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 if (len == -1)
5466 clear_tv(&var1);
5467 return FAIL;
5468 }
5469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005470 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005471
5472 if (len == -1)
5473 {
5474 key = get_tv_string(&var1);
5475 if (*key == NUL)
5476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005477 if (verbose)
5478 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005479 clear_tv(&var1);
5480 return FAIL;
5481 }
5482 }
5483
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005484 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005485
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005486 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005487 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 if (len == -1)
5489 clear_tv(&var1);
5490 if (item == NULL)
5491 return FAIL;
5492
5493 copy_tv(&item->di_tv, &var1);
5494 clear_tv(rettv);
5495 *rettv = var1;
5496 }
5497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 }
5499 }
5500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 return OK;
5502}
5503
5504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 * Get an option value.
5506 * "arg" points to the '&' or '+' before the option name.
5507 * "arg" is advanced to character after the option name.
5508 * Return OK or FAIL.
5509 */
5510 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005513 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 int evaluate;
5515{
5516 char_u *option_end;
5517 long numval;
5518 char_u *stringval;
5519 int opt_type;
5520 int c;
5521 int working = (**arg == '+'); /* has("+option") */
5522 int ret = OK;
5523 int opt_flags;
5524
5525 /*
5526 * Isolate the option name and find its value.
5527 */
5528 option_end = find_option_end(arg, &opt_flags);
5529 if (option_end == NULL)
5530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 EMSG2(_("E112: Option name missing: %s"), *arg);
5533 return FAIL;
5534 }
5535
5536 if (!evaluate)
5537 {
5538 *arg = option_end;
5539 return OK;
5540 }
5541
5542 c = *option_end;
5543 *option_end = NUL;
5544 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 if (opt_type == -3) /* invalid name */
5548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 EMSG2(_("E113: Unknown option: %s"), *arg);
5551 ret = FAIL;
5552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
5555 if (opt_type == -2) /* hidden string option */
5556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 rettv->v_type = VAR_STRING;
5558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560 else if (opt_type == -1) /* hidden number option */
5561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 rettv->v_type = VAR_NUMBER;
5563 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565 else if (opt_type == 1) /* number option */
5566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_NUMBER;
5568 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else /* string option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_STRING;
5573 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 }
5576 else if (working && (opt_type == -2 || opt_type == -1))
5577 ret = FAIL;
5578
5579 *option_end = c; /* put back for error messages */
5580 *arg = option_end;
5581
5582 return ret;
5583}
5584
5585/*
5586 * Allocate a variable for a string constant.
5587 * Return OK or FAIL.
5588 */
5589 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 int evaluate;
5594{
5595 char_u *p;
5596 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 int extra = 0;
5598
5599 /*
5600 * Find the end of the string, skipping backslashed characters.
5601 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 if (*p == '\\' && p[1] != NUL)
5605 {
5606 ++p;
5607 /* A "\<x>" form occupies at least 4 characters, and produces up
5608 * to 6 characters: reserve space for 2 extra */
5609 if (*p == '<')
5610 extra += 2;
5611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613
5614 if (*p != '"')
5615 {
5616 EMSG2(_("E114: Missing quote: %s"), *arg);
5617 return FAIL;
5618 }
5619
5620 /* If only parsing, set *arg and return here */
5621 if (!evaluate)
5622 {
5623 *arg = p + 1;
5624 return OK;
5625 }
5626
5627 /*
5628 * Copy the string into allocated memory, handling backslashed
5629 * characters.
5630 */
5631 name = alloc((unsigned)(p - *arg + extra));
5632 if (name == NULL)
5633 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 rettv->v_type = VAR_STRING;
5635 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 if (*p == '\\')
5640 {
5641 switch (*++p)
5642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 case 'b': *name++ = BS; ++p; break;
5644 case 'e': *name++ = ESC; ++p; break;
5645 case 'f': *name++ = FF; ++p; break;
5646 case 'n': *name++ = NL; ++p; break;
5647 case 'r': *name++ = CAR; ++p; break;
5648 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 case 'X': /* hex: "\x1", "\x12" */
5651 case 'x':
5652 case 'u': /* Unicode: "\u0023" */
5653 case 'U':
5654 if (vim_isxdigit(p[1]))
5655 {
5656 int n, nr;
5657 int c = toupper(*p);
5658
5659 if (c == 'X')
5660 n = 2;
5661 else
5662 n = 4;
5663 nr = 0;
5664 while (--n >= 0 && vim_isxdigit(p[1]))
5665 {
5666 ++p;
5667 nr = (nr << 4) + hex2nr(*p);
5668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670#ifdef FEAT_MBYTE
5671 /* For "\u" store the number according to
5672 * 'encoding'. */
5673 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 else
5676#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 break;
5680
5681 /* octal: "\1", "\12", "\123" */
5682 case '0':
5683 case '1':
5684 case '2':
5685 case '3':
5686 case '4':
5687 case '5':
5688 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 case '7': *name = *p++ - '0';
5690 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 *name = (*name << 3) + *p++ - '0';
5693 if (*p >= '0' && *p <= '7')
5694 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 break;
5698
5699 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (extra != 0)
5702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 break;
5705 }
5706 /* FALLTHROUGH */
5707
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 break;
5710 }
5711 }
5712 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 *arg = p + 1;
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 return OK;
5720}
5721
5722/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * Return OK or FAIL.
5725 */
5726 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 int evaluate;
5731{
5732 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 int reduce = 0;
5735
5736 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 */
5739 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 break;
5745 ++reduce;
5746 ++p;
5747 }
5748 }
5749
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 return FAIL;
5754 }
5755
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 if (!evaluate)
5758 {
5759 *arg = p + 1;
5760 return OK;
5761 }
5762
5763 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 */
5766 str = alloc((unsigned)((p - *arg) - reduce));
5767 if (str == NULL)
5768 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 rettv->v_type = VAR_STRING;
5770 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 break;
5778 ++p;
5779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 *arg = p + 1;
5784
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 return OK;
5786}
5787
5788/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789 * Allocate a variable for a List and fill it from "*arg".
5790 * Return OK or FAIL.
5791 */
5792 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005793get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005795 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 int evaluate;
5797{
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 list_T *l = NULL;
5799 typval_T tv;
5800 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801
5802 if (evaluate)
5803 {
5804 l = list_alloc();
5805 if (l == NULL)
5806 return FAIL;
5807 }
5808
5809 *arg = skipwhite(*arg + 1);
5810 while (**arg != ']' && **arg != NUL)
5811 {
5812 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5813 goto failret;
5814 if (evaluate)
5815 {
5816 item = listitem_alloc();
5817 if (item != NULL)
5818 {
5819 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005820 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 list_append(l, item);
5822 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005823 else
5824 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 }
5826
5827 if (**arg == ']')
5828 break;
5829 if (**arg != ',')
5830 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 goto failret;
5833 }
5834 *arg = skipwhite(*arg + 1);
5835 }
5836
5837 if (**arg != ']')
5838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840failret:
5841 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005842 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 return FAIL;
5844 }
5845
5846 *arg = skipwhite(*arg + 1);
5847 if (evaluate)
5848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005849 rettv->v_type = VAR_LIST;
5850 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 ++l->lv_refcount;
5852 }
5853
5854 return OK;
5855}
5856
5857/*
5858 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005859 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005861 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862list_alloc()
5863{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005864 list_T *l;
5865
5866 l = (list_T *)alloc_clear(sizeof(list_T));
5867 if (l != NULL)
5868 {
5869 /* Prepend the list to the list of lists for garbage collection. */
5870 if (first_list != NULL)
5871 first_list->lv_used_prev = l;
5872 l->lv_used_prev = NULL;
5873 l->lv_used_next = first_list;
5874 first_list = l;
5875 }
5876 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877}
5878
5879/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005880 * Allocate an empty list for a return value.
5881 * Returns OK or FAIL.
5882 */
5883 static int
5884rettv_list_alloc(rettv)
5885 typval_T *rettv;
5886{
5887 list_T *l = list_alloc();
5888
5889 if (l == NULL)
5890 return FAIL;
5891
5892 rettv->vval.v_list = l;
5893 rettv->v_type = VAR_LIST;
5894 ++l->lv_refcount;
5895 return OK;
5896}
5897
5898/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 * Unreference a list: decrement the reference count and free it when it
5900 * becomes zero.
5901 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005902 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005906 if (l != NULL && --l->lv_refcount <= 0)
5907 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908}
5909
5910/*
5911 * Free a list, including all items it points to.
5912 * Ignores the reference count.
5913 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005914 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005915list_free(l, recurse)
5916 list_T *l;
5917 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918{
Bram Moolenaar33570922005-01-25 22:26:29 +00005919 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005921 /* Remove the list from the list of lists for garbage collection. */
5922 if (l->lv_used_prev == NULL)
5923 first_list = l->lv_used_next;
5924 else
5925 l->lv_used_prev->lv_used_next = l->lv_used_next;
5926 if (l->lv_used_next != NULL)
5927 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5928
Bram Moolenaard9fba312005-06-26 22:34:35 +00005929 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005931 /* Remove the item before deleting it. */
5932 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005933 if (recurse || (item->li_tv.v_type != VAR_LIST
5934 && item->li_tv.v_type != VAR_DICT))
5935 clear_tv(&item->li_tv);
5936 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 }
5938 vim_free(l);
5939}
5940
5941/*
5942 * Allocate a list item.
5943 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005944 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945listitem_alloc()
5946{
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948}
5949
5950/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005951 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 */
5953 static void
5954listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005955 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005957 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 vim_free(item);
5959}
5960
5961/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005962 * Remove a list item from a List and free it. Also clears the value.
5963 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005964 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005965listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 list_T *l;
5967 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005968{
5969 list_remove(l, item, item);
5970 listitem_free(item);
5971}
5972
5973/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 * Get the number of items in a list.
5975 */
5976 static long
5977list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005978 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 if (l == NULL)
5981 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005982 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983}
5984
5985/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 * Return TRUE when two lists have exactly the same values.
5987 */
5988 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005989list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 list_T *l1;
5991 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005992 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005993 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994{
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005997 if (l1 == NULL || l2 == NULL)
5998 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005999 if (l1 == l2)
6000 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006001 if (list_len(l1) != list_len(l2))
6002 return FALSE;
6003
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004 for (item1 = l1->lv_first, item2 = l2->lv_first;
6005 item1 != NULL && item2 != NULL;
6006 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006007 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 return FALSE;
6009 return item1 == NULL && item2 == NULL;
6010}
6011
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006012#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6013 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014/*
6015 * Return the dictitem that an entry in a hashtable points to.
6016 */
6017 dictitem_T *
6018dict_lookup(hi)
6019 hashitem_T *hi;
6020{
6021 return HI2DI(hi);
6022}
6023#endif
6024
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006026 * Return TRUE when two dictionaries have exactly the same key/values.
6027 */
6028 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 dict_T *d1;
6031 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006032 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006033 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 hashitem_T *hi;
6036 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006037 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006038
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006039 if (d1 == NULL || d2 == NULL)
6040 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006041 if (d1 == d2)
6042 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 if (dict_len(d1) != dict_len(d2))
6044 return FALSE;
6045
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006046 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006049 if (!HASHITEM_EMPTY(hi))
6050 {
6051 item2 = dict_find(d2, hi->hi_key, -1);
6052 if (item2 == NULL)
6053 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006054 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006055 return FALSE;
6056 --todo;
6057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 }
6059 return TRUE;
6060}
6061
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006062static int tv_equal_recurse_limit;
6063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006065 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006066 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006067 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 */
6069 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006070tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 typval_T *tv1;
6072 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073 int ic; /* ignore case */
6074 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075{
6076 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006077 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006079 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006081 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006082 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006084 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085 * recursiveness to a limit. We guess they are equal then.
6086 * A fixed limit has the problem of still taking an awful long time.
6087 * Reduce the limit every time running into it. That should work fine for
6088 * deeply linked structures that are not recursively linked and catch
6089 * recursiveness quickly. */
6090 if (!recursive)
6091 tv_equal_recurse_limit = 1000;
6092 if (recursive_cnt >= tv_equal_recurse_limit)
6093 {
6094 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006095 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006097
6098 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101 ++recursive_cnt;
6102 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6103 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006104 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006105
6106 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 ++recursive_cnt;
6108 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6109 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006110 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111
6112 case VAR_FUNC:
6113 return (tv1->vval.v_string != NULL
6114 && tv2->vval.v_string != NULL
6115 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6116
6117 case VAR_NUMBER:
6118 return tv1->vval.v_number == tv2->vval.v_number;
6119
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006120#ifdef FEAT_FLOAT
6121 case VAR_FLOAT:
6122 return tv1->vval.v_float == tv2->vval.v_float;
6123#endif
6124
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006125 case VAR_STRING:
6126 s1 = get_tv_string_buf(tv1, buf1);
6127 s2 = get_tv_string_buf(tv2, buf2);
6128 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006130
6131 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 return TRUE;
6133}
6134
6135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 * Locate item with index "n" in list "l" and return it.
6137 * A negative index is counted from the end; -1 is the last item.
6138 * Returns NULL when "n" is out of range.
6139 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006140 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 long n;
6144{
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 long idx;
6147
6148 if (l == NULL)
6149 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006150
6151 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153 n = l->lv_len + n;
6154
6155 /* Check for index out of range. */
6156 if (n < 0 || n >= l->lv_len)
6157 return NULL;
6158
6159 /* When there is a cached index may start search from there. */
6160 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006162 if (n < l->lv_idx / 2)
6163 {
6164 /* closest to the start of the list */
6165 item = l->lv_first;
6166 idx = 0;
6167 }
6168 else if (n > (l->lv_idx + l->lv_len) / 2)
6169 {
6170 /* closest to the end of the list */
6171 item = l->lv_last;
6172 idx = l->lv_len - 1;
6173 }
6174 else
6175 {
6176 /* closest to the cached index */
6177 item = l->lv_idx_item;
6178 idx = l->lv_idx;
6179 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 }
6181 else
6182 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006183 if (n < l->lv_len / 2)
6184 {
6185 /* closest to the start of the list */
6186 item = l->lv_first;
6187 idx = 0;
6188 }
6189 else
6190 {
6191 /* closest to the end of the list */
6192 item = l->lv_last;
6193 idx = l->lv_len - 1;
6194 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006196
6197 while (n > idx)
6198 {
6199 /* search forward */
6200 item = item->li_next;
6201 ++idx;
6202 }
6203 while (n < idx)
6204 {
6205 /* search backward */
6206 item = item->li_prev;
6207 --idx;
6208 }
6209
6210 /* cache the used index */
6211 l->lv_idx = idx;
6212 l->lv_idx_item = item;
6213
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 return item;
6215}
6216
6217/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006218 * Get list item "l[idx]" as a number.
6219 */
6220 static long
6221list_find_nr(l, idx, errorp)
6222 list_T *l;
6223 long idx;
6224 int *errorp; /* set to TRUE when something wrong */
6225{
6226 listitem_T *li;
6227
6228 li = list_find(l, idx);
6229 if (li == NULL)
6230 {
6231 if (errorp != NULL)
6232 *errorp = TRUE;
6233 return -1L;
6234 }
6235 return get_tv_number_chk(&li->li_tv, errorp);
6236}
6237
6238/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006239 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6240 */
6241 char_u *
6242list_find_str(l, idx)
6243 list_T *l;
6244 long idx;
6245{
6246 listitem_T *li;
6247
6248 li = list_find(l, idx - 1);
6249 if (li == NULL)
6250 {
6251 EMSGN(_(e_listidx), idx);
6252 return NULL;
6253 }
6254 return get_tv_string(&li->li_tv);
6255}
6256
6257/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006258 * Locate "item" list "l" and return its index.
6259 * Returns -1 when "item" is not in the list.
6260 */
6261 static long
6262list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 list_T *l;
6264 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006265{
6266 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268
6269 if (l == NULL)
6270 return -1;
6271 idx = 0;
6272 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6273 ++idx;
6274 if (li == NULL)
6275 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006276 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006277}
6278
6279/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280 * Append item "item" to the end of list "l".
6281 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 list_T *l;
6285 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286{
6287 if (l->lv_last == NULL)
6288 {
6289 /* empty list */
6290 l->lv_first = item;
6291 l->lv_last = item;
6292 item->li_prev = NULL;
6293 }
6294 else
6295 {
6296 l->lv_last->li_next = item;
6297 item->li_prev = l->lv_last;
6298 l->lv_last = item;
6299 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 item->li_next = NULL;
6302}
6303
6304/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006308 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 list_T *l;
6311 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006313 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314
Bram Moolenaar05159a02005-02-26 23:04:13 +00006315 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006317 copy_tv(tv, &li->li_tv);
6318 list_append(l, li);
6319 return OK;
6320}
6321
6322/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006323 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006324 * Return FAIL when out of memory.
6325 */
6326 int
6327list_append_dict(list, dict)
6328 list_T *list;
6329 dict_T *dict;
6330{
6331 listitem_T *li = listitem_alloc();
6332
6333 if (li == NULL)
6334 return FAIL;
6335 li->li_tv.v_type = VAR_DICT;
6336 li->li_tv.v_lock = 0;
6337 li->li_tv.vval.v_dict = dict;
6338 list_append(list, li);
6339 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 return OK;
6341}
6342
6343/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006344 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006345 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006346 * Returns FAIL when out of memory.
6347 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006348 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006349list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006350 list_T *l;
6351 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353{
6354 listitem_T *li = listitem_alloc();
6355
6356 if (li == NULL)
6357 return FAIL;
6358 list_append(l, li);
6359 li->li_tv.v_type = VAR_STRING;
6360 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006361 if (str == NULL)
6362 li->li_tv.vval.v_string = NULL;
6363 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006364 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006365 return FAIL;
6366 return OK;
6367}
6368
6369/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006370 * Append "n" to list "l".
6371 * Returns FAIL when out of memory.
6372 */
6373 static int
6374list_append_number(l, n)
6375 list_T *l;
6376 varnumber_T n;
6377{
6378 listitem_T *li;
6379
6380 li = listitem_alloc();
6381 if (li == NULL)
6382 return FAIL;
6383 li->li_tv.v_type = VAR_NUMBER;
6384 li->li_tv.v_lock = 0;
6385 li->li_tv.vval.v_number = n;
6386 list_append(l, li);
6387 return OK;
6388}
6389
6390/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006392 * If "item" is NULL append at the end.
6393 * Return FAIL when out of memory.
6394 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006396list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 list_T *l;
6398 typval_T *tv;
6399 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006400{
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402
6403 if (ni == NULL)
6404 return FAIL;
6405 copy_tv(tv, &ni->li_tv);
6406 if (item == NULL)
6407 /* Append new item at end of list. */
6408 list_append(l, ni);
6409 else
6410 {
6411 /* Insert new item before existing item. */
6412 ni->li_prev = item->li_prev;
6413 ni->li_next = item;
6414 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006415 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006416 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_idx;
6418 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006422 l->lv_idx_item = NULL;
6423 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 }
6427 return OK;
6428}
6429
6430/*
6431 * Extend "l1" with "l2".
6432 * If "bef" is NULL append at the end, otherwise insert before this item.
6433 * Returns FAIL when out of memory.
6434 */
6435 static int
6436list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006437 list_T *l1;
6438 list_T *l2;
6439 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440{
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006442 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006444 /* We also quit the loop when we have inserted the original item count of
6445 * the list, avoid a hang when we extend a list with itself. */
6446 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6448 return FAIL;
6449 return OK;
6450}
6451
6452/*
6453 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6454 * Return FAIL when out of memory.
6455 */
6456 static int
6457list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 list_T *l1;
6459 list_T *l2;
6460 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461{
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006464 if (l1 == NULL || l2 == NULL)
6465 return FAIL;
6466
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469 if (l == NULL)
6470 return FAIL;
6471 tv->v_type = VAR_LIST;
6472 tv->vval.v_list = l;
6473
6474 /* append all items from the second list */
6475 return list_extend(l, l2, NULL);
6476}
6477
6478/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006479 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482 * Returns NULL when out of memory.
6483 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489{
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 list_T *copy;
6491 listitem_T *item;
6492 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493
6494 if (orig == NULL)
6495 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496
6497 copy = list_alloc();
6498 if (copy != NULL)
6499 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 if (copyID != 0)
6501 {
6502 /* Do this before adding the items, because one of the items may
6503 * refer back to this list. */
6504 orig->lv_copyID = copyID;
6505 orig->lv_copylist = copy;
6506 }
6507 for (item = orig->lv_first; item != NULL && !got_int;
6508 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 {
6510 ni = listitem_alloc();
6511 if (ni == NULL)
6512 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 {
6515 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6516 {
6517 vim_free(ni);
6518 break;
6519 }
6520 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006522 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 list_append(copy, ni);
6524 }
6525 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 if (item != NULL)
6527 {
6528 list_unref(copy);
6529 copy = NULL;
6530 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 }
6532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 return copy;
6534}
6535
6536/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006538 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006540 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006541list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 list_T *l;
6543 listitem_T *item;
6544 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545{
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 /* notify watchers */
6549 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006551 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 list_fix_watch(l, ip);
6553 if (ip == item2)
6554 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556
6557 if (item2->li_next == NULL)
6558 l->lv_last = item->li_prev;
6559 else
6560 item2->li_next->li_prev = item->li_prev;
6561 if (item->li_prev == NULL)
6562 l->lv_first = item2->li_next;
6563 else
6564 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006565 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566}
6567
6568/*
6569 * Return an allocated string with the string representation of a list.
6570 * May return NULL.
6571 */
6572 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006573list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006575 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576{
6577 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578
6579 if (tv->vval.v_list == NULL)
6580 return NULL;
6581 ga_init2(&ga, (int)sizeof(char), 80);
6582 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006584 {
6585 vim_free(ga.ga_data);
6586 return NULL;
6587 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588 ga_append(&ga, ']');
6589 ga_append(&ga, NUL);
6590 return (char_u *)ga.ga_data;
6591}
6592
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006593typedef struct join_S {
6594 char_u *s;
6595 char_u *tofree;
6596} join_T;
6597
6598 static int
6599list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6600 garray_T *gap; /* to store the result in */
6601 list_T *l;
6602 char_u *sep;
6603 int echo_style;
6604 int copyID;
6605 garray_T *join_gap; /* to keep each list item string */
6606{
6607 int i;
6608 join_T *p;
6609 int len;
6610 int sumlen = 0;
6611 int first = TRUE;
6612 char_u *tofree;
6613 char_u numbuf[NUMBUFLEN];
6614 listitem_T *item;
6615 char_u *s;
6616
6617 /* Stringify each item in the list. */
6618 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6619 {
6620 if (echo_style)
6621 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6622 else
6623 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6624 if (s == NULL)
6625 return FAIL;
6626
6627 len = (int)STRLEN(s);
6628 sumlen += len;
6629
6630 ga_grow(join_gap, 1);
6631 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6632 if (tofree != NULL || s != numbuf)
6633 {
6634 p->s = s;
6635 p->tofree = tofree;
6636 }
6637 else
6638 {
6639 p->s = vim_strnsave(s, len);
6640 p->tofree = p->s;
6641 }
6642
6643 line_breakcheck();
6644 }
6645
6646 /* Allocate result buffer with its total size, avoid re-allocation and
6647 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6648 if (join_gap->ga_len >= 2)
6649 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6650 if (ga_grow(gap, sumlen + 2) == FAIL)
6651 return FAIL;
6652
6653 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6654 {
6655 if (first)
6656 first = FALSE;
6657 else
6658 ga_concat(gap, sep);
6659 p = ((join_T *)join_gap->ga_data) + i;
6660
6661 if (p->s != NULL)
6662 ga_concat(gap, p->s);
6663 line_breakcheck();
6664 }
6665
6666 return OK;
6667}
6668
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006671 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006672 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006675list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006677 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006679 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006680 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006682 garray_T join_ga;
6683 int retval;
6684 join_T *p;
6685 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6688 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6689
6690 /* Dispose each item in join_ga. */
6691 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006692 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006693 p = (join_T *)join_ga.ga_data;
6694 for (i = 0; i < join_ga.ga_len; ++i)
6695 {
6696 vim_free(p->tofree);
6697 ++p;
6698 }
6699 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006700 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006701
6702 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703}
6704
6705/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006706 * Garbage collection for lists and dictionaries.
6707 *
6708 * We use reference counts to be able to free most items right away when they
6709 * are no longer used. But for composite items it's possible that it becomes
6710 * unused while the reference count is > 0: When there is a recursive
6711 * reference. Example:
6712 * :let l = [1, 2, 3]
6713 * :let d = {9: l}
6714 * :let l[1] = d
6715 *
6716 * Since this is quite unusual we handle this with garbage collection: every
6717 * once in a while find out which lists and dicts are not referenced from any
6718 * variable.
6719 *
6720 * Here is a good reference text about garbage collection (refers to Python
6721 * but it applies to all reference-counting mechanisms):
6722 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006723 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724
6725/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006726 * Do garbage collection for lists and dicts.
6727 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006728 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006729 int
6730garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006732 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 buf_T *buf;
6734 win_T *wp;
6735 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006736 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006737 int did_free;
6738 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006739#ifdef FEAT_WINDOWS
6740 tabpage_T *tp;
6741#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006743 /* Only do this once. */
6744 want_garbage_collect = FALSE;
6745 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006746 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006747
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006748 /* We advance by two because we add one for items referenced through
6749 * previous_funccal. */
6750 current_copyID += COPYID_INC;
6751 copyID = current_copyID;
6752
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753 /*
6754 * 1. Go through all accessible variables and mark all lists and dicts
6755 * with copyID.
6756 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006757
6758 /* Don't free variables in the previous_funccal list unless they are only
6759 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006760 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006761 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6762 {
6763 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6764 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6765 }
6766
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 /* script-local variables */
6768 for (i = 1; i <= ga_scripts.ga_len; ++i)
6769 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6770
6771 /* buffer-local variables */
6772 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6773 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6774
6775 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006776 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6778
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779#ifdef FEAT_WINDOWS
6780 /* tabpage-local variables */
6781 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6782 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6783#endif
6784
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785 /* global variables */
6786 set_ref_in_ht(&globvarht, copyID);
6787
6788 /* function-local variables */
6789 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6790 {
6791 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6792 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6793 }
6794
Bram Moolenaard812df62008-11-09 12:46:09 +00006795 /* v: vars */
6796 set_ref_in_ht(&vimvarht, copyID);
6797
Bram Moolenaar1dced572012-04-05 16:54:08 +02006798#ifdef FEAT_LUA
6799 set_ref_in_lua(copyID);
6800#endif
6801
Bram Moolenaardb913952012-06-29 12:54:53 +02006802#ifdef FEAT_PYTHON
6803 set_ref_in_python(copyID);
6804#endif
6805
6806#ifdef FEAT_PYTHON3
6807 set_ref_in_python3(copyID);
6808#endif
6809
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006810 /*
6811 * 2. Free lists and dictionaries that are not referenced.
6812 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006813 did_free = free_unref_items(copyID);
6814
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006815 /*
6816 * 3. Check if any funccal can be freed now.
6817 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 for (pfc = &previous_funccal; *pfc != NULL; )
6819 {
6820 if (can_free_funccal(*pfc, copyID))
6821 {
6822 fc = *pfc;
6823 *pfc = fc->caller;
6824 free_funccal(fc, TRUE);
6825 did_free = TRUE;
6826 did_free_funccal = TRUE;
6827 }
6828 else
6829 pfc = &(*pfc)->caller;
6830 }
6831 if (did_free_funccal)
6832 /* When a funccal was freed some more items might be garbage
6833 * collected, so run again. */
6834 (void)garbage_collect();
6835
6836 return did_free;
6837}
6838
6839/*
6840 * Free lists and dictionaries that are no longer referenced.
6841 */
6842 static int
6843free_unref_items(copyID)
6844 int copyID;
6845{
6846 dict_T *dd;
6847 list_T *ll;
6848 int did_free = FALSE;
6849
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006851 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 */
6853 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006856 /* Free the Dictionary and ordinary items it contains, but don't
6857 * recurse into Lists and Dictionaries, they will be in the list
6858 * of dicts or list of lists. */
6859 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 did_free = TRUE;
6861
6862 /* restart, next dict may also have been freed */
6863 dd = first_dict;
6864 }
6865 else
6866 dd = dd->dv_used_next;
6867
6868 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 * Go through the list of lists and free items without the copyID.
6870 * But don't free a list that has a watcher (used in a for loop), these
6871 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 */
6873 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6875 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006877 /* Free the List and ordinary items it contains, but don't recurse
6878 * into Lists and Dictionaries, they will be in the list of dicts
6879 * or list of lists. */
6880 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 did_free = TRUE;
6882
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006883 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 ll = first_list;
6885 }
6886 else
6887 ll = ll->lv_used_next;
6888
6889 return did_free;
6890}
6891
6892/*
6893 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6894 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006895 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896set_ref_in_ht(ht, copyID)
6897 hashtab_T *ht;
6898 int copyID;
6899{
6900 int todo;
6901 hashitem_T *hi;
6902
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006903 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 for (hi = ht->ht_array; todo > 0; ++hi)
6905 if (!HASHITEM_EMPTY(hi))
6906 {
6907 --todo;
6908 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6909 }
6910}
6911
6912/*
6913 * Mark all lists and dicts referenced through list "l" with "copyID".
6914 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006915 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916set_ref_in_list(l, copyID)
6917 list_T *l;
6918 int copyID;
6919{
6920 listitem_T *li;
6921
6922 for (li = l->lv_first; li != NULL; li = li->li_next)
6923 set_ref_in_item(&li->li_tv, copyID);
6924}
6925
6926/*
6927 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6928 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006929 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930set_ref_in_item(tv, copyID)
6931 typval_T *tv;
6932 int copyID;
6933{
6934 dict_T *dd;
6935 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006936
6937 switch (tv->v_type)
6938 {
6939 case VAR_DICT:
6940 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006941 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 /* Didn't see this dict yet. */
6944 dd->dv_copyID = copyID;
6945 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006946 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006948
6949 case VAR_LIST:
6950 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006951 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006952 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 /* Didn't see this list yet. */
6954 ll->lv_copyID = copyID;
6955 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006956 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006958 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006960}
6961
6962/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006963 * Allocate an empty header for a dictionary.
6964 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006965 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966dict_alloc()
6967{
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006972 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006973 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 if (first_dict != NULL)
6975 first_dict->dv_used_prev = d;
6976 d->dv_used_next = first_dict;
6977 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006978 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979
Bram Moolenaar33570922005-01-25 22:26:29 +00006980 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006981 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006982 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006983 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006984 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006985 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006987}
6988
6989/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006990 * Allocate an empty dict for a return value.
6991 * Returns OK or FAIL.
6992 */
6993 static int
6994rettv_dict_alloc(rettv)
6995 typval_T *rettv;
6996{
6997 dict_T *d = dict_alloc();
6998
6999 if (d == NULL)
7000 return FAIL;
7001
7002 rettv->vval.v_dict = d;
7003 rettv->v_type = VAR_DICT;
7004 ++d->dv_refcount;
7005 return OK;
7006}
7007
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Unreference a Dictionary: decrement the reference count and free it when it
7011 * becomes zero.
7012 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007013 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007017 if (d != NULL && --d->dv_refcount <= 0)
7018 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019}
7020
7021/*
7022 * Free a Dictionary, including all items it contains.
7023 * Ignores the reference count.
7024 */
7025 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007026dict_free(d, recurse)
7027 dict_T *d;
7028 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007030 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 /* Remove the dict from the list of dicts for garbage collection. */
7035 if (d->dv_used_prev == NULL)
7036 first_dict = d->dv_used_next;
7037 else
7038 d->dv_used_prev->dv_used_next = d->dv_used_next;
7039 if (d->dv_used_next != NULL)
7040 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7041
7042 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007043 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007044 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 if (!HASHITEM_EMPTY(hi))
7048 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049 /* Remove the item before deleting it, just in case there is
7050 * something recursive causing trouble. */
7051 di = HI2DI(hi);
7052 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007053 if (recurse || (di->di_tv.v_type != VAR_LIST
7054 && di->di_tv.v_type != VAR_DICT))
7055 clear_tv(&di->di_tv);
7056 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 --todo;
7058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061 vim_free(d);
7062}
7063
7064/*
7065 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 * The "key" is copied to the new item.
7067 * Note that the value of the item "di_tv" still needs to be initialized!
7068 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007069 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007070 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071dictitem_alloc(key)
7072 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073{
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007076 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007078 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007079 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007080 di->di_flags = 0;
7081 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083}
7084
7085/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007086 * Make a copy of a Dictionary item.
7087 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091{
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007094 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7095 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096 if (di != NULL)
7097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007098 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007099 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 copy_tv(&org->di_tv, &di->di_tv);
7101 }
7102 return di;
7103}
7104
7105/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106 * Remove item "item" from Dictionary "dict" and free it.
7107 */
7108 static void
7109dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 dict_T *dict;
7111 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 if (HASHITEM_EMPTY(hi))
7117 EMSG2(_(e_intern2), "dictitem_remove()");
7118 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 dictitem_free(item);
7121}
7122
7123/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124 * Free a dict item. Also clears the value.
7125 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007126 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007129{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 clear_tv(&item->di_tv);
7131 vim_free(item);
7132}
7133
7134/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7136 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007137 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138 * Returns NULL when out of memory.
7139 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007141dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145{
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 dict_T *copy;
7147 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150
7151 if (orig == NULL)
7152 return NULL;
7153
7154 copy = dict_alloc();
7155 if (copy != NULL)
7156 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007157 if (copyID != 0)
7158 {
7159 orig->dv_copyID = copyID;
7160 orig->dv_copydict = copy;
7161 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007162 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007163 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 --todo;
7168
7169 di = dictitem_alloc(hi->hi_key);
7170 if (di == NULL)
7171 break;
7172 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007173 {
7174 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7175 copyID) == FAIL)
7176 {
7177 vim_free(di);
7178 break;
7179 }
7180 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 else
7182 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7183 if (dict_add(copy, di) == FAIL)
7184 {
7185 dictitem_free(di);
7186 break;
7187 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 if (todo > 0)
7193 {
7194 dict_unref(copy);
7195 copy = NULL;
7196 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 }
7198
7199 return copy;
7200}
7201
7202/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007204 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007206 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007208 dict_T *d;
7209 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212}
7213
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007215 * Add a number or string entry to dictionary "d".
7216 * When "str" is NULL use number "nr", otherwise use "str".
7217 * Returns FAIL when out of memory and when key already exists.
7218 */
7219 int
7220dict_add_nr_str(d, key, nr, str)
7221 dict_T *d;
7222 char *key;
7223 long nr;
7224 char_u *str;
7225{
7226 dictitem_T *item;
7227
7228 item = dictitem_alloc((char_u *)key);
7229 if (item == NULL)
7230 return FAIL;
7231 item->di_tv.v_lock = 0;
7232 if (str == NULL)
7233 {
7234 item->di_tv.v_type = VAR_NUMBER;
7235 item->di_tv.vval.v_number = nr;
7236 }
7237 else
7238 {
7239 item->di_tv.v_type = VAR_STRING;
7240 item->di_tv.vval.v_string = vim_strsave(str);
7241 }
7242 if (dict_add(d, item) == FAIL)
7243 {
7244 dictitem_free(item);
7245 return FAIL;
7246 }
7247 return OK;
7248}
7249
7250/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007251 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007252 * Returns FAIL when out of memory and when key already exists.
7253 */
7254 int
7255dict_add_list(d, key, list)
7256 dict_T *d;
7257 char *key;
7258 list_T *list;
7259{
7260 dictitem_T *item;
7261
7262 item = dictitem_alloc((char_u *)key);
7263 if (item == NULL)
7264 return FAIL;
7265 item->di_tv.v_lock = 0;
7266 item->di_tv.v_type = VAR_LIST;
7267 item->di_tv.vval.v_list = list;
7268 if (dict_add(d, item) == FAIL)
7269 {
7270 dictitem_free(item);
7271 return FAIL;
7272 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007273 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007274 return OK;
7275}
7276
7277/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 * Get the number of items in a Dictionary.
7279 */
7280 static long
7281dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 if (d == NULL)
7285 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287}
7288
7289/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 * Find item "key[len]" in Dictionary "d".
7291 * If "len" is negative use strlen(key).
7292 * Returns NULL when not found.
7293 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007294 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 char_u *key;
7298 int len;
7299{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300#define AKEYLEN 200
7301 char_u buf[AKEYLEN];
7302 char_u *akey;
7303 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 if (len < 0)
7307 akey = key;
7308 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 tofree = akey = vim_strnsave(key, len);
7311 if (akey == NULL)
7312 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007313 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 else
7315 {
7316 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007317 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 akey = buf;
7319 }
7320
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 vim_free(tofree);
7323 if (HASHITEM_EMPTY(hi))
7324 return NULL;
7325 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326}
7327
7328/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007329 * Get a string item from a dictionary.
7330 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007331 * Returns NULL if the entry doesn't exist or out of memory.
7332 */
7333 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007334get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007335 dict_T *d;
7336 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007337 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338{
7339 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007341
7342 di = dict_find(d, key, -1);
7343 if (di == NULL)
7344 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007345 s = get_tv_string(&di->di_tv);
7346 if (save && s != NULL)
7347 s = vim_strsave(s);
7348 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007349}
7350
7351/*
7352 * Get a number item from a dictionary.
7353 * Returns 0 if the entry doesn't exist or out of memory.
7354 */
7355 long
7356get_dict_number(d, key)
7357 dict_T *d;
7358 char_u *key;
7359{
7360 dictitem_T *di;
7361
7362 di = dict_find(d, key, -1);
7363 if (di == NULL)
7364 return 0;
7365 return get_tv_number(&di->di_tv);
7366}
7367
7368/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369 * Return an allocated string with the string representation of a Dictionary.
7370 * May return NULL.
7371 */
7372 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007373dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007375 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376{
7377 garray_T ga;
7378 int first = TRUE;
7379 char_u *tofree;
7380 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 return NULL;
7388 ga_init2(&ga, (int)sizeof(char), 80);
7389 ga_append(&ga, '{');
7390
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007391 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007392 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 --todo;
7397
7398 if (first)
7399 first = FALSE;
7400 else
7401 ga_concat(&ga, (char_u *)", ");
7402
7403 tofree = string_quote(hi->hi_key, FALSE);
7404 if (tofree != NULL)
7405 {
7406 ga_concat(&ga, tofree);
7407 vim_free(tofree);
7408 }
7409 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 if (s != NULL)
7412 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007414 if (s == NULL)
7415 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 if (todo > 0)
7419 {
7420 vim_free(ga.ga_data);
7421 return NULL;
7422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423
7424 ga_append(&ga, '}');
7425 ga_append(&ga, NUL);
7426 return (char_u *)ga.ga_data;
7427}
7428
7429/*
7430 * Allocate a variable for a Dictionary and fill it from "*arg".
7431 * Return OK or FAIL. Returns NOTDONE for {expr}.
7432 */
7433 static int
7434get_dict_tv(arg, rettv, evaluate)
7435 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 int evaluate;
7438{
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 dict_T *d = NULL;
7440 typval_T tvkey;
7441 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007442 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446
7447 /*
7448 * First check if it's not a curly-braces thing: {expr}.
7449 * Must do this without evaluating, otherwise a function may be called
7450 * twice. Unfortunately this means we need to call eval1() twice for the
7451 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007452 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454 if (*start != '}')
7455 {
7456 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7457 return FAIL;
7458 if (*start == '}')
7459 return NOTDONE;
7460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461
7462 if (evaluate)
7463 {
7464 d = dict_alloc();
7465 if (d == NULL)
7466 return FAIL;
7467 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007468 tvkey.v_type = VAR_UNKNOWN;
7469 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
7471 *arg = skipwhite(*arg + 1);
7472 while (**arg != '}' && **arg != NUL)
7473 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475 goto failret;
7476 if (**arg != ':')
7477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007478 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 goto failret;
7481 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007482 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007484 key = get_tv_string_buf_chk(&tvkey, buf);
7485 if (key == NULL || *key == NUL)
7486 {
7487 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7488 if (key != NULL)
7489 EMSG(_(e_emptykey));
7490 clear_tv(&tvkey);
7491 goto failret;
7492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494
7495 *arg = skipwhite(*arg + 1);
7496 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7497 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007498 if (evaluate)
7499 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 goto failret;
7501 }
7502 if (evaluate)
7503 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 if (item != NULL)
7506 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007507 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 clear_tv(&tv);
7510 goto failret;
7511 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 item = dictitem_alloc(key);
7513 clear_tv(&tvkey);
7514 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007517 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 if (dict_add(d, item) == FAIL)
7519 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 }
7521 }
7522
7523 if (**arg == '}')
7524 break;
7525 if (**arg != ',')
7526 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007527 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 goto failret;
7529 }
7530 *arg = skipwhite(*arg + 1);
7531 }
7532
7533 if (**arg != '}')
7534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007535 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536failret:
7537 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007538 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 return FAIL;
7540 }
7541
7542 *arg = skipwhite(*arg + 1);
7543 if (evaluate)
7544 {
7545 rettv->v_type = VAR_DICT;
7546 rettv->vval.v_dict = d;
7547 ++d->dv_refcount;
7548 }
7549
7550 return OK;
7551}
7552
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007554 * Return a string with the string representation of a variable.
7555 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007556 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007557 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007558 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007559 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007560 */
7561 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007562echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007564 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007565 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007566 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007568 static int recurse = 0;
7569 char_u *r = NULL;
7570
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007573 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 *tofree = NULL;
7575 return NULL;
7576 }
7577 ++recurse;
7578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579 switch (tv->v_type)
7580 {
7581 case VAR_FUNC:
7582 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 r = tv->vval.v_string;
7584 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007586 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007587 if (tv->vval.v_list == NULL)
7588 {
7589 *tofree = NULL;
7590 r = NULL;
7591 }
7592 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7593 {
7594 *tofree = NULL;
7595 r = (char_u *)"[...]";
7596 }
7597 else
7598 {
7599 tv->vval.v_list->lv_copyID = copyID;
7600 *tofree = list2string(tv, copyID);
7601 r = *tofree;
7602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007603 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606 if (tv->vval.v_dict == NULL)
7607 {
7608 *tofree = NULL;
7609 r = NULL;
7610 }
7611 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7612 {
7613 *tofree = NULL;
7614 r = (char_u *)"{...}";
7615 }
7616 else
7617 {
7618 tv->vval.v_dict->dv_copyID = copyID;
7619 *tofree = dict2string(tv, copyID);
7620 r = *tofree;
7621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007622 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007623
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007624 case VAR_STRING:
7625 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 *tofree = NULL;
7627 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007630#ifdef FEAT_FLOAT
7631 case VAR_FLOAT:
7632 *tofree = NULL;
7633 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7634 r = numbuf;
7635 break;
7636#endif
7637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007639 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642
7643 --recurse;
7644 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007645}
7646
7647/*
7648 * Return a string with the string representation of a variable.
7649 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7650 * "numbuf" is used for a number.
7651 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007652 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 */
7654 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007655tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007657 char_u **tofree;
7658 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007659 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660{
7661 switch (tv->v_type)
7662 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007663 case VAR_FUNC:
7664 *tofree = string_quote(tv->vval.v_string, TRUE);
7665 return *tofree;
7666 case VAR_STRING:
7667 *tofree = string_quote(tv->vval.v_string, FALSE);
7668 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007669#ifdef FEAT_FLOAT
7670 case VAR_FLOAT:
7671 *tofree = NULL;
7672 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7673 return numbuf;
7674#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007681 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007682 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683}
7684
7685/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 * Return string "str" in ' quotes, doubling ' characters.
7687 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 */
7690 static char_u *
7691string_quote(str, function)
7692 char_u *str;
7693 int function;
7694{
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 char_u *p, *r, *s;
7697
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 len = (function ? 13 : 3);
7699 if (str != NULL)
7700 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007701 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 for (p = str; *p != NUL; mb_ptr_adv(p))
7703 if (*p == '\'')
7704 ++len;
7705 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 s = r = alloc(len);
7707 if (r != NULL)
7708 {
7709 if (function)
7710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007712 r += 10;
7713 }
7714 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 if (str != NULL)
7717 for (p = str; *p != NUL; )
7718 {
7719 if (*p == '\'')
7720 *r++ = '\'';
7721 MB_COPY_CHAR(p, r);
7722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 if (function)
7725 *r++ = ')';
7726 *r++ = NUL;
7727 }
7728 return s;
7729}
7730
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007731#ifdef FEAT_FLOAT
7732/*
7733 * Convert the string "text" to a floating point number.
7734 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7735 * this always uses a decimal point.
7736 * Returns the length of the text that was consumed.
7737 */
7738 static int
7739string2float(text, value)
7740 char_u *text;
7741 float_T *value; /* result stored here */
7742{
7743 char *s = (char *)text;
7744 float_T f;
7745
7746 f = strtod(s, &s);
7747 *value = f;
7748 return (int)((char_u *)s - text);
7749}
7750#endif
7751
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 * Get the value of an environment variable.
7754 * "arg" is pointing to the '$'. It is advanced to after the name.
7755 * If the environment variable was not set, silently assume it is empty.
7756 * Always return OK.
7757 */
7758 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 int evaluate;
7763{
7764 char_u *string = NULL;
7765 int len;
7766 int cc;
7767 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007768 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
7770 ++*arg;
7771 name = *arg;
7772 len = get_env_len(arg);
7773 if (evaluate)
7774 {
7775 if (len != 0)
7776 {
7777 cc = name[len];
7778 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007779 /* first try vim_getenv(), fast for normal environment vars */
7780 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 {
7783 if (!mustfree)
7784 string = vim_strsave(string);
7785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 else
7787 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007788 if (mustfree)
7789 vim_free(string);
7790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 /* next try expanding things like $VIM and ${HOME} */
7792 string = expand_env_save(name - 1);
7793 if (string != NULL && *string == '$')
7794 {
7795 vim_free(string);
7796 string = NULL;
7797 }
7798 }
7799 name[len] = cc;
7800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801 rettv->v_type = VAR_STRING;
7802 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804
7805 return OK;
7806}
7807
7808/*
7809 * Array with names and number of arguments of all internal functions
7810 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7811 */
7812static struct fst
7813{
7814 char *f_name; /* function name */
7815 char f_min_argc; /* minimal number of arguments */
7816 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007818 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819} functions[] =
7820{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007821#ifdef FEAT_FLOAT
7822 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007823 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007825 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007826 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"append", 2, 2, f_append},
7828 {"argc", 0, 0, f_argc},
7829 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007830 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007831#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007832 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007833 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007834 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007837 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {"bufexists", 1, 1, f_bufexists},
7839 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7840 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7841 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7842 {"buflisted", 1, 1, f_buflisted},
7843 {"bufloaded", 1, 1, f_bufloaded},
7844 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007845 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"bufwinnr", 1, 1, f_bufwinnr},
7847 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007848 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850#ifdef FEAT_FLOAT
7851 {"ceil", 1, 1, f_ceil},
7852#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007853 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"char2nr", 1, 1, f_char2nr},
7855 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007858#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007859 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007860 {"complete_add", 1, 1, f_complete_add},
7861 {"complete_check", 0, 0, f_complete_check},
7862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007864 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#ifdef FEAT_FLOAT
7866 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007871 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007872 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"delete", 1, 1, f_delete},
7874 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007875 {"diff_filler", 1, 1, f_diff_filler},
7876 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007877 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007879 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"eventhandler", 0, 0, f_eventhandler},
7881 {"executable", 1, 1, f_executable},
7882 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007883#ifdef FEAT_FLOAT
7884 {"exp", 1, 1, f_exp},
7885#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007886 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007887 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007888 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7890 {"filereadable", 1, 1, f_filereadable},
7891 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007893 {"finddir", 1, 3, f_finddir},
7894 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007895#ifdef FEAT_FLOAT
7896 {"float2nr", 1, 1, f_float2nr},
7897 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007898 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007900 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"fnamemodify", 2, 2, f_fnamemodify},
7902 {"foldclosed", 1, 1, f_foldclosed},
7903 {"foldclosedend", 1, 1, f_foldclosedend},
7904 {"foldlevel", 1, 1, f_foldlevel},
7905 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007906 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007909 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007910 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007911 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"getbufvar", 2, 2, f_getbufvar},
7913 {"getchar", 0, 1, f_getchar},
7914 {"getcharmod", 0, 0, f_getcharmod},
7915 {"getcmdline", 0, 0, f_getcmdline},
7916 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007917 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007919 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007920 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"getfsize", 1, 1, f_getfsize},
7922 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007923 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007924 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007925 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007926 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007927 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007928 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007929 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007930 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007932 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007933 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"getwinposx", 0, 0, f_getwinposx},
7935 {"getwinposy", 0, 0, f_getwinposy},
7936 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007937 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007938 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007940 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007941 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007942 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7944 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7945 {"histadd", 2, 2, f_histadd},
7946 {"histdel", 1, 2, f_histdel},
7947 {"histget", 1, 2, f_histget},
7948 {"histnr", 1, 1, f_histnr},
7949 {"hlID", 1, 1, f_hlID},
7950 {"hlexists", 1, 1, f_hlexists},
7951 {"hostname", 0, 0, f_hostname},
7952 {"iconv", 3, 3, f_iconv},
7953 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007955 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007957 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"inputrestore", 0, 0, f_inputrestore},
7959 {"inputsave", 0, 0, f_inputsave},
7960 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007962 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007964 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007969 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"libcall", 3, 3, f_libcall},
7971 {"libcallnr", 3, 3, f_libcallnr},
7972 {"line", 1, 1, f_line},
7973 {"line2byte", 1, 1, f_line2byte},
7974 {"lispindent", 1, 1, f_lispindent},
7975 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007977 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007978 {"log10", 1, 1, f_log10},
7979#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007980#ifdef FEAT_LUA
7981 {"luaeval", 1, 2, f_luaeval},
7982#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007984 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007985 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007986 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007987 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007988 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007989 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007990 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007991 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007992 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007993 {"max", 1, 1, f_max},
7994 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007995#ifdef vim_mkdir
7996 {"mkdir", 1, 3, f_mkdir},
7997#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007999#ifdef FEAT_MZSCHEME
8000 {"mzeval", 1, 1, f_mzeval},
8001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {"nextnonblank", 1, 1, f_nextnonblank},
8003 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008004 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008005 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008006#ifdef FEAT_FLOAT
8007 {"pow", 2, 2, f_pow},
8008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008010 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008011 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008012#ifdef FEAT_PYTHON3
8013 {"py3eval", 1, 1, f_py3eval},
8014#endif
8015#ifdef FEAT_PYTHON
8016 {"pyeval", 1, 1, f_pyeval},
8017#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008018 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008019 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008020 {"reltime", 0, 2, f_reltime},
8021 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"remote_expr", 2, 3, f_remote_expr},
8023 {"remote_foreground", 1, 1, f_remote_foreground},
8024 {"remote_peek", 1, 2, f_remote_peek},
8025 {"remote_read", 1, 1, f_remote_read},
8026 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008027 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008029 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008031 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008032#ifdef FEAT_FLOAT
8033 {"round", 1, 1, f_round},
8034#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00008035 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008036 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008037 {"searchpair", 3, 7, f_searchpair},
8038 {"searchpairpos", 3, 7, f_searchpairpos},
8039 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"server2client", 2, 2, f_server2client},
8041 {"serverlist", 0, 0, f_serverlist},
8042 {"setbufvar", 3, 3, f_setbufvar},
8043 {"setcmdpos", 1, 1, f_setcmdpos},
8044 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008045 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008046 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008047 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008048 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008050 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008051 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008053 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008057 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008059 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008060 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008061 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008062 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008063 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064#ifdef FEAT_FLOAT
8065 {"sqrt", 1, 1, f_sqrt},
8066 {"str2float", 1, 1, f_str2float},
8067#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008068 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008069 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008070 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071#ifdef HAVE_STRFTIME
8072 {"strftime", 1, 2, f_strftime},
8073#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008075 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"strlen", 1, 1, f_strlen},
8077 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008078 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008080 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"submatch", 1, 1, f_submatch},
8082 {"substitute", 4, 4, f_substitute},
8083 {"synID", 3, 3, f_synID},
8084 {"synIDattr", 2, 3, f_synIDattr},
8085 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008086 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008087 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008088 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008089 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008090 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008091 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008092 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008093 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008094#ifdef FEAT_FLOAT
8095 {"tan", 1, 1, f_tan},
8096 {"tanh", 1, 1, f_tanh},
8097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008099 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"tolower", 1, 1, f_tolower},
8101 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008102 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"trunc", 1, 1, f_trunc},
8105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008107 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008108 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008109 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"virtcol", 1, 1, f_virtcol},
8111 {"visualmode", 0, 1, f_visualmode},
8112 {"winbufnr", 1, 1, f_winbufnr},
8113 {"wincol", 0, 0, f_wincol},
8114 {"winheight", 1, 1, f_winheight},
8115 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008116 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008118 {"winrestview", 1, 1, f_winrestview},
8119 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008121 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008122 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123};
8124
8125#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8126
8127/*
8128 * Function given to ExpandGeneric() to obtain the list of internal
8129 * or user defined function names.
8130 */
8131 char_u *
8132get_function_name(xp, idx)
8133 expand_T *xp;
8134 int idx;
8135{
8136 static int intidx = -1;
8137 char_u *name;
8138
8139 if (idx == 0)
8140 intidx = -1;
8141 if (intidx < 0)
8142 {
8143 name = get_user_func_name(xp, idx);
8144 if (name != NULL)
8145 return name;
8146 }
8147 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8148 {
8149 STRCPY(IObuff, functions[intidx].f_name);
8150 STRCAT(IObuff, "(");
8151 if (functions[intidx].f_max_argc == 0)
8152 STRCAT(IObuff, ")");
8153 return IObuff;
8154 }
8155
8156 return NULL;
8157}
8158
8159/*
8160 * Function given to ExpandGeneric() to obtain the list of internal or
8161 * user defined variable or function names.
8162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 char_u *
8164get_expr_name(xp, idx)
8165 expand_T *xp;
8166 int idx;
8167{
8168 static int intidx = -1;
8169 char_u *name;
8170
8171 if (idx == 0)
8172 intidx = -1;
8173 if (intidx < 0)
8174 {
8175 name = get_function_name(xp, idx);
8176 if (name != NULL)
8177 return name;
8178 }
8179 return get_user_var_name(xp, ++intidx);
8180}
8181
8182#endif /* FEAT_CMDL_COMPL */
8183
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008184#if defined(EBCDIC) || defined(PROTO)
8185/*
8186 * Compare struct fst by function name.
8187 */
8188 static int
8189compare_func_name(s1, s2)
8190 const void *s1;
8191 const void *s2;
8192{
8193 struct fst *p1 = (struct fst *)s1;
8194 struct fst *p2 = (struct fst *)s2;
8195
8196 return STRCMP(p1->f_name, p2->f_name);
8197}
8198
8199/*
8200 * Sort the function table by function name.
8201 * The sorting of the table above is ASCII dependant.
8202 * On machines using EBCDIC we have to sort it.
8203 */
8204 static void
8205sortFunctions()
8206{
8207 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8208
8209 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8210}
8211#endif
8212
8213
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214/*
8215 * Find internal function in table above.
8216 * Return index, or -1 if not found
8217 */
8218 static int
8219find_internal_func(name)
8220 char_u *name; /* name of the function */
8221{
8222 int first = 0;
8223 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8224 int cmp;
8225 int x;
8226
8227 /*
8228 * Find the function name in the table. Binary search.
8229 */
8230 while (first <= last)
8231 {
8232 x = first + ((unsigned)(last - first) >> 1);
8233 cmp = STRCMP(name, functions[x].f_name);
8234 if (cmp < 0)
8235 last = x - 1;
8236 else if (cmp > 0)
8237 first = x + 1;
8238 else
8239 return x;
8240 }
8241 return -1;
8242}
8243
8244/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8246 * name it contains, otherwise return "name".
8247 */
8248 static char_u *
8249deref_func_name(name, lenp)
8250 char_u *name;
8251 int *lenp;
8252{
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008254 int cc;
8255
8256 cc = name[*lenp];
8257 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008258 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008261 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008262 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008263 {
8264 *lenp = 0;
8265 return (char_u *)""; /* just in case */
8266 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008267 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 }
8270
8271 return name;
8272}
8273
8274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 * Allocate a variable for the result of a function.
8276 * Return OK or FAIL.
8277 */
8278 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008279get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8280 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 char_u *name; /* name of the function */
8282 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 char_u **arg; /* argument, pointing to the '(' */
8285 linenr_T firstline; /* first line of range */
8286 linenr_T lastline; /* last line of range */
8287 int *doesrange; /* return: function handled range */
8288 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291 char_u *argp;
8292 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008293 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 int argcount = 0; /* number of arguments found */
8295
8296 /*
8297 * Get the arguments.
8298 */
8299 argp = *arg;
8300 while (argcount < MAX_FUNC_ARGS)
8301 {
8302 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8303 if (*argp == ')' || *argp == ',' || *argp == NUL)
8304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8306 {
8307 ret = FAIL;
8308 break;
8309 }
8310 ++argcount;
8311 if (*argp != ',')
8312 break;
8313 }
8314 if (*argp == ')')
8315 ++argp;
8316 else
8317 ret = FAIL;
8318
8319 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008321 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 {
8324 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008325 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008326 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008327 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329
8330 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
8333 *arg = skipwhite(argp);
8334 return ret;
8335}
8336
8337
8338/*
8339 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008340 * Return OK when the function can't be called, FAIL otherwise.
8341 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 */
8343 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008344call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008346 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008350 typval_T *argvars; /* vars for arguments, must have "argcount"
8351 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 linenr_T firstline; /* first line of range */
8353 linenr_T lastline; /* last line of range */
8354 int *doesrange; /* return: function handled range */
8355 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008356 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359#define ERROR_UNKNOWN 0
8360#define ERROR_TOOMANY 1
8361#define ERROR_TOOFEW 2
8362#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008363#define ERROR_DICT 4
8364#define ERROR_NONE 5
8365#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 int error = ERROR_NONE;
8367 int i;
8368 int llen;
8369 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370#define FLEN_FIXED 40
8371 char_u fname_buf[FLEN_FIXED + 1];
8372 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008373 char_u *name;
8374
8375 /* Make a copy of the name, if it comes from a funcref variable it could
8376 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008377 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008378 if (name == NULL)
8379 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
8381 /*
8382 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8383 * Change <SNR>123_name() to K_SNR 123_name().
8384 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 llen = eval_fname_script(name);
8387 if (llen > 0)
8388 {
8389 fname_buf[0] = K_SPECIAL;
8390 fname_buf[1] = KS_EXTRA;
8391 fname_buf[2] = (int)KE_SNR;
8392 i = 3;
8393 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8394 {
8395 if (current_SID <= 0)
8396 error = ERROR_SCRIPT;
8397 else
8398 {
8399 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8400 i = (int)STRLEN(fname_buf);
8401 }
8402 }
8403 if (i + STRLEN(name + llen) < FLEN_FIXED)
8404 {
8405 STRCPY(fname_buf + i, name + llen);
8406 fname = fname_buf;
8407 }
8408 else
8409 {
8410 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8411 if (fname == NULL)
8412 error = ERROR_OTHER;
8413 else
8414 {
8415 mch_memmove(fname, fname_buf, (size_t)i);
8416 STRCPY(fname + i, name + llen);
8417 }
8418 }
8419 }
8420 else
8421 fname = name;
8422
8423 *doesrange = FALSE;
8424
8425
8426 /* execute the function if no errors detected and executing */
8427 if (evaluate && error == ERROR_NONE)
8428 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008429 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8430 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 error = ERROR_UNKNOWN;
8432
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008433 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {
8435 /*
8436 * User defined function.
8437 */
8438 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008441 /* Trigger FuncUndefined event, may load the function. */
8442 if (fp == NULL
8443 && apply_autocmds(EVENT_FUNCUNDEFINED,
8444 fname, fname, TRUE, NULL)
8445 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008447 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 fp = find_func(fname);
8449 }
8450#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008451 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008452 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 {
8454 /* loaded a package, search for the function again */
8455 fp = find_func(fname);
8456 }
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 if (fp != NULL)
8459 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008460 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008462 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008464 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008466 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008467 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 else
8469 {
8470 /*
8471 * Call the user function.
8472 * Save and restore search patterns, script variables and
8473 * redo buffer.
8474 */
8475 save_search_patterns();
8476 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008477 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008480 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8481 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8482 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008483 /* Function was unreferenced while being used, free it
8484 * now. */
8485 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 restoreRedobuff();
8487 restore_search_patterns();
8488 error = ERROR_NONE;
8489 }
8490 }
8491 }
8492 else
8493 {
8494 /*
8495 * Find the function name in the table, call its implementation.
8496 */
8497 i = find_internal_func(fname);
8498 if (i >= 0)
8499 {
8500 if (argcount < functions[i].f_min_argc)
8501 error = ERROR_TOOFEW;
8502 else if (argcount > functions[i].f_max_argc)
8503 error = ERROR_TOOMANY;
8504 else
8505 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008507 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 error = ERROR_NONE;
8509 }
8510 }
8511 }
8512 /*
8513 * The function call (or "FuncUndefined" autocommand sequence) might
8514 * have been aborted by an error, an interrupt, or an explicitly thrown
8515 * exception that has not been caught so far. This situation can be
8516 * tested for by calling aborting(). For an error in an internal
8517 * function or for the "E132" error in call_user_func(), however, the
8518 * throw point at which the "force_abort" flag (temporarily reset by
8519 * emsg()) is normally updated has not been reached yet. We need to
8520 * update that flag first to make aborting() reliable.
8521 */
8522 update_force_abort();
8523 }
8524 if (error == ERROR_NONE)
8525 ret = OK;
8526
8527 /*
8528 * Report an error unless the argument evaluation or function call has been
8529 * cancelled due to an aborting error, an interrupt, or an exception.
8530 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008531 if (!aborting())
8532 {
8533 switch (error)
8534 {
8535 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008536 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008537 break;
8538 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008539 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008540 break;
8541 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008542 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008543 name);
8544 break;
8545 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008546 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008547 name);
8548 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008549 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008550 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008551 name);
8552 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 }
8554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 if (fname != name && fname != fname_buf)
8557 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008558 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559
8560 return ret;
8561}
8562
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563/*
8564 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008565 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008566 */
8567 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008568emsg_funcname(ermsg, name)
8569 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008570 char_u *name;
8571{
8572 char_u *p;
8573
8574 if (*name == K_SPECIAL)
8575 p = concat_str((char_u *)"<SNR>", name + 3);
8576 else
8577 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008578 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008579 if (p != name)
8580 vim_free(p);
8581}
8582
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008583/*
8584 * Return TRUE for a non-zero Number and a non-empty String.
8585 */
8586 static int
8587non_zero_arg(argvars)
8588 typval_T *argvars;
8589{
8590 return ((argvars[0].v_type == VAR_NUMBER
8591 && argvars[0].vval.v_number != 0)
8592 || (argvars[0].v_type == VAR_STRING
8593 && argvars[0].vval.v_string != NULL
8594 && *argvars[0].vval.v_string != NUL));
8595}
8596
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597/*********************************************
8598 * Implementation of the built-in functions
8599 */
8600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008601#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008602static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8603
8604/*
8605 * Get the float value of "argvars[0]" into "f".
8606 * Returns FAIL when the argument is not a Number or Float.
8607 */
8608 static int
8609get_float_arg(argvars, f)
8610 typval_T *argvars;
8611 float_T *f;
8612{
8613 if (argvars[0].v_type == VAR_FLOAT)
8614 {
8615 *f = argvars[0].vval.v_float;
8616 return OK;
8617 }
8618 if (argvars[0].v_type == VAR_NUMBER)
8619 {
8620 *f = (float_T)argvars[0].vval.v_number;
8621 return OK;
8622 }
8623 EMSG(_("E808: Number or Float required"));
8624 return FAIL;
8625}
8626
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008627/*
8628 * "abs(expr)" function
8629 */
8630 static void
8631f_abs(argvars, rettv)
8632 typval_T *argvars;
8633 typval_T *rettv;
8634{
8635 if (argvars[0].v_type == VAR_FLOAT)
8636 {
8637 rettv->v_type = VAR_FLOAT;
8638 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8639 }
8640 else
8641 {
8642 varnumber_T n;
8643 int error = FALSE;
8644
8645 n = get_tv_number_chk(&argvars[0], &error);
8646 if (error)
8647 rettv->vval.v_number = -1;
8648 else if (n > 0)
8649 rettv->vval.v_number = n;
8650 else
8651 rettv->vval.v_number = -n;
8652 }
8653}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008654
8655/*
8656 * "acos()" function
8657 */
8658 static void
8659f_acos(argvars, rettv)
8660 typval_T *argvars;
8661 typval_T *rettv;
8662{
8663 float_T f;
8664
8665 rettv->v_type = VAR_FLOAT;
8666 if (get_float_arg(argvars, &f) == OK)
8667 rettv->vval.v_float = acos(f);
8668 else
8669 rettv->vval.v_float = 0.0;
8670}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008671#endif
8672
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008674 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 */
8676 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008677f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008686 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008687 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008688 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008690 }
8691 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008692 EMSG(_(e_listreq));
8693}
8694
8695/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008696 * "and(expr, expr)" function
8697 */
8698 static void
8699f_and(argvars, rettv)
8700 typval_T *argvars;
8701 typval_T *rettv;
8702{
8703 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8704 & get_tv_number_chk(&argvars[1], NULL);
8705}
8706
8707/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008708 * "append(lnum, string/list)" function
8709 */
8710 static void
8711f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 typval_T *argvars;
8713 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008714{
8715 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 list_T *l = NULL;
8718 listitem_T *li = NULL;
8719 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 long added = 0;
8721
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722 lnum = get_tv_lnum(argvars);
8723 if (lnum >= 0
8724 && lnum <= curbuf->b_ml.ml_line_count
8725 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008726 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008727 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008728 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729 l = argvars[1].vval.v_list;
8730 if (l == NULL)
8731 return;
8732 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008733 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 for (;;)
8735 {
8736 if (l == NULL)
8737 tv = &argvars[1]; /* append a string */
8738 else if (li == NULL)
8739 break; /* end of list */
8740 else
8741 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 line = get_tv_string_chk(tv);
8743 if (line == NULL) /* type error */
8744 {
8745 rettv->vval.v_number = 1; /* Failed */
8746 break;
8747 }
8748 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008749 ++added;
8750 if (l == NULL)
8751 break;
8752 li = li->li_next;
8753 }
8754
8755 appended_lines_mark(lnum, added);
8756 if (curwin->w_cursor.lnum > lnum)
8757 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008759 else
8760 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761}
8762
8763/*
8764 * "argc()" function
8765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008768 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772}
8773
8774/*
8775 * "argidx()" function
8776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008779 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784
8785/*
8786 * "argv(nr)" function
8787 */
8788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *argvars;
8791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
8793 int idx;
8794
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008795 if (argvars[0].v_type != VAR_UNKNOWN)
8796 {
8797 idx = get_tv_number_chk(&argvars[0], NULL);
8798 if (idx >= 0 && idx < ARGCOUNT)
8799 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8800 else
8801 rettv->vval.v_string = NULL;
8802 rettv->v_type = VAR_STRING;
8803 }
8804 else if (rettv_list_alloc(rettv) == OK)
8805 for (idx = 0; idx < ARGCOUNT; ++idx)
8806 list_append_string(rettv->vval.v_list,
8807 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808}
8809
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008810#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008811/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008812 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008813 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008814 static void
8815f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008816 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008817 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008818{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008819 float_T f;
8820
8821 rettv->v_type = VAR_FLOAT;
8822 if (get_float_arg(argvars, &f) == OK)
8823 rettv->vval.v_float = asin(f);
8824 else
8825 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008826}
8827
8828/*
8829 * "atan()" function
8830 */
8831 static void
8832f_atan(argvars, rettv)
8833 typval_T *argvars;
8834 typval_T *rettv;
8835{
8836 float_T f;
8837
8838 rettv->v_type = VAR_FLOAT;
8839 if (get_float_arg(argvars, &f) == OK)
8840 rettv->vval.v_float = atan(f);
8841 else
8842 rettv->vval.v_float = 0.0;
8843}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008844
8845/*
8846 * "atan2()" function
8847 */
8848 static void
8849f_atan2(argvars, rettv)
8850 typval_T *argvars;
8851 typval_T *rettv;
8852{
8853 float_T fx, fy;
8854
8855 rettv->v_type = VAR_FLOAT;
8856 if (get_float_arg(argvars, &fx) == OK
8857 && get_float_arg(&argvars[1], &fy) == OK)
8858 rettv->vval.v_float = atan2(fx, fy);
8859 else
8860 rettv->vval.v_float = 0.0;
8861}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008862#endif
8863
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864/*
8865 * "browse(save, title, initdir, default)" function
8866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008869 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872#ifdef FEAT_BROWSE
8873 int save;
8874 char_u *title;
8875 char_u *initdir;
8876 char_u *defname;
8877 char_u buf[NUMBUFLEN];
8878 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 save = get_tv_number_chk(&argvars[0], &error);
8882 title = get_tv_string_chk(&argvars[1]);
8883 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8884 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 if (error || title == NULL || initdir == NULL || defname == NULL)
8887 rettv->vval.v_string = NULL;
8888 else
8889 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008890 do_browse(save ? BROWSE_SAVE : 0,
8891 title, defname, NULL, initdir, NULL, curbuf);
8892#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008894#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008896}
8897
8898/*
8899 * "browsedir(title, initdir)" function
8900 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008905{
8906#ifdef FEAT_BROWSE
8907 char_u *title;
8908 char_u *initdir;
8909 char_u buf[NUMBUFLEN];
8910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 title = get_tv_string_chk(&argvars[0]);
8912 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 if (title == NULL || initdir == NULL)
8915 rettv->vval.v_string = NULL;
8916 else
8917 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008918 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923}
8924
Bram Moolenaar33570922005-01-25 22:26:29 +00008925static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927/*
8928 * Find a buffer by number or exact name.
8929 */
8930 static buf_T *
8931find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
8934 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008936 if (avar->v_type == VAR_NUMBER)
8937 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008938 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008940 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008941 if (buf == NULL)
8942 {
8943 /* No full path name match, try a match with a URL or a "nofile"
8944 * buffer, these don't use the full path. */
8945 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8946 if (buf->b_fname != NULL
8947 && (path_with_url(buf->b_fname)
8948#ifdef FEAT_QUICKFIX
8949 || bt_nofile(buf)
8950#endif
8951 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008953 break;
8954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 }
8956 return buf;
8957}
8958
8959/*
8960 * "bufexists(expr)" function
8961 */
8962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008964 typval_T *argvars;
8965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968}
8969
8970/*
8971 * "buflisted(expr)" function
8972 */
8973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 typval_T *argvars;
8976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 buf_T *buf;
8979
8980 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984/*
8985 * "bufloaded(expr)" function
8986 */
8987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 buf_T *buf;
8993
8994 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
Bram Moolenaar33570922005-01-25 22:26:29 +00008998static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000/*
9001 * Get buffer by number or pattern.
9002 */
9003 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 int save_magic;
9009 char_u *save_cpo;
9010 buf_T *buf;
9011
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 if (tv->v_type == VAR_NUMBER)
9013 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009014 if (tv->v_type != VAR_STRING)
9015 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 if (name == NULL || *name == NUL)
9017 return curbuf;
9018 if (name[0] == '$' && name[1] == NUL)
9019 return lastbuf;
9020
9021 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9022 save_magic = p_magic;
9023 p_magic = TRUE;
9024 save_cpo = p_cpo;
9025 p_cpo = (char_u *)"";
9026
9027 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9028 TRUE, FALSE));
9029
9030 p_magic = save_magic;
9031 p_cpo = save_cpo;
9032
9033 /* If not found, try expanding the name, like done for bufexists(). */
9034 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036
9037 return buf;
9038}
9039
9040/*
9041 * "bufname(expr)" function
9042 */
9043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 typval_T *argvars;
9046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 buf_T *buf;
9049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 buf = get_buf_tv(&argvars[0]);
9053 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 --emsg_off;
9059}
9060
9061/*
9062 * "bufnr(expr)" function
9063 */
9064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T *argvars;
9067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
9069 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009070 int error = FALSE;
9071 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009076 --emsg_off;
9077
9078 /* If the buffer isn't found and the second argument is not zero create a
9079 * new buffer. */
9080 if (buf == NULL
9081 && argvars[1].v_type != VAR_UNKNOWN
9082 && get_tv_number_chk(&argvars[1], &error) != 0
9083 && !error
9084 && (name = get_tv_string_chk(&argvars[0])) != NULL
9085 && !error)
9086 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9087
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092}
9093
9094/*
9095 * "bufwinnr(nr)" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102#ifdef FEAT_WINDOWS
9103 win_T *wp;
9104 int winnr = 0;
9105#endif
9106 buf_T *buf;
9107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111#ifdef FEAT_WINDOWS
9112 for (wp = firstwin; wp; wp = wp->w_next)
9113 {
9114 ++winnr;
9115 if (wp->w_buffer == buf)
9116 break;
9117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#endif
9122 --emsg_off;
9123}
9124
9125/*
9126 * "byte2line(byte)" function
9127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009130 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#else
9136 long boff = 0;
9137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 (linenr_T)0, &boff);
9144#endif
9145}
9146
9147/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009148 * "byteidx()" function
9149 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009152 typval_T *argvars;
9153 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009154{
9155#ifdef FEAT_MBYTE
9156 char_u *t;
9157#endif
9158 char_u *str;
9159 long idx;
9160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009161 str = get_tv_string_chk(&argvars[0]);
9162 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009165 return;
9166
9167#ifdef FEAT_MBYTE
9168 t = str;
9169 for ( ; idx > 0; idx--)
9170 {
9171 if (*t == NUL) /* EOL reached */
9172 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009173 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009174 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009175 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009176#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009177 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009179#endif
9180}
9181
Bram Moolenaardb913952012-06-29 12:54:53 +02009182 int
9183func_call(name, args, selfdict, rettv)
9184 char_u *name;
9185 typval_T *args;
9186 dict_T *selfdict;
9187 typval_T *rettv;
9188{
9189 listitem_T *item;
9190 typval_T argv[MAX_FUNC_ARGS + 1];
9191 int argc = 0;
9192 int dummy;
9193 int r = 0;
9194
9195 for (item = args->vval.v_list->lv_first; item != NULL;
9196 item = item->li_next)
9197 {
9198 if (argc == MAX_FUNC_ARGS)
9199 {
9200 EMSG(_("E699: Too many arguments"));
9201 break;
9202 }
9203 /* Make a copy of each argument. This is needed to be able to set
9204 * v_lock to VAR_FIXED in the copy without changing the original list.
9205 */
9206 copy_tv(&item->li_tv, &argv[argc++]);
9207 }
9208
9209 if (item == NULL)
9210 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9211 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9212 &dummy, TRUE, selfdict);
9213
9214 /* Free the arguments. */
9215 while (argc > 0)
9216 clear_tv(&argv[--argc]);
9217
9218 return r;
9219}
9220
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009221/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009222 * "call(func, arglist)" function
9223 */
9224 static void
9225f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *argvars;
9227 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009228{
9229 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009231
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009232 if (argvars[1].v_type != VAR_LIST)
9233 {
9234 EMSG(_(e_listreq));
9235 return;
9236 }
9237 if (argvars[1].vval.v_list == NULL)
9238 return;
9239
9240 if (argvars[0].v_type == VAR_FUNC)
9241 func = argvars[0].vval.v_string;
9242 else
9243 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009244 if (*func == NUL)
9245 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246
Bram Moolenaare9a41262005-01-15 22:18:47 +00009247 if (argvars[2].v_type != VAR_UNKNOWN)
9248 {
9249 if (argvars[2].v_type != VAR_DICT)
9250 {
9251 EMSG(_(e_dictreq));
9252 return;
9253 }
9254 selfdict = argvars[2].vval.v_dict;
9255 }
9256
Bram Moolenaardb913952012-06-29 12:54:53 +02009257 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009258}
9259
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009260#ifdef FEAT_FLOAT
9261/*
9262 * "ceil({float})" function
9263 */
9264 static void
9265f_ceil(argvars, rettv)
9266 typval_T *argvars;
9267 typval_T *rettv;
9268{
9269 float_T f;
9270
9271 rettv->v_type = VAR_FLOAT;
9272 if (get_float_arg(argvars, &f) == OK)
9273 rettv->vval.v_float = ceil(f);
9274 else
9275 rettv->vval.v_float = 0.0;
9276}
9277#endif
9278
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009279/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009280 * "changenr()" function
9281 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009282 static void
9283f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009284 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009285 typval_T *rettv;
9286{
9287 rettv->vval.v_number = curbuf->b_u_seq_cur;
9288}
9289
9290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 * "char2nr(string)" function
9292 */
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298#ifdef FEAT_MBYTE
9299 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 else
9302#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304}
9305
9306/*
9307 * "cindent(lnum)" function
9308 */
9309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 typval_T *argvars;
9312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314#ifdef FEAT_CINDENT
9315 pos_T pos;
9316 linenr_T lnum;
9317
9318 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9321 {
9322 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 curwin->w_cursor = pos;
9325 }
9326 else
9327#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329}
9330
9331/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009332 * "clearmatches()" function
9333 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009334 static void
9335f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009336 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009337 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009338{
9339#ifdef FEAT_SEARCH_EXTRA
9340 clear_matches(curwin);
9341#endif
9342}
9343
9344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 * "col(string)" function
9346 */
9347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009349 typval_T *argvars;
9350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
9352 colnr_T col = 0;
9353 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009354 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009356 fp = var2fpos(&argvars[0], FALSE, &fnum);
9357 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 {
9359 if (fp->col == MAXCOL)
9360 {
9361 /* '> can be MAXCOL, get the length of the line then */
9362 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009363 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 else
9365 col = MAXCOL;
9366 }
9367 else
9368 {
9369 col = fp->col + 1;
9370#ifdef FEAT_VIRTUALEDIT
9371 /* col(".") when the cursor is on the NUL at the end of the line
9372 * because of "coladd" can be seen as an extra column. */
9373 if (virtual_active() && fp == &curwin->w_cursor)
9374 {
9375 char_u *p = ml_get_cursor();
9376
9377 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9378 curwin->w_virtcol - curwin->w_cursor.coladd))
9379 {
9380# ifdef FEAT_MBYTE
9381 int l;
9382
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009383 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 col += l;
9385# else
9386 if (*p != NUL && p[1] == NUL)
9387 ++col;
9388# endif
9389 }
9390 }
9391#endif
9392 }
9393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
Bram Moolenaar572cb562005-08-05 21:35:02 +00009397#if defined(FEAT_INS_EXPAND)
9398/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009399 * "complete()" function
9400 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009401 static void
9402f_complete(argvars, rettv)
9403 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009404 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009405{
9406 int startcol;
9407
9408 if ((State & INSERT) == 0)
9409 {
9410 EMSG(_("E785: complete() can only be used in Insert mode"));
9411 return;
9412 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009413
9414 /* Check for undo allowed here, because if something was already inserted
9415 * the line was already saved for undo and this check isn't done. */
9416 if (!undo_allowed())
9417 return;
9418
Bram Moolenaarade00832006-03-10 21:46:58 +00009419 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9420 {
9421 EMSG(_(e_invarg));
9422 return;
9423 }
9424
9425 startcol = get_tv_number_chk(&argvars[0], NULL);
9426 if (startcol <= 0)
9427 return;
9428
9429 set_completion(startcol - 1, argvars[1].vval.v_list);
9430}
9431
9432/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009433 * "complete_add()" function
9434 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009435 static void
9436f_complete_add(argvars, rettv)
9437 typval_T *argvars;
9438 typval_T *rettv;
9439{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009440 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009441}
9442
9443/*
9444 * "complete_check()" function
9445 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009446 static void
9447f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009448 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009449 typval_T *rettv;
9450{
9451 int saved = RedrawingDisabled;
9452
9453 RedrawingDisabled = 0;
9454 ins_compl_check_keys(0);
9455 rettv->vval.v_number = compl_interrupted;
9456 RedrawingDisabled = saved;
9457}
9458#endif
9459
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460/*
9461 * "confirm(message, buttons[, default [, type]])" function
9462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009465 typval_T *argvars UNUSED;
9466 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9469 char_u *message;
9470 char_u *buttons = NULL;
9471 char_u buf[NUMBUFLEN];
9472 char_u buf2[NUMBUFLEN];
9473 int def = 1;
9474 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 char_u *typestr;
9476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 message = get_tv_string_chk(&argvars[0]);
9479 if (message == NULL)
9480 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009481 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009483 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9484 if (buttons == NULL)
9485 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009486 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009488 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009489 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009491 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9492 if (typestr == NULL)
9493 error = TRUE;
9494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 switch (TOUPPER_ASC(*typestr))
9497 {
9498 case 'E': type = VIM_ERROR; break;
9499 case 'Q': type = VIM_QUESTION; break;
9500 case 'I': type = VIM_INFO; break;
9501 case 'W': type = VIM_WARNING; break;
9502 case 'G': type = VIM_GENERIC; break;
9503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 }
9505 }
9506 }
9507 }
9508
9509 if (buttons == NULL || *buttons == NUL)
9510 buttons = (char_u *)_("&Ok");
9511
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009512 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009514 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515#endif
9516}
9517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518/*
9519 * "copy()" function
9520 */
9521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009523 typval_T *argvars;
9524 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009526 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009529#ifdef FEAT_FLOAT
9530/*
9531 * "cos()" function
9532 */
9533 static void
9534f_cos(argvars, rettv)
9535 typval_T *argvars;
9536 typval_T *rettv;
9537{
9538 float_T f;
9539
9540 rettv->v_type = VAR_FLOAT;
9541 if (get_float_arg(argvars, &f) == OK)
9542 rettv->vval.v_float = cos(f);
9543 else
9544 rettv->vval.v_float = 0.0;
9545}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009546
9547/*
9548 * "cosh()" function
9549 */
9550 static void
9551f_cosh(argvars, rettv)
9552 typval_T *argvars;
9553 typval_T *rettv;
9554{
9555 float_T f;
9556
9557 rettv->v_type = VAR_FLOAT;
9558 if (get_float_arg(argvars, &f) == OK)
9559 rettv->vval.v_float = cosh(f);
9560 else
9561 rettv->vval.v_float = 0.0;
9562}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009563#endif
9564
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566 * "count()" function
9567 */
9568 static void
9569f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *argvars;
9571 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009572{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009573 long n = 0;
9574 int ic = FALSE;
9575
Bram Moolenaare9a41262005-01-15 22:18:47 +00009576 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009577 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009578 listitem_T *li;
9579 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009580 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009581
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 if ((l = argvars[0].vval.v_list) != NULL)
9583 {
9584 li = l->lv_first;
9585 if (argvars[2].v_type != VAR_UNKNOWN)
9586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 int error = FALSE;
9588
9589 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009590 if (argvars[3].v_type != VAR_UNKNOWN)
9591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 idx = get_tv_number_chk(&argvars[3], &error);
9593 if (!error)
9594 {
9595 li = list_find(l, idx);
9596 if (li == NULL)
9597 EMSGN(_(e_listidx), idx);
9598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 if (error)
9601 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009602 }
9603
9604 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009605 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 ++n;
9607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 else if (argvars[0].v_type == VAR_DICT)
9610 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 int todo;
9612 dict_T *d;
9613 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009615 if ((d = argvars[0].vval.v_dict) != NULL)
9616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009617 int error = FALSE;
9618
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 if (argvars[2].v_type != VAR_UNKNOWN)
9620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 if (argvars[3].v_type != VAR_UNKNOWN)
9623 EMSG(_(e_invarg));
9624 }
9625
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009626 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009628 {
9629 if (!HASHITEM_EMPTY(hi))
9630 {
9631 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009632 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009633 ++n;
9634 }
9635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 }
9637 }
9638 else
9639 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009640 rettv->vval.v_number = n;
9641}
9642
9643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9645 *
9646 * Checks the existence of a cscope connection.
9647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009650 typval_T *argvars UNUSED;
9651 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653#ifdef FEAT_CSCOPE
9654 int num = 0;
9655 char_u *dbpath = NULL;
9656 char_u *prepend = NULL;
9657 char_u buf[NUMBUFLEN];
9658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009659 if (argvars[0].v_type != VAR_UNKNOWN
9660 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 num = (int)get_tv_number(&argvars[0]);
9663 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009664 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 }
9667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669#endif
9670}
9671
9672/*
9673 * "cursor(lnum, col)" function
9674 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009675 * Moves the cursor to the specified line and column.
9676 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009684#ifdef FEAT_VIRTUALEDIT
9685 long coladd = 0;
9686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009688 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009689 if (argvars[1].v_type == VAR_UNKNOWN)
9690 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009691 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009692
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009693 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009694 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009695 line = pos.lnum;
9696 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009697#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009698 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009699#endif
9700 }
9701 else
9702 {
9703 line = get_tv_lnum(argvars);
9704 col = get_tv_number_chk(&argvars[1], NULL);
9705#ifdef FEAT_VIRTUALEDIT
9706 if (argvars[2].v_type != VAR_UNKNOWN)
9707 coladd = get_tv_number_chk(&argvars[2], NULL);
9708#endif
9709 }
9710 if (line < 0 || col < 0
9711#ifdef FEAT_VIRTUALEDIT
9712 || coladd < 0
9713#endif
9714 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (line > 0)
9717 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 if (col > 0)
9719 curwin->w_cursor.col = col - 1;
9720#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009721 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722#endif
9723
9724 /* Make sure the cursor is in a valid position. */
9725 check_cursor();
9726#ifdef FEAT_MBYTE
9727 /* Correct cursor for multi-byte character. */
9728 if (has_mbyte)
9729 mb_adjust_cursor();
9730#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009731
9732 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009733 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734}
9735
9736/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009737 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 */
9739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009741 typval_T *argvars;
9742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009744 int noref = 0;
9745
9746 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009748 if (noref < 0 || noref > 1)
9749 EMSG(_(e_invarg));
9750 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009751 {
9752 current_copyID += COPYID_INC;
9753 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755}
9756
9757/*
9758 * "delete()" function
9759 */
9760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009762 typval_T *argvars;
9763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
9765 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771/*
9772 * "did_filetype()" function
9773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009776 typval_T *argvars UNUSED;
9777 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#endif
9782}
9783
9784/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009785 * "diff_filler()" function
9786 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009789 typval_T *argvars UNUSED;
9790 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009791{
9792#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009794#endif
9795}
9796
9797/*
9798 * "diff_hlID()" function
9799 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009802 typval_T *argvars UNUSED;
9803 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009804{
9805#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009807 static linenr_T prev_lnum = 0;
9808 static int changedtick = 0;
9809 static int fnum = 0;
9810 static int change_start = 0;
9811 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009812 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009813 int filler_lines;
9814 int col;
9815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 if (lnum < 0) /* ignore type error in {lnum} arg */
9817 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009818 if (lnum != prev_lnum
9819 || changedtick != curbuf->b_changedtick
9820 || fnum != curbuf->b_fnum)
9821 {
9822 /* New line, buffer, change: need to get the values. */
9823 filler_lines = diff_check(curwin, lnum);
9824 if (filler_lines < 0)
9825 {
9826 if (filler_lines == -1)
9827 {
9828 change_start = MAXCOL;
9829 change_end = -1;
9830 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9831 hlID = HLF_ADD; /* added line */
9832 else
9833 hlID = HLF_CHD; /* changed line */
9834 }
9835 else
9836 hlID = HLF_ADD; /* added line */
9837 }
9838 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009839 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009840 prev_lnum = lnum;
9841 changedtick = curbuf->b_changedtick;
9842 fnum = curbuf->b_fnum;
9843 }
9844
9845 if (hlID == HLF_CHD || hlID == HLF_TXD)
9846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009848 if (col >= change_start && col <= change_end)
9849 hlID = HLF_TXD; /* changed text */
9850 else
9851 hlID = HLF_CHD; /* changed line */
9852 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009853 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009854#endif
9855}
9856
9857/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009858 * "empty({expr})" function
9859 */
9860 static void
9861f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009862 typval_T *argvars;
9863 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009864{
9865 int n;
9866
9867 switch (argvars[0].v_type)
9868 {
9869 case VAR_STRING:
9870 case VAR_FUNC:
9871 n = argvars[0].vval.v_string == NULL
9872 || *argvars[0].vval.v_string == NUL;
9873 break;
9874 case VAR_NUMBER:
9875 n = argvars[0].vval.v_number == 0;
9876 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009877#ifdef FEAT_FLOAT
9878 case VAR_FLOAT:
9879 n = argvars[0].vval.v_float == 0.0;
9880 break;
9881#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009882 case VAR_LIST:
9883 n = argvars[0].vval.v_list == NULL
9884 || argvars[0].vval.v_list->lv_first == NULL;
9885 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886 case VAR_DICT:
9887 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009889 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009890 default:
9891 EMSG2(_(e_intern2), "f_empty()");
9892 n = 0;
9893 }
9894
9895 rettv->vval.v_number = n;
9896}
9897
9898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 * "escape({string}, {chars})" function
9900 */
9901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009903 typval_T *argvars;
9904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
9906 char_u buf[NUMBUFLEN];
9907
Bram Moolenaar758711c2005-02-02 23:11:38 +00009908 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9909 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911}
9912
9913/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009914 * "eval()" function
9915 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009916 static void
9917f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 typval_T *argvars;
9919 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009920{
9921 char_u *s;
9922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 s = get_tv_string_chk(&argvars[0]);
9924 if (s != NULL)
9925 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9928 {
9929 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009930 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932 else if (*s != NUL)
9933 EMSG(_(e_trailing));
9934}
9935
9936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 * "eventhandler()" function
9938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009941 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947/*
9948 * "executable()" function
9949 */
9950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 typval_T *argvars;
9953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956}
9957
9958/*
9959 * "exists()" function
9960 */
9961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *argvars;
9964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966 char_u *p;
9967 char_u *name;
9968 int n = FALSE;
9969 int len = 0;
9970
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009971 no_autoload = TRUE;
9972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 if (*p == '$') /* environment variable */
9975 {
9976 /* first try "normal" environment variables (fast) */
9977 if (mch_getenv(p + 1) != NULL)
9978 n = TRUE;
9979 else
9980 {
9981 /* try expanding things like $VIM and ${HOME} */
9982 p = expand_env_save(p);
9983 if (p != NULL && *p != '$')
9984 n = TRUE;
9985 vim_free(p);
9986 }
9987 }
9988 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009991 if (*skipwhite(p) != NUL)
9992 n = FALSE; /* trailing garbage */
9993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 else if (*p == '*') /* internal or user defined function */
9995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009996 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 }
9998 else if (*p == ':')
9999 {
10000 n = cmd_exists(p + 1);
10001 }
10002 else if (*p == '#')
10003 {
10004#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010005 if (p[1] == '#')
10006 n = autocmd_supported(p + 2);
10007 else
10008 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009#endif
10010 }
10011 else /* internal variable */
10012 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010013 char_u *tofree;
10014 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010016 /* get_name_len() takes care of expanding curly braces */
10017 name = p;
10018 len = get_name_len(&p, &tofree, TRUE, FALSE);
10019 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010021 if (tofree != NULL)
10022 name = tofree;
10023 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10024 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010026 /* handle d.key, l[idx], f(expr) */
10027 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10028 if (n)
10029 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010032 if (*p != NUL)
10033 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010035 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010039
10040 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010043#ifdef FEAT_FLOAT
10044/*
10045 * "exp()" function
10046 */
10047 static void
10048f_exp(argvars, rettv)
10049 typval_T *argvars;
10050 typval_T *rettv;
10051{
10052 float_T f;
10053
10054 rettv->v_type = VAR_FLOAT;
10055 if (get_float_arg(argvars, &f) == OK)
10056 rettv->vval.v_float = exp(f);
10057 else
10058 rettv->vval.v_float = 0.0;
10059}
10060#endif
10061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062/*
10063 * "expand()" function
10064 */
10065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069{
10070 char_u *s;
10071 int len;
10072 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010073 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010076 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010079 if (argvars[1].v_type != VAR_UNKNOWN
10080 && argvars[2].v_type != VAR_UNKNOWN
10081 && get_tv_number_chk(&argvars[2], &error)
10082 && !error)
10083 {
10084 rettv->v_type = VAR_LIST;
10085 rettv->vval.v_list = NULL;
10086 }
10087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 if (*s == '%' || *s == '#' || *s == '<')
10090 {
10091 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010092 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010094 if (rettv->v_type == VAR_LIST)
10095 {
10096 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10097 list_append_string(rettv->vval.v_list, result, -1);
10098 else
10099 vim_free(result);
10100 }
10101 else
10102 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 }
10104 else
10105 {
10106 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010107 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108 if (argvars[1].v_type != VAR_UNKNOWN
10109 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010110 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 if (!error)
10112 {
10113 ExpandInit(&xpc);
10114 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010115 if (p_wic)
10116 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010117 if (rettv->v_type == VAR_STRING)
10118 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10119 options, WILD_ALL);
10120 else if (rettv_list_alloc(rettv) != FAIL)
10121 {
10122 int i;
10123
10124 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10125 for (i = 0; i < xpc.xp_numfiles; i++)
10126 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10127 ExpandCleanup(&xpc);
10128 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 }
10130 else
10131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133}
10134
10135/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010136 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010137 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010138 */
10139 static void
10140f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010141 typval_T *argvars;
10142 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010143{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010144 char *arg_errmsg = N_("extend() argument");
10145
Bram Moolenaare9a41262005-01-15 22:18:47 +000010146 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010148 list_T *l1, *l2;
10149 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010150 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010152
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 l1 = argvars[0].vval.v_list;
10154 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010155 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010156 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010157 {
10158 if (argvars[2].v_type != VAR_UNKNOWN)
10159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 before = get_tv_number_chk(&argvars[2], &error);
10161 if (error)
10162 return; /* type error; errmsg already given */
10163
Bram Moolenaar758711c2005-02-02 23:11:38 +000010164 if (before == l1->lv_len)
10165 item = NULL;
10166 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010168 item = list_find(l1, before);
10169 if (item == NULL)
10170 {
10171 EMSGN(_(e_listidx), before);
10172 return;
10173 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010174 }
10175 }
10176 else
10177 item = NULL;
10178 list_extend(l1, l2, item);
10179
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 copy_tv(&argvars[0], rettv);
10181 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010182 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10184 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010185 dict_T *d1, *d2;
10186 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 char_u *action;
10188 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010189 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010190 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191
10192 d1 = argvars[0].vval.v_dict;
10193 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010194 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010195 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 {
10197 /* Check the third argument. */
10198 if (argvars[2].v_type != VAR_UNKNOWN)
10199 {
10200 static char *(av[]) = {"keep", "force", "error"};
10201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 action = get_tv_string_chk(&argvars[2]);
10203 if (action == NULL)
10204 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 for (i = 0; i < 3; ++i)
10206 if (STRCMP(action, av[i]) == 0)
10207 break;
10208 if (i == 3)
10209 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010210 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010211 return;
10212 }
10213 }
10214 else
10215 action = (char_u *)"force";
10216
10217 /* Go over all entries in the second dict and add them to the
10218 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010219 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010220 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010222 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010223 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010224 --todo;
10225 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010226 if (d1->dv_scope != 0)
10227 {
10228 /* Disallow replacing a builtin function in l: and g:.
10229 * Check the key to be valid when adding to any
10230 * scope. */
10231 if (d1->dv_scope == VAR_DEF_SCOPE
10232 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10233 && var_check_func_name(hi2->hi_key,
10234 di1 == NULL))
10235 break;
10236 if (!valid_varname(hi2->hi_key))
10237 break;
10238 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010239 if (di1 == NULL)
10240 {
10241 di1 = dictitem_copy(HI2DI(hi2));
10242 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10243 dictitem_free(di1);
10244 }
10245 else if (*action == 'e')
10246 {
10247 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10248 break;
10249 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010250 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 {
10252 clear_tv(&di1->di_tv);
10253 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10254 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 }
10256 }
10257
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 copy_tv(&argvars[0], rettv);
10259 }
10260 }
10261 else
10262 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010263}
10264
10265/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010266 * "feedkeys()" function
10267 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010268 static void
10269f_feedkeys(argvars, rettv)
10270 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010271 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010272{
10273 int remap = TRUE;
10274 char_u *keys, *flags;
10275 char_u nbuf[NUMBUFLEN];
10276 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010277 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010278
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010279 /* This is not allowed in the sandbox. If the commands would still be
10280 * executed in the sandbox it would be OK, but it probably happens later,
10281 * when "sandbox" is no longer set. */
10282 if (check_secure())
10283 return;
10284
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010285 keys = get_tv_string(&argvars[0]);
10286 if (*keys != NUL)
10287 {
10288 if (argvars[1].v_type != VAR_UNKNOWN)
10289 {
10290 flags = get_tv_string_buf(&argvars[1], nbuf);
10291 for ( ; *flags != NUL; ++flags)
10292 {
10293 switch (*flags)
10294 {
10295 case 'n': remap = FALSE; break;
10296 case 'm': remap = TRUE; break;
10297 case 't': typed = TRUE; break;
10298 }
10299 }
10300 }
10301
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010302 /* Need to escape K_SPECIAL and CSI before putting the string in the
10303 * typeahead buffer. */
10304 keys_esc = vim_strsave_escape_csi(keys);
10305 if (keys_esc != NULL)
10306 {
10307 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010308 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010309 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010310 if (vgetc_busy)
10311 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010312 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010313 }
10314}
10315
10316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 * "filereadable()" function
10318 */
10319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010324 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 char_u *p;
10326 int n;
10327
Bram Moolenaarc236c162008-07-13 17:41:49 +000010328#ifndef O_NONBLOCK
10329# define O_NONBLOCK 0
10330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010332 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10333 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 {
10335 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010336 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 }
10338 else
10339 n = FALSE;
10340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342}
10343
10344/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010345 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 * rights to write into.
10347 */
10348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010350 typval_T *argvars;
10351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010353 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354}
10355
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010356static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010357
10358 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010359findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T *argvars;
10361 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010362 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010363{
10364#ifdef FEAT_SEARCHPATH
10365 char_u *fname;
10366 char_u *fresult = NULL;
10367 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10368 char_u *p;
10369 char_u pathbuf[NUMBUFLEN];
10370 int count = 1;
10371 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010372 int error = FALSE;
10373#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010374
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010375 rettv->vval.v_string = NULL;
10376 rettv->v_type = VAR_STRING;
10377
10378#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010380
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010381 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010383 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10384 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010385 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 else
10387 {
10388 if (*p != NUL)
10389 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010391 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010392 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010393 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010394 }
10395
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010396 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10397 error = TRUE;
10398
10399 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 do
10402 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010403 if (rettv->v_type == VAR_STRING)
10404 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 fresult = find_file_in_path_option(first ? fname : NULL,
10406 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010407 0, first, path,
10408 find_what,
10409 curbuf->b_ffname,
10410 find_what == FINDFILE_DIR
10411 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010413
10414 if (fresult != NULL && rettv->v_type == VAR_LIST)
10415 list_append_string(rettv->vval.v_list, fresult, -1);
10416
10417 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010420 if (rettv->v_type == VAR_STRING)
10421 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010422#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010423}
10424
Bram Moolenaar33570922005-01-25 22:26:29 +000010425static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10426static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010427
10428/*
10429 * Implementation of map() and filter().
10430 */
10431 static void
10432filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 typval_T *argvars;
10434 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010435 int map;
10436{
10437 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010439 listitem_T *li, *nli;
10440 list_T *l = NULL;
10441 dictitem_T *di;
10442 hashtab_T *ht;
10443 hashitem_T *hi;
10444 dict_T *d = NULL;
10445 typval_T save_val;
10446 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010447 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010448 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010449 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10450 char *arg_errmsg = (map ? N_("map() argument")
10451 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010452 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010453 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010454
Bram Moolenaare9a41262005-01-15 22:18:47 +000010455 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010456 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010457 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010458 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 return;
10460 }
10461 else if (argvars[0].v_type == VAR_DICT)
10462 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010463 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010464 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 return;
10466 }
10467 else
10468 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010469 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010470 return;
10471 }
10472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 expr = get_tv_string_buf_chk(&argvars[1], buf);
10474 /* On type errors, the preceding call has already displayed an error
10475 * message. Avoid a misleading error message for an empty string that
10476 * was not passed as argument. */
10477 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 prepare_vimvar(VV_VAL, &save_val);
10480 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010481
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010482 /* We reset "did_emsg" to be able to detect whether an error
10483 * occurred during evaluation of the expression. */
10484 save_did_emsg = did_emsg;
10485 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010486
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010487 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 vimvars[VV_KEY].vv_type = VAR_STRING;
10491
10492 ht = &d->dv_hashtab;
10493 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010494 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 if (!HASHITEM_EMPTY(hi))
10498 {
10499 --todo;
10500 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010501 if (tv_check_lock(di->di_tv.v_lock,
10502 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 break;
10504 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010505 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010506 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 break;
10508 if (!map && rem)
10509 dictitem_remove(d, di);
10510 clear_tv(&vimvars[VV_KEY].vv_tv);
10511 }
10512 }
10513 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 }
10515 else
10516 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010517 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 for (li = l->lv_first; li != NULL; li = nli)
10520 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010521 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010522 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010524 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010525 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010526 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010527 break;
10528 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010530 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010531 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010532 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010533
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010534 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010536
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010537 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010538 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010539
10540 copy_tv(&argvars[0], rettv);
10541}
10542
10543 static int
10544filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010545 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010546 char_u *expr;
10547 int map;
10548 int *remp;
10549{
Bram Moolenaar33570922005-01-25 22:26:29 +000010550 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010552 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555 s = expr;
10556 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010557 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 if (*s != NUL) /* check for trailing chars after expr */
10559 {
10560 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010561 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 }
10563 if (map)
10564 {
10565 /* map(): replace the list item value */
10566 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010567 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 *tv = rettv;
10569 }
10570 else
10571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 int error = FALSE;
10573
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 /* On type error, nothing has been removed; return FAIL to stop the
10578 * loop. The error message was given by get_tv_number_chk(). */
10579 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010580 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010582 retval = OK;
10583theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010584 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010585 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010586}
10587
10588/*
10589 * "filter()" function
10590 */
10591 static void
10592f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 typval_T *argvars;
10594 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010595{
10596 filter_map(argvars, rettv, FALSE);
10597}
10598
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010600 * "finddir({fname}[, {path}[, {count}]])" function
10601 */
10602 static void
10603f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010604 typval_T *argvars;
10605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010606{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010607 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010608}
10609
10610/*
10611 * "findfile({fname}[, {path}[, {count}]])" function
10612 */
10613 static void
10614f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010615 typval_T *argvars;
10616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010617{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010618 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010619}
10620
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010621#ifdef FEAT_FLOAT
10622/*
10623 * "float2nr({float})" function
10624 */
10625 static void
10626f_float2nr(argvars, rettv)
10627 typval_T *argvars;
10628 typval_T *rettv;
10629{
10630 float_T f;
10631
10632 if (get_float_arg(argvars, &f) == OK)
10633 {
10634 if (f < -0x7fffffff)
10635 rettv->vval.v_number = -0x7fffffff;
10636 else if (f > 0x7fffffff)
10637 rettv->vval.v_number = 0x7fffffff;
10638 else
10639 rettv->vval.v_number = (varnumber_T)f;
10640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010641}
10642
10643/*
10644 * "floor({float})" function
10645 */
10646 static void
10647f_floor(argvars, rettv)
10648 typval_T *argvars;
10649 typval_T *rettv;
10650{
10651 float_T f;
10652
10653 rettv->v_type = VAR_FLOAT;
10654 if (get_float_arg(argvars, &f) == OK)
10655 rettv->vval.v_float = floor(f);
10656 else
10657 rettv->vval.v_float = 0.0;
10658}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010659
10660/*
10661 * "fmod()" function
10662 */
10663 static void
10664f_fmod(argvars, rettv)
10665 typval_T *argvars;
10666 typval_T *rettv;
10667{
10668 float_T fx, fy;
10669
10670 rettv->v_type = VAR_FLOAT;
10671 if (get_float_arg(argvars, &fx) == OK
10672 && get_float_arg(&argvars[1], &fy) == OK)
10673 rettv->vval.v_float = fmod(fx, fy);
10674 else
10675 rettv->vval.v_float = 0.0;
10676}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010677#endif
10678
Bram Moolenaar0d660222005-01-07 21:51:51 +000010679/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010680 * "fnameescape({string})" function
10681 */
10682 static void
10683f_fnameescape(argvars, rettv)
10684 typval_T *argvars;
10685 typval_T *rettv;
10686{
10687 rettv->vval.v_string = vim_strsave_fnameescape(
10688 get_tv_string(&argvars[0]), FALSE);
10689 rettv->v_type = VAR_STRING;
10690}
10691
10692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 * "fnamemodify({fname}, {mods})" function
10694 */
10695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *argvars;
10698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699{
10700 char_u *fname;
10701 char_u *mods;
10702 int usedlen = 0;
10703 int len;
10704 char_u *fbuf = NULL;
10705 char_u buf[NUMBUFLEN];
10706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 fname = get_tv_string_chk(&argvars[0]);
10708 mods = get_tv_string_buf_chk(&argvars[1], buf);
10709 if (fname == NULL || mods == NULL)
10710 fname = NULL;
10711 else
10712 {
10713 len = (int)STRLEN(fname);
10714 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 vim_free(fbuf);
10723}
10724
Bram Moolenaar33570922005-01-25 22:26:29 +000010725static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726
10727/*
10728 * "foldclosed()" function
10729 */
10730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010732 typval_T *argvars;
10733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 int end;
10735{
10736#ifdef FEAT_FOLDING
10737 linenr_T lnum;
10738 linenr_T first, last;
10739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010740 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10742 {
10743 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10744 {
10745 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 return;
10750 }
10751 }
10752#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754}
10755
10756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010757 * "foldclosed()" function
10758 */
10759 static void
10760f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010761 typval_T *argvars;
10762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010763{
10764 foldclosed_both(argvars, rettv, FALSE);
10765}
10766
10767/*
10768 * "foldclosedend()" function
10769 */
10770 static void
10771f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010772 typval_T *argvars;
10773 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774{
10775 foldclosed_both(argvars, rettv, TRUE);
10776}
10777
10778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 * "foldlevel()" function
10780 */
10781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785{
10786#ifdef FEAT_FOLDING
10787 linenr_T lnum;
10788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793}
10794
10795/*
10796 * "foldtext()" function
10797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802{
10803#ifdef FEAT_FOLDING
10804 linenr_T lnum;
10805 char_u *s;
10806 char_u *r;
10807 int len;
10808 char *txt;
10809#endif
10810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 rettv->v_type = VAR_STRING;
10812 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010814 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10815 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10816 <= curbuf->b_ml.ml_line_count
10817 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 {
10819 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10821 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 {
10823 if (!linewhite(lnum))
10824 break;
10825 ++lnum;
10826 }
10827
10828 /* Find interesting text in this line. */
10829 s = skipwhite(ml_get(lnum));
10830 /* skip C comment-start */
10831 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010834 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010836 {
10837 s = skipwhite(ml_get(lnum + 1));
10838 if (*s == '*')
10839 s = skipwhite(s + 1);
10840 }
10841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 txt = _("+-%s%3ld lines: ");
10843 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 + 20 /* for %3ld */
10846 + STRLEN(s))); /* concatenated */
10847 if (r != NULL)
10848 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10850 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10851 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 len = (int)STRLEN(r);
10853 STRCAT(r, s);
10854 /* remove 'foldmarker' and 'commentstring' */
10855 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 }
10858 }
10859#endif
10860}
10861
10862/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010863 * "foldtextresult(lnum)" function
10864 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010869{
10870#ifdef FEAT_FOLDING
10871 linenr_T lnum;
10872 char_u *text;
10873 char_u buf[51];
10874 foldinfo_T foldinfo;
10875 int fold_count;
10876#endif
10877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->v_type = VAR_STRING;
10879 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010880#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 /* treat illegal types and illegal string values for {lnum} the same */
10883 if (lnum < 0)
10884 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010885 fold_count = foldedCount(curwin, lnum, &foldinfo);
10886 if (fold_count > 0)
10887 {
10888 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10889 &foldinfo, buf);
10890 if (text == buf)
10891 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010893 }
10894#endif
10895}
10896
10897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 * "foreground()" function
10899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010901f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010902 typval_T *argvars UNUSED;
10903 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905#ifdef FEAT_GUI
10906 if (gui.in_use)
10907 gui_mch_set_foreground();
10908#else
10909# ifdef WIN32
10910 win32_set_foreground();
10911# endif
10912#endif
10913}
10914
10915/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010916 * "function()" function
10917 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010920 typval_T *argvars;
10921 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010922{
10923 char_u *s;
10924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010926 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010927 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010928 /* Don't check an autoload name for existence here. */
10929 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010930 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010931 else
10932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->vval.v_string = vim_strsave(s);
10934 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010935 }
10936}
10937
10938/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010939 * "garbagecollect()" function
10940 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010941 static void
10942f_garbagecollect(argvars, rettv)
10943 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010944 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010945{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010946 /* This is postponed until we are back at the toplevel, because we may be
10947 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10948 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010949
10950 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10951 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010952}
10953
10954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010955 * "get()" function
10956 */
10957 static void
10958f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010959 typval_T *argvars;
10960 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010961{
Bram Moolenaar33570922005-01-25 22:26:29 +000010962 listitem_T *li;
10963 list_T *l;
10964 dictitem_T *di;
10965 dict_T *d;
10966 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010967
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010969 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 int error = FALSE;
10973
10974 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10975 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010977 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 else if (argvars[0].v_type == VAR_DICT)
10980 {
10981 if ((d = argvars[0].vval.v_dict) != NULL)
10982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010983 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 if (di != NULL)
10985 tv = &di->di_tv;
10986 }
10987 }
10988 else
10989 EMSG2(_(e_listdictarg), "get()");
10990
10991 if (tv == NULL)
10992 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994 copy_tv(&argvars[2], rettv);
10995 }
10996 else
10997 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010998}
10999
Bram Moolenaar342337a2005-07-21 21:11:17 +000011000static 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 +000011001
11002/*
11003 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011004 * Return a range (from start to end) of lines in rettv from the specified
11005 * buffer.
11006 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011007 */
11008 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011009get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011010 buf_T *buf;
11011 linenr_T start;
11012 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011013 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011014 typval_T *rettv;
11015{
11016 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011017
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011018 if (retlist && rettv_list_alloc(rettv) == FAIL)
11019 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011020
11021 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11022 return;
11023
11024 if (!retlist)
11025 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011026 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11027 p = ml_get_buf(buf, start, FALSE);
11028 else
11029 p = (char_u *)"";
11030
11031 rettv->v_type = VAR_STRING;
11032 rettv->vval.v_string = vim_strsave(p);
11033 }
11034 else
11035 {
11036 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011037 return;
11038
11039 if (start < 1)
11040 start = 1;
11041 if (end > buf->b_ml.ml_line_count)
11042 end = buf->b_ml.ml_line_count;
11043 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011044 if (list_append_string(rettv->vval.v_list,
11045 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011046 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011047 }
11048}
11049
11050/*
11051 * "getbufline()" function
11052 */
11053 static void
11054f_getbufline(argvars, rettv)
11055 typval_T *argvars;
11056 typval_T *rettv;
11057{
11058 linenr_T lnum;
11059 linenr_T end;
11060 buf_T *buf;
11061
11062 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11063 ++emsg_off;
11064 buf = get_buf_tv(&argvars[0]);
11065 --emsg_off;
11066
Bram Moolenaar661b1822005-07-28 22:36:45 +000011067 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011068 if (argvars[2].v_type == VAR_UNKNOWN)
11069 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011070 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011071 end = get_tv_lnum_buf(&argvars[2], buf);
11072
Bram Moolenaar342337a2005-07-21 21:11:17 +000011073 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011074}
11075
Bram Moolenaar0d660222005-01-07 21:51:51 +000011076/*
11077 * "getbufvar()" function
11078 */
11079 static void
11080f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011081 typval_T *argvars;
11082 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011083{
11084 buf_T *buf;
11085 buf_T *save_curbuf;
11086 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11090 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011091 ++emsg_off;
11092 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011093
11094 rettv->v_type = VAR_STRING;
11095 rettv->vval.v_string = NULL;
11096
11097 if (buf != NULL && varname != NULL)
11098 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011099 /* set curbuf to be our buf, temporarily */
11100 save_curbuf = curbuf;
11101 curbuf = buf;
11102
Bram Moolenaar0d660222005-01-07 21:51:51 +000011103 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011104 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011105 else if (STRCMP(varname, "changedtick") == 0)
11106 {
11107 rettv->v_type = VAR_NUMBER;
11108 rettv->vval.v_number = curbuf->b_changedtick;
11109 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110 else
11111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 if (*varname == NUL)
11113 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11114 * scope prefix before the NUL byte is required by
11115 * find_var_in_ht(). */
11116 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011118 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011122
11123 /* restore previous notion of curbuf */
11124 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 }
11126
11127 --emsg_off;
11128}
11129
11130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 * "getchar()" function
11132 */
11133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011141 /* Position the cursor. Needed after a message that ends in a space. */
11142 windgoto(msg_row, msg_col);
11143
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 ++no_mapping;
11145 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011146 for (;;)
11147 {
11148 if (argvars[0].v_type == VAR_UNKNOWN)
11149 /* getchar(): blocking wait. */
11150 n = safe_vgetc();
11151 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11152 /* getchar(1): only check if char avail */
11153 n = vpeekc();
11154 else if (error || vpeekc() == NUL)
11155 /* illegal argument or getchar(0) and no char avail: return zero */
11156 n = 0;
11157 else
11158 /* getchar(0) and char avail: return char */
11159 n = safe_vgetc();
11160 if (n == K_IGNORE)
11161 continue;
11162 break;
11163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 --no_mapping;
11165 --allow_keys;
11166
Bram Moolenaar219b8702006-11-01 14:32:36 +000011167 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11168 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11169 vimvars[VV_MOUSE_COL].vv_nr = 0;
11170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (IS_SPECIAL(n) || mod_mask != 0)
11173 {
11174 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11175 int i = 0;
11176
11177 /* Turn a special key into three bytes, plus modifier. */
11178 if (mod_mask != 0)
11179 {
11180 temp[i++] = K_SPECIAL;
11181 temp[i++] = KS_MODIFIER;
11182 temp[i++] = mod_mask;
11183 }
11184 if (IS_SPECIAL(n))
11185 {
11186 temp[i++] = K_SPECIAL;
11187 temp[i++] = K_SECOND(n);
11188 temp[i++] = K_THIRD(n);
11189 }
11190#ifdef FEAT_MBYTE
11191 else if (has_mbyte)
11192 i += (*mb_char2bytes)(n, temp + i);
11193#endif
11194 else
11195 temp[i++] = n;
11196 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011197 rettv->v_type = VAR_STRING;
11198 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011199
11200#ifdef FEAT_MOUSE
11201 if (n == K_LEFTMOUSE
11202 || n == K_LEFTMOUSE_NM
11203 || n == K_LEFTDRAG
11204 || n == K_LEFTRELEASE
11205 || n == K_LEFTRELEASE_NM
11206 || n == K_MIDDLEMOUSE
11207 || n == K_MIDDLEDRAG
11208 || n == K_MIDDLERELEASE
11209 || n == K_RIGHTMOUSE
11210 || n == K_RIGHTDRAG
11211 || n == K_RIGHTRELEASE
11212 || n == K_X1MOUSE
11213 || n == K_X1DRAG
11214 || n == K_X1RELEASE
11215 || n == K_X2MOUSE
11216 || n == K_X2DRAG
11217 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011218 || n == K_MOUSELEFT
11219 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011220 || n == K_MOUSEDOWN
11221 || n == K_MOUSEUP)
11222 {
11223 int row = mouse_row;
11224 int col = mouse_col;
11225 win_T *win;
11226 linenr_T lnum;
11227# ifdef FEAT_WINDOWS
11228 win_T *wp;
11229# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011230 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011231
11232 if (row >= 0 && col >= 0)
11233 {
11234 /* Find the window at the mouse coordinates and compute the
11235 * text position. */
11236 win = mouse_find_win(&row, &col);
11237 (void)mouse_comp_pos(win, &row, &col, &lnum);
11238# ifdef FEAT_WINDOWS
11239 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011240 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011241# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011242 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011243 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11244 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11245 }
11246 }
11247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 }
11249}
11250
11251/*
11252 * "getcharmod()" function
11253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011256 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260}
11261
11262/*
11263 * "getcmdline()" function
11264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011267 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->v_type = VAR_STRING;
11271 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272}
11273
11274/*
11275 * "getcmdpos()" function
11276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283}
11284
11285/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011286 * "getcmdtype()" function
11287 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011288 static void
11289f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011290 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011291 typval_T *rettv;
11292{
11293 rettv->v_type = VAR_STRING;
11294 rettv->vval.v_string = alloc(2);
11295 if (rettv->vval.v_string != NULL)
11296 {
11297 rettv->vval.v_string[0] = get_cmdline_type();
11298 rettv->vval.v_string[1] = NUL;
11299 }
11300}
11301
11302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 * "getcwd()" function
11304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011310 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011313 rettv->vval.v_string = NULL;
11314 cwd = alloc(MAXPATHL);
11315 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011317 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11318 {
11319 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011321 if (rettv->vval.v_string != NULL)
11322 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011324 }
11325 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 }
11327}
11328
11329/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011330 * "getfontname()" function
11331 */
11332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->v_type = VAR_STRING;
11338 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011339#ifdef FEAT_GUI
11340 if (gui.in_use)
11341 {
11342 GuiFont font;
11343 char_u *name = NULL;
11344
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011345 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011346 {
11347 /* Get the "Normal" font. Either the name saved by
11348 * hl_set_font_name() or from the font ID. */
11349 font = gui.norm_font;
11350 name = hl_get_font_name();
11351 }
11352 else
11353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011355 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11356 return;
11357 font = gui_mch_get_font(name, FALSE);
11358 if (font == NOFONT)
11359 return; /* Invalid font name, return empty string. */
11360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011362 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011363 gui_mch_free_font(font);
11364 }
11365#endif
11366}
11367
11368/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011369 * "getfperm({fname})" function
11370 */
11371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011375{
11376 char_u *fname;
11377 struct stat st;
11378 char_u *perm = NULL;
11379 char_u flags[] = "rwx";
11380 int i;
11381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011385 if (mch_stat((char *)fname, &st) >= 0)
11386 {
11387 perm = vim_strsave((char_u *)"---------");
11388 if (perm != NULL)
11389 {
11390 for (i = 0; i < 9; i++)
11391 {
11392 if (st.st_mode & (1 << (8 - i)))
11393 perm[i] = flags[i % 3];
11394 }
11395 }
11396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011398}
11399
11400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 * "getfsize({fname})" function
11402 */
11403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011405 typval_T *argvars;
11406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407{
11408 char_u *fname;
11409 struct stat st;
11410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414
11415 if (mch_stat((char *)fname, &st) >= 0)
11416 {
11417 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011422
11423 /* non-perfect check for overflow */
11424 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11425 rettv->vval.v_number = -2;
11426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 }
11428 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430}
11431
11432/*
11433 * "getftime({fname})" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440 char_u *fname;
11441 struct stat st;
11442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444
11445 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449}
11450
11451/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011452 * "getftype({fname})" function
11453 */
11454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011456 typval_T *argvars;
11457 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011458{
11459 char_u *fname;
11460 struct stat st;
11461 char_u *type = NULL;
11462 char *t;
11463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011467 if (mch_lstat((char *)fname, &st) >= 0)
11468 {
11469#ifdef S_ISREG
11470 if (S_ISREG(st.st_mode))
11471 t = "file";
11472 else if (S_ISDIR(st.st_mode))
11473 t = "dir";
11474# ifdef S_ISLNK
11475 else if (S_ISLNK(st.st_mode))
11476 t = "link";
11477# endif
11478# ifdef S_ISBLK
11479 else if (S_ISBLK(st.st_mode))
11480 t = "bdev";
11481# endif
11482# ifdef S_ISCHR
11483 else if (S_ISCHR(st.st_mode))
11484 t = "cdev";
11485# endif
11486# ifdef S_ISFIFO
11487 else if (S_ISFIFO(st.st_mode))
11488 t = "fifo";
11489# endif
11490# ifdef S_ISSOCK
11491 else if (S_ISSOCK(st.st_mode))
11492 t = "fifo";
11493# endif
11494 else
11495 t = "other";
11496#else
11497# ifdef S_IFMT
11498 switch (st.st_mode & S_IFMT)
11499 {
11500 case S_IFREG: t = "file"; break;
11501 case S_IFDIR: t = "dir"; break;
11502# ifdef S_IFLNK
11503 case S_IFLNK: t = "link"; break;
11504# endif
11505# ifdef S_IFBLK
11506 case S_IFBLK: t = "bdev"; break;
11507# endif
11508# ifdef S_IFCHR
11509 case S_IFCHR: t = "cdev"; break;
11510# endif
11511# ifdef S_IFIFO
11512 case S_IFIFO: t = "fifo"; break;
11513# endif
11514# ifdef S_IFSOCK
11515 case S_IFSOCK: t = "socket"; break;
11516# endif
11517 default: t = "other";
11518 }
11519# else
11520 if (mch_isdir(fname))
11521 t = "dir";
11522 else
11523 t = "file";
11524# endif
11525#endif
11526 type = vim_strsave((char_u *)t);
11527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011529}
11530
11531/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011532 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533 */
11534 static void
11535f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011536 typval_T *argvars;
11537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011538{
11539 linenr_T lnum;
11540 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011541 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542
11543 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011544 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011545 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011546 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011547 retlist = FALSE;
11548 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011550 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011552 retlist = TRUE;
11553 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011554
Bram Moolenaar342337a2005-07-21 21:11:17 +000011555 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011556}
11557
11558/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011559 * "getmatches()" function
11560 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011561 static void
11562f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011563 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011564 typval_T *rettv;
11565{
11566#ifdef FEAT_SEARCH_EXTRA
11567 dict_T *dict;
11568 matchitem_T *cur = curwin->w_match_head;
11569
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011570 if (rettv_list_alloc(rettv) == OK)
11571 {
11572 while (cur != NULL)
11573 {
11574 dict = dict_alloc();
11575 if (dict == NULL)
11576 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011577 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11578 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11579 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11580 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11581 list_append_dict(rettv->vval.v_list, dict);
11582 cur = cur->next;
11583 }
11584 }
11585#endif
11586}
11587
11588/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011589 * "getpid()" function
11590 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011591 static void
11592f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011593 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011594 typval_T *rettv;
11595{
11596 rettv->vval.v_number = mch_get_pid();
11597}
11598
11599/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011600 * "getpos(string)" function
11601 */
11602 static void
11603f_getpos(argvars, rettv)
11604 typval_T *argvars;
11605 typval_T *rettv;
11606{
11607 pos_T *fp;
11608 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011609 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011610
11611 if (rettv_list_alloc(rettv) == OK)
11612 {
11613 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011614 fp = var2fpos(&argvars[0], TRUE, &fnum);
11615 if (fnum != -1)
11616 list_append_number(l, (varnumber_T)fnum);
11617 else
11618 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011619 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11620 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011621 list_append_number(l, (fp != NULL)
11622 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011623 : (varnumber_T)0);
11624 list_append_number(l,
11625#ifdef FEAT_VIRTUALEDIT
11626 (fp != NULL) ? (varnumber_T)fp->coladd :
11627#endif
11628 (varnumber_T)0);
11629 }
11630 else
11631 rettv->vval.v_number = FALSE;
11632}
11633
11634/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011635 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011636 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011637 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011638f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011639 typval_T *argvars UNUSED;
11640 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011641{
11642#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011643 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011644#endif
11645
Bram Moolenaar2641f772005-03-25 21:58:17 +000011646#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011647 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011648 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011649 wp = NULL;
11650 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11651 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011652 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011653 if (wp == NULL)
11654 return;
11655 }
11656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011657 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011658 }
11659#endif
11660}
11661
11662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 * "getreg()" function
11664 */
11665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011667 typval_T *argvars;
11668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669{
11670 char_u *strregname;
11671 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011672 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 strregname = get_tv_string_chk(&argvars[0]);
11678 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011679 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 regname = (strregname == NULL ? '"' : *strregname);
11685 if (regname == 0)
11686 regname = '"';
11687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 rettv->vval.v_string = error ? NULL :
11690 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691}
11692
11693/*
11694 * "getregtype()" function
11695 */
11696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011698 typval_T *argvars;
11699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700{
11701 char_u *strregname;
11702 int regname;
11703 char_u buf[NUMBUFLEN + 2];
11704 long reglen = 0;
11705
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011706 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 {
11708 strregname = get_tv_string_chk(&argvars[0]);
11709 if (strregname == NULL) /* type error; errmsg already given */
11710 {
11711 rettv->v_type = VAR_STRING;
11712 rettv->vval.v_string = NULL;
11713 return;
11714 }
11715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 else
11717 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719
11720 regname = (strregname == NULL ? '"' : *strregname);
11721 if (regname == 0)
11722 regname = '"';
11723
11724 buf[0] = NUL;
11725 buf[1] = NUL;
11726 switch (get_reg_type(regname, &reglen))
11727 {
11728 case MLINE: buf[0] = 'V'; break;
11729 case MCHAR: buf[0] = 'v'; break;
11730#ifdef FEAT_VISUAL
11731 case MBLOCK:
11732 buf[0] = Ctrl_V;
11733 sprintf((char *)buf + 1, "%ld", reglen + 1);
11734 break;
11735#endif
11736 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->v_type = VAR_STRING;
11738 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739}
11740
11741/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011742 * "gettabvar()" function
11743 */
11744 static void
11745f_gettabvar(argvars, rettv)
11746 typval_T *argvars;
11747 typval_T *rettv;
11748{
11749 tabpage_T *tp;
11750 dictitem_T *v;
11751 char_u *varname;
11752
11753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = NULL;
11755
11756 varname = get_tv_string_chk(&argvars[1]);
11757 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11758 if (tp != NULL && varname != NULL)
11759 {
11760 /* look up the variable */
11761 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11762 if (v != NULL)
11763 copy_tv(&v->di_tv, rettv);
11764 }
11765}
11766
11767/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011768 * "gettabwinvar()" function
11769 */
11770 static void
11771f_gettabwinvar(argvars, rettv)
11772 typval_T *argvars;
11773 typval_T *rettv;
11774{
11775 getwinvar(argvars, rettv, 1);
11776}
11777
11778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 * "getwinposx()" function
11780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787#ifdef FEAT_GUI
11788 if (gui.in_use)
11789 {
11790 int x, y;
11791
11792 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 }
11795#endif
11796}
11797
11798/*
11799 * "getwinposy()" function
11800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807#ifdef FEAT_GUI
11808 if (gui.in_use)
11809 {
11810 int x, y;
11811
11812 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 }
11815#endif
11816}
11817
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011818/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011819 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011820 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011821 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011822find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011823 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011824 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011825{
11826#ifdef FEAT_WINDOWS
11827 win_T *wp;
11828#endif
11829 int nr;
11830
11831 nr = get_tv_number_chk(vp, NULL);
11832
11833#ifdef FEAT_WINDOWS
11834 if (nr < 0)
11835 return NULL;
11836 if (nr == 0)
11837 return curwin;
11838
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011839 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11840 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011841 if (--nr <= 0)
11842 break;
11843 return wp;
11844#else
11845 if (nr == 0 || nr == 1)
11846 return curwin;
11847 return NULL;
11848#endif
11849}
11850
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851/*
11852 * "getwinvar()" function
11853 */
11854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *argvars;
11857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011859 getwinvar(argvars, rettv, 0);
11860}
11861
11862/*
11863 * getwinvar() and gettabwinvar()
11864 */
11865 static void
11866getwinvar(argvars, rettv, off)
11867 typval_T *argvars;
11868 typval_T *rettv;
11869 int off; /* 1 for gettabwinvar() */
11870{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 win_T *win, *oldcurwin;
11872 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011873 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011874 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011876#ifdef FEAT_WINDOWS
11877 if (off == 1)
11878 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11879 else
11880 tp = curtab;
11881#endif
11882 win = find_win_by_nr(&argvars[off], tp);
11883 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011884 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886 rettv->v_type = VAR_STRING;
11887 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888
11889 if (win != NULL && varname != NULL)
11890 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011891 /* Set curwin to be our win, temporarily. Also set curbuf, so
11892 * that we can get buffer-local options. */
11893 oldcurwin = curwin;
11894 curwin = win;
11895 curbuf = win->w_buffer;
11896
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 else
11900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 if (*varname == NUL)
11902 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11903 * scope prefix before the NUL byte is required by
11904 * find_var_in_ht(). */
11905 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011907 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011909 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011911
11912 /* restore previous notion of curwin */
11913 curwin = oldcurwin;
11914 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 }
11916
11917 --emsg_off;
11918}
11919
11920/*
11921 * "glob()" function
11922 */
11923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011925 typval_T *argvars;
11926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011928 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011930 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011932 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011933 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011935 if (argvars[1].v_type != VAR_UNKNOWN)
11936 {
11937 if (get_tv_number_chk(&argvars[1], &error))
11938 options |= WILD_KEEP_ALL;
11939 if (argvars[2].v_type != VAR_UNKNOWN
11940 && get_tv_number_chk(&argvars[2], &error))
11941 {
11942 rettv->v_type = VAR_LIST;
11943 rettv->vval.v_list = NULL;
11944 }
11945 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011946 if (!error)
11947 {
11948 ExpandInit(&xpc);
11949 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011950 if (p_wic)
11951 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011952 if (rettv->v_type == VAR_STRING)
11953 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011954 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011955 else if (rettv_list_alloc(rettv) != FAIL)
11956 {
11957 int i;
11958
11959 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11960 NULL, options, WILD_ALL_KEEP);
11961 for (i = 0; i < xpc.xp_numfiles; i++)
11962 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11963
11964 ExpandCleanup(&xpc);
11965 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011966 }
11967 else
11968 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969}
11970
11971/*
11972 * "globpath()" function
11973 */
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011979 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011982 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011984 /* When the optional second argument is non-zero, don't remove matches
11985 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11986 if (argvars[2].v_type != VAR_UNKNOWN
11987 && get_tv_number_chk(&argvars[2], &error))
11988 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011990 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011991 rettv->vval.v_string = NULL;
11992 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011993 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11994 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995}
11996
11997/*
11998 * "has()" function
11999 */
12000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
12005 int i;
12006 char_u *name;
12007 int n = FALSE;
12008 static char *(has_list[]) =
12009 {
12010#ifdef AMIGA
12011 "amiga",
12012# ifdef FEAT_ARP
12013 "arp",
12014# endif
12015#endif
12016#ifdef __BEOS__
12017 "beos",
12018#endif
12019#ifdef MSDOS
12020# ifdef DJGPP
12021 "dos32",
12022# else
12023 "dos16",
12024# endif
12025#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012026#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 "mac",
12028#endif
12029#if defined(MACOS_X_UNIX)
12030 "macunix",
12031#endif
12032#ifdef OS2
12033 "os2",
12034#endif
12035#ifdef __QNX__
12036 "qnx",
12037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038#ifdef UNIX
12039 "unix",
12040#endif
12041#ifdef VMS
12042 "vms",
12043#endif
12044#ifdef WIN16
12045 "win16",
12046#endif
12047#ifdef WIN32
12048 "win32",
12049#endif
12050#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12051 "win32unix",
12052#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012053#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 "win64",
12055#endif
12056#ifdef EBCDIC
12057 "ebcdic",
12058#endif
12059#ifndef CASE_INSENSITIVE_FILENAME
12060 "fname_case",
12061#endif
12062#ifdef FEAT_ARABIC
12063 "arabic",
12064#endif
12065#ifdef FEAT_AUTOCMD
12066 "autocmd",
12067#endif
12068#ifdef FEAT_BEVAL
12069 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012070# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12071 "balloon_multiline",
12072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073#endif
12074#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12075 "builtin_terms",
12076# ifdef ALL_BUILTIN_TCAPS
12077 "all_builtin_terms",
12078# endif
12079#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012080#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12081 || defined(FEAT_GUI_W32) \
12082 || defined(FEAT_GUI_MOTIF))
12083 "browsefilter",
12084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085#ifdef FEAT_BYTEOFF
12086 "byte_offset",
12087#endif
12088#ifdef FEAT_CINDENT
12089 "cindent",
12090#endif
12091#ifdef FEAT_CLIENTSERVER
12092 "clientserver",
12093#endif
12094#ifdef FEAT_CLIPBOARD
12095 "clipboard",
12096#endif
12097#ifdef FEAT_CMDL_COMPL
12098 "cmdline_compl",
12099#endif
12100#ifdef FEAT_CMDHIST
12101 "cmdline_hist",
12102#endif
12103#ifdef FEAT_COMMENTS
12104 "comments",
12105#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012106#ifdef FEAT_CONCEAL
12107 "conceal",
12108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109#ifdef FEAT_CRYPT
12110 "cryptv",
12111#endif
12112#ifdef FEAT_CSCOPE
12113 "cscope",
12114#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012115#ifdef FEAT_CURSORBIND
12116 "cursorbind",
12117#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012118#ifdef CURSOR_SHAPE
12119 "cursorshape",
12120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121#ifdef DEBUG
12122 "debug",
12123#endif
12124#ifdef FEAT_CON_DIALOG
12125 "dialog_con",
12126#endif
12127#ifdef FEAT_GUI_DIALOG
12128 "dialog_gui",
12129#endif
12130#ifdef FEAT_DIFF
12131 "diff",
12132#endif
12133#ifdef FEAT_DIGRAPHS
12134 "digraphs",
12135#endif
12136#ifdef FEAT_DND
12137 "dnd",
12138#endif
12139#ifdef FEAT_EMACS_TAGS
12140 "emacs_tags",
12141#endif
12142 "eval", /* always present, of course! */
12143#ifdef FEAT_EX_EXTRA
12144 "ex_extra",
12145#endif
12146#ifdef FEAT_SEARCH_EXTRA
12147 "extra_search",
12148#endif
12149#ifdef FEAT_FKMAP
12150 "farsi",
12151#endif
12152#ifdef FEAT_SEARCHPATH
12153 "file_in_path",
12154#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012155#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012156 "filterpipe",
12157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158#ifdef FEAT_FIND_ID
12159 "find_in_path",
12160#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012161#ifdef FEAT_FLOAT
12162 "float",
12163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164#ifdef FEAT_FOLDING
12165 "folding",
12166#endif
12167#ifdef FEAT_FOOTER
12168 "footer",
12169#endif
12170#if !defined(USE_SYSTEM) && defined(UNIX)
12171 "fork",
12172#endif
12173#ifdef FEAT_GETTEXT
12174 "gettext",
12175#endif
12176#ifdef FEAT_GUI
12177 "gui",
12178#endif
12179#ifdef FEAT_GUI_ATHENA
12180# ifdef FEAT_GUI_NEXTAW
12181 "gui_neXtaw",
12182# else
12183 "gui_athena",
12184# endif
12185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186#ifdef FEAT_GUI_GTK
12187 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012190#ifdef FEAT_GUI_GNOME
12191 "gui_gnome",
12192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193#ifdef FEAT_GUI_MAC
12194 "gui_mac",
12195#endif
12196#ifdef FEAT_GUI_MOTIF
12197 "gui_motif",
12198#endif
12199#ifdef FEAT_GUI_PHOTON
12200 "gui_photon",
12201#endif
12202#ifdef FEAT_GUI_W16
12203 "gui_win16",
12204#endif
12205#ifdef FEAT_GUI_W32
12206 "gui_win32",
12207#endif
12208#ifdef FEAT_HANGULIN
12209 "hangul_input",
12210#endif
12211#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12212 "iconv",
12213#endif
12214#ifdef FEAT_INS_EXPAND
12215 "insert_expand",
12216#endif
12217#ifdef FEAT_JUMPLIST
12218 "jumplist",
12219#endif
12220#ifdef FEAT_KEYMAP
12221 "keymap",
12222#endif
12223#ifdef FEAT_LANGMAP
12224 "langmap",
12225#endif
12226#ifdef FEAT_LIBCALL
12227 "libcall",
12228#endif
12229#ifdef FEAT_LINEBREAK
12230 "linebreak",
12231#endif
12232#ifdef FEAT_LISP
12233 "lispindent",
12234#endif
12235#ifdef FEAT_LISTCMDS
12236 "listcmds",
12237#endif
12238#ifdef FEAT_LOCALMAP
12239 "localmap",
12240#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012241#ifdef FEAT_LUA
12242# ifndef DYNAMIC_LUA
12243 "lua",
12244# endif
12245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246#ifdef FEAT_MENU
12247 "menu",
12248#endif
12249#ifdef FEAT_SESSION
12250 "mksession",
12251#endif
12252#ifdef FEAT_MODIFY_FNAME
12253 "modify_fname",
12254#endif
12255#ifdef FEAT_MOUSE
12256 "mouse",
12257#endif
12258#ifdef FEAT_MOUSESHAPE
12259 "mouseshape",
12260#endif
12261#if defined(UNIX) || defined(VMS)
12262# ifdef FEAT_MOUSE_DEC
12263 "mouse_dec",
12264# endif
12265# ifdef FEAT_MOUSE_GPM
12266 "mouse_gpm",
12267# endif
12268# ifdef FEAT_MOUSE_JSB
12269 "mouse_jsbterm",
12270# endif
12271# ifdef FEAT_MOUSE_NET
12272 "mouse_netterm",
12273# endif
12274# ifdef FEAT_MOUSE_PTERM
12275 "mouse_pterm",
12276# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012277# ifdef FEAT_SYSMOUSE
12278 "mouse_sysmouse",
12279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280# ifdef FEAT_MOUSE_XTERM
12281 "mouse_xterm",
12282# endif
12283#endif
12284#ifdef FEAT_MBYTE
12285 "multi_byte",
12286#endif
12287#ifdef FEAT_MBYTE_IME
12288 "multi_byte_ime",
12289#endif
12290#ifdef FEAT_MULTI_LANG
12291 "multi_lang",
12292#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012293#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012294#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012295 "mzscheme",
12296#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#ifdef FEAT_OLE
12299 "ole",
12300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301#ifdef FEAT_PATH_EXTRA
12302 "path_extra",
12303#endif
12304#ifdef FEAT_PERL
12305#ifndef DYNAMIC_PERL
12306 "perl",
12307#endif
12308#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012309#ifdef FEAT_PERSISTENT_UNDO
12310 "persistent_undo",
12311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312#ifdef FEAT_PYTHON
12313#ifndef DYNAMIC_PYTHON
12314 "python",
12315#endif
12316#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012317#ifdef FEAT_PYTHON3
12318#ifndef DYNAMIC_PYTHON3
12319 "python3",
12320#endif
12321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322#ifdef FEAT_POSTSCRIPT
12323 "postscript",
12324#endif
12325#ifdef FEAT_PRINTER
12326 "printer",
12327#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012328#ifdef FEAT_PROFILE
12329 "profile",
12330#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012331#ifdef FEAT_RELTIME
12332 "reltime",
12333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_QUICKFIX
12335 "quickfix",
12336#endif
12337#ifdef FEAT_RIGHTLEFT
12338 "rightleft",
12339#endif
12340#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12341 "ruby",
12342#endif
12343#ifdef FEAT_SCROLLBIND
12344 "scrollbind",
12345#endif
12346#ifdef FEAT_CMDL_INFO
12347 "showcmd",
12348 "cmdline_info",
12349#endif
12350#ifdef FEAT_SIGNS
12351 "signs",
12352#endif
12353#ifdef FEAT_SMARTINDENT
12354 "smartindent",
12355#endif
12356#ifdef FEAT_SNIFF
12357 "sniff",
12358#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012359#ifdef STARTUPTIME
12360 "startuptime",
12361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362#ifdef FEAT_STL_OPT
12363 "statusline",
12364#endif
12365#ifdef FEAT_SUN_WORKSHOP
12366 "sun_workshop",
12367#endif
12368#ifdef FEAT_NETBEANS_INTG
12369 "netbeans_intg",
12370#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012371#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012372 "spell",
12373#endif
12374#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 "syntax",
12376#endif
12377#if defined(USE_SYSTEM) || !defined(UNIX)
12378 "system",
12379#endif
12380#ifdef FEAT_TAG_BINS
12381 "tag_binary",
12382#endif
12383#ifdef FEAT_TAG_OLDSTATIC
12384 "tag_old_static",
12385#endif
12386#ifdef FEAT_TAG_ANYWHITE
12387 "tag_any_white",
12388#endif
12389#ifdef FEAT_TCL
12390# ifndef DYNAMIC_TCL
12391 "tcl",
12392# endif
12393#endif
12394#ifdef TERMINFO
12395 "terminfo",
12396#endif
12397#ifdef FEAT_TERMRESPONSE
12398 "termresponse",
12399#endif
12400#ifdef FEAT_TEXTOBJ
12401 "textobjects",
12402#endif
12403#ifdef HAVE_TGETENT
12404 "tgetent",
12405#endif
12406#ifdef FEAT_TITLE
12407 "title",
12408#endif
12409#ifdef FEAT_TOOLBAR
12410 "toolbar",
12411#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012412#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12413 "unnamedplus",
12414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415#ifdef FEAT_USR_CMDS
12416 "user-commands", /* was accidentally included in 5.4 */
12417 "user_commands",
12418#endif
12419#ifdef FEAT_VIMINFO
12420 "viminfo",
12421#endif
12422#ifdef FEAT_VERTSPLIT
12423 "vertsplit",
12424#endif
12425#ifdef FEAT_VIRTUALEDIT
12426 "virtualedit",
12427#endif
12428#ifdef FEAT_VISUAL
12429 "visual",
12430#endif
12431#ifdef FEAT_VISUALEXTRA
12432 "visualextra",
12433#endif
12434#ifdef FEAT_VREPLACE
12435 "vreplace",
12436#endif
12437#ifdef FEAT_WILDIGN
12438 "wildignore",
12439#endif
12440#ifdef FEAT_WILDMENU
12441 "wildmenu",
12442#endif
12443#ifdef FEAT_WINDOWS
12444 "windows",
12445#endif
12446#ifdef FEAT_WAK
12447 "winaltkeys",
12448#endif
12449#ifdef FEAT_WRITEBACKUP
12450 "writebackup",
12451#endif
12452#ifdef FEAT_XIM
12453 "xim",
12454#endif
12455#ifdef FEAT_XFONTSET
12456 "xfontset",
12457#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012458#ifdef FEAT_XPM_W32
12459 "xpm_w32",
12460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461#ifdef USE_XSMP
12462 "xsmp",
12463#endif
12464#ifdef USE_XSMP_INTERACT
12465 "xsmp_interact",
12466#endif
12467#ifdef FEAT_XCLIPBOARD
12468 "xterm_clipboard",
12469#endif
12470#ifdef FEAT_XTERM_SAVE
12471 "xterm_save",
12472#endif
12473#if defined(UNIX) && defined(FEAT_X11)
12474 "X11",
12475#endif
12476 NULL
12477 };
12478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 for (i = 0; has_list[i] != NULL; ++i)
12481 if (STRICMP(name, has_list[i]) == 0)
12482 {
12483 n = TRUE;
12484 break;
12485 }
12486
12487 if (n == FALSE)
12488 {
12489 if (STRNICMP(name, "patch", 5) == 0)
12490 n = has_patch(atoi((char *)name + 5));
12491 else if (STRICMP(name, "vim_starting") == 0)
12492 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012493#ifdef FEAT_MBYTE
12494 else if (STRICMP(name, "multi_byte_encoding") == 0)
12495 n = has_mbyte;
12496#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012497#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12498 else if (STRICMP(name, "balloon_multiline") == 0)
12499 n = multiline_balloon_available();
12500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#ifdef DYNAMIC_TCL
12502 else if (STRICMP(name, "tcl") == 0)
12503 n = tcl_enabled(FALSE);
12504#endif
12505#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12506 else if (STRICMP(name, "iconv") == 0)
12507 n = iconv_enabled(FALSE);
12508#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012509#ifdef DYNAMIC_LUA
12510 else if (STRICMP(name, "lua") == 0)
12511 n = lua_enabled(FALSE);
12512#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012513#ifdef DYNAMIC_MZSCHEME
12514 else if (STRICMP(name, "mzscheme") == 0)
12515 n = mzscheme_enabled(FALSE);
12516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517#ifdef DYNAMIC_RUBY
12518 else if (STRICMP(name, "ruby") == 0)
12519 n = ruby_enabled(FALSE);
12520#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012521#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522#ifdef DYNAMIC_PYTHON
12523 else if (STRICMP(name, "python") == 0)
12524 n = python_enabled(FALSE);
12525#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012526#endif
12527#ifdef FEAT_PYTHON3
12528#ifdef DYNAMIC_PYTHON3
12529 else if (STRICMP(name, "python3") == 0)
12530 n = python3_enabled(FALSE);
12531#endif
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533#ifdef DYNAMIC_PERL
12534 else if (STRICMP(name, "perl") == 0)
12535 n = perl_enabled(FALSE);
12536#endif
12537#ifdef FEAT_GUI
12538 else if (STRICMP(name, "gui_running") == 0)
12539 n = (gui.in_use || gui.starting);
12540# ifdef FEAT_GUI_W32
12541 else if (STRICMP(name, "gui_win32s") == 0)
12542 n = gui_is_win32s();
12543# endif
12544# ifdef FEAT_BROWSE
12545 else if (STRICMP(name, "browse") == 0)
12546 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12547# endif
12548#endif
12549#ifdef FEAT_SYN_HL
12550 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012551 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552#endif
12553#if defined(WIN3264)
12554 else if (STRICMP(name, "win95") == 0)
12555 n = mch_windows95();
12556#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012557#ifdef FEAT_NETBEANS_INTG
12558 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012559 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 }
12562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012563 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564}
12565
12566/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012567 * "has_key()" function
12568 */
12569 static void
12570f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *argvars;
12572 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012573{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012574 if (argvars[0].v_type != VAR_DICT)
12575 {
12576 EMSG(_(e_dictreq));
12577 return;
12578 }
12579 if (argvars[0].vval.v_dict == NULL)
12580 return;
12581
12582 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012583 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012584}
12585
12586/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012587 * "haslocaldir()" function
12588 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012589 static void
12590f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012591 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012592 typval_T *rettv;
12593{
12594 rettv->vval.v_number = (curwin->w_localdir != NULL);
12595}
12596
12597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598 * "hasmapto()" function
12599 */
12600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012601f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 typval_T *argvars;
12603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604{
12605 char_u *name;
12606 char_u *mode;
12607 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012608 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012611 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 mode = (char_u *)"nvo";
12613 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012616 if (argvars[2].v_type != VAR_UNKNOWN)
12617 abbr = get_tv_number(&argvars[2]);
12618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619
Bram Moolenaar2c932302006-03-18 21:42:09 +000012620 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624}
12625
12626/*
12627 * "histadd()" function
12628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633{
12634#ifdef FEAT_CMDHIST
12635 int histype;
12636 char_u *str;
12637 char_u buf[NUMBUFLEN];
12638#endif
12639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 if (check_restricted() || check_secure())
12642 return;
12643#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12645 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 if (histype >= 0)
12647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 if (*str != NUL)
12650 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012651 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 return;
12655 }
12656 }
12657#endif
12658}
12659
12660/*
12661 * "histdel()" function
12662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012665 typval_T *argvars UNUSED;
12666 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667{
12668#ifdef FEAT_CMDHIST
12669 int n;
12670 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012671 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12674 if (str == NULL)
12675 n = 0;
12676 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012678 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012679 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012681 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 else
12684 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 get_tv_string_buf(&argvars[1], buf));
12687 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688#endif
12689}
12690
12691/*
12692 * "histget()" function
12693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012696 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698{
12699#ifdef FEAT_CMDHIST
12700 int type;
12701 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12705 if (str == NULL)
12706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 {
12709 type = get_histtype(str);
12710 if (argvars[1].v_type == VAR_UNKNOWN)
12711 idx = get_history_idx(type);
12712 else
12713 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12714 /* -1 on type error */
12715 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721}
12722
12723/*
12724 * "histnr()" function
12725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730{
12731 int i;
12732
12733#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 char_u *history = get_tv_string_chk(&argvars[0]);
12735
12736 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 if (i >= HIST_CMD && i < HIST_COUNT)
12738 i = get_history_idx(i);
12739 else
12740#endif
12741 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 * "highlightID(name)" function
12747 */
12748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 typval_T *argvars;
12751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754}
12755
12756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757 * "highlight_exists()" function
12758 */
12759 static void
12760f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 typval_T *argvars;
12762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012763{
12764 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12765}
12766
12767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 * "hostname()" function
12769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012772 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
12775 char_u hostname[256];
12776
12777 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 rettv->v_type = VAR_STRING;
12779 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780}
12781
12782/*
12783 * iconv() function
12784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790#ifdef FEAT_MBYTE
12791 char_u buf1[NUMBUFLEN];
12792 char_u buf2[NUMBUFLEN];
12793 char_u *from, *to, *str;
12794 vimconv_T vimconv;
12795#endif
12796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->v_type = VAR_STRING;
12798 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799
12800#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 str = get_tv_string(&argvars[0]);
12802 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12803 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 vimconv.vc_type = CONV_NONE;
12805 convert_setup(&vimconv, from, to);
12806
12807 /* If the encodings are equal, no conversion needed. */
12808 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812
12813 convert_setup(&vimconv, NULL, NULL);
12814 vim_free(from);
12815 vim_free(to);
12816#endif
12817}
12818
12819/*
12820 * "indent()" function
12821 */
12822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827 linenr_T lnum;
12828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834}
12835
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012836/*
12837 * "index()" function
12838 */
12839 static void
12840f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 typval_T *argvars;
12842 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012843{
Bram Moolenaar33570922005-01-25 22:26:29 +000012844 list_T *l;
12845 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012846 long idx = 0;
12847 int ic = FALSE;
12848
12849 rettv->vval.v_number = -1;
12850 if (argvars[0].v_type != VAR_LIST)
12851 {
12852 EMSG(_(e_listreq));
12853 return;
12854 }
12855 l = argvars[0].vval.v_list;
12856 if (l != NULL)
12857 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012858 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012859 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012861 int error = FALSE;
12862
Bram Moolenaar758711c2005-02-02 23:11:38 +000012863 /* Start at specified item. Use the cached index that list_find()
12864 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012866 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012867 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012868 ic = get_tv_number_chk(&argvars[3], &error);
12869 if (error)
12870 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012871 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012872
Bram Moolenaar758711c2005-02-02 23:11:38 +000012873 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012874 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012875 {
12876 rettv->vval.v_number = idx;
12877 break;
12878 }
12879 }
12880}
12881
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882static int inputsecret_flag = 0;
12883
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012884static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12885
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012887 * This function is used by f_input() and f_inputdialog() functions. The third
12888 * argument to f_input() specifies the type of completion to use at the
12889 * prompt. The third argument to f_inputdialog() specifies the value to return
12890 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 */
12892 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012893get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 typval_T *argvars;
12895 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012896 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 char_u *p = NULL;
12900 int c;
12901 char_u buf[NUMBUFLEN];
12902 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012904 int xp_type = EXPAND_NOTHING;
12905 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012908 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909
12910#ifdef NO_CONSOLE_INPUT
12911 /* While starting up, there is no place to enter text. */
12912 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914#endif
12915
12916 cmd_silent = FALSE; /* Want to see the prompt. */
12917 if (prompt != NULL)
12918 {
12919 /* Only the part of the message after the last NL is considered as
12920 * prompt for the command line */
12921 p = vim_strrchr(prompt, '\n');
12922 if (p == NULL)
12923 p = prompt;
12924 else
12925 {
12926 ++p;
12927 c = *p;
12928 *p = NUL;
12929 msg_start();
12930 msg_clr_eos();
12931 msg_puts_attr(prompt, echo_attr);
12932 msg_didout = FALSE;
12933 msg_starthere();
12934 *p = c;
12935 }
12936 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012938 if (argvars[1].v_type != VAR_UNKNOWN)
12939 {
12940 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12941 if (defstr != NULL)
12942 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012944 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012945 {
12946 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012947 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012948 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012949
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012950 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012951 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012952
Bram Moolenaar4463f292005-09-25 22:20:24 +000012953 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12954 if (xp_name == NULL)
12955 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012956
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012957 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012958
Bram Moolenaar4463f292005-09-25 22:20:24 +000012959 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12960 &xp_arg) == FAIL)
12961 return;
12962 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012963 }
12964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012965 if (defstr != NULL)
12966 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012967 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12968 xp_type, xp_arg);
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012969 if (rettv->vval.v_string == NULL
12970 && argvars[1].v_type != VAR_UNKNOWN
12971 && argvars[2].v_type != VAR_UNKNOWN)
12972 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
12973 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012974
12975 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 /* since the user typed this, no need to wait for return */
12978 need_wait_return = FALSE;
12979 msg_didout = FALSE;
12980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 cmd_silent = cmd_silent_save;
12982}
12983
12984/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012985 * "input()" function
12986 * Also handles inputsecret() when inputsecret is set.
12987 */
12988 static void
12989f_input(argvars, rettv)
12990 typval_T *argvars;
12991 typval_T *rettv;
12992{
12993 get_user_input(argvars, rettv, FALSE);
12994}
12995
12996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 * "inputdialog()" function
12998 */
12999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013001 typval_T *argvars;
13002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003{
13004#if defined(FEAT_GUI_TEXTDIALOG)
13005 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13006 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13007 {
13008 char_u *message;
13009 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013012 message = get_tv_string_chk(&argvars[0]);
13013 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013014 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013015 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 else
13017 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 if (message != NULL && defstr != NULL
13019 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013020 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 else
13023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 if (message != NULL && defstr != NULL
13025 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013026 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013027 rettv->vval.v_string = vim_strsave(
13028 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 }
13034 else
13035#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013036 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037}
13038
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013039/*
13040 * "inputlist()" function
13041 */
13042 static void
13043f_inputlist(argvars, rettv)
13044 typval_T *argvars;
13045 typval_T *rettv;
13046{
13047 listitem_T *li;
13048 int selected;
13049 int mouse_used;
13050
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013051#ifdef NO_CONSOLE_INPUT
13052 /* While starting up, there is no place to enter text. */
13053 if (no_console_input())
13054 return;
13055#endif
13056 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13057 {
13058 EMSG2(_(e_listarg), "inputlist()");
13059 return;
13060 }
13061
13062 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013063 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013064 lines_left = Rows; /* avoid more prompt */
13065 msg_scroll = TRUE;
13066 msg_clr_eos();
13067
13068 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13069 {
13070 msg_puts(get_tv_string(&li->li_tv));
13071 msg_putchar('\n');
13072 }
13073
13074 /* Ask for choice. */
13075 selected = prompt_for_number(&mouse_used);
13076 if (mouse_used)
13077 selected -= lines_left;
13078
13079 rettv->vval.v_number = selected;
13080}
13081
13082
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13084
13085/*
13086 * "inputrestore()" function
13087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013089f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092{
13093 if (ga_userinput.ga_len > 0)
13094 {
13095 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13097 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013098 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 }
13100 else if (p_verbose > 1)
13101 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013102 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 }
13105}
13106
13107/*
13108 * "inputsave()" function
13109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013115 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 if (ga_grow(&ga_userinput, 1) == OK)
13117 {
13118 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13119 + ga_userinput.ga_len);
13120 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013121 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 }
13123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013124 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125}
13126
13127/*
13128 * "inputsecret()" function
13129 */
13130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 typval_T *argvars;
13133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134{
13135 ++cmdline_star;
13136 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 --cmdline_star;
13139 --inputsecret_flag;
13140}
13141
13142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013143 * "insert()" function
13144 */
13145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013147 typval_T *argvars;
13148 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013149{
13150 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013151 listitem_T *item;
13152 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013154
13155 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013156 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013157 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013158 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013159 {
13160 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 before = get_tv_number_chk(&argvars[2], &error);
13162 if (error)
13163 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164
Bram Moolenaar758711c2005-02-02 23:11:38 +000013165 if (before == l->lv_len)
13166 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 else
13168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013169 item = list_find(l, before);
13170 if (item == NULL)
13171 {
13172 EMSGN(_(e_listidx), before);
13173 l = NULL;
13174 }
13175 }
13176 if (l != NULL)
13177 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013178 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013179 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013180 }
13181 }
13182}
13183
13184/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013185 * "invert(expr)" function
13186 */
13187 static void
13188f_invert(argvars, rettv)
13189 typval_T *argvars;
13190 typval_T *rettv;
13191{
13192 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13193}
13194
13195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 * "isdirectory()" function
13197 */
13198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204}
13205
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013206/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013207 * "islocked()" function
13208 */
13209 static void
13210f_islocked(argvars, rettv)
13211 typval_T *argvars;
13212 typval_T *rettv;
13213{
13214 lval_T lv;
13215 char_u *end;
13216 dictitem_T *di;
13217
13218 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013219 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13220 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013221 if (end != NULL && lv.ll_name != NULL)
13222 {
13223 if (*end != NUL)
13224 EMSG(_(e_trailing));
13225 else
13226 {
13227 if (lv.ll_tv == NULL)
13228 {
13229 if (check_changedtick(lv.ll_name))
13230 rettv->vval.v_number = 1; /* always locked */
13231 else
13232 {
13233 di = find_var(lv.ll_name, NULL);
13234 if (di != NULL)
13235 {
13236 /* Consider a variable locked when:
13237 * 1. the variable itself is locked
13238 * 2. the value of the variable is locked.
13239 * 3. the List or Dict value is locked.
13240 */
13241 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13242 || tv_islocked(&di->di_tv));
13243 }
13244 }
13245 }
13246 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013247 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013248 else if (lv.ll_newkey != NULL)
13249 EMSG2(_(e_dictkey), lv.ll_newkey);
13250 else if (lv.ll_list != NULL)
13251 /* List item. */
13252 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13253 else
13254 /* Dictionary item. */
13255 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13256 }
13257 }
13258
13259 clear_lval(&lv);
13260}
13261
Bram Moolenaar33570922005-01-25 22:26:29 +000013262static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013263
13264/*
13265 * Turn a dict into a list:
13266 * "what" == 0: list of keys
13267 * "what" == 1: list of values
13268 * "what" == 2: list of items
13269 */
13270 static void
13271dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013272 typval_T *argvars;
13273 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013274 int what;
13275{
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 list_T *l2;
13277 dictitem_T *di;
13278 hashitem_T *hi;
13279 listitem_T *li;
13280 listitem_T *li2;
13281 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013282 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013283
Bram Moolenaar8c711452005-01-14 21:53:12 +000013284 if (argvars[0].v_type != VAR_DICT)
13285 {
13286 EMSG(_(e_dictreq));
13287 return;
13288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013289 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013290 return;
13291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013292 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013293 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013294
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013295 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013296 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013300 --todo;
13301 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013302
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013303 li = listitem_alloc();
13304 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013306 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013307
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013308 if (what == 0)
13309 {
13310 /* keys() */
13311 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013312 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013313 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13314 }
13315 else if (what == 1)
13316 {
13317 /* values() */
13318 copy_tv(&di->di_tv, &li->li_tv);
13319 }
13320 else
13321 {
13322 /* items() */
13323 l2 = list_alloc();
13324 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013325 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013326 li->li_tv.vval.v_list = l2;
13327 if (l2 == NULL)
13328 break;
13329 ++l2->lv_refcount;
13330
13331 li2 = listitem_alloc();
13332 if (li2 == NULL)
13333 break;
13334 list_append(l2, li2);
13335 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013336 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013337 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13338
13339 li2 = listitem_alloc();
13340 if (li2 == NULL)
13341 break;
13342 list_append(l2, li2);
13343 copy_tv(&di->di_tv, &li2->li_tv);
13344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345 }
13346 }
13347}
13348
13349/*
13350 * "items(dict)" function
13351 */
13352 static void
13353f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *argvars;
13355 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013356{
13357 dict_list(argvars, rettv, 2);
13358}
13359
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013361 * "join()" function
13362 */
13363 static void
13364f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *argvars;
13366 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013367{
13368 garray_T ga;
13369 char_u *sep;
13370
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013371 if (argvars[0].v_type != VAR_LIST)
13372 {
13373 EMSG(_(e_listreq));
13374 return;
13375 }
13376 if (argvars[0].vval.v_list == NULL)
13377 return;
13378 if (argvars[1].v_type == VAR_UNKNOWN)
13379 sep = (char_u *)" ";
13380 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013382
13383 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384
13385 if (sep != NULL)
13386 {
13387 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013388 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 ga_append(&ga, NUL);
13390 rettv->vval.v_string = (char_u *)ga.ga_data;
13391 }
13392 else
13393 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013394}
13395
13396/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013397 * "keys()" function
13398 */
13399 static void
13400f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013401 typval_T *argvars;
13402 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013403{
13404 dict_list(argvars, rettv, 0);
13405}
13406
13407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 * "last_buffer_nr()" function.
13409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414{
13415 int n = 0;
13416 buf_T *buf;
13417
13418 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13419 if (n < buf->b_fnum)
13420 n = buf->b_fnum;
13421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013423}
13424
13425/*
13426 * "len()" function
13427 */
13428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013430 typval_T *argvars;
13431 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013432{
13433 switch (argvars[0].v_type)
13434 {
13435 case VAR_STRING:
13436 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013437 rettv->vval.v_number = (varnumber_T)STRLEN(
13438 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013439 break;
13440 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013442 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013443 case VAR_DICT:
13444 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13445 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013446 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013447 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013448 break;
13449 }
13450}
13451
Bram Moolenaar33570922005-01-25 22:26:29 +000013452static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013453
13454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013458 int type;
13459{
13460#ifdef FEAT_LIBCALL
13461 char_u *string_in;
13462 char_u **string_result;
13463 int nr_result;
13464#endif
13465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013467 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013468 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013469
13470 if (check_restricted() || check_secure())
13471 return;
13472
13473#ifdef FEAT_LIBCALL
13474 /* The first two args must be strings, otherwise its meaningless */
13475 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13476 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013477 string_in = NULL;
13478 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479 string_in = argvars[2].vval.v_string;
13480 if (type == VAR_NUMBER)
13481 string_result = NULL;
13482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484 if (mch_libcall(argvars[0].vval.v_string,
13485 argvars[1].vval.v_string,
13486 string_in,
13487 argvars[2].vval.v_number,
13488 string_result,
13489 &nr_result) == OK
13490 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 }
13493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494}
13495
13496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497 * "libcall()" function
13498 */
13499 static void
13500f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013501 typval_T *argvars;
13502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013503{
13504 libcall_common(argvars, rettv, VAR_STRING);
13505}
13506
13507/*
13508 * "libcallnr()" function
13509 */
13510 static void
13511f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013512 typval_T *argvars;
13513 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013514{
13515 libcall_common(argvars, rettv, VAR_NUMBER);
13516}
13517
13518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 * "line(string)" function
13520 */
13521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *argvars;
13524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525{
13526 linenr_T lnum = 0;
13527 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013528 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013530 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 if (fp != NULL)
13532 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534}
13535
13536/*
13537 * "line2byte(lnum)" function
13538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543{
13544#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546#else
13547 linenr_T lnum;
13548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13554 if (rettv->vval.v_number >= 0)
13555 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556#endif
13557}
13558
13559/*
13560 * "lispindent(lnum)" function
13561 */
13562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *argvars;
13565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566{
13567#ifdef FEAT_LISP
13568 pos_T pos;
13569 linenr_T lnum;
13570
13571 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13574 {
13575 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 curwin->w_cursor = pos;
13578 }
13579 else
13580#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582}
13583
13584/*
13585 * "localtime()" function
13586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013589 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593}
13594
Bram Moolenaar33570922005-01-25 22:26:29 +000013595static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596
13597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *argvars;
13600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 int exact;
13602{
13603 char_u *keys;
13604 char_u *which;
13605 char_u buf[NUMBUFLEN];
13606 char_u *keys_buf = NULL;
13607 char_u *rhs;
13608 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013609 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013610 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013611 mapblock_T *mp;
13612 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613
13614 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->v_type = VAR_STRING;
13616 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 if (*keys == NUL)
13620 return;
13621
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013622 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013625 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013626 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013627 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013628 if (argvars[3].v_type != VAR_UNKNOWN)
13629 get_dict = get_tv_number(&argvars[3]);
13630 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 else
13633 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 if (which == NULL)
13635 return;
13636
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 mode = get_map_mode(&which, 0);
13638
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013639 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013640 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013642
13643 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013645 /* Return a string. */
13646 if (rhs != NULL)
13647 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
Bram Moolenaarbd743252010-10-20 21:23:33 +020013649 }
13650 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13651 {
13652 /* Return a dictionary. */
13653 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13654 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13655 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaarbd743252010-10-20 21:23:33 +020013657 dict_add_nr_str(dict, "lhs", 0L, lhs);
13658 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13659 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13660 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13661 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13662 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13663 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13664 dict_add_nr_str(dict, "mode", 0L, mapmode);
13665
13666 vim_free(lhs);
13667 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 }
13669}
13670
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013671#ifdef FEAT_FLOAT
13672/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013673 * "log()" function
13674 */
13675 static void
13676f_log(argvars, rettv)
13677 typval_T *argvars;
13678 typval_T *rettv;
13679{
13680 float_T f;
13681
13682 rettv->v_type = VAR_FLOAT;
13683 if (get_float_arg(argvars, &f) == OK)
13684 rettv->vval.v_float = log(f);
13685 else
13686 rettv->vval.v_float = 0.0;
13687}
13688
13689/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013690 * "log10()" function
13691 */
13692 static void
13693f_log10(argvars, rettv)
13694 typval_T *argvars;
13695 typval_T *rettv;
13696{
13697 float_T f;
13698
13699 rettv->v_type = VAR_FLOAT;
13700 if (get_float_arg(argvars, &f) == OK)
13701 rettv->vval.v_float = log10(f);
13702 else
13703 rettv->vval.v_float = 0.0;
13704}
13705#endif
13706
Bram Moolenaar1dced572012-04-05 16:54:08 +020013707#ifdef FEAT_LUA
13708/*
13709 * "luaeval()" function
13710 */
13711 static void
13712f_luaeval(argvars, rettv)
13713 typval_T *argvars;
13714 typval_T *rettv;
13715{
13716 char_u *str;
13717 char_u buf[NUMBUFLEN];
13718
13719 str = get_tv_string_buf(&argvars[0], buf);
13720 do_luaeval(str, argvars + 1, rettv);
13721}
13722#endif
13723
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013725 * "map()" function
13726 */
13727 static void
13728f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013731{
13732 filter_map(argvars, rettv, TRUE);
13733}
13734
13735/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013736 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 */
13738 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013739f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *argvars;
13741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744}
13745
13746/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 */
13749 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013750f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013751 typval_T *argvars;
13752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755}
13756
Bram Moolenaar33570922005-01-25 22:26:29 +000013757static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758
13759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013761 typval_T *argvars;
13762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 int type;
13764{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013765 char_u *str = NULL;
13766 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 char_u *pat;
13768 regmatch_T regmatch;
13769 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013770 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 char_u *save_cpo;
13772 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013773 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013774 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013775 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 list_T *l = NULL;
13777 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013778 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013779 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
13781 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13782 save_cpo = p_cpo;
13783 p_cpo = (char_u *)"";
13784
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013785 rettv->vval.v_number = -1;
13786 if (type == 3)
13787 {
13788 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013789 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013790 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013791 }
13792 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->v_type = VAR_STRING;
13795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013798 if (argvars[0].v_type == VAR_LIST)
13799 {
13800 if ((l = argvars[0].vval.v_list) == NULL)
13801 goto theend;
13802 li = l->lv_first;
13803 }
13804 else
13805 expr = str = get_tv_string(&argvars[0]);
13806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013807 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13808 if (pat == NULL)
13809 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013810
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 int error = FALSE;
13814
13815 start = get_tv_number_chk(&argvars[2], &error);
13816 if (error)
13817 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013818 if (l != NULL)
13819 {
13820 li = list_find(l, start);
13821 if (li == NULL)
13822 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013823 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013824 }
13825 else
13826 {
13827 if (start < 0)
13828 start = 0;
13829 if (start > (long)STRLEN(str))
13830 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013831 /* When "count" argument is there ignore matches before "start",
13832 * otherwise skip part of the string. Differs when pattern is "^"
13833 * or "\<". */
13834 if (argvars[3].v_type != VAR_UNKNOWN)
13835 startcol = start;
13836 else
13837 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013838 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013839
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013840 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 nth = get_tv_number_chk(&argvars[3], &error);
13842 if (error)
13843 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 }
13845
13846 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13847 if (regmatch.regprog != NULL)
13848 {
13849 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013850
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013851 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013852 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013853 if (l != NULL)
13854 {
13855 if (li == NULL)
13856 {
13857 match = FALSE;
13858 break;
13859 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013860 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013861 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013862 if (str == NULL)
13863 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013864 }
13865
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013866 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013867
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013868 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013869 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013870 if (l == NULL && !match)
13871 break;
13872
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013873 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013874 if (l != NULL)
13875 {
13876 li = li->li_next;
13877 ++idx;
13878 }
13879 else
13880 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013881#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013882 startcol = (colnr_T)(regmatch.startp[0]
13883 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013884#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013885 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013886#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013887 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013888 }
13889
13890 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013892 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013893 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013894 int i;
13895
13896 /* return list with matched string and submatches */
13897 for (i = 0; i < NSUBEXP; ++i)
13898 {
13899 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013900 {
13901 if (list_append_string(rettv->vval.v_list,
13902 (char_u *)"", 0) == FAIL)
13903 break;
13904 }
13905 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013906 regmatch.startp[i],
13907 (int)(regmatch.endp[i] - regmatch.startp[i]))
13908 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013909 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013910 }
13911 }
13912 else if (type == 2)
13913 {
13914 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915 if (l != NULL)
13916 copy_tv(&li->li_tv, rettv);
13917 else
13918 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 }
13921 else if (l != NULL)
13922 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 else
13924 {
13925 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 (varnumber_T)(regmatch.startp[0] - str);
13928 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013931 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 }
13933 }
13934 vim_free(regmatch.regprog);
13935 }
13936
13937theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013938 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 p_cpo = save_cpo;
13940}
13941
13942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943 * "match()" function
13944 */
13945 static void
13946f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013947 typval_T *argvars;
13948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949{
13950 find_some_match(argvars, rettv, 1);
13951}
13952
13953/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013954 * "matchadd()" function
13955 */
13956 static void
13957f_matchadd(argvars, rettv)
13958 typval_T *argvars;
13959 typval_T *rettv;
13960{
13961#ifdef FEAT_SEARCH_EXTRA
13962 char_u buf[NUMBUFLEN];
13963 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13964 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13965 int prio = 10; /* default priority */
13966 int id = -1;
13967 int error = FALSE;
13968
13969 rettv->vval.v_number = -1;
13970
13971 if (grp == NULL || pat == NULL)
13972 return;
13973 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013974 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013975 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013976 if (argvars[3].v_type != VAR_UNKNOWN)
13977 id = get_tv_number_chk(&argvars[3], &error);
13978 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013979 if (error == TRUE)
13980 return;
13981 if (id >= 1 && id <= 3)
13982 {
13983 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13984 return;
13985 }
13986
13987 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13988#endif
13989}
13990
13991/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013992 * "matcharg()" function
13993 */
13994 static void
13995f_matcharg(argvars, rettv)
13996 typval_T *argvars;
13997 typval_T *rettv;
13998{
13999 if (rettv_list_alloc(rettv) == OK)
14000 {
14001#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014002 int id = get_tv_number(&argvars[0]);
14003 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014004
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014005 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014006 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014007 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14008 {
14009 list_append_string(rettv->vval.v_list,
14010 syn_id2name(m->hlg_id), -1);
14011 list_append_string(rettv->vval.v_list, m->pattern, -1);
14012 }
14013 else
14014 {
14015 list_append_string(rettv->vval.v_list, NUL, -1);
14016 list_append_string(rettv->vval.v_list, NUL, -1);
14017 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014018 }
14019#endif
14020 }
14021}
14022
14023/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014024 * "matchdelete()" function
14025 */
14026 static void
14027f_matchdelete(argvars, rettv)
14028 typval_T *argvars;
14029 typval_T *rettv;
14030{
14031#ifdef FEAT_SEARCH_EXTRA
14032 rettv->vval.v_number = match_delete(curwin,
14033 (int)get_tv_number(&argvars[0]), TRUE);
14034#endif
14035}
14036
14037/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014038 * "matchend()" function
14039 */
14040 static void
14041f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 typval_T *argvars;
14043 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044{
14045 find_some_match(argvars, rettv, 0);
14046}
14047
14048/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014049 * "matchlist()" function
14050 */
14051 static void
14052f_matchlist(argvars, rettv)
14053 typval_T *argvars;
14054 typval_T *rettv;
14055{
14056 find_some_match(argvars, rettv, 3);
14057}
14058
14059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060 * "matchstr()" function
14061 */
14062 static void
14063f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014064 typval_T *argvars;
14065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066{
14067 find_some_match(argvars, rettv, 2);
14068}
14069
Bram Moolenaar33570922005-01-25 22:26:29 +000014070static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014071
14072 static void
14073max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014076 int domax;
14077{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014078 long n = 0;
14079 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014081
14082 if (argvars[0].v_type == VAR_LIST)
14083 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014084 list_T *l;
14085 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014086
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014087 l = argvars[0].vval.v_list;
14088 if (l != NULL)
14089 {
14090 li = l->lv_first;
14091 if (li != NULL)
14092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014094 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014095 {
14096 li = li->li_next;
14097 if (li == NULL)
14098 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014100 if (domax ? i > n : i < n)
14101 n = i;
14102 }
14103 }
14104 }
14105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014106 else if (argvars[0].v_type == VAR_DICT)
14107 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014108 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014109 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014111 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014112
14113 d = argvars[0].vval.v_dict;
14114 if (d != NULL)
14115 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014116 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014119 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014120 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014121 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014122 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014123 if (first)
14124 {
14125 n = i;
14126 first = FALSE;
14127 }
14128 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014129 n = i;
14130 }
14131 }
14132 }
14133 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014134 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014135 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014137}
14138
14139/*
14140 * "max()" function
14141 */
14142 static void
14143f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 typval_T *argvars;
14145 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014146{
14147 max_min(argvars, rettv, TRUE);
14148}
14149
14150/*
14151 * "min()" function
14152 */
14153 static void
14154f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 typval_T *argvars;
14156 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014157{
14158 max_min(argvars, rettv, FALSE);
14159}
14160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014161static int mkdir_recurse __ARGS((char_u *dir, int prot));
14162
14163/*
14164 * Create the directory in which "dir" is located, and higher levels when
14165 * needed.
14166 */
14167 static int
14168mkdir_recurse(dir, prot)
14169 char_u *dir;
14170 int prot;
14171{
14172 char_u *p;
14173 char_u *updir;
14174 int r = FAIL;
14175
14176 /* Get end of directory name in "dir".
14177 * We're done when it's "/" or "c:/". */
14178 p = gettail_sep(dir);
14179 if (p <= get_past_head(dir))
14180 return OK;
14181
14182 /* If the directory exists we're done. Otherwise: create it.*/
14183 updir = vim_strnsave(dir, (int)(p - dir));
14184 if (updir == NULL)
14185 return FAIL;
14186 if (mch_isdir(updir))
14187 r = OK;
14188 else if (mkdir_recurse(updir, prot) == OK)
14189 r = vim_mkdir_emsg(updir, prot);
14190 vim_free(updir);
14191 return r;
14192}
14193
14194#ifdef vim_mkdir
14195/*
14196 * "mkdir()" function
14197 */
14198 static void
14199f_mkdir(argvars, rettv)
14200 typval_T *argvars;
14201 typval_T *rettv;
14202{
14203 char_u *dir;
14204 char_u buf[NUMBUFLEN];
14205 int prot = 0755;
14206
14207 rettv->vval.v_number = FAIL;
14208 if (check_restricted() || check_secure())
14209 return;
14210
14211 dir = get_tv_string_buf(&argvars[0], buf);
14212 if (argvars[1].v_type != VAR_UNKNOWN)
14213 {
14214 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 prot = get_tv_number_chk(&argvars[2], NULL);
14216 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014217 mkdir_recurse(dir, prot);
14218 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014220}
14221#endif
14222
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224 * "mode()" function
14225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014228 typval_T *argvars;
14229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014231 char_u buf[3];
14232
14233 buf[1] = NUL;
14234 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
14236#ifdef FEAT_VISUAL
14237 if (VIsual_active)
14238 {
14239 if (VIsual_select)
14240 buf[0] = VIsual_mode + 's' - 'v';
14241 else
14242 buf[0] = VIsual_mode;
14243 }
14244 else
14245#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014246 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14247 || State == CONFIRM)
14248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014250 if (State == ASKMORE)
14251 buf[1] = 'm';
14252 else if (State == CONFIRM)
14253 buf[1] = '?';
14254 }
14255 else if (State == EXTERNCMD)
14256 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 else if (State & INSERT)
14258 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014259#ifdef FEAT_VREPLACE
14260 if (State & VREPLACE_FLAG)
14261 {
14262 buf[0] = 'R';
14263 buf[1] = 'v';
14264 }
14265 else
14266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 if (State & REPLACE_FLAG)
14268 buf[0] = 'R';
14269 else
14270 buf[0] = 'i';
14271 }
14272 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014275 if (exmode_active)
14276 buf[1] = 'v';
14277 }
14278 else if (exmode_active)
14279 {
14280 buf[0] = 'c';
14281 buf[1] = 'e';
14282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014286 if (finish_op)
14287 buf[1] = 'o';
14288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014290 /* Clear out the minor mode when the argument is not a non-zero number or
14291 * non-empty string. */
14292 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014293 buf[1] = NUL;
14294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295 rettv->vval.v_string = vim_strsave(buf);
14296 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297}
14298
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014299#ifdef FEAT_MZSCHEME
14300/*
14301 * "mzeval()" function
14302 */
14303 static void
14304f_mzeval(argvars, rettv)
14305 typval_T *argvars;
14306 typval_T *rettv;
14307{
14308 char_u *str;
14309 char_u buf[NUMBUFLEN];
14310
14311 str = get_tv_string_buf(&argvars[0], buf);
14312 do_mzeval(str, rettv);
14313}
14314#endif
14315
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317 * "nextnonblank()" function
14318 */
14319 static void
14320f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 typval_T *argvars;
14322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323{
14324 linenr_T lnum;
14325
14326 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329 {
14330 lnum = 0;
14331 break;
14332 }
14333 if (*skipwhite(ml_get(lnum)) != NUL)
14334 break;
14335 }
14336 rettv->vval.v_number = lnum;
14337}
14338
14339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 * "nr2char()" function
14341 */
14342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014344 typval_T *argvars;
14345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346{
14347 char_u buf[NUMBUFLEN];
14348
14349#ifdef FEAT_MBYTE
14350 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 else
14353#endif
14354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014355 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 buf[1] = NUL;
14357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 rettv->v_type = VAR_STRING;
14359 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014360}
14361
14362/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014363 * "or(expr, expr)" function
14364 */
14365 static void
14366f_or(argvars, rettv)
14367 typval_T *argvars;
14368 typval_T *rettv;
14369{
14370 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14371 | get_tv_number_chk(&argvars[1], NULL);
14372}
14373
14374/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014375 * "pathshorten()" function
14376 */
14377 static void
14378f_pathshorten(argvars, rettv)
14379 typval_T *argvars;
14380 typval_T *rettv;
14381{
14382 char_u *p;
14383
14384 rettv->v_type = VAR_STRING;
14385 p = get_tv_string_chk(&argvars[0]);
14386 if (p == NULL)
14387 rettv->vval.v_string = NULL;
14388 else
14389 {
14390 p = vim_strsave(p);
14391 rettv->vval.v_string = p;
14392 if (p != NULL)
14393 shorten_dir(p);
14394 }
14395}
14396
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014397#ifdef FEAT_FLOAT
14398/*
14399 * "pow()" function
14400 */
14401 static void
14402f_pow(argvars, rettv)
14403 typval_T *argvars;
14404 typval_T *rettv;
14405{
14406 float_T fx, fy;
14407
14408 rettv->v_type = VAR_FLOAT;
14409 if (get_float_arg(argvars, &fx) == OK
14410 && get_float_arg(&argvars[1], &fy) == OK)
14411 rettv->vval.v_float = pow(fx, fy);
14412 else
14413 rettv->vval.v_float = 0.0;
14414}
14415#endif
14416
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014417/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418 * "prevnonblank()" function
14419 */
14420 static void
14421f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014422 typval_T *argvars;
14423 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014424{
14425 linenr_T lnum;
14426
14427 lnum = get_tv_lnum(argvars);
14428 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14429 lnum = 0;
14430 else
14431 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14432 --lnum;
14433 rettv->vval.v_number = lnum;
14434}
14435
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014436#ifdef HAVE_STDARG_H
14437/* This dummy va_list is here because:
14438 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14439 * - locally in the function results in a "used before set" warning
14440 * - using va_start() to initialize it gives "function with fixed args" error */
14441static va_list ap;
14442#endif
14443
Bram Moolenaar8c711452005-01-14 21:53:12 +000014444/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014445 * "printf()" function
14446 */
14447 static void
14448f_printf(argvars, rettv)
14449 typval_T *argvars;
14450 typval_T *rettv;
14451{
14452 rettv->v_type = VAR_STRING;
14453 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014454#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014455 {
14456 char_u buf[NUMBUFLEN];
14457 int len;
14458 char_u *s;
14459 int saved_did_emsg = did_emsg;
14460 char *fmt;
14461
14462 /* Get the required length, allocate the buffer and do it for real. */
14463 did_emsg = FALSE;
14464 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014465 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014466 if (!did_emsg)
14467 {
14468 s = alloc(len + 1);
14469 if (s != NULL)
14470 {
14471 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014472 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014473 }
14474 }
14475 did_emsg |= saved_did_emsg;
14476 }
14477#endif
14478}
14479
14480/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014481 * "pumvisible()" function
14482 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014483 static void
14484f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014485 typval_T *argvars UNUSED;
14486 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014487{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014488#ifdef FEAT_INS_EXPAND
14489 if (pum_visible())
14490 rettv->vval.v_number = 1;
14491#endif
14492}
14493
Bram Moolenaardb913952012-06-29 12:54:53 +020014494#ifdef FEAT_PYTHON3
14495/*
14496 * "py3eval()" function
14497 */
14498 static void
14499f_py3eval(argvars, rettv)
14500 typval_T *argvars;
14501 typval_T *rettv;
14502{
14503 char_u *str;
14504 char_u buf[NUMBUFLEN];
14505
14506 str = get_tv_string_buf(&argvars[0], buf);
14507 do_py3eval(str, rettv);
14508}
14509#endif
14510
14511#ifdef FEAT_PYTHON
14512/*
14513 * "pyeval()" function
14514 */
14515 static void
14516f_pyeval(argvars, rettv)
14517 typval_T *argvars;
14518 typval_T *rettv;
14519{
14520 char_u *str;
14521 char_u buf[NUMBUFLEN];
14522
14523 str = get_tv_string_buf(&argvars[0], buf);
14524 do_pyeval(str, rettv);
14525}
14526#endif
14527
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014528/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014529 * "range()" function
14530 */
14531 static void
14532f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014533 typval_T *argvars;
14534 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014535{
14536 long start;
14537 long end;
14538 long stride = 1;
14539 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543 if (argvars[1].v_type == VAR_UNKNOWN)
14544 {
14545 end = start - 1;
14546 start = 0;
14547 }
14548 else
14549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014553 }
14554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 if (error)
14556 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014557 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014558 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014559 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014560 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014561 else
14562 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014563 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014564 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014565 if (list_append_number(rettv->vval.v_list,
14566 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014567 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014568 }
14569}
14570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014571/*
14572 * "readfile()" function
14573 */
14574 static void
14575f_readfile(argvars, rettv)
14576 typval_T *argvars;
14577 typval_T *rettv;
14578{
14579 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014580 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014581 char_u *fname;
14582 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014583 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14584 int io_size = sizeof(buf);
14585 int readlen; /* size of last fread() */
14586 char_u *prev = NULL; /* previously read bytes, if any */
14587 long prevlen = 0; /* length of data in prev */
14588 long prevsize = 0; /* size of prev buffer */
14589 long maxline = MAXLNUM;
14590 long cnt = 0;
14591 char_u *p; /* position in buf */
14592 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014594 if (argvars[1].v_type != VAR_UNKNOWN)
14595 {
14596 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14597 binary = TRUE;
14598 if (argvars[2].v_type != VAR_UNKNOWN)
14599 maxline = get_tv_number(&argvars[2]);
14600 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014602 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014603 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014604
14605 /* Always open the file in binary mode, library functions have a mind of
14606 * their own about CR-LF conversion. */
14607 fname = get_tv_string(&argvars[0]);
14608 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14609 {
14610 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14611 return;
14612 }
14613
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014614 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014615 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014616 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014617
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014618 /* This for loop processes what was read, but is also entered at end
14619 * of file so that either:
14620 * - an incomplete line gets written
14621 * - a "binary" file gets an empty line at the end if it ends in a
14622 * newline. */
14623 for (p = buf, start = buf;
14624 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14625 ++p)
14626 {
14627 if (*p == '\n' || readlen <= 0)
14628 {
14629 listitem_T *li;
14630 char_u *s = NULL;
14631 long_u len = p - start;
14632
14633 /* Finished a line. Remove CRs before NL. */
14634 if (readlen > 0 && !binary)
14635 {
14636 while (len > 0 && start[len - 1] == '\r')
14637 --len;
14638 /* removal may cross back to the "prev" string */
14639 if (len == 0)
14640 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14641 --prevlen;
14642 }
14643 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014644 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014645 else
14646 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014647 /* Change "prev" buffer to be the right size. This way
14648 * the bytes are only copied once, and very long lines are
14649 * allocated only once. */
14650 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014651 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014652 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014653 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014654 prev = NULL; /* the list will own the string */
14655 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014656 }
14657 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014658 if (s == NULL)
14659 {
14660 do_outofmem_msg((long_u) prevlen + len + 1);
14661 failed = TRUE;
14662 break;
14663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014665 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014666 {
14667 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014668 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014669 break;
14670 }
14671 li->li_tv.v_type = VAR_STRING;
14672 li->li_tv.v_lock = 0;
14673 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014674 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014675
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014676 start = p + 1; /* step over newline */
14677 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 break;
14679 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014680 else if (*p == NUL)
14681 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014682#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014683 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14684 * when finding the BF and check the previous two bytes. */
14685 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014686 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014687 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14688 * + 1, these may be in the "prev" string. */
14689 char_u back1 = p >= buf + 1 ? p[-1]
14690 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14691 char_u back2 = p >= buf + 2 ? p[-2]
14692 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14693 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014694
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014695 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014696 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014697 char_u *dest = p - 2;
14698
14699 /* Usually a BOM is at the beginning of a file, and so at
14700 * the beginning of a line; then we can just step over it.
14701 */
14702 if (start == dest)
14703 start = p + 1;
14704 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014705 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014706 /* have to shuffle buf to close gap */
14707 int adjust_prevlen = 0;
14708
14709 if (dest < buf)
14710 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014711 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014712 dest = buf;
14713 }
14714 if (readlen > p - buf + 1)
14715 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14716 readlen -= 3 - adjust_prevlen;
14717 prevlen -= adjust_prevlen;
14718 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014719 }
14720 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014722#endif
14723 } /* for */
14724
14725 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14726 break;
14727 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 /* There's part of a line in buf, store it in "prev". */
14730 if (p - start + prevlen >= prevsize)
14731 {
14732 /* need bigger "prev" buffer */
14733 char_u *newprev;
14734
14735 /* A common use case is ordinary text files and "prev" gets a
14736 * fragment of a line, so the first allocation is made
14737 * small, to avoid repeatedly 'allocing' large and
14738 * 'reallocing' small. */
14739 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014740 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 else
14742 {
14743 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014744 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014745 prevsize = grow50pc > growmin ? grow50pc : growmin;
14746 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014747 newprev = prev == NULL ? alloc(prevsize)
14748 : vim_realloc(prev, prevsize);
14749 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014750 {
14751 do_outofmem_msg((long_u)prevsize);
14752 failed = TRUE;
14753 break;
14754 }
14755 prev = newprev;
14756 }
14757 /* Add the line part to end of "prev". */
14758 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014759 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014760 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014761 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014762
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014763 /*
14764 * For a negative line count use only the lines at the end of the file,
14765 * free the rest.
14766 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014767 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014768 while (cnt > -maxline)
14769 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014770 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014771 --cnt;
14772 }
14773
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014774 if (failed)
14775 {
14776 list_free(rettv->vval.v_list, TRUE);
14777 /* readfile doc says an empty list is returned on error */
14778 rettv->vval.v_list = list_alloc();
14779 }
14780
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014781 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014782 fclose(fd);
14783}
14784
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014785#if defined(FEAT_RELTIME)
14786static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14787
14788/*
14789 * Convert a List to proftime_T.
14790 * Return FAIL when there is something wrong.
14791 */
14792 static int
14793list2proftime(arg, tm)
14794 typval_T *arg;
14795 proftime_T *tm;
14796{
14797 long n1, n2;
14798 int error = FALSE;
14799
14800 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14801 || arg->vval.v_list->lv_len != 2)
14802 return FAIL;
14803 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14804 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14805# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014806 tm->HighPart = n1;
14807 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014808# else
14809 tm->tv_sec = n1;
14810 tm->tv_usec = n2;
14811# endif
14812 return error ? FAIL : OK;
14813}
14814#endif /* FEAT_RELTIME */
14815
14816/*
14817 * "reltime()" function
14818 */
14819 static void
14820f_reltime(argvars, rettv)
14821 typval_T *argvars;
14822 typval_T *rettv;
14823{
14824#ifdef FEAT_RELTIME
14825 proftime_T res;
14826 proftime_T start;
14827
14828 if (argvars[0].v_type == VAR_UNKNOWN)
14829 {
14830 /* No arguments: get current time. */
14831 profile_start(&res);
14832 }
14833 else if (argvars[1].v_type == VAR_UNKNOWN)
14834 {
14835 if (list2proftime(&argvars[0], &res) == FAIL)
14836 return;
14837 profile_end(&res);
14838 }
14839 else
14840 {
14841 /* Two arguments: compute the difference. */
14842 if (list2proftime(&argvars[0], &start) == FAIL
14843 || list2proftime(&argvars[1], &res) == FAIL)
14844 return;
14845 profile_sub(&res, &start);
14846 }
14847
14848 if (rettv_list_alloc(rettv) == OK)
14849 {
14850 long n1, n2;
14851
14852# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014853 n1 = res.HighPart;
14854 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014855# else
14856 n1 = res.tv_sec;
14857 n2 = res.tv_usec;
14858# endif
14859 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14860 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14861 }
14862#endif
14863}
14864
14865/*
14866 * "reltimestr()" function
14867 */
14868 static void
14869f_reltimestr(argvars, rettv)
14870 typval_T *argvars;
14871 typval_T *rettv;
14872{
14873#ifdef FEAT_RELTIME
14874 proftime_T tm;
14875#endif
14876
14877 rettv->v_type = VAR_STRING;
14878 rettv->vval.v_string = NULL;
14879#ifdef FEAT_RELTIME
14880 if (list2proftime(&argvars[0], &tm) == OK)
14881 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14882#endif
14883}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014884
Bram Moolenaar0d660222005-01-07 21:51:51 +000014885#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14886static void make_connection __ARGS((void));
14887static int check_connection __ARGS((void));
14888
14889 static void
14890make_connection()
14891{
14892 if (X_DISPLAY == NULL
14893# ifdef FEAT_GUI
14894 && !gui.in_use
14895# endif
14896 )
14897 {
14898 x_force_connect = TRUE;
14899 setup_term_clip();
14900 x_force_connect = FALSE;
14901 }
14902}
14903
14904 static int
14905check_connection()
14906{
14907 make_connection();
14908 if (X_DISPLAY == NULL)
14909 {
14910 EMSG(_("E240: No connection to Vim server"));
14911 return FAIL;
14912 }
14913 return OK;
14914}
14915#endif
14916
14917#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014918static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919
14920 static void
14921remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 typval_T *argvars;
14923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014924 int expr;
14925{
14926 char_u *server_name;
14927 char_u *keys;
14928 char_u *r = NULL;
14929 char_u buf[NUMBUFLEN];
14930# ifdef WIN32
14931 HWND w;
14932# else
14933 Window w;
14934# endif
14935
14936 if (check_restricted() || check_secure())
14937 return;
14938
14939# ifdef FEAT_X11
14940 if (check_connection() == FAIL)
14941 return;
14942# endif
14943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 server_name = get_tv_string_chk(&argvars[0]);
14945 if (server_name == NULL)
14946 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 keys = get_tv_string_buf(&argvars[1], buf);
14948# ifdef WIN32
14949 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14950# else
14951 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14952 < 0)
14953# endif
14954 {
14955 if (r != NULL)
14956 EMSG(r); /* sending worked but evaluation failed */
14957 else
14958 EMSG2(_("E241: Unable to send to %s"), server_name);
14959 return;
14960 }
14961
14962 rettv->vval.v_string = r;
14963
14964 if (argvars[2].v_type != VAR_UNKNOWN)
14965 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014966 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014967 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014970 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014971 v.di_tv.v_type = VAR_STRING;
14972 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 idvar = get_tv_string_chk(&argvars[2]);
14974 if (idvar != NULL)
14975 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 }
14978}
14979#endif
14980
14981/*
14982 * "remote_expr()" function
14983 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 static void
14985f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988{
14989 rettv->v_type = VAR_STRING;
14990 rettv->vval.v_string = NULL;
14991#ifdef FEAT_CLIENTSERVER
14992 remote_common(argvars, rettv, TRUE);
14993#endif
14994}
14995
14996/*
14997 * "remote_foreground()" function
14998 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014999 static void
15000f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015001 typval_T *argvars UNUSED;
15002 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015004#ifdef FEAT_CLIENTSERVER
15005# ifdef WIN32
15006 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015007 {
15008 char_u *server_name = get_tv_string_chk(&argvars[0]);
15009
15010 if (server_name != NULL)
15011 serverForeground(server_name);
15012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013# else
15014 /* Send a foreground() expression to the server. */
15015 argvars[1].v_type = VAR_STRING;
15016 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15017 argvars[2].v_type = VAR_UNKNOWN;
15018 remote_common(argvars, rettv, TRUE);
15019 vim_free(argvars[1].vval.v_string);
15020# endif
15021#endif
15022}
15023
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 static void
15025f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028{
15029#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031 char_u *s = NULL;
15032# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015033 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015035 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036
15037 if (check_restricted() || check_secure())
15038 {
15039 rettv->vval.v_number = -1;
15040 return;
15041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015042 serverid = get_tv_string_chk(&argvars[0]);
15043 if (serverid == NULL)
15044 {
15045 rettv->vval.v_number = -1;
15046 return; /* type error; errmsg already given */
15047 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015049 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050 if (n == 0)
15051 rettv->vval.v_number = -1;
15052 else
15053 {
15054 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15055 rettv->vval.v_number = (s != NULL);
15056 }
15057# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 if (check_connection() == FAIL)
15059 return;
15060
15061 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015062 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063# endif
15064
15065 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015067 char_u *retvar;
15068
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 v.di_tv.v_type = VAR_STRING;
15070 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015071 retvar = get_tv_string_chk(&argvars[1]);
15072 if (retvar != NULL)
15073 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 }
15076#else
15077 rettv->vval.v_number = -1;
15078#endif
15079}
15080
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081 static void
15082f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015083 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085{
15086 char_u *r = NULL;
15087
15088#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 char_u *serverid = get_tv_string_chk(&argvars[0]);
15090
15091 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 {
15093# ifdef WIN32
15094 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015095 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015096
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015097 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098 if (n != 0)
15099 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15100 if (r == NULL)
15101# else
15102 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104# endif
15105 EMSG(_("E277: Unable to read a server reply"));
15106 }
15107#endif
15108 rettv->v_type = VAR_STRING;
15109 rettv->vval.v_string = r;
15110}
15111
15112/*
15113 * "remote_send()" function
15114 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115 static void
15116f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119{
15120 rettv->v_type = VAR_STRING;
15121 rettv->vval.v_string = NULL;
15122#ifdef FEAT_CLIENTSERVER
15123 remote_common(argvars, rettv, FALSE);
15124#endif
15125}
15126
15127/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015128 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015129 */
15130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 typval_T *argvars;
15133 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015134{
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 list_T *l;
15136 listitem_T *item, *item2;
15137 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015138 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015139 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015140 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 dict_T *d;
15142 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015143 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015144
Bram Moolenaar8c711452005-01-14 21:53:12 +000015145 if (argvars[0].v_type == VAR_DICT)
15146 {
15147 if (argvars[2].v_type != VAR_UNKNOWN)
15148 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015149 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015150 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 key = get_tv_string_chk(&argvars[1]);
15153 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015155 di = dict_find(d, key, -1);
15156 if (di == NULL)
15157 EMSG2(_(e_dictkey), key);
15158 else
15159 {
15160 *rettv = di->di_tv;
15161 init_tv(&di->di_tv);
15162 dictitem_remove(d, di);
15163 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015164 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015165 }
15166 }
15167 else if (argvars[0].v_type != VAR_LIST)
15168 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015169 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015170 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015172 int error = FALSE;
15173
15174 idx = get_tv_number_chk(&argvars[1], &error);
15175 if (error)
15176 ; /* type error: do nothing, errmsg already given */
15177 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015178 EMSGN(_(e_listidx), idx);
15179 else
15180 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015181 if (argvars[2].v_type == VAR_UNKNOWN)
15182 {
15183 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015184 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015185 *rettv = item->li_tv;
15186 vim_free(item);
15187 }
15188 else
15189 {
15190 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191 end = get_tv_number_chk(&argvars[2], &error);
15192 if (error)
15193 ; /* type error: do nothing */
15194 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015195 EMSGN(_(e_listidx), end);
15196 else
15197 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015198 int cnt = 0;
15199
15200 for (li = item; li != NULL; li = li->li_next)
15201 {
15202 ++cnt;
15203 if (li == item2)
15204 break;
15205 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015206 if (li == NULL) /* didn't find "item2" after "item" */
15207 EMSG(_(e_invrange));
15208 else
15209 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015210 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015211 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015212 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015213 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015214 l->lv_first = item;
15215 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015216 item->li_prev = NULL;
15217 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015218 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015219 }
15220 }
15221 }
15222 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015223 }
15224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225}
15226
15227/*
15228 * "rename({from}, {to})" function
15229 */
15230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *argvars;
15233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234{
15235 char_u buf[NUMBUFLEN];
15236
15237 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015240 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15241 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242}
15243
15244/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015245 * "repeat()" function
15246 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015247 static void
15248f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015249 typval_T *argvars;
15250 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015251{
15252 char_u *p;
15253 int n;
15254 int slen;
15255 int len;
15256 char_u *r;
15257 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015258
15259 n = get_tv_number(&argvars[1]);
15260 if (argvars[0].v_type == VAR_LIST)
15261 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015262 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015263 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015264 if (list_extend(rettv->vval.v_list,
15265 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015266 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015267 }
15268 else
15269 {
15270 p = get_tv_string(&argvars[0]);
15271 rettv->v_type = VAR_STRING;
15272 rettv->vval.v_string = NULL;
15273
15274 slen = (int)STRLEN(p);
15275 len = slen * n;
15276 if (len <= 0)
15277 return;
15278
15279 r = alloc(len + 1);
15280 if (r != NULL)
15281 {
15282 for (i = 0; i < n; i++)
15283 mch_memmove(r + i * slen, p, (size_t)slen);
15284 r[len] = NUL;
15285 }
15286
15287 rettv->vval.v_string = r;
15288 }
15289}
15290
15291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 * "resolve()" function
15293 */
15294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015295f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015296 typval_T *argvars;
15297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298{
15299 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015300#ifdef HAVE_READLINK
15301 char_u *buf = NULL;
15302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305#ifdef FEAT_SHORTCUT
15306 {
15307 char_u *v = NULL;
15308
15309 v = mch_resolve_shortcut(p);
15310 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 }
15315#else
15316# ifdef HAVE_READLINK
15317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 char_u *cpy;
15319 int len;
15320 char_u *remain = NULL;
15321 char_u *q;
15322 int is_relative_to_current = FALSE;
15323 int has_trailing_pathsep = FALSE;
15324 int limit = 100;
15325
15326 p = vim_strsave(p);
15327
15328 if (p[0] == '.' && (vim_ispathsep(p[1])
15329 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15330 is_relative_to_current = TRUE;
15331
15332 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015333 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015336 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338
15339 q = getnextcomp(p);
15340 if (*q != NUL)
15341 {
15342 /* Separate the first path component in "p", and keep the
15343 * remainder (beginning with the path separator). */
15344 remain = vim_strsave(q - 1);
15345 q[-1] = NUL;
15346 }
15347
Bram Moolenaard9462e32011-04-11 21:35:11 +020015348 buf = alloc(MAXPATHL + 1);
15349 if (buf == NULL)
15350 goto fail;
15351
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 for (;;)
15353 {
15354 for (;;)
15355 {
15356 len = readlink((char *)p, (char *)buf, MAXPATHL);
15357 if (len <= 0)
15358 break;
15359 buf[len] = NUL;
15360
15361 if (limit-- == 0)
15362 {
15363 vim_free(p);
15364 vim_free(remain);
15365 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 goto fail;
15368 }
15369
15370 /* Ensure that the result will have a trailing path separator
15371 * if the argument has one. */
15372 if (remain == NULL && has_trailing_pathsep)
15373 add_pathsep(buf);
15374
15375 /* Separate the first path component in the link value and
15376 * concatenate the remainders. */
15377 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15378 if (*q != NUL)
15379 {
15380 if (remain == NULL)
15381 remain = vim_strsave(q - 1);
15382 else
15383 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015384 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 if (cpy != NULL)
15386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 vim_free(remain);
15388 remain = cpy;
15389 }
15390 }
15391 q[-1] = NUL;
15392 }
15393
15394 q = gettail(p);
15395 if (q > p && *q == NUL)
15396 {
15397 /* Ignore trailing path separator. */
15398 q[-1] = NUL;
15399 q = gettail(p);
15400 }
15401 if (q > p && !mch_isFullName(buf))
15402 {
15403 /* symlink is relative to directory of argument */
15404 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15405 if (cpy != NULL)
15406 {
15407 STRCPY(cpy, p);
15408 STRCPY(gettail(cpy), buf);
15409 vim_free(p);
15410 p = cpy;
15411 }
15412 }
15413 else
15414 {
15415 vim_free(p);
15416 p = vim_strsave(buf);
15417 }
15418 }
15419
15420 if (remain == NULL)
15421 break;
15422
15423 /* Append the first path component of "remain" to "p". */
15424 q = getnextcomp(remain + 1);
15425 len = q - remain - (*q != NUL);
15426 cpy = vim_strnsave(p, STRLEN(p) + len);
15427 if (cpy != NULL)
15428 {
15429 STRNCAT(cpy, remain, len);
15430 vim_free(p);
15431 p = cpy;
15432 }
15433 /* Shorten "remain". */
15434 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015435 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 else
15437 {
15438 vim_free(remain);
15439 remain = NULL;
15440 }
15441 }
15442
15443 /* If the result is a relative path name, make it explicitly relative to
15444 * the current directory if and only if the argument had this form. */
15445 if (!vim_ispathsep(*p))
15446 {
15447 if (is_relative_to_current
15448 && *p != NUL
15449 && !(p[0] == '.'
15450 && (p[1] == NUL
15451 || vim_ispathsep(p[1])
15452 || (p[1] == '.'
15453 && (p[2] == NUL
15454 || vim_ispathsep(p[2]))))))
15455 {
15456 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015457 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 if (cpy != NULL)
15459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 vim_free(p);
15461 p = cpy;
15462 }
15463 }
15464 else if (!is_relative_to_current)
15465 {
15466 /* Strip leading "./". */
15467 q = p;
15468 while (q[0] == '.' && vim_ispathsep(q[1]))
15469 q += 2;
15470 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015471 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 }
15473 }
15474
15475 /* Ensure that the result will have no trailing path separator
15476 * if the argument had none. But keep "/" or "//". */
15477 if (!has_trailing_pathsep)
15478 {
15479 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015480 if (after_pathsep(p, q))
15481 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 }
15483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 }
15486# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488# endif
15489#endif
15490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492
15493#ifdef HAVE_READLINK
15494fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015495 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498}
15499
15500/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015501 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 */
15503 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015504f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *argvars;
15506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507{
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 list_T *l;
15509 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 if (argvars[0].v_type != VAR_LIST)
15512 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015513 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015514 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515 {
15516 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015517 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015518 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015519 while (li != NULL)
15520 {
15521 ni = li->li_prev;
15522 list_append(l, li);
15523 li = ni;
15524 }
15525 rettv->vval.v_list = l;
15526 rettv->v_type = VAR_LIST;
15527 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015528 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530}
15531
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015532#define SP_NOMOVE 0x01 /* don't move cursor */
15533#define SP_REPEAT 0x02 /* repeat to find outer pair */
15534#define SP_RETCOUNT 0x04 /* return matchcount */
15535#define SP_SETPCMARK 0x08 /* set previous context mark */
15536#define SP_START 0x10 /* accept match at start position */
15537#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15538#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015539
Bram Moolenaar33570922005-01-25 22:26:29 +000015540static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015541
15542/*
15543 * Get flags for a search function.
15544 * Possibly sets "p_ws".
15545 * Returns BACKWARD, FORWARD or zero (for an error).
15546 */
15547 static int
15548get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015550 int *flagsp;
15551{
15552 int dir = FORWARD;
15553 char_u *flags;
15554 char_u nbuf[NUMBUFLEN];
15555 int mask;
15556
15557 if (varp->v_type != VAR_UNKNOWN)
15558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015559 flags = get_tv_string_buf_chk(varp, nbuf);
15560 if (flags == NULL)
15561 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 while (*flags != NUL)
15563 {
15564 switch (*flags)
15565 {
15566 case 'b': dir = BACKWARD; break;
15567 case 'w': p_ws = TRUE; break;
15568 case 'W': p_ws = FALSE; break;
15569 default: mask = 0;
15570 if (flagsp != NULL)
15571 switch (*flags)
15572 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015573 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015574 case 'e': mask = SP_END; break;
15575 case 'm': mask = SP_RETCOUNT; break;
15576 case 'n': mask = SP_NOMOVE; break;
15577 case 'p': mask = SP_SUBPAT; break;
15578 case 'r': mask = SP_REPEAT; break;
15579 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580 }
15581 if (mask == 0)
15582 {
15583 EMSG2(_(e_invarg2), flags);
15584 dir = 0;
15585 }
15586 else
15587 *flagsp |= mask;
15588 }
15589 if (dir == 0)
15590 break;
15591 ++flags;
15592 }
15593 }
15594 return dir;
15595}
15596
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015598 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015600 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015601search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015602 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015603 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015604 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015606 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 char_u *pat;
15608 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015609 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 int save_p_ws = p_ws;
15611 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015612 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015613 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015614 proftime_T tm;
15615#ifdef FEAT_RELTIME
15616 long time_limit = 0;
15617#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015618 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015619 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015621 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015622 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015623 if (dir == 0)
15624 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015625 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015626 if (flags & SP_START)
15627 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015628 if (flags & SP_END)
15629 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015630
Bram Moolenaar76929292008-01-06 19:07:36 +000015631 /* Optional arguments: line number to stop searching and timeout. */
15632 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015633 {
15634 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15635 if (lnum_stop < 0)
15636 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015637#ifdef FEAT_RELTIME
15638 if (argvars[3].v_type != VAR_UNKNOWN)
15639 {
15640 time_limit = get_tv_number_chk(&argvars[3], NULL);
15641 if (time_limit < 0)
15642 goto theend;
15643 }
15644#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015645 }
15646
Bram Moolenaar76929292008-01-06 19:07:36 +000015647#ifdef FEAT_RELTIME
15648 /* Set the time limit, if there is one. */
15649 profile_setlimit(time_limit, &tm);
15650#endif
15651
Bram Moolenaar231334e2005-07-25 20:46:57 +000015652 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015653 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015654 * Check to make sure only those flags are set.
15655 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15656 * flags cannot be set. Check for that condition also.
15657 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015658 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015659 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015662 goto theend;
15663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015665 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015666 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015667 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015668 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015670 if (flags & SP_SUBPAT)
15671 retval = subpatnum;
15672 else
15673 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015674 if (flags & SP_SETPCMARK)
15675 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015677 if (match_pos != NULL)
15678 {
15679 /* Store the match cursor position */
15680 match_pos->lnum = pos.lnum;
15681 match_pos->col = pos.col + 1;
15682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 /* "/$" will put the cursor after the end of the line, may need to
15684 * correct that here */
15685 check_cursor();
15686 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015687
15688 /* If 'n' flag is used: restore cursor position. */
15689 if (flags & SP_NOMOVE)
15690 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015691 else
15692 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015693theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015695
15696 return retval;
15697}
15698
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015699#ifdef FEAT_FLOAT
15700/*
15701 * "round({float})" function
15702 */
15703 static void
15704f_round(argvars, rettv)
15705 typval_T *argvars;
15706 typval_T *rettv;
15707{
15708 float_T f;
15709
15710 rettv->v_type = VAR_FLOAT;
15711 if (get_float_arg(argvars, &f) == OK)
15712 /* round() is not in C90, use ceil() or floor() instead. */
15713 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15714 else
15715 rettv->vval.v_float = 0.0;
15716}
15717#endif
15718
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015719/*
15720 * "search()" function
15721 */
15722 static void
15723f_search(argvars, rettv)
15724 typval_T *argvars;
15725 typval_T *rettv;
15726{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015727 int flags = 0;
15728
15729 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730}
15731
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015733 * "searchdecl()" function
15734 */
15735 static void
15736f_searchdecl(argvars, rettv)
15737 typval_T *argvars;
15738 typval_T *rettv;
15739{
15740 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015741 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015742 int error = FALSE;
15743 char_u *name;
15744
15745 rettv->vval.v_number = 1; /* default: FAIL */
15746
15747 name = get_tv_string_chk(&argvars[0]);
15748 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015749 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015750 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015751 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15752 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15753 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015754 if (!error && name != NULL)
15755 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015756 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015757}
15758
15759/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015760 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015762 static int
15763searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015764 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015765 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766{
15767 char_u *spat, *mpat, *epat;
15768 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 int dir;
15771 int flags = 0;
15772 char_u nbuf1[NUMBUFLEN];
15773 char_u nbuf2[NUMBUFLEN];
15774 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015775 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015776 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015777 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 spat = get_tv_string_chk(&argvars[0]);
15781 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15782 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15783 if (spat == NULL || mpat == NULL || epat == NULL)
15784 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 /* Handle the optional fourth argument: flags */
15787 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015788 if (dir == 0)
15789 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015790
15791 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015792 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15793 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015794 if ((flags & (SP_END | SP_SUBPAT)) != 0
15795 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015796 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015797 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015798 goto theend;
15799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015801 /* Using 'r' implies 'W', otherwise it doesn't work. */
15802 if (flags & SP_REPEAT)
15803 p_ws = FALSE;
15804
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015805 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015806 if (argvars[3].v_type == VAR_UNKNOWN
15807 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 skip = (char_u *)"";
15809 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015812 if (argvars[5].v_type != VAR_UNKNOWN)
15813 {
15814 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15815 if (lnum_stop < 0)
15816 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015817#ifdef FEAT_RELTIME
15818 if (argvars[6].v_type != VAR_UNKNOWN)
15819 {
15820 time_limit = get_tv_number_chk(&argvars[6], NULL);
15821 if (time_limit < 0)
15822 goto theend;
15823 }
15824#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015825 }
15826 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015827 if (skip == NULL)
15828 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015830 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015831 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015832
15833theend:
15834 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015835
15836 return retval;
15837}
15838
15839/*
15840 * "searchpair()" function
15841 */
15842 static void
15843f_searchpair(argvars, rettv)
15844 typval_T *argvars;
15845 typval_T *rettv;
15846{
15847 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15848}
15849
15850/*
15851 * "searchpairpos()" function
15852 */
15853 static void
15854f_searchpairpos(argvars, rettv)
15855 typval_T *argvars;
15856 typval_T *rettv;
15857{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015858 pos_T match_pos;
15859 int lnum = 0;
15860 int col = 0;
15861
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015862 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015863 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015864
15865 if (searchpair_cmn(argvars, &match_pos) > 0)
15866 {
15867 lnum = match_pos.lnum;
15868 col = match_pos.col;
15869 }
15870
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015871 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15872 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015873}
15874
15875/*
15876 * Search for a start/middle/end thing.
15877 * Used by searchpair(), see its documentation for the details.
15878 * Returns 0 or -1 for no match,
15879 */
15880 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015881do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15882 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015883 char_u *spat; /* start pattern */
15884 char_u *mpat; /* middle pattern */
15885 char_u *epat; /* end pattern */
15886 int dir; /* BACKWARD or FORWARD */
15887 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015888 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015889 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015890 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015891 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015892{
15893 char_u *save_cpo;
15894 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15895 long retval = 0;
15896 pos_T pos;
15897 pos_T firstpos;
15898 pos_T foundpos;
15899 pos_T save_cursor;
15900 pos_T save_pos;
15901 int n;
15902 int r;
15903 int nest = 1;
15904 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015905 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015906 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015907
15908 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15909 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015910 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015911
Bram Moolenaar76929292008-01-06 19:07:36 +000015912#ifdef FEAT_RELTIME
15913 /* Set the time limit, if there is one. */
15914 profile_setlimit(time_limit, &tm);
15915#endif
15916
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015917 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15918 * start/middle/end (pat3, for the top pair). */
15919 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15920 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15921 if (pat2 == NULL || pat3 == NULL)
15922 goto theend;
15923 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15924 if (*mpat == NUL)
15925 STRCPY(pat3, pat2);
15926 else
15927 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15928 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015929 if (flags & SP_START)
15930 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015931
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 save_cursor = curwin->w_cursor;
15933 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015934 clearpos(&firstpos);
15935 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 pat = pat3;
15937 for (;;)
15938 {
15939 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015940 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15942 /* didn't find it or found the first match again: FAIL */
15943 break;
15944
15945 if (firstpos.lnum == 0)
15946 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015947 if (equalpos(pos, foundpos))
15948 {
15949 /* Found the same position again. Can happen with a pattern that
15950 * has "\zs" at the end and searching backwards. Advance one
15951 * character and try again. */
15952 if (dir == BACKWARD)
15953 decl(&pos);
15954 else
15955 incl(&pos);
15956 }
15957 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015959 /* clear the start flag to avoid getting stuck here */
15960 options &= ~SEARCH_START;
15961
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962 /* If the skip pattern matches, ignore this match. */
15963 if (*skip != NUL)
15964 {
15965 save_pos = curwin->w_cursor;
15966 curwin->w_cursor = pos;
15967 r = eval_to_bool(skip, &err, NULL, FALSE);
15968 curwin->w_cursor = save_pos;
15969 if (err)
15970 {
15971 /* Evaluating {skip} caused an error, break here. */
15972 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015973 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 break;
15975 }
15976 if (r)
15977 continue;
15978 }
15979
15980 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15981 {
15982 /* Found end when searching backwards or start when searching
15983 * forward: nested pair. */
15984 ++nest;
15985 pat = pat2; /* nested, don't search for middle */
15986 }
15987 else
15988 {
15989 /* Found end when searching forward or start when searching
15990 * backward: end of (nested) pair; or found middle in outer pair. */
15991 if (--nest == 1)
15992 pat = pat3; /* outer level, search for middle */
15993 }
15994
15995 if (nest == 0)
15996 {
15997 /* Found the match: return matchcount or line number. */
15998 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015999 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016001 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016002 if (flags & SP_SETPCMARK)
16003 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 curwin->w_cursor = pos;
16005 if (!(flags & SP_REPEAT))
16006 break;
16007 nest = 1; /* search for next unmatched */
16008 }
16009 }
16010
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016011 if (match_pos != NULL)
16012 {
16013 /* Store the match cursor position */
16014 match_pos->lnum = curwin->w_cursor.lnum;
16015 match_pos->col = curwin->w_cursor.col + 1;
16016 }
16017
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016019 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 curwin->w_cursor = save_cursor;
16021
16022theend:
16023 vim_free(pat2);
16024 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016025 if (p_cpo == empty_option)
16026 p_cpo = save_cpo;
16027 else
16028 /* Darn, evaluating the {skip} expression changed the value. */
16029 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016030
16031 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032}
16033
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016034/*
16035 * "searchpos()" function
16036 */
16037 static void
16038f_searchpos(argvars, rettv)
16039 typval_T *argvars;
16040 typval_T *rettv;
16041{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016042 pos_T match_pos;
16043 int lnum = 0;
16044 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016045 int n;
16046 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016047
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016048 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016049 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016051 n = search_cmn(argvars, &match_pos, &flags);
16052 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016053 {
16054 lnum = match_pos.lnum;
16055 col = match_pos.col;
16056 }
16057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016058 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16059 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016060 if (flags & SP_SUBPAT)
16061 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016062}
16063
16064
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065 static void
16066f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016067 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070#ifdef FEAT_CLIENTSERVER
16071 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016072 char_u *server = get_tv_string_chk(&argvars[0]);
16073 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074
Bram Moolenaar0d660222005-01-07 21:51:51 +000016075 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016076 if (server == NULL || reply == NULL)
16077 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016078 if (check_restricted() || check_secure())
16079 return;
16080# ifdef FEAT_X11
16081 if (check_connection() == FAIL)
16082 return;
16083# endif
16084
16085 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 EMSG(_("E258: Unable to send to client"));
16088 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090 rettv->vval.v_number = 0;
16091#else
16092 rettv->vval.v_number = -1;
16093#endif
16094}
16095
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096 static void
16097f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016100{
16101 char_u *r = NULL;
16102
16103#ifdef FEAT_CLIENTSERVER
16104# ifdef WIN32
16105 r = serverGetVimNames();
16106# else
16107 make_connection();
16108 if (X_DISPLAY != NULL)
16109 r = serverGetVimNames(X_DISPLAY);
16110# endif
16111#endif
16112 rettv->v_type = VAR_STRING;
16113 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114}
16115
16116/*
16117 * "setbufvar()" function
16118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016120f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016121 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016122 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123{
16124 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016127 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 char_u nbuf[NUMBUFLEN];
16129
16130 if (check_restricted() || check_secure())
16131 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16133 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016134 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 varp = &argvars[2];
16136
16137 if (buf != NULL && varname != NULL && varp != NULL)
16138 {
16139 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141
16142 if (*varname == '&')
16143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 long numval;
16145 char_u *strval;
16146 int error = FALSE;
16147
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016149 numval = get_tv_number_chk(varp, &error);
16150 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 if (!error && strval != NULL)
16152 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 }
16154 else
16155 {
16156 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16157 if (bufvarname != NULL)
16158 {
16159 STRCPY(bufvarname, "b:");
16160 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016161 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 vim_free(bufvarname);
16163 }
16164 }
16165
16166 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169}
16170
16171/*
16172 * "setcmdpos()" function
16173 */
16174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 typval_T *argvars;
16177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016179 int pos = (int)get_tv_number(&argvars[0]) - 1;
16180
16181 if (pos >= 0)
16182 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183}
16184
16185/*
16186 * "setline()" function
16187 */
16188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016189f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016190 typval_T *argvars;
16191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192{
16193 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016194 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016195 list_T *l = NULL;
16196 listitem_T *li = NULL;
16197 long added = 0;
16198 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016200 lnum = get_tv_lnum(&argvars[0]);
16201 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016203 l = argvars[1].vval.v_list;
16204 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016206 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016207 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016208
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016209 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016210 for (;;)
16211 {
16212 if (l != NULL)
16213 {
16214 /* list argument, get next string */
16215 if (li == NULL)
16216 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016218 li = li->li_next;
16219 }
16220
16221 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016222 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016223 break;
16224 if (lnum <= curbuf->b_ml.ml_line_count)
16225 {
16226 /* existing line, replace it */
16227 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16228 {
16229 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016230 if (lnum == curwin->w_cursor.lnum)
16231 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016232 rettv->vval.v_number = 0; /* OK */
16233 }
16234 }
16235 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16236 {
16237 /* lnum is one past the last line, append the line */
16238 ++added;
16239 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16240 rettv->vval.v_number = 0; /* OK */
16241 }
16242
16243 if (l == NULL) /* only one string argument */
16244 break;
16245 ++lnum;
16246 }
16247
16248 if (added > 0)
16249 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250}
16251
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016252static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16253
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016255 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016256 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016257 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016258set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016259 win_T *wp UNUSED;
16260 typval_T *list_arg UNUSED;
16261 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016262 typval_T *rettv;
16263{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016264#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016265 char_u *act;
16266 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016267#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016268
Bram Moolenaar2641f772005-03-25 21:58:17 +000016269 rettv->vval.v_number = -1;
16270
16271#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016272 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016273 EMSG(_(e_listreq));
16274 else
16275 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016276 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016277
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016278 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016279 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016280 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016281 if (act == NULL)
16282 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016283 if (*act == 'a' || *act == 'r')
16284 action = *act;
16285 }
16286
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016287 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016288 rettv->vval.v_number = 0;
16289 }
16290#endif
16291}
16292
16293/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016294 * "setloclist()" function
16295 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016296 static void
16297f_setloclist(argvars, rettv)
16298 typval_T *argvars;
16299 typval_T *rettv;
16300{
16301 win_T *win;
16302
16303 rettv->vval.v_number = -1;
16304
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016305 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016306 if (win != NULL)
16307 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16308}
16309
16310/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016311 * "setmatches()" function
16312 */
16313 static void
16314f_setmatches(argvars, rettv)
16315 typval_T *argvars;
16316 typval_T *rettv;
16317{
16318#ifdef FEAT_SEARCH_EXTRA
16319 list_T *l;
16320 listitem_T *li;
16321 dict_T *d;
16322
16323 rettv->vval.v_number = -1;
16324 if (argvars[0].v_type != VAR_LIST)
16325 {
16326 EMSG(_(e_listreq));
16327 return;
16328 }
16329 if ((l = argvars[0].vval.v_list) != NULL)
16330 {
16331
16332 /* To some extent make sure that we are dealing with a list from
16333 * "getmatches()". */
16334 li = l->lv_first;
16335 while (li != NULL)
16336 {
16337 if (li->li_tv.v_type != VAR_DICT
16338 || (d = li->li_tv.vval.v_dict) == NULL)
16339 {
16340 EMSG(_(e_invarg));
16341 return;
16342 }
16343 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16344 && dict_find(d, (char_u *)"pattern", -1) != NULL
16345 && dict_find(d, (char_u *)"priority", -1) != NULL
16346 && dict_find(d, (char_u *)"id", -1) != NULL))
16347 {
16348 EMSG(_(e_invarg));
16349 return;
16350 }
16351 li = li->li_next;
16352 }
16353
16354 clear_matches(curwin);
16355 li = l->lv_first;
16356 while (li != NULL)
16357 {
16358 d = li->li_tv.vval.v_dict;
16359 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16360 get_dict_string(d, (char_u *)"pattern", FALSE),
16361 (int)get_dict_number(d, (char_u *)"priority"),
16362 (int)get_dict_number(d, (char_u *)"id"));
16363 li = li->li_next;
16364 }
16365 rettv->vval.v_number = 0;
16366 }
16367#endif
16368}
16369
16370/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016371 * "setpos()" function
16372 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016373 static void
16374f_setpos(argvars, rettv)
16375 typval_T *argvars;
16376 typval_T *rettv;
16377{
16378 pos_T pos;
16379 int fnum;
16380 char_u *name;
16381
Bram Moolenaar08250432008-02-13 11:42:46 +000016382 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016383 name = get_tv_string_chk(argvars);
16384 if (name != NULL)
16385 {
16386 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16387 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016388 if (--pos.col < 0)
16389 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016390 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016391 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016392 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016393 if (fnum == curbuf->b_fnum)
16394 {
16395 curwin->w_cursor = pos;
16396 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016397 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016398 }
16399 else
16400 EMSG(_(e_invarg));
16401 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016402 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16403 {
16404 /* set mark */
16405 if (setmark_pos(name[1], &pos, fnum) == OK)
16406 rettv->vval.v_number = 0;
16407 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016408 else
16409 EMSG(_(e_invarg));
16410 }
16411 }
16412}
16413
16414/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016415 * "setqflist()" function
16416 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016417 static void
16418f_setqflist(argvars, rettv)
16419 typval_T *argvars;
16420 typval_T *rettv;
16421{
16422 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16423}
16424
16425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 * "setreg()" function
16427 */
16428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 typval_T *argvars;
16431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432{
16433 int regname;
16434 char_u *strregname;
16435 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016436 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 int append;
16438 char_u yank_type;
16439 long block_len;
16440
16441 block_len = -1;
16442 yank_type = MAUTO;
16443 append = FALSE;
16444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016446 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 if (strregname == NULL)
16449 return; /* type error; errmsg already given */
16450 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 if (regname == 0 || regname == '@')
16452 regname = '"';
16453 else if (regname == '=')
16454 return;
16455
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 stropt = get_tv_string_chk(&argvars[2]);
16459 if (stropt == NULL)
16460 return; /* type error */
16461 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 switch (*stropt)
16463 {
16464 case 'a': case 'A': /* append */
16465 append = TRUE;
16466 break;
16467 case 'v': case 'c': /* character-wise selection */
16468 yank_type = MCHAR;
16469 break;
16470 case 'V': case 'l': /* line-wise selection */
16471 yank_type = MLINE;
16472 break;
16473#ifdef FEAT_VISUAL
16474 case 'b': case Ctrl_V: /* block-wise selection */
16475 yank_type = MBLOCK;
16476 if (VIM_ISDIGIT(stropt[1]))
16477 {
16478 ++stropt;
16479 block_len = getdigits(&stropt) - 1;
16480 --stropt;
16481 }
16482 break;
16483#endif
16484 }
16485 }
16486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 strval = get_tv_string_chk(&argvars[1]);
16488 if (strval != NULL)
16489 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492}
16493
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016494/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016495 * "settabvar()" function
16496 */
16497 static void
16498f_settabvar(argvars, rettv)
16499 typval_T *argvars;
16500 typval_T *rettv;
16501{
16502 tabpage_T *save_curtab;
16503 char_u *varname, *tabvarname;
16504 typval_T *varp;
16505 tabpage_T *tp;
16506
16507 rettv->vval.v_number = 0;
16508
16509 if (check_restricted() || check_secure())
16510 return;
16511
16512 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16513 varname = get_tv_string_chk(&argvars[1]);
16514 varp = &argvars[2];
16515
16516 if (tp != NULL && varname != NULL && varp != NULL)
16517 {
16518 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016519 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016520
16521 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16522 if (tabvarname != NULL)
16523 {
16524 STRCPY(tabvarname, "t:");
16525 STRCPY(tabvarname + 2, varname);
16526 set_var(tabvarname, varp, TRUE);
16527 vim_free(tabvarname);
16528 }
16529
16530 /* Restore current tabpage */
16531 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016532 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016533 }
16534}
16535
16536/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016537 * "settabwinvar()" function
16538 */
16539 static void
16540f_settabwinvar(argvars, rettv)
16541 typval_T *argvars;
16542 typval_T *rettv;
16543{
16544 setwinvar(argvars, rettv, 1);
16545}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546
16547/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016548 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 typval_T *argvars;
16553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016555 setwinvar(argvars, rettv, 0);
16556}
16557
16558/*
16559 * "setwinvar()" and "settabwinvar()" functions
16560 */
16561 static void
16562setwinvar(argvars, rettv, off)
16563 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016564 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016565 int off;
16566{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 win_T *win;
16568#ifdef FEAT_WINDOWS
16569 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016570 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571#endif
16572 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016573 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016575 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576
16577 if (check_restricted() || check_secure())
16578 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016579
16580#ifdef FEAT_WINDOWS
16581 if (off == 1)
16582 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16583 else
16584 tp = curtab;
16585#endif
16586 win = find_win_by_nr(&argvars[off], tp);
16587 varname = get_tv_string_chk(&argvars[off + 1]);
16588 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589
16590 if (win != NULL && varname != NULL && varp != NULL)
16591 {
16592#ifdef FEAT_WINDOWS
16593 /* set curwin to be our win, temporarily */
16594 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016595 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016596 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016597 if (!win_valid(win))
16598 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 curwin = win;
16600 curbuf = curwin->w_buffer;
16601#endif
16602
16603 if (*varname == '&')
16604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 long numval;
16606 char_u *strval;
16607 int error = FALSE;
16608
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016610 numval = get_tv_number_chk(varp, &error);
16611 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016612 if (!error && strval != NULL)
16613 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 }
16615 else
16616 {
16617 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16618 if (winvarname != NULL)
16619 {
16620 STRCPY(winvarname, "w:");
16621 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016622 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 vim_free(winvarname);
16624 }
16625 }
16626
16627#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016628 /* Restore current tabpage and window, if still valid (autocomands can
16629 * make them invalid). */
16630 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016631 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 if (win_valid(save_curwin))
16633 {
16634 curwin = save_curwin;
16635 curbuf = curwin->w_buffer;
16636 }
16637#endif
16638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639}
16640
16641/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016642 * "shellescape({string})" function
16643 */
16644 static void
16645f_shellescape(argvars, rettv)
16646 typval_T *argvars;
16647 typval_T *rettv;
16648{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016649 rettv->vval.v_string = vim_strsave_shellescape(
16650 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016651 rettv->v_type = VAR_STRING;
16652}
16653
16654/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 */
16657 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016659 typval_T *argvars;
16660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016662 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663
Bram Moolenaar0d660222005-01-07 21:51:51 +000016664 p = get_tv_string(&argvars[0]);
16665 rettv->vval.v_string = vim_strsave(p);
16666 simplify_filename(rettv->vval.v_string); /* simplify in place */
16667 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668}
16669
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016670#ifdef FEAT_FLOAT
16671/*
16672 * "sin()" function
16673 */
16674 static void
16675f_sin(argvars, rettv)
16676 typval_T *argvars;
16677 typval_T *rettv;
16678{
16679 float_T f;
16680
16681 rettv->v_type = VAR_FLOAT;
16682 if (get_float_arg(argvars, &f) == OK)
16683 rettv->vval.v_float = sin(f);
16684 else
16685 rettv->vval.v_float = 0.0;
16686}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016687
16688/*
16689 * "sinh()" function
16690 */
16691 static void
16692f_sinh(argvars, rettv)
16693 typval_T *argvars;
16694 typval_T *rettv;
16695{
16696 float_T f;
16697
16698 rettv->v_type = VAR_FLOAT;
16699 if (get_float_arg(argvars, &f) == OK)
16700 rettv->vval.v_float = sinh(f);
16701 else
16702 rettv->vval.v_float = 0.0;
16703}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016704#endif
16705
Bram Moolenaar0d660222005-01-07 21:51:51 +000016706static int
16707#ifdef __BORLANDC__
16708 _RTLENTRYF
16709#endif
16710 item_compare __ARGS((const void *s1, const void *s2));
16711static int
16712#ifdef __BORLANDC__
16713 _RTLENTRYF
16714#endif
16715 item_compare2 __ARGS((const void *s1, const void *s2));
16716
16717static int item_compare_ic;
16718static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016719static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016720static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016721#define ITEM_COMPARE_FAIL 999
16722
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 static int
16727#ifdef __BORLANDC__
16728_RTLENTRYF
16729#endif
16730item_compare(s1, s2)
16731 const void *s1;
16732 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016734 char_u *p1, *p2;
16735 char_u *tofree1, *tofree2;
16736 int res;
16737 char_u numbuf1[NUMBUFLEN];
16738 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016740 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16741 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016742 if (p1 == NULL)
16743 p1 = (char_u *)"";
16744 if (p2 == NULL)
16745 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016746 if (item_compare_ic)
16747 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016749 res = STRCMP(p1, p2);
16750 vim_free(tofree1);
16751 vim_free(tofree2);
16752 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753}
16754
16755 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016756#ifdef __BORLANDC__
16757_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016759item_compare2(s1, s2)
16760 const void *s1;
16761 const void *s2;
16762{
16763 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016764 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016765 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016766 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016768 /* shortcut after failure in previous call; compare all items equal */
16769 if (item_compare_func_err)
16770 return 0;
16771
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016772 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16773 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16775 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016776
16777 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016778 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016779 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16780 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 clear_tv(&argv[0]);
16782 clear_tv(&argv[1]);
16783
16784 if (res == FAIL)
16785 res = ITEM_COMPARE_FAIL;
16786 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16788 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016789 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016790 clear_tv(&rettv);
16791 return res;
16792}
16793
16794/*
16795 * "sort({list})" function
16796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016798f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016799 typval_T *argvars;
16800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801{
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 list_T *l;
16803 listitem_T *li;
16804 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016805 long len;
16806 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807
Bram Moolenaar0d660222005-01-07 21:51:51 +000016808 if (argvars[0].v_type != VAR_LIST)
16809 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 else
16811 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016812 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016813 if (l == NULL || tv_check_lock(l->lv_lock,
16814 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016815 return;
16816 rettv->vval.v_list = l;
16817 rettv->v_type = VAR_LIST;
16818 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819
Bram Moolenaar0d660222005-01-07 21:51:51 +000016820 len = list_len(l);
16821 if (len <= 1)
16822 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823
Bram Moolenaar0d660222005-01-07 21:51:51 +000016824 item_compare_ic = FALSE;
16825 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016826 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016827 if (argvars[1].v_type != VAR_UNKNOWN)
16828 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016829 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016830 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016831 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016832 else
16833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016834 int error = FALSE;
16835
16836 i = get_tv_number_chk(&argvars[1], &error);
16837 if (error)
16838 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839 if (i == 1)
16840 item_compare_ic = TRUE;
16841 else
16842 item_compare_func = get_tv_string(&argvars[1]);
16843 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016844
16845 if (argvars[2].v_type != VAR_UNKNOWN)
16846 {
16847 /* optional third argument: {dict} */
16848 if (argvars[2].v_type != VAR_DICT)
16849 {
16850 EMSG(_(e_dictreq));
16851 return;
16852 }
16853 item_compare_selfdict = argvars[2].vval.v_dict;
16854 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 if (ptrs == NULL)
16860 return;
16861 i = 0;
16862 for (li = l->lv_first; li != NULL; li = li->li_next)
16863 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866 /* test the compare function */
16867 if (item_compare_func != NULL
16868 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16869 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016870 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872 {
16873 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875 item_compare_func == NULL ? item_compare : item_compare2);
16876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016877 if (!item_compare_func_err)
16878 {
16879 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016880 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 l->lv_len = 0;
16882 for (i = 0; i < len; ++i)
16883 list_append(l, ptrs[i]);
16884 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 }
16886
16887 vim_free(ptrs);
16888 }
16889}
16890
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016891/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016892 * "soundfold({word})" function
16893 */
16894 static void
16895f_soundfold(argvars, rettv)
16896 typval_T *argvars;
16897 typval_T *rettv;
16898{
16899 char_u *s;
16900
16901 rettv->v_type = VAR_STRING;
16902 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016903#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016904 rettv->vval.v_string = eval_soundfold(s);
16905#else
16906 rettv->vval.v_string = vim_strsave(s);
16907#endif
16908}
16909
16910/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016911 * "spellbadword()" function
16912 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016913 static void
16914f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016915 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016916 typval_T *rettv;
16917{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016918 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016919 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016920 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016921
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016922 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016923 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016924
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016925#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016926 if (argvars[0].v_type == VAR_UNKNOWN)
16927 {
16928 /* Find the start and length of the badly spelled word. */
16929 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16930 if (len != 0)
16931 word = ml_get_cursor();
16932 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016933 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016934 {
16935 char_u *str = get_tv_string_chk(&argvars[0]);
16936 int capcol = -1;
16937
16938 if (str != NULL)
16939 {
16940 /* Check the argument for spelling. */
16941 while (*str != NUL)
16942 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016943 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016944 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016945 {
16946 word = str;
16947 break;
16948 }
16949 str += len;
16950 }
16951 }
16952 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016953#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016955 list_append_string(rettv->vval.v_list, word, len);
16956 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016957 attr == HLF_SPB ? "bad" :
16958 attr == HLF_SPR ? "rare" :
16959 attr == HLF_SPL ? "local" :
16960 attr == HLF_SPC ? "caps" :
16961 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016962}
16963
16964/*
16965 * "spellsuggest()" function
16966 */
16967 static void
16968f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016969 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016970 typval_T *rettv;
16971{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016972#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016973 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016974 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016975 int maxcount;
16976 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016977 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016978 listitem_T *li;
16979 int need_capital = FALSE;
16980#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016981
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016982 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016983 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016984
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016985#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016986 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016987 {
16988 str = get_tv_string(&argvars[0]);
16989 if (argvars[1].v_type != VAR_UNKNOWN)
16990 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016991 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016992 if (maxcount <= 0)
16993 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016994 if (argvars[2].v_type != VAR_UNKNOWN)
16995 {
16996 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16997 if (typeerr)
16998 return;
16999 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017000 }
17001 else
17002 maxcount = 25;
17003
Bram Moolenaar4770d092006-01-12 23:22:24 +000017004 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017005
17006 for (i = 0; i < ga.ga_len; ++i)
17007 {
17008 str = ((char_u **)ga.ga_data)[i];
17009
17010 li = listitem_alloc();
17011 if (li == NULL)
17012 vim_free(str);
17013 else
17014 {
17015 li->li_tv.v_type = VAR_STRING;
17016 li->li_tv.v_lock = 0;
17017 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017018 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017019 }
17020 }
17021 ga_clear(&ga);
17022 }
17023#endif
17024}
17025
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017027f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017028 typval_T *argvars;
17029 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030{
17031 char_u *str;
17032 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017033 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 regmatch_T regmatch;
17035 char_u patbuf[NUMBUFLEN];
17036 char_u *save_cpo;
17037 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017039 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041
17042 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17043 save_cpo = p_cpo;
17044 p_cpo = (char_u *)"";
17045
17046 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017047 if (argvars[1].v_type != VAR_UNKNOWN)
17048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017049 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17050 if (pat == NULL)
17051 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017054 }
17055 if (pat == NULL || *pat == NUL)
17056 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017058 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017060 if (typeerr)
17061 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17064 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017067 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017069 if (*str == NUL)
17070 match = FALSE; /* empty item at the end */
17071 else
17072 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073 if (match)
17074 end = regmatch.startp[0];
17075 else
17076 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017077 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17078 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017080 if (list_append_string(rettv->vval.v_list, str,
17081 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083 }
17084 if (!match)
17085 break;
17086 /* Advance to just after the match. */
17087 if (regmatch.endp[0] > str)
17088 col = 0;
17089 else
17090 {
17091 /* Don't get stuck at the same match. */
17092#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017093 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094#else
17095 col = 1;
17096#endif
17097 }
17098 str = regmatch.endp[0];
17099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105}
17106
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017107#ifdef FEAT_FLOAT
17108/*
17109 * "sqrt()" function
17110 */
17111 static void
17112f_sqrt(argvars, rettv)
17113 typval_T *argvars;
17114 typval_T *rettv;
17115{
17116 float_T f;
17117
17118 rettv->v_type = VAR_FLOAT;
17119 if (get_float_arg(argvars, &f) == OK)
17120 rettv->vval.v_float = sqrt(f);
17121 else
17122 rettv->vval.v_float = 0.0;
17123}
17124
17125/*
17126 * "str2float()" function
17127 */
17128 static void
17129f_str2float(argvars, rettv)
17130 typval_T *argvars;
17131 typval_T *rettv;
17132{
17133 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17134
17135 if (*p == '+')
17136 p = skipwhite(p + 1);
17137 (void)string2float(p, &rettv->vval.v_float);
17138 rettv->v_type = VAR_FLOAT;
17139}
17140#endif
17141
Bram Moolenaar2c932302006-03-18 21:42:09 +000017142/*
17143 * "str2nr()" function
17144 */
17145 static void
17146f_str2nr(argvars, rettv)
17147 typval_T *argvars;
17148 typval_T *rettv;
17149{
17150 int base = 10;
17151 char_u *p;
17152 long n;
17153
17154 if (argvars[1].v_type != VAR_UNKNOWN)
17155 {
17156 base = get_tv_number(&argvars[1]);
17157 if (base != 8 && base != 10 && base != 16)
17158 {
17159 EMSG(_(e_invarg));
17160 return;
17161 }
17162 }
17163
17164 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017165 if (*p == '+')
17166 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017167 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17168 rettv->vval.v_number = n;
17169}
17170
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171#ifdef HAVE_STRFTIME
17172/*
17173 * "strftime({format}[, {time}])" function
17174 */
17175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 typval_T *argvars;
17178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179{
17180 char_u result_buf[256];
17181 struct tm *curtime;
17182 time_t seconds;
17183 char_u *p;
17184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017187 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017188 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 seconds = time(NULL);
17190 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 curtime = localtime(&seconds);
17193 /* MSVC returns NULL for an invalid value of seconds. */
17194 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 else
17197 {
17198# ifdef FEAT_MBYTE
17199 vimconv_T conv;
17200 char_u *enc;
17201
17202 conv.vc_type = CONV_NONE;
17203 enc = enc_locale();
17204 convert_setup(&conv, p_enc, enc);
17205 if (conv.vc_type != CONV_NONE)
17206 p = string_convert(&conv, p, NULL);
17207# endif
17208 if (p != NULL)
17209 (void)strftime((char *)result_buf, sizeof(result_buf),
17210 (char *)p, curtime);
17211 else
17212 result_buf[0] = NUL;
17213
17214# ifdef FEAT_MBYTE
17215 if (conv.vc_type != CONV_NONE)
17216 vim_free(p);
17217 convert_setup(&conv, enc, p_enc);
17218 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 else
17221# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017222 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223
17224# ifdef FEAT_MBYTE
17225 /* Release conversion descriptors */
17226 convert_setup(&conv, NULL, NULL);
17227 vim_free(enc);
17228# endif
17229 }
17230}
17231#endif
17232
17233/*
17234 * "stridx()" function
17235 */
17236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017237f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017238 typval_T *argvars;
17239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240{
17241 char_u buf[NUMBUFLEN];
17242 char_u *needle;
17243 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017244 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017248 needle = get_tv_string_chk(&argvars[1]);
17249 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017250 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 if (needle == NULL || haystack == NULL)
17252 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaar33570922005-01-25 22:26:29 +000017254 if (argvars[2].v_type != VAR_UNKNOWN)
17255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 int error = FALSE;
17257
17258 start_idx = get_tv_number_chk(&argvars[2], &error);
17259 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017261 if (start_idx >= 0)
17262 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 }
17264
17265 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17266 if (pos != NULL)
17267 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268}
17269
17270/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 * "string()" function
17272 */
17273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017275 typval_T *argvars;
17276 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017277{
17278 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017279 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017281 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017282 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017283 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017284 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017285 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286}
17287
17288/*
17289 * "strlen()" function
17290 */
17291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017293 typval_T *argvars;
17294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 rettv->vval.v_number = (varnumber_T)(STRLEN(
17297 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
17300/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017301 * "strchars()" function
17302 */
17303 static void
17304f_strchars(argvars, rettv)
17305 typval_T *argvars;
17306 typval_T *rettv;
17307{
17308 char_u *s = get_tv_string(&argvars[0]);
17309#ifdef FEAT_MBYTE
17310 varnumber_T len = 0;
17311
17312 while (*s != NUL)
17313 {
17314 mb_cptr2char_adv(&s);
17315 ++len;
17316 }
17317 rettv->vval.v_number = len;
17318#else
17319 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17320#endif
17321}
17322
17323/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017324 * "strdisplaywidth()" function
17325 */
17326 static void
17327f_strdisplaywidth(argvars, rettv)
17328 typval_T *argvars;
17329 typval_T *rettv;
17330{
17331 char_u *s = get_tv_string(&argvars[0]);
17332 int col = 0;
17333
17334 if (argvars[1].v_type != VAR_UNKNOWN)
17335 col = get_tv_number(&argvars[1]);
17336
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017337 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017338}
17339
17340/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017341 * "strwidth()" function
17342 */
17343 static void
17344f_strwidth(argvars, rettv)
17345 typval_T *argvars;
17346 typval_T *rettv;
17347{
17348 char_u *s = get_tv_string(&argvars[0]);
17349
17350 rettv->vval.v_number = (varnumber_T)(
17351#ifdef FEAT_MBYTE
17352 mb_string2cells(s, -1)
17353#else
17354 STRLEN(s)
17355#endif
17356 );
17357}
17358
17359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 * "strpart()" function
17361 */
17362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 typval_T *argvars;
17365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
17367 char_u *p;
17368 int n;
17369 int len;
17370 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 slen = (int)STRLEN(p);
17375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 n = get_tv_number_chk(&argvars[1], &error);
17377 if (error)
17378 len = 0;
17379 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 else
17382 len = slen - n; /* default len: all bytes that are available. */
17383
17384 /*
17385 * Only return the overlap between the specified part and the actual
17386 * string.
17387 */
17388 if (n < 0)
17389 {
17390 len += n;
17391 n = 0;
17392 }
17393 else if (n > slen)
17394 n = slen;
17395 if (len < 0)
17396 len = 0;
17397 else if (n + len > slen)
17398 len = slen - n;
17399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400 rettv->v_type = VAR_STRING;
17401 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402}
17403
17404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405 * "strridx()" function
17406 */
17407 static void
17408f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 typval_T *argvars;
17410 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017411{
17412 char_u buf[NUMBUFLEN];
17413 char_u *needle;
17414 char_u *haystack;
17415 char_u *rest;
17416 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017417 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 needle = get_tv_string_chk(&argvars[1]);
17420 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421
17422 rettv->vval.v_number = -1;
17423 if (needle == NULL || haystack == NULL)
17424 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017425
17426 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017427 if (argvars[2].v_type != VAR_UNKNOWN)
17428 {
17429 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017431 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017433 }
17434 else
17435 end_idx = haystack_len;
17436
Bram Moolenaar0d660222005-01-07 21:51:51 +000017437 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017438 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017439 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017440 lastmatch = haystack + end_idx;
17441 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017442 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017443 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017444 for (rest = haystack; *rest != '\0'; ++rest)
17445 {
17446 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017447 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448 break;
17449 lastmatch = rest;
17450 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017452
17453 if (lastmatch == NULL)
17454 rettv->vval.v_number = -1;
17455 else
17456 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17457}
17458
17459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 * "strtrans()" function
17461 */
17462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017463f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017464 typval_T *argvars;
17465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017467 rettv->v_type = VAR_STRING;
17468 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469}
17470
17471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017472 * "submatch()" function
17473 */
17474 static void
17475f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017476 typval_T *argvars;
17477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017478{
17479 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017480 rettv->vval.v_string =
17481 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017482}
17483
17484/*
17485 * "substitute()" function
17486 */
17487 static void
17488f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 typval_T *argvars;
17490 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017491{
17492 char_u patbuf[NUMBUFLEN];
17493 char_u subbuf[NUMBUFLEN];
17494 char_u flagsbuf[NUMBUFLEN];
17495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017496 char_u *str = get_tv_string_chk(&argvars[0]);
17497 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17498 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17499 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17500
Bram Moolenaar0d660222005-01-07 21:51:51 +000017501 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017502 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17503 rettv->vval.v_string = NULL;
17504 else
17505 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506}
17507
17508/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017509 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017513 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515{
17516 int id = 0;
17517#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017518 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 long col;
17520 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017521 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017523 lnum = get_tv_lnum(argvars); /* -1 on type error */
17524 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17525 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017527 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017528 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017529 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530#endif
17531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017532 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533}
17534
17535/*
17536 * "synIDattr(id, what [, mode])" function
17537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017540 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542{
17543 char_u *p = NULL;
17544#ifdef FEAT_SYN_HL
17545 int id;
17546 char_u *what;
17547 char_u *mode;
17548 char_u modebuf[NUMBUFLEN];
17549 int modec;
17550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017551 id = get_tv_number(&argvars[0]);
17552 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017557 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 modec = 0; /* replace invalid with current */
17559 }
17560 else
17561 {
17562#ifdef FEAT_GUI
17563 if (gui.in_use)
17564 modec = 'g';
17565 else
17566#endif
17567 if (t_colors > 1)
17568 modec = 'c';
17569 else
17570 modec = 't';
17571 }
17572
17573
17574 switch (TOLOWER_ASC(what[0]))
17575 {
17576 case 'b':
17577 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17578 p = highlight_color(id, what, modec);
17579 else /* bold */
17580 p = highlight_has_attr(id, HL_BOLD, modec);
17581 break;
17582
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017583 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 p = highlight_color(id, what, modec);
17585 break;
17586
17587 case 'i':
17588 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17589 p = highlight_has_attr(id, HL_INVERSE, modec);
17590 else /* italic */
17591 p = highlight_has_attr(id, HL_ITALIC, modec);
17592 break;
17593
17594 case 'n': /* name */
17595 p = get_highlight_name(NULL, id - 1);
17596 break;
17597
17598 case 'r': /* reverse */
17599 p = highlight_has_attr(id, HL_INVERSE, modec);
17600 break;
17601
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017602 case 's':
17603 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17604 p = highlight_color(id, what, modec);
17605 else /* standout */
17606 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 break;
17608
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017609 case 'u':
17610 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17611 /* underline */
17612 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17613 else
17614 /* undercurl */
17615 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 break;
17617 }
17618
17619 if (p != NULL)
17620 p = vim_strsave(p);
17621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622 rettv->v_type = VAR_STRING;
17623 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624}
17625
17626/*
17627 * "synIDtrans(id)" function
17628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633{
17634 int id;
17635
17636#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017637 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638
17639 if (id > 0)
17640 id = syn_get_final_id(id);
17641 else
17642#endif
17643 id = 0;
17644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017645 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646}
17647
17648/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017649 * "synconcealed(lnum, col)" function
17650 */
17651 static void
17652f_synconcealed(argvars, rettv)
17653 typval_T *argvars UNUSED;
17654 typval_T *rettv;
17655{
17656#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17657 long lnum;
17658 long col;
17659 int syntax_flags = 0;
17660 int cchar;
17661 int matchid = 0;
17662 char_u str[NUMBUFLEN];
17663#endif
17664
17665 rettv->v_type = VAR_LIST;
17666 rettv->vval.v_list = NULL;
17667
17668#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17669 lnum = get_tv_lnum(argvars); /* -1 on type error */
17670 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17671
17672 vim_memset(str, NUL, sizeof(str));
17673
17674 if (rettv_list_alloc(rettv) != FAIL)
17675 {
17676 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17677 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17678 && curwin->w_p_cole > 0)
17679 {
17680 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17681 syntax_flags = get_syntax_info(&matchid);
17682
17683 /* get the conceal character */
17684 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17685 {
17686 cchar = syn_get_sub_char();
17687 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17688 cchar = lcs_conceal;
17689 if (cchar != NUL)
17690 {
17691# ifdef FEAT_MBYTE
17692 if (has_mbyte)
17693 (*mb_char2bytes)(cchar, str);
17694 else
17695# endif
17696 str[0] = cchar;
17697 }
17698 }
17699 }
17700
17701 list_append_number(rettv->vval.v_list,
17702 (syntax_flags & HL_CONCEAL) != 0);
17703 /* -1 to auto-determine strlen */
17704 list_append_string(rettv->vval.v_list, str, -1);
17705 list_append_number(rettv->vval.v_list, matchid);
17706 }
17707#endif
17708}
17709
17710/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017711 * "synstack(lnum, col)" function
17712 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017713 static void
17714f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017715 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017716 typval_T *rettv;
17717{
17718#ifdef FEAT_SYN_HL
17719 long lnum;
17720 long col;
17721 int i;
17722 int id;
17723#endif
17724
17725 rettv->v_type = VAR_LIST;
17726 rettv->vval.v_list = NULL;
17727
17728#ifdef FEAT_SYN_HL
17729 lnum = get_tv_lnum(argvars); /* -1 on type error */
17730 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17731
17732 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017733 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017734 && rettv_list_alloc(rettv) != FAIL)
17735 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017736 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017737 for (i = 0; ; ++i)
17738 {
17739 id = syn_get_stack_item(i);
17740 if (id < 0)
17741 break;
17742 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17743 break;
17744 }
17745 }
17746#endif
17747}
17748
17749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 * "system()" function
17751 */
17752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 typval_T *argvars;
17755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017757 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017759 char_u *infile = NULL;
17760 char_u buf[NUMBUFLEN];
17761 int err = FALSE;
17762 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017764 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017765 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017766
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017767 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017768 {
17769 /*
17770 * Write the string to a temp file, to be used for input of the shell
17771 * command.
17772 */
17773 if ((infile = vim_tempname('i')) == NULL)
17774 {
17775 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017776 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017777 }
17778
17779 fd = mch_fopen((char *)infile, WRITEBIN);
17780 if (fd == NULL)
17781 {
17782 EMSG2(_(e_notopen), infile);
17783 goto done;
17784 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 p = get_tv_string_buf_chk(&argvars[1], buf);
17786 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017787 {
17788 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017789 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017790 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017791 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17792 err = TRUE;
17793 if (fclose(fd) != 0)
17794 err = TRUE;
17795 if (err)
17796 {
17797 EMSG(_("E677: Error writing temp file"));
17798 goto done;
17799 }
17800 }
17801
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017802 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17803 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017804
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805#ifdef USE_CR
17806 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017807 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 {
17809 char_u *s;
17810
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017811 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 {
17813 if (*s == CAR)
17814 *s = NL;
17815 }
17816 }
17817#else
17818# ifdef USE_CRNL
17819 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017820 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 {
17822 char_u *s, *d;
17823
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017824 d = res;
17825 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 {
17827 if (s[0] == CAR && s[1] == NL)
17828 ++s;
17829 *d++ = *s;
17830 }
17831 *d = NUL;
17832 }
17833# endif
17834#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017835
17836done:
17837 if (infile != NULL)
17838 {
17839 mch_remove(infile);
17840 vim_free(infile);
17841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017842 rettv->v_type = VAR_STRING;
17843 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844}
17845
17846/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017847 * "tabpagebuflist()" function
17848 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017849 static void
17850f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017851 typval_T *argvars UNUSED;
17852 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017853{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017854#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017855 tabpage_T *tp;
17856 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017857
17858 if (argvars[0].v_type == VAR_UNKNOWN)
17859 wp = firstwin;
17860 else
17861 {
17862 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17863 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017864 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017865 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017866 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017867 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017868 for (; wp != NULL; wp = wp->w_next)
17869 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017870 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017871 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017872 }
17873#endif
17874}
17875
17876
17877/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017878 * "tabpagenr()" function
17879 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017880 static void
17881f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017882 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017883 typval_T *rettv;
17884{
17885 int nr = 1;
17886#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017887 char_u *arg;
17888
17889 if (argvars[0].v_type != VAR_UNKNOWN)
17890 {
17891 arg = get_tv_string_chk(&argvars[0]);
17892 nr = 0;
17893 if (arg != NULL)
17894 {
17895 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017896 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017897 else
17898 EMSG2(_(e_invexpr2), arg);
17899 }
17900 }
17901 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017902 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017903#endif
17904 rettv->vval.v_number = nr;
17905}
17906
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017907
17908#ifdef FEAT_WINDOWS
17909static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17910
17911/*
17912 * Common code for tabpagewinnr() and winnr().
17913 */
17914 static int
17915get_winnr(tp, argvar)
17916 tabpage_T *tp;
17917 typval_T *argvar;
17918{
17919 win_T *twin;
17920 int nr = 1;
17921 win_T *wp;
17922 char_u *arg;
17923
17924 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17925 if (argvar->v_type != VAR_UNKNOWN)
17926 {
17927 arg = get_tv_string_chk(argvar);
17928 if (arg == NULL)
17929 nr = 0; /* type error; errmsg already given */
17930 else if (STRCMP(arg, "$") == 0)
17931 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17932 else if (STRCMP(arg, "#") == 0)
17933 {
17934 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17935 if (twin == NULL)
17936 nr = 0;
17937 }
17938 else
17939 {
17940 EMSG2(_(e_invexpr2), arg);
17941 nr = 0;
17942 }
17943 }
17944
17945 if (nr > 0)
17946 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17947 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017948 {
17949 if (wp == NULL)
17950 {
17951 /* didn't find it in this tabpage */
17952 nr = 0;
17953 break;
17954 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017955 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017956 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017957 return nr;
17958}
17959#endif
17960
17961/*
17962 * "tabpagewinnr()" function
17963 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017964 static void
17965f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017966 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017967 typval_T *rettv;
17968{
17969 int nr = 1;
17970#ifdef FEAT_WINDOWS
17971 tabpage_T *tp;
17972
17973 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17974 if (tp == NULL)
17975 nr = 0;
17976 else
17977 nr = get_winnr(tp, &argvars[1]);
17978#endif
17979 rettv->vval.v_number = nr;
17980}
17981
17982
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017983/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017984 * "tagfiles()" function
17985 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017986 static void
17987f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017988 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017989 typval_T *rettv;
17990{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017991 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017992 tagname_T tn;
17993 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017994
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017995 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017996 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017997 fname = alloc(MAXPATHL);
17998 if (fname == NULL)
17999 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018000
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018001 for (first = TRUE; ; first = FALSE)
18002 if (get_tagfname(&tn, first, fname) == FAIL
18003 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018004 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018005 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018006 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018007}
18008
18009/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018010 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018011 */
18012 static void
18013f_taglist(argvars, rettv)
18014 typval_T *argvars;
18015 typval_T *rettv;
18016{
18017 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018018
18019 tag_pattern = get_tv_string(&argvars[0]);
18020
18021 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018022 if (*tag_pattern == NUL)
18023 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018024
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018025 if (rettv_list_alloc(rettv) == OK)
18026 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018027}
18028
18029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 * "tempname()" function
18031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018033f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036{
18037 static int x = 'A';
18038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039 rettv->v_type = VAR_STRING;
18040 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041
18042 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18043 * names. Skip 'I' and 'O', they are used for shell redirection. */
18044 do
18045 {
18046 if (x == 'Z')
18047 x = '0';
18048 else if (x == '9')
18049 x = 'A';
18050 else
18051 {
18052#ifdef EBCDIC
18053 if (x == 'I')
18054 x = 'J';
18055 else if (x == 'R')
18056 x = 'S';
18057 else
18058#endif
18059 ++x;
18060 }
18061 } while (x == 'I' || x == 'O');
18062}
18063
18064/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018065 * "test(list)" function: Just checking the walls...
18066 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018067 static void
18068f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018069 typval_T *argvars UNUSED;
18070 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018071{
18072 /* Used for unit testing. Change the code below to your liking. */
18073#if 0
18074 listitem_T *li;
18075 list_T *l;
18076 char_u *bad, *good;
18077
18078 if (argvars[0].v_type != VAR_LIST)
18079 return;
18080 l = argvars[0].vval.v_list;
18081 if (l == NULL)
18082 return;
18083 li = l->lv_first;
18084 if (li == NULL)
18085 return;
18086 bad = get_tv_string(&li->li_tv);
18087 li = li->li_next;
18088 if (li == NULL)
18089 return;
18090 good = get_tv_string(&li->li_tv);
18091 rettv->vval.v_number = test_edit_score(bad, good);
18092#endif
18093}
18094
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018095#ifdef FEAT_FLOAT
18096/*
18097 * "tan()" function
18098 */
18099 static void
18100f_tan(argvars, rettv)
18101 typval_T *argvars;
18102 typval_T *rettv;
18103{
18104 float_T f;
18105
18106 rettv->v_type = VAR_FLOAT;
18107 if (get_float_arg(argvars, &f) == OK)
18108 rettv->vval.v_float = tan(f);
18109 else
18110 rettv->vval.v_float = 0.0;
18111}
18112
18113/*
18114 * "tanh()" function
18115 */
18116 static void
18117f_tanh(argvars, rettv)
18118 typval_T *argvars;
18119 typval_T *rettv;
18120{
18121 float_T f;
18122
18123 rettv->v_type = VAR_FLOAT;
18124 if (get_float_arg(argvars, &f) == OK)
18125 rettv->vval.v_float = tanh(f);
18126 else
18127 rettv->vval.v_float = 0.0;
18128}
18129#endif
18130
Bram Moolenaard52d9742005-08-21 22:20:28 +000018131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 * "tolower(string)" function
18133 */
18134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018135f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 typval_T *argvars;
18137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138{
18139 char_u *p;
18140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 p = vim_strsave(get_tv_string(&argvars[0]));
18142 rettv->v_type = VAR_STRING;
18143 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144
18145 if (p != NULL)
18146 while (*p != NUL)
18147 {
18148#ifdef FEAT_MBYTE
18149 int l;
18150
18151 if (enc_utf8)
18152 {
18153 int c, lc;
18154
18155 c = utf_ptr2char(p);
18156 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018157 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 /* TODO: reallocate string when byte count changes. */
18159 if (utf_char2len(lc) == l)
18160 utf_char2bytes(lc, p);
18161 p += l;
18162 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018163 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 p += l; /* skip multi-byte character */
18165 else
18166#endif
18167 {
18168 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18169 ++p;
18170 }
18171 }
18172}
18173
18174/*
18175 * "toupper(string)" function
18176 */
18177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018179 typval_T *argvars;
18180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018182 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018183 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184}
18185
18186/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018187 * "tr(string, fromstr, tostr)" function
18188 */
18189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018191 typval_T *argvars;
18192 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018193{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018194 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018195 char_u *fromstr;
18196 char_u *tostr;
18197 char_u *p;
18198#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018199 int inlen;
18200 int fromlen;
18201 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018202 int idx;
18203 char_u *cpstr;
18204 int cplen;
18205 int first = TRUE;
18206#endif
18207 char_u buf[NUMBUFLEN];
18208 char_u buf2[NUMBUFLEN];
18209 garray_T ga;
18210
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018211 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018212 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18213 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018214
18215 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018216 rettv->v_type = VAR_STRING;
18217 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018218 if (fromstr == NULL || tostr == NULL)
18219 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018220 ga_init2(&ga, (int)sizeof(char), 80);
18221
18222#ifdef FEAT_MBYTE
18223 if (!has_mbyte)
18224#endif
18225 /* not multi-byte: fromstr and tostr must be the same length */
18226 if (STRLEN(fromstr) != STRLEN(tostr))
18227 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018228#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018229error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018230#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018231 EMSG2(_(e_invarg2), fromstr);
18232 ga_clear(&ga);
18233 return;
18234 }
18235
18236 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018237 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018238 {
18239#ifdef FEAT_MBYTE
18240 if (has_mbyte)
18241 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018242 inlen = (*mb_ptr2len)(in_str);
18243 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018244 cplen = inlen;
18245 idx = 0;
18246 for (p = fromstr; *p != NUL; p += fromlen)
18247 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018248 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018249 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018250 {
18251 for (p = tostr; *p != NUL; p += tolen)
18252 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018253 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018254 if (idx-- == 0)
18255 {
18256 cplen = tolen;
18257 cpstr = p;
18258 break;
18259 }
18260 }
18261 if (*p == NUL) /* tostr is shorter than fromstr */
18262 goto error;
18263 break;
18264 }
18265 ++idx;
18266 }
18267
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018268 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018269 {
18270 /* Check that fromstr and tostr have the same number of
18271 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018272 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018273 first = FALSE;
18274 for (p = tostr; *p != NUL; p += tolen)
18275 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018276 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018277 --idx;
18278 }
18279 if (idx != 0)
18280 goto error;
18281 }
18282
18283 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018284 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018285 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018286
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018287 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018288 }
18289 else
18290#endif
18291 {
18292 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018293 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018294 if (p != NULL)
18295 ga_append(&ga, tostr[p - fromstr]);
18296 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018297 ga_append(&ga, *in_str);
18298 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018299 }
18300 }
18301
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018302 /* add a terminating NUL */
18303 ga_grow(&ga, 1);
18304 ga_append(&ga, NUL);
18305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018307}
18308
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018309#ifdef FEAT_FLOAT
18310/*
18311 * "trunc({float})" function
18312 */
18313 static void
18314f_trunc(argvars, rettv)
18315 typval_T *argvars;
18316 typval_T *rettv;
18317{
18318 float_T f;
18319
18320 rettv->v_type = VAR_FLOAT;
18321 if (get_float_arg(argvars, &f) == OK)
18322 /* trunc() is not in C90, use floor() or ceil() instead. */
18323 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18324 else
18325 rettv->vval.v_float = 0.0;
18326}
18327#endif
18328
Bram Moolenaar8299df92004-07-10 09:47:34 +000018329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 * "type(expr)" function
18331 */
18332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018333f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018334 typval_T *argvars;
18335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018337 int n;
18338
18339 switch (argvars[0].v_type)
18340 {
18341 case VAR_NUMBER: n = 0; break;
18342 case VAR_STRING: n = 1; break;
18343 case VAR_FUNC: n = 2; break;
18344 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018345 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018346#ifdef FEAT_FLOAT
18347 case VAR_FLOAT: n = 5; break;
18348#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018349 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18350 }
18351 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352}
18353
18354/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018355 * "undofile(name)" function
18356 */
18357 static void
18358f_undofile(argvars, rettv)
18359 typval_T *argvars;
18360 typval_T *rettv;
18361{
18362 rettv->v_type = VAR_STRING;
18363#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018364 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018365 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018366
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018367 if (*fname == NUL)
18368 {
18369 /* If there is no file name there will be no undo file. */
18370 rettv->vval.v_string = NULL;
18371 }
18372 else
18373 {
18374 char_u *ffname = FullName_save(fname, FALSE);
18375
18376 if (ffname != NULL)
18377 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18378 vim_free(ffname);
18379 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018380 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018381#else
18382 rettv->vval.v_string = NULL;
18383#endif
18384}
18385
18386/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018387 * "undotree()" function
18388 */
18389 static void
18390f_undotree(argvars, rettv)
18391 typval_T *argvars UNUSED;
18392 typval_T *rettv;
18393{
18394 if (rettv_dict_alloc(rettv) == OK)
18395 {
18396 dict_T *dict = rettv->vval.v_dict;
18397 list_T *list;
18398
Bram Moolenaar730cde92010-06-27 05:18:54 +020018399 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018400 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018401 dict_add_nr_str(dict, "save_last",
18402 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018403 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18404 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018405 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018406
18407 list = list_alloc();
18408 if (list != NULL)
18409 {
18410 u_eval_tree(curbuf->b_u_oldhead, list);
18411 dict_add_list(dict, "entries", list);
18412 }
18413 }
18414}
18415
18416/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018417 * "values(dict)" function
18418 */
18419 static void
18420f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018421 typval_T *argvars;
18422 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018423{
18424 dict_list(argvars, rettv, 1);
18425}
18426
18427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 * "virtcol(string)" function
18429 */
18430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018432 typval_T *argvars;
18433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434{
18435 colnr_T vcol = 0;
18436 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018437 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018439 fp = var2fpos(&argvars[0], FALSE, &fnum);
18440 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18441 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 {
18443 getvvcol(curwin, fp, NULL, NULL, &vcol);
18444 ++vcol;
18445 }
18446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018447 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448}
18449
18450/*
18451 * "visualmode()" function
18452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018454f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018455 typval_T *argvars UNUSED;
18456 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457{
18458#ifdef FEAT_VISUAL
18459 char_u str[2];
18460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 str[0] = curbuf->b_visual_mode_eval;
18463 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465
18466 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018467 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469#endif
18470}
18471
18472/*
18473 * "winbufnr(nr)" function
18474 */
18475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018477 typval_T *argvars;
18478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479{
18480 win_T *wp;
18481
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018482 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487}
18488
18489/*
18490 * "wincol()" function
18491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018493f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018494 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496{
18497 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018498 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499}
18500
18501/*
18502 * "winheight(nr)" function
18503 */
18504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018506 typval_T *argvars;
18507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508{
18509 win_T *wp;
18510
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018511 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018515 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516}
18517
18518/*
18519 * "winline()" function
18520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018522f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018523 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525{
18526 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528}
18529
18530/*
18531 * "winnr()" function
18532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018535 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537{
18538 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018539
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018541 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018543 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544}
18545
18546/*
18547 * "winrestcmd()" function
18548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018551 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553{
18554#ifdef FEAT_WINDOWS
18555 win_T *wp;
18556 int winnr = 1;
18557 garray_T ga;
18558 char_u buf[50];
18559
18560 ga_init2(&ga, (int)sizeof(char), 70);
18561 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18562 {
18563 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18564 ga_concat(&ga, buf);
18565# ifdef FEAT_VERTSPLIT
18566 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18567 ga_concat(&ga, buf);
18568# endif
18569 ++winnr;
18570 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018571 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578}
18579
18580/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018581 * "winrestview()" function
18582 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018583 static void
18584f_winrestview(argvars, rettv)
18585 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018586 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018587{
18588 dict_T *dict;
18589
18590 if (argvars[0].v_type != VAR_DICT
18591 || (dict = argvars[0].vval.v_dict) == NULL)
18592 EMSG(_(e_invarg));
18593 else
18594 {
18595 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18596 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18597#ifdef FEAT_VIRTUALEDIT
18598 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18599#endif
18600 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018601 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018602
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018603 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018604#ifdef FEAT_DIFF
18605 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18606#endif
18607 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18608 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18609
18610 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018611 win_new_height(curwin, curwin->w_height);
18612# ifdef FEAT_VERTSPLIT
18613 win_new_width(curwin, W_WIDTH(curwin));
18614# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018615 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018616
18617 if (curwin->w_topline == 0)
18618 curwin->w_topline = 1;
18619 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18620 curwin->w_topline = curbuf->b_ml.ml_line_count;
18621#ifdef FEAT_DIFF
18622 check_topfill(curwin, TRUE);
18623#endif
18624 }
18625}
18626
18627/*
18628 * "winsaveview()" function
18629 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018630 static void
18631f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018632 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018633 typval_T *rettv;
18634{
18635 dict_T *dict;
18636
Bram Moolenaara800b422010-06-27 01:15:55 +020018637 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018638 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018639 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018640
18641 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18642 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18643#ifdef FEAT_VIRTUALEDIT
18644 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18645#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018646 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018647 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18648
18649 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18650#ifdef FEAT_DIFF
18651 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18652#endif
18653 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18654 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18655}
18656
18657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 * "winwidth(nr)" function
18659 */
18660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 typval_T *argvars;
18663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664{
18665 win_T *wp;
18666
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018667 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 else
18671#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018674 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675#endif
18676}
18677
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018679 * "writefile()" function
18680 */
18681 static void
18682f_writefile(argvars, rettv)
18683 typval_T *argvars;
18684 typval_T *rettv;
18685{
18686 int binary = FALSE;
18687 char_u *fname;
18688 FILE *fd;
18689 listitem_T *li;
18690 char_u *s;
18691 int ret = 0;
18692 int c;
18693
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018694 if (check_restricted() || check_secure())
18695 return;
18696
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018697 if (argvars[0].v_type != VAR_LIST)
18698 {
18699 EMSG2(_(e_listarg), "writefile()");
18700 return;
18701 }
18702 if (argvars[0].vval.v_list == NULL)
18703 return;
18704
18705 if (argvars[2].v_type != VAR_UNKNOWN
18706 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18707 binary = TRUE;
18708
18709 /* Always open the file in binary mode, library functions have a mind of
18710 * their own about CR-LF conversion. */
18711 fname = get_tv_string(&argvars[1]);
18712 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18713 {
18714 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18715 ret = -1;
18716 }
18717 else
18718 {
18719 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18720 li = li->li_next)
18721 {
18722 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18723 {
18724 if (*s == '\n')
18725 c = putc(NUL, fd);
18726 else
18727 c = putc(*s, fd);
18728 if (c == EOF)
18729 {
18730 ret = -1;
18731 break;
18732 }
18733 }
18734 if (!binary || li->li_next != NULL)
18735 if (putc('\n', fd) == EOF)
18736 {
18737 ret = -1;
18738 break;
18739 }
18740 if (ret < 0)
18741 {
18742 EMSG(_(e_write));
18743 break;
18744 }
18745 }
18746 fclose(fd);
18747 }
18748
18749 rettv->vval.v_number = ret;
18750}
18751
18752/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018753 * "xor(expr, expr)" function
18754 */
18755 static void
18756f_xor(argvars, rettv)
18757 typval_T *argvars;
18758 typval_T *rettv;
18759{
18760 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18761 ^ get_tv_number_chk(&argvars[1], NULL);
18762}
18763
18764
18765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018767 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 */
18769 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018770var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018772 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018773 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018775 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018777 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778
Bram Moolenaara5525202006-03-02 22:52:09 +000018779 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018780 if (varp->v_type == VAR_LIST)
18781 {
18782 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018783 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018784 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018785 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018786
18787 l = varp->vval.v_list;
18788 if (l == NULL)
18789 return NULL;
18790
18791 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018792 pos.lnum = list_find_nr(l, 0L, &error);
18793 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018794 return NULL; /* invalid line number */
18795
18796 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018797 pos.col = list_find_nr(l, 1L, &error);
18798 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018799 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018800 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018801
18802 /* We accept "$" for the column number: last column. */
18803 li = list_find(l, 1L);
18804 if (li != NULL && li->li_tv.v_type == VAR_STRING
18805 && li->li_tv.vval.v_string != NULL
18806 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18807 pos.col = len + 1;
18808
Bram Moolenaara5525202006-03-02 22:52:09 +000018809 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018810 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018811 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018812 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018813
Bram Moolenaara5525202006-03-02 22:52:09 +000018814#ifdef FEAT_VIRTUALEDIT
18815 /* Get the virtual offset. Defaults to zero. */
18816 pos.coladd = list_find_nr(l, 2L, &error);
18817 if (error)
18818 pos.coladd = 0;
18819#endif
18820
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018821 return &pos;
18822 }
18823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018824 name = get_tv_string_chk(varp);
18825 if (name == NULL)
18826 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018827 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018829#ifdef FEAT_VISUAL
18830 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18831 {
18832 if (VIsual_active)
18833 return &VIsual;
18834 return &curwin->w_cursor;
18835 }
18836#endif
18837 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018839 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18841 return NULL;
18842 return pp;
18843 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018844
18845#ifdef FEAT_VIRTUALEDIT
18846 pos.coladd = 0;
18847#endif
18848
Bram Moolenaar477933c2007-07-17 14:32:23 +000018849 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018850 {
18851 pos.col = 0;
18852 if (name[1] == '0') /* "w0": first visible line */
18853 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018854 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018855 pos.lnum = curwin->w_topline;
18856 return &pos;
18857 }
18858 else if (name[1] == '$') /* "w$": last visible line */
18859 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018860 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018861 pos.lnum = curwin->w_botline - 1;
18862 return &pos;
18863 }
18864 }
18865 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018867 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 {
18869 pos.lnum = curbuf->b_ml.ml_line_count;
18870 pos.col = 0;
18871 }
18872 else
18873 {
18874 pos.lnum = curwin->w_cursor.lnum;
18875 pos.col = (colnr_T)STRLEN(ml_get_curline());
18876 }
18877 return &pos;
18878 }
18879 return NULL;
18880}
18881
18882/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018883 * Convert list in "arg" into a position and optional file number.
18884 * When "fnump" is NULL there is no file number, only 3 items.
18885 * Note that the column is passed on as-is, the caller may want to decrement
18886 * it to use 1 for the first column.
18887 * Return FAIL when conversion is not possible, doesn't check the position for
18888 * validity.
18889 */
18890 static int
18891list2fpos(arg, posp, fnump)
18892 typval_T *arg;
18893 pos_T *posp;
18894 int *fnump;
18895{
18896 list_T *l = arg->vval.v_list;
18897 long i = 0;
18898 long n;
18899
Bram Moolenaarbde35262006-07-23 20:12:24 +000018900 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18901 * when "fnump" isn't NULL and "coladd" is optional. */
18902 if (arg->v_type != VAR_LIST
18903 || l == NULL
18904 || l->lv_len < (fnump == NULL ? 2 : 3)
18905 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018906 return FAIL;
18907
18908 if (fnump != NULL)
18909 {
18910 n = list_find_nr(l, i++, NULL); /* fnum */
18911 if (n < 0)
18912 return FAIL;
18913 if (n == 0)
18914 n = curbuf->b_fnum; /* current buffer */
18915 *fnump = n;
18916 }
18917
18918 n = list_find_nr(l, i++, NULL); /* lnum */
18919 if (n < 0)
18920 return FAIL;
18921 posp->lnum = n;
18922
18923 n = list_find_nr(l, i++, NULL); /* col */
18924 if (n < 0)
18925 return FAIL;
18926 posp->col = n;
18927
18928#ifdef FEAT_VIRTUALEDIT
18929 n = list_find_nr(l, i, NULL);
18930 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018931 posp->coladd = 0;
18932 else
18933 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018934#endif
18935
18936 return OK;
18937}
18938
18939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 * Get the length of an environment variable name.
18941 * Advance "arg" to the first character after the name.
18942 * Return 0 for error.
18943 */
18944 static int
18945get_env_len(arg)
18946 char_u **arg;
18947{
18948 char_u *p;
18949 int len;
18950
18951 for (p = *arg; vim_isIDc(*p); ++p)
18952 ;
18953 if (p == *arg) /* no name found */
18954 return 0;
18955
18956 len = (int)(p - *arg);
18957 *arg = p;
18958 return len;
18959}
18960
18961/*
18962 * Get the length of the name of a function or internal variable.
18963 * "arg" is advanced to the first non-white character after the name.
18964 * Return 0 if something is wrong.
18965 */
18966 static int
18967get_id_len(arg)
18968 char_u **arg;
18969{
18970 char_u *p;
18971 int len;
18972
18973 /* Find the end of the name. */
18974 for (p = *arg; eval_isnamec(*p); ++p)
18975 ;
18976 if (p == *arg) /* no name found */
18977 return 0;
18978
18979 len = (int)(p - *arg);
18980 *arg = skipwhite(p);
18981
18982 return len;
18983}
18984
18985/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018986 * Get the length of the name of a variable or function.
18987 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018989 * Return -1 if curly braces expansion failed.
18990 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 * If the name contains 'magic' {}'s, expand them and return the
18992 * expanded name in an allocated string via 'alias' - caller must free.
18993 */
18994 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018995get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 char_u **arg;
18997 char_u **alias;
18998 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018999 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000{
19001 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 char_u *p;
19003 char_u *expr_start;
19004 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005
19006 *alias = NULL; /* default to no alias */
19007
19008 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19009 && (*arg)[2] == (int)KE_SNR)
19010 {
19011 /* hard coded <SNR>, already translated */
19012 *arg += 3;
19013 return get_id_len(arg) + 3;
19014 }
19015 len = eval_fname_script(*arg);
19016 if (len > 0)
19017 {
19018 /* literal "<SID>", "s:" or "<SNR>" */
19019 *arg += len;
19020 }
19021
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019023 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019025 p = find_name_end(*arg, &expr_start, &expr_end,
19026 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 if (expr_start != NULL)
19028 {
19029 char_u *temp_string;
19030
19031 if (!evaluate)
19032 {
19033 len += (int)(p - *arg);
19034 *arg = skipwhite(p);
19035 return len;
19036 }
19037
19038 /*
19039 * Include any <SID> etc in the expanded string:
19040 * Thus the -len here.
19041 */
19042 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19043 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019044 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 *alias = temp_string;
19046 *arg = skipwhite(p);
19047 return (int)STRLEN(temp_string);
19048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049
19050 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019051 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 EMSG2(_(e_invexpr2), *arg);
19053
19054 return len;
19055}
19056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019057/*
19058 * Find the end of a variable or function name, taking care of magic braces.
19059 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19060 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019061 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019062 * Return a pointer to just after the name. Equal to "arg" if there is no
19063 * valid name.
19064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019066find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 char_u *arg;
19068 char_u **expr_start;
19069 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019070 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019072 int mb_nest = 0;
19073 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 char_u *p;
19075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019076 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078 *expr_start = NULL;
19079 *expr_end = NULL;
19080 }
19081
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019082 /* Quick check for valid starting character. */
19083 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19084 return arg;
19085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019086 for (p = arg; *p != NUL
19087 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019088 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019089 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019090 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019091 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019093 if (*p == '\'')
19094 {
19095 /* skip over 'string' to avoid counting [ and ] inside it. */
19096 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19097 ;
19098 if (*p == NUL)
19099 break;
19100 }
19101 else if (*p == '"')
19102 {
19103 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19104 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19105 if (*p == '\\' && p[1] != NUL)
19106 ++p;
19107 if (*p == NUL)
19108 break;
19109 }
19110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019111 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 if (*p == '[')
19114 ++br_nest;
19115 else if (*p == ']')
19116 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019119 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 if (*p == '{')
19122 {
19123 mb_nest++;
19124 if (expr_start != NULL && *expr_start == NULL)
19125 *expr_start = p;
19126 }
19127 else if (*p == '}')
19128 {
19129 mb_nest--;
19130 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19131 *expr_end = p;
19132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 }
19135
19136 return p;
19137}
19138
19139/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019140 * Expands out the 'magic' {}'s in a variable/function name.
19141 * Note that this can call itself recursively, to deal with
19142 * constructs like foo{bar}{baz}{bam}
19143 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19144 * "in_start" ^
19145 * "expr_start" ^
19146 * "expr_end" ^
19147 * "in_end" ^
19148 *
19149 * Returns a new allocated string, which the caller must free.
19150 * Returns NULL for failure.
19151 */
19152 static char_u *
19153make_expanded_name(in_start, expr_start, expr_end, in_end)
19154 char_u *in_start;
19155 char_u *expr_start;
19156 char_u *expr_end;
19157 char_u *in_end;
19158{
19159 char_u c1;
19160 char_u *retval = NULL;
19161 char_u *temp_result;
19162 char_u *nextcmd = NULL;
19163
19164 if (expr_end == NULL || in_end == NULL)
19165 return NULL;
19166 *expr_start = NUL;
19167 *expr_end = NUL;
19168 c1 = *in_end;
19169 *in_end = NUL;
19170
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019171 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019172 if (temp_result != NULL && nextcmd == NULL)
19173 {
19174 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19175 + (in_end - expr_end) + 1));
19176 if (retval != NULL)
19177 {
19178 STRCPY(retval, in_start);
19179 STRCAT(retval, temp_result);
19180 STRCAT(retval, expr_end + 1);
19181 }
19182 }
19183 vim_free(temp_result);
19184
19185 *in_end = c1; /* put char back for error messages */
19186 *expr_start = '{';
19187 *expr_end = '}';
19188
19189 if (retval != NULL)
19190 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019191 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019192 if (expr_start != NULL)
19193 {
19194 /* Further expansion! */
19195 temp_result = make_expanded_name(retval, expr_start,
19196 expr_end, temp_result);
19197 vim_free(retval);
19198 retval = temp_result;
19199 }
19200 }
19201
19202 return retval;
19203}
19204
19205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019207 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 */
19209 static int
19210eval_isnamec(c)
19211 int c;
19212{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019213 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19214}
19215
19216/*
19217 * Return TRUE if character "c" can be used as the first character in a
19218 * variable or function name (excluding '{' and '}').
19219 */
19220 static int
19221eval_isnamec1(c)
19222 int c;
19223{
19224 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225}
19226
19227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 * Set number v: variable to "val".
19229 */
19230 void
19231set_vim_var_nr(idx, val)
19232 int idx;
19233 long val;
19234{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019235 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236}
19237
19238/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019239 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 */
19241 long
19242get_vim_var_nr(idx)
19243 int idx;
19244{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019245 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019248/*
19249 * Get string v: variable value. Uses a static buffer, can only be used once.
19250 */
19251 char_u *
19252get_vim_var_str(idx)
19253 int idx;
19254{
19255 return get_tv_string(&vimvars[idx].vv_tv);
19256}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019257
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019259 * Get List v: variable value. Caller must take care of reference count when
19260 * needed.
19261 */
19262 list_T *
19263get_vim_var_list(idx)
19264 int idx;
19265{
19266 return vimvars[idx].vv_list;
19267}
19268
19269/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019270 * Set v:char to character "c".
19271 */
19272 void
19273set_vim_var_char(c)
19274 int c;
19275{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019276 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019277
19278#ifdef FEAT_MBYTE
19279 if (has_mbyte)
19280 buf[(*mb_char2bytes)(c, buf)] = NUL;
19281 else
19282#endif
19283 {
19284 buf[0] = c;
19285 buf[1] = NUL;
19286 }
19287 set_vim_var_string(VV_CHAR, buf, -1);
19288}
19289
19290/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019291 * Set v:count to "count" and v:count1 to "count1".
19292 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 */
19294 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019295set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 long count;
19297 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019298 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019300 if (set_prevcount)
19301 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019302 vimvars[VV_COUNT].vv_nr = count;
19303 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304}
19305
19306/*
19307 * Set string v: variable to a copy of "val".
19308 */
19309 void
19310set_vim_var_string(idx, val, len)
19311 int idx;
19312 char_u *val;
19313 int len; /* length of "val" to use or -1 (whole string) */
19314{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019315 /* Need to do this (at least) once, since we can't initialize a union.
19316 * Will always be invoked when "v:progname" is set. */
19317 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19318
Bram Moolenaare9a41262005-01-15 22:18:47 +000019319 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019321 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019323 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019325 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326}
19327
19328/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019329 * Set List v: variable to "val".
19330 */
19331 void
19332set_vim_var_list(idx, val)
19333 int idx;
19334 list_T *val;
19335{
19336 list_unref(vimvars[idx].vv_list);
19337 vimvars[idx].vv_list = val;
19338 if (val != NULL)
19339 ++val->lv_refcount;
19340}
19341
19342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 * Set v:register if needed.
19344 */
19345 void
19346set_reg_var(c)
19347 int c;
19348{
19349 char_u regname;
19350
19351 if (c == 0 || c == ' ')
19352 regname = '"';
19353 else
19354 regname = c;
19355 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019356 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 set_vim_var_string(VV_REG, &regname, 1);
19358}
19359
19360/*
19361 * Get or set v:exception. If "oldval" == NULL, return the current value.
19362 * Otherwise, restore the value to "oldval" and return NULL.
19363 * Must always be called in pairs to save and restore v:exception! Does not
19364 * take care of memory allocations.
19365 */
19366 char_u *
19367v_exception(oldval)
19368 char_u *oldval;
19369{
19370 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019371 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372
Bram Moolenaare9a41262005-01-15 22:18:47 +000019373 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 return NULL;
19375}
19376
19377/*
19378 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19379 * Otherwise, restore the value to "oldval" and return NULL.
19380 * Must always be called in pairs to save and restore v:throwpoint! Does not
19381 * take care of memory allocations.
19382 */
19383 char_u *
19384v_throwpoint(oldval)
19385 char_u *oldval;
19386{
19387 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019388 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389
Bram Moolenaare9a41262005-01-15 22:18:47 +000019390 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 return NULL;
19392}
19393
19394#if defined(FEAT_AUTOCMD) || defined(PROTO)
19395/*
19396 * Set v:cmdarg.
19397 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19398 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19399 * Must always be called in pairs!
19400 */
19401 char_u *
19402set_cmdarg(eap, oldarg)
19403 exarg_T *eap;
19404 char_u *oldarg;
19405{
19406 char_u *oldval;
19407 char_u *newval;
19408 unsigned len;
19409
Bram Moolenaare9a41262005-01-15 22:18:47 +000019410 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019411 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019413 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019414 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019415 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 }
19417
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019418 if (eap->force_bin == FORCE_BIN)
19419 len = 6;
19420 else if (eap->force_bin == FORCE_NOBIN)
19421 len = 8;
19422 else
19423 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019424
19425 if (eap->read_edit)
19426 len += 7;
19427
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019428 if (eap->force_ff != 0)
19429 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19430# ifdef FEAT_MBYTE
19431 if (eap->force_enc != 0)
19432 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019433 if (eap->bad_char != 0)
19434 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019435# endif
19436
19437 newval = alloc(len + 1);
19438 if (newval == NULL)
19439 return NULL;
19440
19441 if (eap->force_bin == FORCE_BIN)
19442 sprintf((char *)newval, " ++bin");
19443 else if (eap->force_bin == FORCE_NOBIN)
19444 sprintf((char *)newval, " ++nobin");
19445 else
19446 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019447
19448 if (eap->read_edit)
19449 STRCAT(newval, " ++edit");
19450
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019451 if (eap->force_ff != 0)
19452 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19453 eap->cmd + eap->force_ff);
19454# ifdef FEAT_MBYTE
19455 if (eap->force_enc != 0)
19456 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19457 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019458 if (eap->bad_char == BAD_KEEP)
19459 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19460 else if (eap->bad_char == BAD_DROP)
19461 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19462 else if (eap->bad_char != 0)
19463 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019464# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019465 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019466 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467}
19468#endif
19469
19470/*
19471 * Get the value of internal variable "name".
19472 * Return OK or FAIL.
19473 */
19474 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019475get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 char_u *name;
19477 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019478 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019479 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480{
19481 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 typval_T *tv = NULL;
19483 typval_T atv;
19484 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486
19487 /* truncate the name, so that we can use strcmp() */
19488 cc = name[len];
19489 name[len] = NUL;
19490
19491 /*
19492 * Check for "b:changedtick".
19493 */
19494 if (STRCMP(name, "b:changedtick") == 0)
19495 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019496 atv.v_type = VAR_NUMBER;
19497 atv.vval.v_number = curbuf->b_changedtick;
19498 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 }
19500
19501 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 * Check for user-defined variables.
19503 */
19504 else
19505 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019506 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019508 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 }
19510
Bram Moolenaare9a41262005-01-15 22:18:47 +000019511 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019513 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019514 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 ret = FAIL;
19516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019517 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019518 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519
19520 name[len] = cc;
19521
19522 return ret;
19523}
19524
19525/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019526 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19527 * Also handle function call with Funcref variable: func(expr)
19528 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19529 */
19530 static int
19531handle_subscript(arg, rettv, evaluate, verbose)
19532 char_u **arg;
19533 typval_T *rettv;
19534 int evaluate; /* do more than finding the end */
19535 int verbose; /* give error messages */
19536{
19537 int ret = OK;
19538 dict_T *selfdict = NULL;
19539 char_u *s;
19540 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019541 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019542
19543 while (ret == OK
19544 && (**arg == '['
19545 || (**arg == '.' && rettv->v_type == VAR_DICT)
19546 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19547 && !vim_iswhite(*(*arg - 1)))
19548 {
19549 if (**arg == '(')
19550 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019551 /* need to copy the funcref so that we can clear rettv */
19552 functv = *rettv;
19553 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019554
19555 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019556 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019557 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019558 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19559 &len, evaluate, selfdict);
19560
19561 /* Clear the funcref afterwards, so that deleting it while
19562 * evaluating the arguments is possible (see test55). */
19563 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019564
19565 /* Stop the expression evaluation when immediately aborting on
19566 * error, or when an interrupt occurred or an exception was thrown
19567 * but not caught. */
19568 if (aborting())
19569 {
19570 if (ret == OK)
19571 clear_tv(rettv);
19572 ret = FAIL;
19573 }
19574 dict_unref(selfdict);
19575 selfdict = NULL;
19576 }
19577 else /* **arg == '[' || **arg == '.' */
19578 {
19579 dict_unref(selfdict);
19580 if (rettv->v_type == VAR_DICT)
19581 {
19582 selfdict = rettv->vval.v_dict;
19583 if (selfdict != NULL)
19584 ++selfdict->dv_refcount;
19585 }
19586 else
19587 selfdict = NULL;
19588 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19589 {
19590 clear_tv(rettv);
19591 ret = FAIL;
19592 }
19593 }
19594 }
19595 dict_unref(selfdict);
19596 return ret;
19597}
19598
19599/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019600 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019601 * value).
19602 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019604alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019605{
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019607}
19608
19609/*
19610 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 * The string "s" must have been allocated, it is consumed.
19612 * Return NULL for out of memory, the variable otherwise.
19613 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 char_u *s;
19617{
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620 rettv = alloc_tv();
19621 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019623 rettv->v_type = VAR_STRING;
19624 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 }
19626 else
19627 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629}
19630
19631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019634 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637{
19638 if (varp != NULL)
19639 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019640 switch (varp->v_type)
19641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019642 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019643 func_unref(varp->vval.v_string);
19644 /*FALLTHROUGH*/
19645 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019646 vim_free(varp->vval.v_string);
19647 break;
19648 case VAR_LIST:
19649 list_unref(varp->vval.v_list);
19650 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019651 case VAR_DICT:
19652 dict_unref(varp->vval.v_dict);
19653 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019654 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019655#ifdef FEAT_FLOAT
19656 case VAR_FLOAT:
19657#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019658 case VAR_UNKNOWN:
19659 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019660 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019661 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019662 break;
19663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 vim_free(varp);
19665 }
19666}
19667
19668/*
19669 * Free the memory for a variable value and set the value to NULL or 0.
19670 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019671 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019673 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674{
19675 if (varp != NULL)
19676 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019677 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019679 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019680 func_unref(varp->vval.v_string);
19681 /*FALLTHROUGH*/
19682 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019683 vim_free(varp->vval.v_string);
19684 varp->vval.v_string = NULL;
19685 break;
19686 case VAR_LIST:
19687 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019688 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019689 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019690 case VAR_DICT:
19691 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019692 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019693 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019695 varp->vval.v_number = 0;
19696 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019697#ifdef FEAT_FLOAT
19698 case VAR_FLOAT:
19699 varp->vval.v_float = 0.0;
19700 break;
19701#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702 case VAR_UNKNOWN:
19703 break;
19704 default:
19705 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019707 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 }
19709}
19710
19711/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712 * Set the value of a variable to NULL without freeing items.
19713 */
19714 static void
19715init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717{
19718 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720}
19721
19722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 * Get the number value of a variable.
19724 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019725 * For incompatible types, return 0.
19726 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19727 * caller of incompatible types: it sets *denote to TRUE if "denote"
19728 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 */
19730 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019734 int error = FALSE;
19735
19736 return get_tv_number_chk(varp, &error); /* return 0L on error */
19737}
19738
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019739 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019740get_tv_number_chk(varp, denote)
19741 typval_T *varp;
19742 int *denote;
19743{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019748 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019749 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019750#ifdef FEAT_FLOAT
19751 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019752 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019753 break;
19754#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019756 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019757 break;
19758 case VAR_STRING:
19759 if (varp->vval.v_string != NULL)
19760 vim_str2nr(varp->vval.v_string, NULL, NULL,
19761 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019762 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019763 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019764 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019765 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019766 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019767 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019768 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019769 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019770 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019773 if (denote == NULL) /* useful for values that must be unsigned */
19774 n = -1;
19775 else
19776 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019777 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778}
19779
19780/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019781 * Get the lnum from the first argument.
19782 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019783 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 */
19785 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019787 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788{
Bram Moolenaar33570922005-01-25 22:26:29 +000019789 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 linenr_T lnum;
19791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019792 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 if (lnum == 0) /* no valid number, try using line() */
19794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 rettv.v_type = VAR_NUMBER;
19796 f_line(argvars, &rettv);
19797 lnum = rettv.vval.v_number;
19798 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 }
19800 return lnum;
19801}
19802
19803/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019804 * Get the lnum from the first argument.
19805 * Also accepts "$", then "buf" is used.
19806 * Returns 0 on error.
19807 */
19808 static linenr_T
19809get_tv_lnum_buf(argvars, buf)
19810 typval_T *argvars;
19811 buf_T *buf;
19812{
19813 if (argvars[0].v_type == VAR_STRING
19814 && argvars[0].vval.v_string != NULL
19815 && argvars[0].vval.v_string[0] == '$'
19816 && buf != NULL)
19817 return buf->b_ml.ml_line_count;
19818 return get_tv_number_chk(&argvars[0], NULL);
19819}
19820
19821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 * Get the string value of a variable.
19823 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019824 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19825 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 * If the String variable has never been set, return an empty string.
19827 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019828 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19829 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 */
19831 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019833 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834{
19835 static char_u mybuf[NUMBUFLEN];
19836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838}
19839
19840 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019843 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019845 char_u *res = get_tv_string_buf_chk(varp, buf);
19846
19847 return res != NULL ? res : (char_u *)"";
19848}
19849
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019850 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019851get_tv_string_chk(varp)
19852 typval_T *varp;
19853{
19854 static char_u mybuf[NUMBUFLEN];
19855
19856 return get_tv_string_buf_chk(varp, mybuf);
19857}
19858
19859 static char_u *
19860get_tv_string_buf_chk(varp, buf)
19861 typval_T *varp;
19862 char_u *buf;
19863{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019864 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019866 case VAR_NUMBER:
19867 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19868 return buf;
19869 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019870 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019871 break;
19872 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019873 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019874 break;
19875 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019876 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019877 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019878#ifdef FEAT_FLOAT
19879 case VAR_FLOAT:
19880 EMSG(_("E806: using Float as a String"));
19881 break;
19882#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019883 case VAR_STRING:
19884 if (varp->vval.v_string != NULL)
19885 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019886 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019888 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019891 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892}
19893
19894/*
19895 * Find variable "name" in the list of variables.
19896 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019897 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019898 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019899 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019901 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019902find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019907 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908
Bram Moolenaara7043832005-01-21 11:56:39 +000019909 ht = find_var_ht(name, &varname);
19910 if (htp != NULL)
19911 *htp = ht;
19912 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019914 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915}
19916
19917/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019919 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019921 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019922find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019923 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019924 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019925 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019926{
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 hashitem_T *hi;
19928
19929 if (*varname == NUL)
19930 {
19931 /* Must be something like "s:", otherwise "ht" would be NULL. */
19932 switch (varname[-2])
19933 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019934 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 case 'g': return &globvars_var;
19936 case 'v': return &vimvars_var;
19937 case 'b': return &curbuf->b_bufvar;
19938 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019939#ifdef FEAT_WINDOWS
19940 case 't': return &curtab->tp_winvar;
19941#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019942 case 'l': return current_funccal == NULL
19943 ? NULL : &current_funccal->l_vars_var;
19944 case 'a': return current_funccal == NULL
19945 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 }
19947 return NULL;
19948 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019949
19950 hi = hash_find(ht, varname);
19951 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019952 {
19953 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019954 * worked find the variable again. Don't auto-load a script if it was
19955 * loaded already, otherwise it would be loaded every time when
19956 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019957 if (ht == &globvarht && !writing)
19958 {
19959 /* Note: script_autoload() may make "hi" invalid. It must either
19960 * be obtained again or not used. */
19961 if (!script_autoload(varname, FALSE) || aborting())
19962 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019963 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019964 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019965 if (HASHITEM_EMPTY(hi))
19966 return NULL;
19967 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019968 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019969}
19970
19971/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019973 * Set "varname" to the start of name without ':'.
19974 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019975 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019976find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 char_u *name;
19978 char_u **varname;
19979{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019980 hashitem_T *hi;
19981
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 if (name[1] != ':')
19983 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019984 /* The name must not start with a colon or #. */
19985 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 return NULL;
19987 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019988
19989 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019990 hi = hash_find(&compat_hashtab, name);
19991 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019992 return &compat_hashtab;
19993
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019995 return &globvarht; /* global variable */
19996 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 }
19998 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019999 if (*name == 'g') /* global variable */
20000 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020001 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20002 */
20003 if (vim_strchr(name + 2, ':') != NULL
20004 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020005 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020007 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020009 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020010#ifdef FEAT_WINDOWS
20011 if (*name == 't') /* tab page variable */
20012 return &curtab->tp_vars.dv_hashtab;
20013#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020014 if (*name == 'v') /* v: variable */
20015 return &vimvarht;
20016 if (*name == 'a' && current_funccal != NULL) /* function argument */
20017 return &current_funccal->l_avars.dv_hashtab;
20018 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20019 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 if (*name == 's' /* script variable */
20021 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20022 return &SCRIPT_VARS(current_SID);
20023 return NULL;
20024}
20025
20026/*
20027 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020028 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 * Returns NULL when it doesn't exist.
20030 */
20031 char_u *
20032get_var_value(name)
20033 char_u *name;
20034{
Bram Moolenaar33570922005-01-25 22:26:29 +000020035 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036
Bram Moolenaara7043832005-01-21 11:56:39 +000020037 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 if (v == NULL)
20039 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041}
20042
20043/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020044 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 * sourcing this script and when executing functions defined in the script.
20046 */
20047 void
20048new_script_vars(id)
20049 scid_T id;
20050{
Bram Moolenaara7043832005-01-21 11:56:39 +000020051 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 hashtab_T *ht;
20053 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020054
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20056 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020057 /* Re-allocating ga_data means that an ht_array pointing to
20058 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020059 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020060 for (i = 1; i <= ga_scripts.ga_len; ++i)
20061 {
20062 ht = &SCRIPT_VARS(i);
20063 if (ht->ht_mask == HT_INIT_SIZE - 1)
20064 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020065 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020067 }
20068
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 while (ga_scripts.ga_len < id)
20070 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020071 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020072 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020073 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 }
20076 }
20077}
20078
20079/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020080 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20081 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 */
20083 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020084init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020085 dict_T *dict;
20086 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020087 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088{
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020090 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020091 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020092 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020093 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020094 dict_var->di_tv.vval.v_dict = dict;
20095 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020096 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20098 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099}
20100
20101/*
20102 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020103 * Frees all allocated variables and the value they contain.
20104 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 */
20106 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020107vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020108 hashtab_T *ht;
20109{
20110 vars_clear_ext(ht, TRUE);
20111}
20112
20113/*
20114 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20115 */
20116 static void
20117vars_clear_ext(ht, free_val)
20118 hashtab_T *ht;
20119 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120{
Bram Moolenaara7043832005-01-21 11:56:39 +000020121 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 hashitem_T *hi;
20123 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020126 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020127 for (hi = ht->ht_array; todo > 0; ++hi)
20128 {
20129 if (!HASHITEM_EMPTY(hi))
20130 {
20131 --todo;
20132
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020134 * ht_array might change then. hash_clear() takes care of it
20135 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 v = HI2DI(hi);
20137 if (free_val)
20138 clear_tv(&v->di_tv);
20139 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20140 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020141 }
20142 }
20143 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020144 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145}
20146
Bram Moolenaara7043832005-01-21 11:56:39 +000020147/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 * Delete a variable from hashtab "ht" at item "hi".
20149 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020152delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 hashtab_T *ht;
20154 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155{
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020157
20158 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020159 clear_tv(&di->di_tv);
20160 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161}
20162
20163/*
20164 * List the value of one internal variable.
20165 */
20166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020167list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020170 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020172 char_u *tofree;
20173 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020174 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020175
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020176 current_copyID += COPYID_INC;
20177 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020179 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020180 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181}
20182
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020184list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 char_u *prefix;
20186 char_u *name;
20187 int type;
20188 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020189 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190{
Bram Moolenaar31859182007-08-14 20:41:13 +000020191 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20192 msg_start();
20193 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 if (name != NULL) /* "a:" vars don't have a name stored */
20195 msg_puts(name);
20196 msg_putchar(' ');
20197 msg_advance(22);
20198 if (type == VAR_NUMBER)
20199 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020200 else if (type == VAR_FUNC)
20201 msg_putchar('*');
20202 else if (type == VAR_LIST)
20203 {
20204 msg_putchar('[');
20205 if (*string == '[')
20206 ++string;
20207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020208 else if (type == VAR_DICT)
20209 {
20210 msg_putchar('{');
20211 if (*string == '{')
20212 ++string;
20213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 else
20215 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020216
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020218
20219 if (type == VAR_FUNC)
20220 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020221 if (*first)
20222 {
20223 msg_clr_eos();
20224 *first = FALSE;
20225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226}
20227
20228/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 * If the variable already exists, the value is updated.
20231 * Otherwise the variable is created.
20232 */
20233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020237 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238{
Bram Moolenaar33570922005-01-25 22:26:29 +000020239 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020243 ht = find_var_ht(name, &varname);
20244 if (ht == NULL || *varname == NUL)
20245 {
20246 EMSG2(_(e_illvar), name);
20247 return;
20248 }
20249 v = find_var_in_ht(ht, varname, TRUE);
20250
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020251 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20252 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020253
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020256 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020257 if (var_check_ro(v->di_flags, name)
20258 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 return;
20260 if (v->di_tv.v_type != tv->v_type
20261 && !((v->di_tv.v_type == VAR_STRING
20262 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020264 || tv->v_type == VAR_NUMBER))
20265#ifdef FEAT_FLOAT
20266 && !((v->di_tv.v_type == VAR_NUMBER
20267 || v->di_tv.v_type == VAR_FLOAT)
20268 && (tv->v_type == VAR_NUMBER
20269 || tv->v_type == VAR_FLOAT))
20270#endif
20271 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020272 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020273 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020274 return;
20275 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020276
20277 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020278 * Handle setting internal v: variables separately: we don't change
20279 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 */
20281 if (ht == &vimvarht)
20282 {
20283 if (v->di_tv.v_type == VAR_STRING)
20284 {
20285 vim_free(v->di_tv.vval.v_string);
20286 if (copy || tv->v_type != VAR_STRING)
20287 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20288 else
20289 {
20290 /* Take over the string to avoid an extra alloc/free. */
20291 v->di_tv.vval.v_string = tv->vval.v_string;
20292 tv->vval.v_string = NULL;
20293 }
20294 }
20295 else if (v->di_tv.v_type != VAR_NUMBER)
20296 EMSG2(_(e_intern2), "set_var()");
20297 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020298 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020300 if (STRCMP(varname, "searchforward") == 0)
20301 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20302 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 return;
20304 }
20305
20306 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 }
20308 else /* add a new variable */
20309 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020310 /* Can't add "v:" variable. */
20311 if (ht == &vimvarht)
20312 {
20313 EMSG2(_(e_illvar), name);
20314 return;
20315 }
20316
Bram Moolenaar92124a32005-06-17 22:03:40 +000020317 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020318 if (!valid_varname(varname))
20319 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020320
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020321 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20322 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020323 if (v == NULL)
20324 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020326 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020328 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 return;
20330 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020331 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020333
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020334 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020335 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020336 else
20337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020338 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020339 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020340 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342}
20343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020344/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020345 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 * Also give an error message.
20347 */
20348 static int
20349var_check_ro(flags, name)
20350 int flags;
20351 char_u *name;
20352{
20353 if (flags & DI_FLAGS_RO)
20354 {
20355 EMSG2(_(e_readonlyvar), name);
20356 return TRUE;
20357 }
20358 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20359 {
20360 EMSG2(_(e_readonlysbx), name);
20361 return TRUE;
20362 }
20363 return FALSE;
20364}
20365
20366/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020367 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20368 * Also give an error message.
20369 */
20370 static int
20371var_check_fixed(flags, name)
20372 int flags;
20373 char_u *name;
20374{
20375 if (flags & DI_FLAGS_FIX)
20376 {
20377 EMSG2(_("E795: Cannot delete variable %s"), name);
20378 return TRUE;
20379 }
20380 return FALSE;
20381}
20382
20383/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020384 * Check if a funcref is assigned to a valid variable name.
20385 * Return TRUE and give an error if not.
20386 */
20387 static int
20388var_check_func_name(name, new_var)
20389 char_u *name; /* points to start of variable name */
20390 int new_var; /* TRUE when creating the variable */
20391{
20392 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20393 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20394 ? name[2] : name[0]))
20395 {
20396 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20397 name);
20398 return TRUE;
20399 }
20400 /* Don't allow hiding a function. When "v" is not NULL we might be
20401 * assigning another function to the same var, the type is checked
20402 * below. */
20403 if (new_var && function_exists(name))
20404 {
20405 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20406 name);
20407 return TRUE;
20408 }
20409 return FALSE;
20410}
20411
20412/*
20413 * Check if a variable name is valid.
20414 * Return FALSE and give an error if not.
20415 */
20416 static int
20417valid_varname(varname)
20418 char_u *varname;
20419{
20420 char_u *p;
20421
20422 for (p = varname; *p != NUL; ++p)
20423 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20424 && *p != AUTOLOAD_CHAR)
20425 {
20426 EMSG2(_(e_illvar), varname);
20427 return FALSE;
20428 }
20429 return TRUE;
20430}
20431
20432/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020433 * Return TRUE if typeval "tv" is set to be locked (immutable).
20434 * Also give an error message, using "name".
20435 */
20436 static int
20437tv_check_lock(lock, name)
20438 int lock;
20439 char_u *name;
20440{
20441 if (lock & VAR_LOCKED)
20442 {
20443 EMSG2(_("E741: Value is locked: %s"),
20444 name == NULL ? (char_u *)_("Unknown") : name);
20445 return TRUE;
20446 }
20447 if (lock & VAR_FIXED)
20448 {
20449 EMSG2(_("E742: Cannot change value of %s"),
20450 name == NULL ? (char_u *)_("Unknown") : name);
20451 return TRUE;
20452 }
20453 return FALSE;
20454}
20455
20456/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020457 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020458 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020459 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020460 * It is OK for "from" and "to" to point to the same item. This is used to
20461 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020462 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020463 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020464copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020465 typval_T *from;
20466 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020468 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020469 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020470 switch (from->v_type)
20471 {
20472 case VAR_NUMBER:
20473 to->vval.v_number = from->vval.v_number;
20474 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020475#ifdef FEAT_FLOAT
20476 case VAR_FLOAT:
20477 to->vval.v_float = from->vval.v_float;
20478 break;
20479#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020480 case VAR_STRING:
20481 case VAR_FUNC:
20482 if (from->vval.v_string == NULL)
20483 to->vval.v_string = NULL;
20484 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020485 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020486 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020487 if (from->v_type == VAR_FUNC)
20488 func_ref(to->vval.v_string);
20489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020490 break;
20491 case VAR_LIST:
20492 if (from->vval.v_list == NULL)
20493 to->vval.v_list = NULL;
20494 else
20495 {
20496 to->vval.v_list = from->vval.v_list;
20497 ++to->vval.v_list->lv_refcount;
20498 }
20499 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020500 case VAR_DICT:
20501 if (from->vval.v_dict == NULL)
20502 to->vval.v_dict = NULL;
20503 else
20504 {
20505 to->vval.v_dict = from->vval.v_dict;
20506 ++to->vval.v_dict->dv_refcount;
20507 }
20508 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020510 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020511 break;
20512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513}
20514
20515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020516 * Make a copy of an item.
20517 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020518 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20519 * reference to an already copied list/dict can be used.
20520 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020521 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020522 static int
20523item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 typval_T *from;
20525 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020526 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020527 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020528{
20529 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020530 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020531
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020533 {
20534 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020535 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020536 }
20537 ++recurse;
20538
20539 switch (from->v_type)
20540 {
20541 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020542#ifdef FEAT_FLOAT
20543 case VAR_FLOAT:
20544#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020545 case VAR_STRING:
20546 case VAR_FUNC:
20547 copy_tv(from, to);
20548 break;
20549 case VAR_LIST:
20550 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020551 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020552 if (from->vval.v_list == NULL)
20553 to->vval.v_list = NULL;
20554 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20555 {
20556 /* use the copy made earlier */
20557 to->vval.v_list = from->vval.v_list->lv_copylist;
20558 ++to->vval.v_list->lv_refcount;
20559 }
20560 else
20561 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20562 if (to->vval.v_list == NULL)
20563 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020564 break;
20565 case VAR_DICT:
20566 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020567 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020568 if (from->vval.v_dict == NULL)
20569 to->vval.v_dict = NULL;
20570 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20571 {
20572 /* use the copy made earlier */
20573 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20574 ++to->vval.v_dict->dv_refcount;
20575 }
20576 else
20577 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20578 if (to->vval.v_dict == NULL)
20579 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020580 break;
20581 default:
20582 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020583 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020584 }
20585 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020586 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020587}
20588
20589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 * ":echo expr1 ..." print each argument separated with a space, add a
20591 * newline at the end.
20592 * ":echon expr1 ..." print each argument plain.
20593 */
20594 void
20595ex_echo(eap)
20596 exarg_T *eap;
20597{
20598 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020599 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020600 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 char_u *p;
20602 int needclr = TRUE;
20603 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020604 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605
20606 if (eap->skip)
20607 ++emsg_skip;
20608 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20609 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020610 /* If eval1() causes an error message the text from the command may
20611 * still need to be cleared. E.g., "echo 22,44". */
20612 need_clr_eos = needclr;
20613
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020615 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 {
20617 /*
20618 * Report the invalid expression unless the expression evaluation
20619 * has been cancelled due to an aborting error, an interrupt, or an
20620 * exception.
20621 */
20622 if (!aborting())
20623 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020624 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 break;
20626 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020627 need_clr_eos = FALSE;
20628
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 if (!eap->skip)
20630 {
20631 if (atstart)
20632 {
20633 atstart = FALSE;
20634 /* Call msg_start() after eval1(), evaluating the expression
20635 * may cause a message to appear. */
20636 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020637 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020638 /* Mark the saved text as finishing the line, so that what
20639 * follows is displayed on a new line when scrolling back
20640 * at the more prompt. */
20641 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 }
20645 else if (eap->cmdidx == CMD_echo)
20646 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020647 current_copyID += COPYID_INC;
20648 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020649 if (p != NULL)
20650 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020652 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020654 if (*p != TAB && needclr)
20655 {
20656 /* remove any text still there from the command */
20657 msg_clr_eos();
20658 needclr = FALSE;
20659 }
20660 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 }
20662 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020663 {
20664#ifdef FEAT_MBYTE
20665 if (has_mbyte)
20666 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020667 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020668
20669 (void)msg_outtrans_len_attr(p, i, echo_attr);
20670 p += i - 1;
20671 }
20672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020677 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 arg = skipwhite(arg);
20681 }
20682 eap->nextcmd = check_nextcmd(arg);
20683
20684 if (eap->skip)
20685 --emsg_skip;
20686 else
20687 {
20688 /* remove text that may still be there from the command */
20689 if (needclr)
20690 msg_clr_eos();
20691 if (eap->cmdidx == CMD_echo)
20692 msg_end();
20693 }
20694}
20695
20696/*
20697 * ":echohl {name}".
20698 */
20699 void
20700ex_echohl(eap)
20701 exarg_T *eap;
20702{
20703 int id;
20704
20705 id = syn_name2id(eap->arg);
20706 if (id == 0)
20707 echo_attr = 0;
20708 else
20709 echo_attr = syn_id2attr(id);
20710}
20711
20712/*
20713 * ":execute expr1 ..." execute the result of an expression.
20714 * ":echomsg expr1 ..." Print a message
20715 * ":echoerr expr1 ..." Print an error
20716 * Each gets spaces around each argument and a newline at the end for
20717 * echo commands
20718 */
20719 void
20720ex_execute(eap)
20721 exarg_T *eap;
20722{
20723 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020724 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 int ret = OK;
20726 char_u *p;
20727 garray_T ga;
20728 int len;
20729 int save_did_emsg;
20730
20731 ga_init2(&ga, 1, 80);
20732
20733 if (eap->skip)
20734 ++emsg_skip;
20735 while (*arg != NUL && *arg != '|' && *arg != '\n')
20736 {
20737 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020738 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 {
20740 /*
20741 * Report the invalid expression unless the expression evaluation
20742 * has been cancelled due to an aborting error, an interrupt, or an
20743 * exception.
20744 */
20745 if (!aborting())
20746 EMSG2(_(e_invexpr2), p);
20747 ret = FAIL;
20748 break;
20749 }
20750
20751 if (!eap->skip)
20752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020753 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 len = (int)STRLEN(p);
20755 if (ga_grow(&ga, len + 2) == FAIL)
20756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 ret = FAIL;
20759 break;
20760 }
20761 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 ga.ga_len += len;
20765 }
20766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020767 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 arg = skipwhite(arg);
20769 }
20770
20771 if (ret != FAIL && ga.ga_data != NULL)
20772 {
20773 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020776 out_flush();
20777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 else if (eap->cmdidx == CMD_echoerr)
20779 {
20780 /* We don't want to abort following commands, restore did_emsg. */
20781 save_did_emsg = did_emsg;
20782 EMSG((char_u *)ga.ga_data);
20783 if (!force_abort)
20784 did_emsg = save_did_emsg;
20785 }
20786 else if (eap->cmdidx == CMD_execute)
20787 do_cmdline((char_u *)ga.ga_data,
20788 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20789 }
20790
20791 ga_clear(&ga);
20792
20793 if (eap->skip)
20794 --emsg_skip;
20795
20796 eap->nextcmd = check_nextcmd(arg);
20797}
20798
20799/*
20800 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20801 * "arg" points to the "&" or '+' when called, to "option" when returning.
20802 * Returns NULL when no option name found. Otherwise pointer to the char
20803 * after the option name.
20804 */
20805 static char_u *
20806find_option_end(arg, opt_flags)
20807 char_u **arg;
20808 int *opt_flags;
20809{
20810 char_u *p = *arg;
20811
20812 ++p;
20813 if (*p == 'g' && p[1] == ':')
20814 {
20815 *opt_flags = OPT_GLOBAL;
20816 p += 2;
20817 }
20818 else if (*p == 'l' && p[1] == ':')
20819 {
20820 *opt_flags = OPT_LOCAL;
20821 p += 2;
20822 }
20823 else
20824 *opt_flags = 0;
20825
20826 if (!ASCII_ISALPHA(*p))
20827 return NULL;
20828 *arg = p;
20829
20830 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20831 p += 4; /* termcap option */
20832 else
20833 while (ASCII_ISALPHA(*p))
20834 ++p;
20835 return p;
20836}
20837
20838/*
20839 * ":function"
20840 */
20841 void
20842ex_function(eap)
20843 exarg_T *eap;
20844{
20845 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020846 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 int j;
20848 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 char_u *name = NULL;
20851 char_u *p;
20852 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020853 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 garray_T newargs;
20855 garray_T newlines;
20856 int varargs = FALSE;
20857 int mustend = FALSE;
20858 int flags = 0;
20859 ufunc_T *fp;
20860 int indent;
20861 int nesting;
20862 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020863 dictitem_T *v;
20864 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020865 static int func_nr = 0; /* number for nameless function */
20866 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020867 hashtab_T *ht;
20868 int todo;
20869 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020870 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871
20872 /*
20873 * ":function" without argument: list functions.
20874 */
20875 if (ends_excmd(*eap->arg))
20876 {
20877 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020878 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020879 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020880 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020881 {
20882 if (!HASHITEM_EMPTY(hi))
20883 {
20884 --todo;
20885 fp = HI2UF(hi);
20886 if (!isdigit(*fp->uf_name))
20887 list_func_head(fp, FALSE);
20888 }
20889 }
20890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 eap->nextcmd = check_nextcmd(eap->arg);
20892 return;
20893 }
20894
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020895 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020896 * ":function /pat": list functions matching pattern.
20897 */
20898 if (*eap->arg == '/')
20899 {
20900 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20901 if (!eap->skip)
20902 {
20903 regmatch_T regmatch;
20904
20905 c = *p;
20906 *p = NUL;
20907 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20908 *p = c;
20909 if (regmatch.regprog != NULL)
20910 {
20911 regmatch.rm_ic = p_ic;
20912
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020913 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020914 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20915 {
20916 if (!HASHITEM_EMPTY(hi))
20917 {
20918 --todo;
20919 fp = HI2UF(hi);
20920 if (!isdigit(*fp->uf_name)
20921 && vim_regexec(&regmatch, fp->uf_name, 0))
20922 list_func_head(fp, FALSE);
20923 }
20924 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020925 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020926 }
20927 }
20928 if (*p == '/')
20929 ++p;
20930 eap->nextcmd = check_nextcmd(p);
20931 return;
20932 }
20933
20934 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020935 * Get the function name. There are these situations:
20936 * func normal function name
20937 * "name" == func, "fudi.fd_dict" == NULL
20938 * dict.func new dictionary entry
20939 * "name" == NULL, "fudi.fd_dict" set,
20940 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20941 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020942 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20944 * dict.func existing dict entry that's not a Funcref
20945 * "name" == NULL, "fudi.fd_dict" set,
20946 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020949 name = trans_function_name(&p, eap->skip, 0, &fudi);
20950 paren = (vim_strchr(p, '(') != NULL);
20951 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 {
20953 /*
20954 * Return on an invalid expression in braces, unless the expression
20955 * evaluation has been cancelled due to an aborting error, an
20956 * interrupt, or an exception.
20957 */
20958 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020959 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020960 if (!eap->skip && fudi.fd_newkey != NULL)
20961 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 else
20966 eap->skip = TRUE;
20967 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020968
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 /* An error in a function call during evaluation of an expression in magic
20970 * braces should not cause the function not to be defined. */
20971 saved_did_emsg = did_emsg;
20972 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973
20974 /*
20975 * ":function func" with only function name: list function.
20976 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020977 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 {
20979 if (!ends_excmd(*skipwhite(p)))
20980 {
20981 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020982 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 }
20984 eap->nextcmd = check_nextcmd(p);
20985 if (eap->nextcmd != NULL)
20986 *p = NUL;
20987 if (!eap->skip && !got_int)
20988 {
20989 fp = find_func(name);
20990 if (fp != NULL)
20991 {
20992 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020993 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020995 if (FUNCLINE(fp, j) == NULL)
20996 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 msg_putchar('\n');
20998 msg_outnum((long)(j + 1));
20999 if (j < 9)
21000 msg_putchar(' ');
21001 if (j < 99)
21002 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021003 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 out_flush(); /* show a line at a time */
21005 ui_breakcheck();
21006 }
21007 if (!got_int)
21008 {
21009 msg_putchar('\n');
21010 msg_puts((char_u *)" endfunction");
21011 }
21012 }
21013 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021014 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 }
21018
21019 /*
21020 * ":function name(arg1, arg2)" Define function.
21021 */
21022 p = skipwhite(p);
21023 if (*p != '(')
21024 {
21025 if (!eap->skip)
21026 {
21027 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021028 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 }
21030 /* attempt to continue by skipping some text */
21031 if (vim_strchr(p, '(') != NULL)
21032 p = vim_strchr(p, '(');
21033 }
21034 p = skipwhite(p + 1);
21035
21036 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21037 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21038
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021039 if (!eap->skip)
21040 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021041 /* Check the name of the function. Unless it's a dictionary function
21042 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021043 if (name != NULL)
21044 arg = name;
21045 else
21046 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021047 if (arg != NULL && (fudi.fd_di == NULL
21048 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021049 {
21050 if (*arg == K_SPECIAL)
21051 j = 3;
21052 else
21053 j = 0;
21054 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21055 : eval_isnamec(arg[j])))
21056 ++j;
21057 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021058 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021059 }
21060 }
21061
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 /*
21063 * Isolate the arguments: "arg1, arg2, ...)"
21064 */
21065 while (*p != ')')
21066 {
21067 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21068 {
21069 varargs = TRUE;
21070 p += 3;
21071 mustend = TRUE;
21072 }
21073 else
21074 {
21075 arg = p;
21076 while (ASCII_ISALNUM(*p) || *p == '_')
21077 ++p;
21078 if (arg == p || isdigit(*arg)
21079 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21080 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21081 {
21082 if (!eap->skip)
21083 EMSG2(_("E125: Illegal argument: %s"), arg);
21084 break;
21085 }
21086 if (ga_grow(&newargs, 1) == FAIL)
21087 goto erret;
21088 c = *p;
21089 *p = NUL;
21090 arg = vim_strsave(arg);
21091 if (arg == NULL)
21092 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021093
21094 /* Check for duplicate argument name. */
21095 for (i = 0; i < newargs.ga_len; ++i)
21096 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21097 {
21098 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21099 goto erret;
21100 }
21101
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21103 *p = c;
21104 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 if (*p == ',')
21106 ++p;
21107 else
21108 mustend = TRUE;
21109 }
21110 p = skipwhite(p);
21111 if (mustend && *p != ')')
21112 {
21113 if (!eap->skip)
21114 EMSG2(_(e_invarg2), eap->arg);
21115 break;
21116 }
21117 }
21118 ++p; /* skip the ')' */
21119
Bram Moolenaare9a41262005-01-15 22:18:47 +000021120 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 for (;;)
21122 {
21123 p = skipwhite(p);
21124 if (STRNCMP(p, "range", 5) == 0)
21125 {
21126 flags |= FC_RANGE;
21127 p += 5;
21128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021129 else if (STRNCMP(p, "dict", 4) == 0)
21130 {
21131 flags |= FC_DICT;
21132 p += 4;
21133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 else if (STRNCMP(p, "abort", 5) == 0)
21135 {
21136 flags |= FC_ABORT;
21137 p += 5;
21138 }
21139 else
21140 break;
21141 }
21142
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021143 /* When there is a line break use what follows for the function body.
21144 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21145 if (*p == '\n')
21146 line_arg = p + 1;
21147 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 EMSG(_(e_trailing));
21149
21150 /*
21151 * Read the body of the function, until ":endfunction" is found.
21152 */
21153 if (KeyTyped)
21154 {
21155 /* Check if the function already exists, don't let the user type the
21156 * whole function before telling him it doesn't work! For a script we
21157 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021158 if (!eap->skip && !eap->forceit)
21159 {
21160 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21161 EMSG(_(e_funcdict));
21162 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021163 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021166 if (!eap->skip && did_emsg)
21167 goto erret;
21168
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 msg_putchar('\n'); /* don't overwrite the function name */
21170 cmdline_row = msg_row;
21171 }
21172
21173 indent = 2;
21174 nesting = 0;
21175 for (;;)
21176 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021177 if (KeyTyped)
21178 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021180 sourcing_lnum_off = sourcing_lnum;
21181
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021182 if (line_arg != NULL)
21183 {
21184 /* Use eap->arg, split up in parts by line breaks. */
21185 theline = line_arg;
21186 p = vim_strchr(theline, '\n');
21187 if (p == NULL)
21188 line_arg += STRLEN(line_arg);
21189 else
21190 {
21191 *p = NUL;
21192 line_arg = p + 1;
21193 }
21194 }
21195 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 theline = getcmdline(':', 0L, indent);
21197 else
21198 theline = eap->getline(':', eap->cookie, indent);
21199 if (KeyTyped)
21200 lines_left = Rows - 1;
21201 if (theline == NULL)
21202 {
21203 EMSG(_("E126: Missing :endfunction"));
21204 goto erret;
21205 }
21206
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021207 /* Detect line continuation: sourcing_lnum increased more than one. */
21208 if (sourcing_lnum > sourcing_lnum_off + 1)
21209 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21210 else
21211 sourcing_lnum_off = 0;
21212
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 if (skip_until != NULL)
21214 {
21215 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21216 * don't check for ":endfunc". */
21217 if (STRCMP(theline, skip_until) == 0)
21218 {
21219 vim_free(skip_until);
21220 skip_until = NULL;
21221 }
21222 }
21223 else
21224 {
21225 /* skip ':' and blanks*/
21226 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21227 ;
21228
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021229 /* Check for "endfunction". */
21230 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021232 if (line_arg == NULL)
21233 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 break;
21235 }
21236
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021237 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 * at "end". */
21239 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21240 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021241 else if (STRNCMP(p, "if", 2) == 0
21242 || STRNCMP(p, "wh", 2) == 0
21243 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 || STRNCMP(p, "try", 3) == 0)
21245 indent += 2;
21246
21247 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021248 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021250 if (*p == '!')
21251 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 p += eval_fname_script(p);
21253 if (ASCII_ISALPHA(*p))
21254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021255 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 if (*skipwhite(p) == '(')
21257 {
21258 ++nesting;
21259 indent += 2;
21260 }
21261 }
21262 }
21263
21264 /* Check for ":append" or ":insert". */
21265 p = skip_range(p, NULL);
21266 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21267 || (p[0] == 'i'
21268 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21269 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21270 skip_until = vim_strsave((char_u *)".");
21271
21272 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21273 arg = skipwhite(skiptowhite(p));
21274 if (arg[0] == '<' && arg[1] =='<'
21275 && ((p[0] == 'p' && p[1] == 'y'
21276 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21277 || (p[0] == 'p' && p[1] == 'e'
21278 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21279 || (p[0] == 't' && p[1] == 'c'
21280 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021281 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21282 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21284 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021285 || (p[0] == 'm' && p[1] == 'z'
21286 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 ))
21288 {
21289 /* ":python <<" continues until a dot, like ":append" */
21290 p = skipwhite(arg + 2);
21291 if (*p == NUL)
21292 skip_until = vim_strsave((char_u *)".");
21293 else
21294 skip_until = vim_strsave(p);
21295 }
21296 }
21297
21298 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021299 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021300 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021301 if (line_arg == NULL)
21302 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021304 }
21305
21306 /* Copy the line to newly allocated memory. get_one_sourceline()
21307 * allocates 250 bytes per line, this saves 80% on average. The cost
21308 * is an extra alloc/free. */
21309 p = vim_strsave(theline);
21310 if (p != NULL)
21311 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021312 if (line_arg == NULL)
21313 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021314 theline = p;
21315 }
21316
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021317 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21318
21319 /* Add NULL lines for continuation lines, so that the line count is
21320 * equal to the index in the growarray. */
21321 while (sourcing_lnum_off-- > 0)
21322 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021323
21324 /* Check for end of eap->arg. */
21325 if (line_arg != NULL && *line_arg == NUL)
21326 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 }
21328
21329 /* Don't define the function when skipping commands or when an error was
21330 * detected. */
21331 if (eap->skip || did_emsg)
21332 goto erret;
21333
21334 /*
21335 * If there are no errors, add the function
21336 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021337 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021338 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021339 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021341 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021342 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021343 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021344 goto erret;
21345 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021346
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021347 fp = find_func(name);
21348 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021350 if (!eap->forceit)
21351 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021352 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021353 goto erret;
21354 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021355 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021356 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021357 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021358 name);
21359 goto erret;
21360 }
21361 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021362 ga_clear_strings(&(fp->uf_args));
21363 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021364 vim_free(name);
21365 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 }
21368 else
21369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 char numbuf[20];
21371
21372 fp = NULL;
21373 if (fudi.fd_newkey == NULL && !eap->forceit)
21374 {
21375 EMSG(_(e_funcdict));
21376 goto erret;
21377 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021378 if (fudi.fd_di == NULL)
21379 {
21380 /* Can't add a function to a locked dictionary */
21381 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21382 goto erret;
21383 }
21384 /* Can't change an existing function if it is locked */
21385 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21386 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021387
21388 /* Give the function a sequential number. Can only be used with a
21389 * Funcref! */
21390 vim_free(name);
21391 sprintf(numbuf, "%d", ++func_nr);
21392 name = vim_strsave((char_u *)numbuf);
21393 if (name == NULL)
21394 goto erret;
21395 }
21396
21397 if (fp == NULL)
21398 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021399 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021400 {
21401 int slen, plen;
21402 char_u *scriptname;
21403
21404 /* Check that the autoload name matches the script name. */
21405 j = FAIL;
21406 if (sourcing_name != NULL)
21407 {
21408 scriptname = autoload_name(name);
21409 if (scriptname != NULL)
21410 {
21411 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021412 plen = (int)STRLEN(p);
21413 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021414 if (slen > plen && fnamecmp(p,
21415 sourcing_name + slen - plen) == 0)
21416 j = OK;
21417 vim_free(scriptname);
21418 }
21419 }
21420 if (j == FAIL)
21421 {
21422 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21423 goto erret;
21424 }
21425 }
21426
21427 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 if (fp == NULL)
21429 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021430
21431 if (fudi.fd_dict != NULL)
21432 {
21433 if (fudi.fd_di == NULL)
21434 {
21435 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021436 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021437 if (fudi.fd_di == NULL)
21438 {
21439 vim_free(fp);
21440 goto erret;
21441 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021442 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21443 {
21444 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021445 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021446 goto erret;
21447 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021448 }
21449 else
21450 /* overwrite existing dict entry */
21451 clear_tv(&fudi.fd_di->di_tv);
21452 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021453 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021455 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021456
21457 /* behave like "dict" was used */
21458 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021459 }
21460
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021462 STRCPY(fp->uf_name, name);
21463 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021465 fp->uf_args = newargs;
21466 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021467#ifdef FEAT_PROFILE
21468 fp->uf_tml_count = NULL;
21469 fp->uf_tml_total = NULL;
21470 fp->uf_tml_self = NULL;
21471 fp->uf_profiling = FALSE;
21472 if (prof_def_func())
21473 func_do_profile(fp);
21474#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021475 fp->uf_varargs = varargs;
21476 fp->uf_flags = flags;
21477 fp->uf_calls = 0;
21478 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021479 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480
21481erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 ga_clear_strings(&newargs);
21483 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021484ret_free:
21485 vim_free(skip_until);
21486 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489}
21490
21491/*
21492 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021493 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 * flags:
21496 * TFN_INT: internal function name OK
21497 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 * Advances "pp" to just after the function name (if no error).
21499 */
21500 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021501trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 char_u **pp;
21503 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021507 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 char_u *start;
21509 char_u *end;
21510 int lead;
21511 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021513 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021514
21515 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021516 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021518
21519 /* Check for hard coded <SNR>: already translated function ID (from a user
21520 * command). */
21521 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21522 && (*pp)[2] == (int)KE_SNR)
21523 {
21524 *pp += 3;
21525 len = get_id_len(pp) + 3;
21526 return vim_strnsave(start, len);
21527 }
21528
21529 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21530 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021532 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021534
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021535 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21536 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021537 if (end == start)
21538 {
21539 if (!skip)
21540 EMSG(_("E129: Function name required"));
21541 goto theend;
21542 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021543 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021544 {
21545 /*
21546 * Report an invalid expression in braces, unless the expression
21547 * evaluation has been cancelled due to an aborting error, an
21548 * interrupt, or an exception.
21549 */
21550 if (!aborting())
21551 {
21552 if (end != NULL)
21553 EMSG2(_(e_invarg2), start);
21554 }
21555 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021556 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021557 goto theend;
21558 }
21559
21560 if (lv.ll_tv != NULL)
21561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021562 if (fdp != NULL)
21563 {
21564 fdp->fd_dict = lv.ll_dict;
21565 fdp->fd_newkey = lv.ll_newkey;
21566 lv.ll_newkey = NULL;
21567 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021568 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021569 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21570 {
21571 name = vim_strsave(lv.ll_tv->vval.v_string);
21572 *pp = end;
21573 }
21574 else
21575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021576 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21577 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578 EMSG(_(e_funcref));
21579 else
21580 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021581 name = NULL;
21582 }
21583 goto theend;
21584 }
21585
21586 if (lv.ll_name == NULL)
21587 {
21588 /* Error found, but continue after the function name. */
21589 *pp = end;
21590 goto theend;
21591 }
21592
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021593 /* Check if the name is a Funcref. If so, use the value. */
21594 if (lv.ll_exp_name != NULL)
21595 {
21596 len = (int)STRLEN(lv.ll_exp_name);
21597 name = deref_func_name(lv.ll_exp_name, &len);
21598 if (name == lv.ll_exp_name)
21599 name = NULL;
21600 }
21601 else
21602 {
21603 len = (int)(end - *pp);
21604 name = deref_func_name(*pp, &len);
21605 if (name == *pp)
21606 name = NULL;
21607 }
21608 if (name != NULL)
21609 {
21610 name = vim_strsave(name);
21611 *pp = end;
21612 goto theend;
21613 }
21614
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021615 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021616 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021617 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021618 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21619 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21620 {
21621 /* When there was "s:" already or the name expanded to get a
21622 * leading "s:" then remove it. */
21623 lv.ll_name += 2;
21624 len -= 2;
21625 lead = 2;
21626 }
21627 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021628 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021629 {
21630 if (lead == 2) /* skip over "s:" */
21631 lv.ll_name += 2;
21632 len = (int)(end - lv.ll_name);
21633 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021634
21635 /*
21636 * Copy the function name to allocated memory.
21637 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21638 * Accept <SNR>123_name() outside a script.
21639 */
21640 if (skip)
21641 lead = 0; /* do nothing */
21642 else if (lead > 0)
21643 {
21644 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021645 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21646 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021647 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021648 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021649 if (current_SID <= 0)
21650 {
21651 EMSG(_(e_usingsid));
21652 goto theend;
21653 }
21654 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21655 lead += (int)STRLEN(sid_buf);
21656 }
21657 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021658 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021659 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021660 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021661 goto theend;
21662 }
21663 name = alloc((unsigned)(len + lead + 1));
21664 if (name != NULL)
21665 {
21666 if (lead > 0)
21667 {
21668 name[0] = K_SPECIAL;
21669 name[1] = KS_EXTRA;
21670 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021671 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021672 STRCPY(name + 3, sid_buf);
21673 }
21674 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21675 name[len + lead] = NUL;
21676 }
21677 *pp = end;
21678
21679theend:
21680 clear_lval(&lv);
21681 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682}
21683
21684/*
21685 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21686 * Return 2 if "p" starts with "s:".
21687 * Return 0 otherwise.
21688 */
21689 static int
21690eval_fname_script(p)
21691 char_u *p;
21692{
21693 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21694 || STRNICMP(p + 1, "SNR>", 4) == 0))
21695 return 5;
21696 if (p[0] == 's' && p[1] == ':')
21697 return 2;
21698 return 0;
21699}
21700
21701/*
21702 * Return TRUE if "p" starts with "<SID>" or "s:".
21703 * Only works if eval_fname_script() returned non-zero for "p"!
21704 */
21705 static int
21706eval_fname_sid(p)
21707 char_u *p;
21708{
21709 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21710}
21711
21712/*
21713 * List the head of the function: "name(arg1, arg2)".
21714 */
21715 static void
21716list_func_head(fp, indent)
21717 ufunc_T *fp;
21718 int indent;
21719{
21720 int j;
21721
21722 msg_start();
21723 if (indent)
21724 MSG_PUTS(" ");
21725 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021726 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 {
21728 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021729 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 }
21731 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021732 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021734 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 {
21736 if (j)
21737 MSG_PUTS(", ");
21738 msg_puts(FUNCARG(fp, j));
21739 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021740 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 {
21742 if (j)
21743 MSG_PUTS(", ");
21744 MSG_PUTS("...");
21745 }
21746 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021747 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021748 if (p_verbose > 0)
21749 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750}
21751
21752/*
21753 * Find a function by name, return pointer to it in ufuncs.
21754 * Return NULL for unknown function.
21755 */
21756 static ufunc_T *
21757find_func(name)
21758 char_u *name;
21759{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021760 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021762 hi = hash_find(&func_hashtab, name);
21763 if (!HASHITEM_EMPTY(hi))
21764 return HI2UF(hi);
21765 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766}
21767
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021768#if defined(EXITFREE) || defined(PROTO)
21769 void
21770free_all_functions()
21771{
21772 hashitem_T *hi;
21773
21774 /* Need to start all over every time, because func_free() may change the
21775 * hash table. */
21776 while (func_hashtab.ht_used > 0)
21777 for (hi = func_hashtab.ht_array; ; ++hi)
21778 if (!HASHITEM_EMPTY(hi))
21779 {
21780 func_free(HI2UF(hi));
21781 break;
21782 }
21783}
21784#endif
21785
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786/*
21787 * Return TRUE if a function "name" exists.
21788 */
21789 static int
21790function_exists(name)
21791 char_u *name;
21792{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021793 char_u *nm = name;
21794 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021795 int n = FALSE;
21796
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021797 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021798 nm = skipwhite(nm);
21799
21800 /* Only accept "funcname", "funcname ", "funcname (..." and
21801 * "funcname(...", not "funcname!...". */
21802 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021803 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021804 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021805 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021806 else
21807 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021808 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021809 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021810 return n;
21811}
21812
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021813/*
21814 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021815 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021816 */
21817 static int
21818builtin_function(name)
21819 char_u *name;
21820{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021821 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21822 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021823}
21824
Bram Moolenaar05159a02005-02-26 23:04:13 +000021825#if defined(FEAT_PROFILE) || defined(PROTO)
21826/*
21827 * Start profiling function "fp".
21828 */
21829 static void
21830func_do_profile(fp)
21831 ufunc_T *fp;
21832{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021833 int len = fp->uf_lines.ga_len;
21834
21835 if (len == 0)
21836 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021837 fp->uf_tm_count = 0;
21838 profile_zero(&fp->uf_tm_self);
21839 profile_zero(&fp->uf_tm_total);
21840 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021841 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021842 if (fp->uf_tml_total == NULL)
21843 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021844 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021845 if (fp->uf_tml_self == NULL)
21846 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021847 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021848 fp->uf_tml_idx = -1;
21849 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21850 || fp->uf_tml_self == NULL)
21851 return; /* out of memory */
21852
21853 fp->uf_profiling = TRUE;
21854}
21855
21856/*
21857 * Dump the profiling results for all functions in file "fd".
21858 */
21859 void
21860func_dump_profile(fd)
21861 FILE *fd;
21862{
21863 hashitem_T *hi;
21864 int todo;
21865 ufunc_T *fp;
21866 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021867 ufunc_T **sorttab;
21868 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021869
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021870 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021871 if (todo == 0)
21872 return; /* nothing to dump */
21873
Bram Moolenaar73830342005-02-28 22:48:19 +000021874 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21875
Bram Moolenaar05159a02005-02-26 23:04:13 +000021876 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21877 {
21878 if (!HASHITEM_EMPTY(hi))
21879 {
21880 --todo;
21881 fp = HI2UF(hi);
21882 if (fp->uf_profiling)
21883 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021884 if (sorttab != NULL)
21885 sorttab[st_len++] = fp;
21886
Bram Moolenaar05159a02005-02-26 23:04:13 +000021887 if (fp->uf_name[0] == K_SPECIAL)
21888 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21889 else
21890 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21891 if (fp->uf_tm_count == 1)
21892 fprintf(fd, "Called 1 time\n");
21893 else
21894 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21895 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21896 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21897 fprintf(fd, "\n");
21898 fprintf(fd, "count total (s) self (s)\n");
21899
21900 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21901 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021902 if (FUNCLINE(fp, i) == NULL)
21903 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021904 prof_func_line(fd, fp->uf_tml_count[i],
21905 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021906 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21907 }
21908 fprintf(fd, "\n");
21909 }
21910 }
21911 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021912
21913 if (sorttab != NULL && st_len > 0)
21914 {
21915 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21916 prof_total_cmp);
21917 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21918 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21919 prof_self_cmp);
21920 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21921 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021922
21923 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021924}
Bram Moolenaar73830342005-02-28 22:48:19 +000021925
21926 static void
21927prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21928 FILE *fd;
21929 ufunc_T **sorttab;
21930 int st_len;
21931 char *title;
21932 int prefer_self; /* when equal print only self time */
21933{
21934 int i;
21935 ufunc_T *fp;
21936
21937 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21938 fprintf(fd, "count total (s) self (s) function\n");
21939 for (i = 0; i < 20 && i < st_len; ++i)
21940 {
21941 fp = sorttab[i];
21942 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21943 prefer_self);
21944 if (fp->uf_name[0] == K_SPECIAL)
21945 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21946 else
21947 fprintf(fd, " %s()\n", fp->uf_name);
21948 }
21949 fprintf(fd, "\n");
21950}
21951
21952/*
21953 * Print the count and times for one function or function line.
21954 */
21955 static void
21956prof_func_line(fd, count, total, self, prefer_self)
21957 FILE *fd;
21958 int count;
21959 proftime_T *total;
21960 proftime_T *self;
21961 int prefer_self; /* when equal print only self time */
21962{
21963 if (count > 0)
21964 {
21965 fprintf(fd, "%5d ", count);
21966 if (prefer_self && profile_equal(total, self))
21967 fprintf(fd, " ");
21968 else
21969 fprintf(fd, "%s ", profile_msg(total));
21970 if (!prefer_self && profile_equal(total, self))
21971 fprintf(fd, " ");
21972 else
21973 fprintf(fd, "%s ", profile_msg(self));
21974 }
21975 else
21976 fprintf(fd, " ");
21977}
21978
21979/*
21980 * Compare function for total time sorting.
21981 */
21982 static int
21983#ifdef __BORLANDC__
21984_RTLENTRYF
21985#endif
21986prof_total_cmp(s1, s2)
21987 const void *s1;
21988 const void *s2;
21989{
21990 ufunc_T *p1, *p2;
21991
21992 p1 = *(ufunc_T **)s1;
21993 p2 = *(ufunc_T **)s2;
21994 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21995}
21996
21997/*
21998 * Compare function for self time sorting.
21999 */
22000 static int
22001#ifdef __BORLANDC__
22002_RTLENTRYF
22003#endif
22004prof_self_cmp(s1, s2)
22005 const void *s1;
22006 const void *s2;
22007{
22008 ufunc_T *p1, *p2;
22009
22010 p1 = *(ufunc_T **)s1;
22011 p2 = *(ufunc_T **)s2;
22012 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22013}
22014
Bram Moolenaar05159a02005-02-26 23:04:13 +000022015#endif
22016
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022017/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022018 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022019 * Return TRUE if a package was loaded.
22020 */
22021 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022022script_autoload(name, reload)
22023 char_u *name;
22024 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022025{
22026 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022027 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022028 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022029 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022030
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022031 /* Return quickly when autoload disabled. */
22032 if (no_autoload)
22033 return FALSE;
22034
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022035 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022036 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022037 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022038 return FALSE;
22039
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022040 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022041
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022042 /* Find the name in the list of previously loaded package names. Skip
22043 * "autoload/", it's always the same. */
22044 for (i = 0; i < ga_loaded.ga_len; ++i)
22045 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22046 break;
22047 if (!reload && i < ga_loaded.ga_len)
22048 ret = FALSE; /* was loaded already */
22049 else
22050 {
22051 /* Remember the name if it wasn't loaded already. */
22052 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22053 {
22054 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22055 tofree = NULL;
22056 }
22057
22058 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022059 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022060 ret = TRUE;
22061 }
22062
22063 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022064 return ret;
22065}
22066
22067/*
22068 * Return the autoload script name for a function or variable name.
22069 * Returns NULL when out of memory.
22070 */
22071 static char_u *
22072autoload_name(name)
22073 char_u *name;
22074{
22075 char_u *p;
22076 char_u *scriptname;
22077
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022078 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022079 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22080 if (scriptname == NULL)
22081 return FALSE;
22082 STRCPY(scriptname, "autoload/");
22083 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022084 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022085 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022086 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022087 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022088 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022089}
22090
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22092
22093/*
22094 * Function given to ExpandGeneric() to obtain the list of user defined
22095 * function names.
22096 */
22097 char_u *
22098get_user_func_name(xp, idx)
22099 expand_T *xp;
22100 int idx;
22101{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022102 static long_u done;
22103 static hashitem_T *hi;
22104 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105
22106 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022108 done = 0;
22109 hi = func_hashtab.ht_array;
22110 }
22111 if (done < func_hashtab.ht_used)
22112 {
22113 if (done++ > 0)
22114 ++hi;
22115 while (HASHITEM_EMPTY(hi))
22116 ++hi;
22117 fp = HI2UF(hi);
22118
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022119 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022120 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022121
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022122 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22123 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124
22125 cat_func_name(IObuff, fp);
22126 if (xp->xp_context != EXPAND_USER_FUNC)
22127 {
22128 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022129 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 STRCAT(IObuff, ")");
22131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 return IObuff;
22133 }
22134 return NULL;
22135}
22136
22137#endif /* FEAT_CMDL_COMPL */
22138
22139/*
22140 * Copy the function name of "fp" to buffer "buf".
22141 * "buf" must be able to hold the function name plus three bytes.
22142 * Takes care of script-local function names.
22143 */
22144 static void
22145cat_func_name(buf, fp)
22146 char_u *buf;
22147 ufunc_T *fp;
22148{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022149 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 {
22151 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022152 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 }
22154 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022155 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156}
22157
22158/*
22159 * ":delfunction {name}"
22160 */
22161 void
22162ex_delfunction(eap)
22163 exarg_T *eap;
22164{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022165 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 char_u *p;
22167 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022168 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169
22170 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022171 name = trans_function_name(&p, eap->skip, 0, &fudi);
22172 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022174 {
22175 if (fudi.fd_dict != NULL && !eap->skip)
22176 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 if (!ends_excmd(*skipwhite(p)))
22180 {
22181 vim_free(name);
22182 EMSG(_(e_trailing));
22183 return;
22184 }
22185 eap->nextcmd = check_nextcmd(p);
22186 if (eap->nextcmd != NULL)
22187 *p = NUL;
22188
22189 if (!eap->skip)
22190 fp = find_func(name);
22191 vim_free(name);
22192
22193 if (!eap->skip)
22194 {
22195 if (fp == NULL)
22196 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022197 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 return;
22199 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022200 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 {
22202 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22203 return;
22204 }
22205
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022206 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022208 /* Delete the dict item that refers to the function, it will
22209 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022210 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022212 else
22213 func_free(fp);
22214 }
22215}
22216
22217/*
22218 * Free a function and remove it from the list of functions.
22219 */
22220 static void
22221func_free(fp)
22222 ufunc_T *fp;
22223{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022224 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022225
22226 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022227 ga_clear_strings(&(fp->uf_args));
22228 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022229#ifdef FEAT_PROFILE
22230 vim_free(fp->uf_tml_count);
22231 vim_free(fp->uf_tml_total);
22232 vim_free(fp->uf_tml_self);
22233#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022234
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022235 /* remove the function from the function hashtable */
22236 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22237 if (HASHITEM_EMPTY(hi))
22238 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022239 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022240 hash_remove(&func_hashtab, hi);
22241
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022242 vim_free(fp);
22243}
22244
22245/*
22246 * Unreference a Function: decrement the reference count and free it when it
22247 * becomes zero. Only for numbered functions.
22248 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022249 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022250func_unref(name)
22251 char_u *name;
22252{
22253 ufunc_T *fp;
22254
22255 if (name != NULL && isdigit(*name))
22256 {
22257 fp = find_func(name);
22258 if (fp == NULL)
22259 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022260 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022261 {
22262 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022263 * when "uf_calls" becomes zero. */
22264 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022265 func_free(fp);
22266 }
22267 }
22268}
22269
22270/*
22271 * Count a reference to a Function.
22272 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022273 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022274func_ref(name)
22275 char_u *name;
22276{
22277 ufunc_T *fp;
22278
22279 if (name != NULL && isdigit(*name))
22280 {
22281 fp = find_func(name);
22282 if (fp == NULL)
22283 EMSG2(_(e_intern2), "func_ref()");
22284 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022285 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 }
22287}
22288
22289/*
22290 * Call a user function.
22291 */
22292 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022293call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 ufunc_T *fp; /* pointer to function */
22295 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022296 typval_T *argvars; /* arguments */
22297 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 linenr_T firstline; /* first line of range */
22299 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022300 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301{
Bram Moolenaar33570922005-01-25 22:26:29 +000022302 char_u *save_sourcing_name;
22303 linenr_T save_sourcing_lnum;
22304 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022305 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 int save_did_emsg;
22307 static int depth = 0;
22308 dictitem_T *v;
22309 int fixvar_idx = 0; /* index in fixvar[] */
22310 int i;
22311 int ai;
22312 char_u numbuf[NUMBUFLEN];
22313 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022314#ifdef FEAT_PROFILE
22315 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022316 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318
22319 /* If depth of calling is getting too high, don't execute the function */
22320 if (depth >= p_mfd)
22321 {
22322 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022323 rettv->v_type = VAR_NUMBER;
22324 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 return;
22326 }
22327 ++depth;
22328
22329 line_breakcheck(); /* check for CTRL-C hit */
22330
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022331 fc = (funccall_T *)alloc(sizeof(funccall_T));
22332 fc->caller = current_funccal;
22333 current_funccal = fc;
22334 fc->func = fp;
22335 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022336 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022337 fc->linenr = 0;
22338 fc->returned = FALSE;
22339 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022341 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22342 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343
Bram Moolenaar33570922005-01-25 22:26:29 +000022344 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022345 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022346 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22347 * each argument variable and saves a lot of time.
22348 */
22349 /*
22350 * Init l: variables.
22351 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022352 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022353 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022354 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022355 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22356 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022357 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022358 name = v->di_key;
22359 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022360 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022361 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022362 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022363 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 v->di_tv.vval.v_dict = selfdict;
22365 ++selfdict->dv_refcount;
22366 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022367
Bram Moolenaar33570922005-01-25 22:26:29 +000022368 /*
22369 * Init a: variables.
22370 * Set a:0 to "argcount".
22371 * Set a:000 to a list with room for the "..." arguments.
22372 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022373 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022374 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022375 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022376 /* Use "name" to avoid a warning from some compiler that checks the
22377 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022378 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022379 name = v->di_key;
22380 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022382 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022384 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022385 v->di_tv.vval.v_list = &fc->l_varlist;
22386 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22387 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22388 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389
22390 /*
22391 * Set a:firstline to "firstline" and a:lastline to "lastline".
22392 * Set a:name to named arguments.
22393 * Set a:N to the "..." arguments.
22394 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022395 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022396 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022397 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022398 (varnumber_T)lastline);
22399 for (i = 0; i < argcount; ++i)
22400 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022402 if (ai < 0)
22403 /* named argument a:name */
22404 name = FUNCARG(fp, i);
22405 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022406 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 /* "..." argument a:1, a:2, etc. */
22408 sprintf((char *)numbuf, "%d", ai + 1);
22409 name = numbuf;
22410 }
22411 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22412 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022413 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022414 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22415 }
22416 else
22417 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022418 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22419 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022420 if (v == NULL)
22421 break;
22422 v->di_flags = DI_FLAGS_RO;
22423 }
22424 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022425 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022426
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022427 /* Note: the values are copied directly to avoid alloc/free.
22428 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022430 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022431
22432 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22433 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022434 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22435 fc->l_listitems[ai].li_tv = argvars[i];
22436 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022437 }
22438 }
22439
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 /* Don't redraw while executing the function. */
22441 ++RedrawingDisabled;
22442 save_sourcing_name = sourcing_name;
22443 save_sourcing_lnum = sourcing_lnum;
22444 sourcing_lnum = 1;
22445 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022446 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 if (sourcing_name != NULL)
22448 {
22449 if (save_sourcing_name != NULL
22450 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22451 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22452 else
22453 STRCPY(sourcing_name, "function ");
22454 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22455
22456 if (p_verbose >= 12)
22457 {
22458 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022459 verbose_enter_scroll();
22460
Bram Moolenaar555b2802005-05-19 21:08:39 +000022461 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 if (p_verbose >= 14)
22463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022465 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022466 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022467 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468
22469 msg_puts((char_u *)"(");
22470 for (i = 0; i < argcount; ++i)
22471 {
22472 if (i > 0)
22473 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022474 if (argvars[i].v_type == VAR_NUMBER)
22475 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 else
22477 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022478 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22479 if (s != NULL)
22480 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022481 if (vim_strsize(s) > MSG_BUF_CLEN)
22482 {
22483 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22484 s = buf;
22485 }
22486 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022487 vim_free(tofree);
22488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 }
22490 }
22491 msg_puts((char_u *)")");
22492 }
22493 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022494
22495 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 --no_wait_return;
22497 }
22498 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022499#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022500 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022501 {
22502 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22503 func_do_profile(fp);
22504 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022505 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022506 {
22507 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022508 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022509 profile_zero(&fp->uf_tm_children);
22510 }
22511 script_prof_save(&wait_start);
22512 }
22513#endif
22514
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022516 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 save_did_emsg = did_emsg;
22518 did_emsg = FALSE;
22519
22520 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022521 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22523
22524 --RedrawingDisabled;
22525
22526 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022527 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022529 clear_tv(rettv);
22530 rettv->v_type = VAR_NUMBER;
22531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 }
22533
Bram Moolenaar05159a02005-02-26 23:04:13 +000022534#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022535 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022536 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022537 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022538 profile_end(&call_start);
22539 profile_sub_wait(&wait_start, &call_start);
22540 profile_add(&fp->uf_tm_total, &call_start);
22541 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022542 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022543 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022544 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22545 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022546 }
22547 }
22548#endif
22549
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550 /* when being verbose, mention the return value */
22551 if (p_verbose >= 12)
22552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022554 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022557 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022558 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022559 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022560 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022563 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022564 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022565 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022566 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022567
Bram Moolenaar555b2802005-05-19 21:08:39 +000022568 /* The value may be very long. Skip the middle part, so that we
22569 * have some idea how it starts and ends. smsg() would always
22570 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022571 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022572 if (s != NULL)
22573 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022574 if (vim_strsize(s) > MSG_BUF_CLEN)
22575 {
22576 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22577 s = buf;
22578 }
22579 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022580 vim_free(tofree);
22581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 }
22583 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022584
22585 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 --no_wait_return;
22587 }
22588
22589 vim_free(sourcing_name);
22590 sourcing_name = save_sourcing_name;
22591 sourcing_lnum = save_sourcing_lnum;
22592 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022593#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022594 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022595 script_prof_restore(&wait_start);
22596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597
22598 if (p_verbose >= 12 && sourcing_name != NULL)
22599 {
22600 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022601 verbose_enter_scroll();
22602
Bram Moolenaar555b2802005-05-19 21:08:39 +000022603 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022605
22606 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 --no_wait_return;
22608 }
22609
22610 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022611 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022613
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022614 /* If the a:000 list and the l: and a: dicts are not referenced we can
22615 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022616 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22617 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22618 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22619 {
22620 free_funccal(fc, FALSE);
22621 }
22622 else
22623 {
22624 hashitem_T *hi;
22625 listitem_T *li;
22626 int todo;
22627
22628 /* "fc" is still in use. This can happen when returning "a:000" or
22629 * assigning "l:" to a global variable.
22630 * Link "fc" in the list for garbage collection later. */
22631 fc->caller = previous_funccal;
22632 previous_funccal = fc;
22633
22634 /* Make a copy of the a: variables, since we didn't do that above. */
22635 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22636 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22637 {
22638 if (!HASHITEM_EMPTY(hi))
22639 {
22640 --todo;
22641 v = HI2DI(hi);
22642 copy_tv(&v->di_tv, &v->di_tv);
22643 }
22644 }
22645
22646 /* Make a copy of the a:000 items, since we didn't do that above. */
22647 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22648 copy_tv(&li->li_tv, &li->li_tv);
22649 }
22650}
22651
22652/*
22653 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022654 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022655 */
22656 static int
22657can_free_funccal(fc, copyID)
22658 funccall_T *fc;
22659 int copyID;
22660{
22661 return (fc->l_varlist.lv_copyID != copyID
22662 && fc->l_vars.dv_copyID != copyID
22663 && fc->l_avars.dv_copyID != copyID);
22664}
22665
22666/*
22667 * Free "fc" and what it contains.
22668 */
22669 static void
22670free_funccal(fc, free_val)
22671 funccall_T *fc;
22672 int free_val; /* a: vars were allocated */
22673{
22674 listitem_T *li;
22675
22676 /* The a: variables typevals may not have been allocated, only free the
22677 * allocated variables. */
22678 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22679
22680 /* free all l: variables */
22681 vars_clear(&fc->l_vars.dv_hashtab);
22682
22683 /* Free the a:000 variables if they were allocated. */
22684 if (free_val)
22685 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22686 clear_tv(&li->li_tv);
22687
22688 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689}
22690
22691/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022692 * Add a number variable "name" to dict "dp" with value "nr".
22693 */
22694 static void
22695add_nr_var(dp, v, name, nr)
22696 dict_T *dp;
22697 dictitem_T *v;
22698 char *name;
22699 varnumber_T nr;
22700{
22701 STRCPY(v->di_key, name);
22702 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22703 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22704 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022705 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 v->di_tv.vval.v_number = nr;
22707}
22708
22709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 * ":return [expr]"
22711 */
22712 void
22713ex_return(eap)
22714 exarg_T *eap;
22715{
22716 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 int returning = FALSE;
22719
22720 if (current_funccal == NULL)
22721 {
22722 EMSG(_("E133: :return not inside a function"));
22723 return;
22724 }
22725
22726 if (eap->skip)
22727 ++emsg_skip;
22728
22729 eap->nextcmd = NULL;
22730 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022731 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 {
22733 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022734 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022736 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 }
22738 /* It's safer to return also on error. */
22739 else if (!eap->skip)
22740 {
22741 /*
22742 * Return unless the expression evaluation has been cancelled due to an
22743 * aborting error, an interrupt, or an exception.
22744 */
22745 if (!aborting())
22746 returning = do_return(eap, FALSE, TRUE, NULL);
22747 }
22748
22749 /* When skipping or the return gets pending, advance to the next command
22750 * in this line (!returning). Otherwise, ignore the rest of the line.
22751 * Following lines will be ignored by get_func_line(). */
22752 if (returning)
22753 eap->nextcmd = NULL;
22754 else if (eap->nextcmd == NULL) /* no argument */
22755 eap->nextcmd = check_nextcmd(arg);
22756
22757 if (eap->skip)
22758 --emsg_skip;
22759}
22760
22761/*
22762 * Return from a function. Possibly makes the return pending. Also called
22763 * for a pending return at the ":endtry" or after returning from an extra
22764 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022765 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022766 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 * FALSE when the return gets pending.
22768 */
22769 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022770do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 exarg_T *eap;
22772 int reanimate;
22773 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022774 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775{
22776 int idx;
22777 struct condstack *cstack = eap->cstack;
22778
22779 if (reanimate)
22780 /* Undo the return. */
22781 current_funccal->returned = FALSE;
22782
22783 /*
22784 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22785 * not in its finally clause (which then is to be executed next) is found.
22786 * In this case, make the ":return" pending for execution at the ":endtry".
22787 * Otherwise, return normally.
22788 */
22789 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22790 if (idx >= 0)
22791 {
22792 cstack->cs_pending[idx] = CSTP_RETURN;
22793
22794 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022795 /* A pending return again gets pending. "rettv" points to an
22796 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022798 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 else
22800 {
22801 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022802 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022804 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022806 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 {
22808 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022809 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 else
22812 EMSG(_(e_outofmem));
22813 }
22814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022815 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816
22817 if (reanimate)
22818 {
22819 /* The pending return value could be overwritten by a ":return"
22820 * without argument in a finally clause; reset the default
22821 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022822 current_funccal->rettv->v_type = VAR_NUMBER;
22823 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 }
22825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022826 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 }
22828 else
22829 {
22830 current_funccal->returned = TRUE;
22831
22832 /* If the return is carried out now, store the return value. For
22833 * a return immediately after reanimation, the value is already
22834 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022835 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022837 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022838 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022840 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 }
22842 }
22843
22844 return idx < 0;
22845}
22846
22847/*
22848 * Free the variable with a pending return value.
22849 */
22850 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022851discard_pending_return(rettv)
22852 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853{
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855}
22856
22857/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022858 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 * is an allocated string. Used by report_pending() for verbose messages.
22860 */
22861 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022862get_return_cmd(rettv)
22863 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022865 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022866 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022867 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022869 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022870 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022871 if (s == NULL)
22872 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022873
22874 STRCPY(IObuff, ":return ");
22875 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22876 if (STRLEN(s) + 8 >= IOSIZE)
22877 STRCPY(IObuff + IOSIZE - 4, "...");
22878 vim_free(tofree);
22879 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880}
22881
22882/*
22883 * Get next function line.
22884 * Called by do_cmdline() to get the next line.
22885 * Returns allocated string, or NULL for end of function.
22886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 char_u *
22888get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022889 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022891 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892{
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022894 ufunc_T *fp = fcp->func;
22895 char_u *retval;
22896 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897
22898 /* If breakpoints have been added/deleted need to check for it. */
22899 if (fcp->dbg_tick != debug_tick)
22900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022901 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 sourcing_lnum);
22903 fcp->dbg_tick = debug_tick;
22904 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022905#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022906 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022907 func_line_end(cookie);
22908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909
Bram Moolenaar05159a02005-02-26 23:04:13 +000022910 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022911 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22912 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 retval = NULL;
22914 else
22915 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022916 /* Skip NULL lines (continuation lines). */
22917 while (fcp->linenr < gap->ga_len
22918 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22919 ++fcp->linenr;
22920 if (fcp->linenr >= gap->ga_len)
22921 retval = NULL;
22922 else
22923 {
22924 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22925 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022926#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022927 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022928 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022929#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 }
22932
22933 /* Did we encounter a breakpoint? */
22934 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022936 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022938 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 sourcing_lnum);
22940 fcp->dbg_tick = debug_tick;
22941 }
22942
22943 return retval;
22944}
22945
Bram Moolenaar05159a02005-02-26 23:04:13 +000022946#if defined(FEAT_PROFILE) || defined(PROTO)
22947/*
22948 * Called when starting to read a function line.
22949 * "sourcing_lnum" must be correct!
22950 * When skipping lines it may not actually be executed, but we won't find out
22951 * until later and we need to store the time now.
22952 */
22953 void
22954func_line_start(cookie)
22955 void *cookie;
22956{
22957 funccall_T *fcp = (funccall_T *)cookie;
22958 ufunc_T *fp = fcp->func;
22959
22960 if (fp->uf_profiling && sourcing_lnum >= 1
22961 && sourcing_lnum <= fp->uf_lines.ga_len)
22962 {
22963 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022964 /* Skip continuation lines. */
22965 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22966 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022967 fp->uf_tml_execed = FALSE;
22968 profile_start(&fp->uf_tml_start);
22969 profile_zero(&fp->uf_tml_children);
22970 profile_get_wait(&fp->uf_tml_wait);
22971 }
22972}
22973
22974/*
22975 * Called when actually executing a function line.
22976 */
22977 void
22978func_line_exec(cookie)
22979 void *cookie;
22980{
22981 funccall_T *fcp = (funccall_T *)cookie;
22982 ufunc_T *fp = fcp->func;
22983
22984 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22985 fp->uf_tml_execed = TRUE;
22986}
22987
22988/*
22989 * Called when done with a function line.
22990 */
22991 void
22992func_line_end(cookie)
22993 void *cookie;
22994{
22995 funccall_T *fcp = (funccall_T *)cookie;
22996 ufunc_T *fp = fcp->func;
22997
22998 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22999 {
23000 if (fp->uf_tml_execed)
23001 {
23002 ++fp->uf_tml_count[fp->uf_tml_idx];
23003 profile_end(&fp->uf_tml_start);
23004 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023005 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023006 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23007 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023008 }
23009 fp->uf_tml_idx = -1;
23010 }
23011}
23012#endif
23013
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014/*
23015 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023016 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 */
23018 int
23019func_has_ended(cookie)
23020 void *cookie;
23021{
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023
23024 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23025 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023026 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 || fcp->returned);
23028}
23029
23030/*
23031 * return TRUE if cookie indicates a function which "abort"s on errors.
23032 */
23033 int
23034func_has_abort(cookie)
23035 void *cookie;
23036{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023037 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038}
23039
23040#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23041typedef enum
23042{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023043 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23044 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23045 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046} var_flavour_T;
23047
23048static var_flavour_T var_flavour __ARGS((char_u *varname));
23049
23050 static var_flavour_T
23051var_flavour(varname)
23052 char_u *varname;
23053{
23054 char_u *p = varname;
23055
23056 if (ASCII_ISUPPER(*p))
23057 {
23058 while (*(++p))
23059 if (ASCII_ISLOWER(*p))
23060 return VAR_FLAVOUR_SESSION;
23061 return VAR_FLAVOUR_VIMINFO;
23062 }
23063 else
23064 return VAR_FLAVOUR_DEFAULT;
23065}
23066#endif
23067
23068#if defined(FEAT_VIMINFO) || defined(PROTO)
23069/*
23070 * Restore global vars that start with a capital from the viminfo file
23071 */
23072 int
23073read_viminfo_varlist(virp, writing)
23074 vir_T *virp;
23075 int writing;
23076{
23077 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023078 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023079 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080
23081 if (!writing && (find_viminfo_parameter('!') != NULL))
23082 {
23083 tab = vim_strchr(virp->vir_line + 1, '\t');
23084 if (tab != NULL)
23085 {
23086 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023087 switch (*tab)
23088 {
23089 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023090#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023091 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023092#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023093 case 'D': type = VAR_DICT; break;
23094 case 'L': type = VAR_LIST; break;
23095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096
23097 tab = vim_strchr(tab, '\t');
23098 if (tab != NULL)
23099 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023100 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023101 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023102 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023104#ifdef FEAT_FLOAT
23105 else if (type == VAR_FLOAT)
23106 (void)string2float(tab + 1, &tv.vval.v_float);
23107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023109 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023110 if (type == VAR_DICT || type == VAR_LIST)
23111 {
23112 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23113
23114 if (etv == NULL)
23115 /* Failed to parse back the dict or list, use it as a
23116 * string. */
23117 tv.v_type = VAR_STRING;
23118 else
23119 {
23120 vim_free(tv.vval.v_string);
23121 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023122 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023123 }
23124 }
23125
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023126 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023127
23128 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023129 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023130 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23131 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 }
23133 }
23134 }
23135
23136 return viminfo_readline(virp);
23137}
23138
23139/*
23140 * Write global vars that start with a capital to the viminfo file
23141 */
23142 void
23143write_viminfo_varlist(fp)
23144 FILE *fp;
23145{
Bram Moolenaar33570922005-01-25 22:26:29 +000023146 hashitem_T *hi;
23147 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023148 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023149 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023150 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023151 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023152 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153
23154 if (find_viminfo_parameter('!') == NULL)
23155 return;
23156
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023157 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023158
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023159 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023162 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023164 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023165 this_var = HI2DI(hi);
23166 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023167 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023169 {
23170 case VAR_STRING: s = "STR"; break;
23171 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023172#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023173 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023174#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023175 case VAR_DICT: s = "DIC"; break;
23176 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023177 default: continue;
23178 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023180 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023181 if (p != NULL)
23182 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023183 vim_free(tofree);
23184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 }
23186 }
23187}
23188#endif
23189
23190#if defined(FEAT_SESSION) || defined(PROTO)
23191 int
23192store_session_globals(fd)
23193 FILE *fd;
23194{
Bram Moolenaar33570922005-01-25 22:26:29 +000023195 hashitem_T *hi;
23196 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023197 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 char_u *p, *t;
23199
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023200 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023201 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023203 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023205 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023206 this_var = HI2DI(hi);
23207 if ((this_var->di_tv.v_type == VAR_NUMBER
23208 || this_var->di_tv.v_type == VAR_STRING)
23209 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023210 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023211 /* Escape special characters with a backslash. Turn a LF and
23212 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023213 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023214 (char_u *)"\\\"\n\r");
23215 if (p == NULL) /* out of memory */
23216 break;
23217 for (t = p; *t != NUL; ++t)
23218 if (*t == '\n')
23219 *t = 'n';
23220 else if (*t == '\r')
23221 *t = 'r';
23222 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023223 this_var->di_key,
23224 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23225 : ' ',
23226 p,
23227 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23228 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023229 || put_eol(fd) == FAIL)
23230 {
23231 vim_free(p);
23232 return FAIL;
23233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 vim_free(p);
23235 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023236#ifdef FEAT_FLOAT
23237 else if (this_var->di_tv.v_type == VAR_FLOAT
23238 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23239 {
23240 float_T f = this_var->di_tv.vval.v_float;
23241 int sign = ' ';
23242
23243 if (f < 0)
23244 {
23245 f = -f;
23246 sign = '-';
23247 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023248 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023249 this_var->di_key, sign, f) < 0)
23250 || put_eol(fd) == FAIL)
23251 return FAIL;
23252 }
23253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 }
23255 }
23256 return OK;
23257}
23258#endif
23259
Bram Moolenaar661b1822005-07-28 22:36:45 +000023260/*
23261 * Display script name where an item was last set.
23262 * Should only be invoked when 'verbose' is non-zero.
23263 */
23264 void
23265last_set_msg(scriptID)
23266 scid_T scriptID;
23267{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023268 char_u *p;
23269
Bram Moolenaar661b1822005-07-28 22:36:45 +000023270 if (scriptID != 0)
23271 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023272 p = home_replace_save(NULL, get_scriptname(scriptID));
23273 if (p != NULL)
23274 {
23275 verbose_enter();
23276 MSG_PUTS(_("\n\tLast set from "));
23277 MSG_PUTS(p);
23278 vim_free(p);
23279 verbose_leave();
23280 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023281 }
23282}
23283
Bram Moolenaard812df62008-11-09 12:46:09 +000023284/*
23285 * List v:oldfiles in a nice way.
23286 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023287 void
23288ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023289 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023290{
23291 list_T *l = vimvars[VV_OLDFILES].vv_list;
23292 listitem_T *li;
23293 int nr = 0;
23294
23295 if (l == NULL)
23296 msg((char_u *)_("No old files"));
23297 else
23298 {
23299 msg_start();
23300 msg_scroll = TRUE;
23301 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23302 {
23303 msg_outnum((long)++nr);
23304 MSG_PUTS(": ");
23305 msg_outtrans(get_tv_string(&li->li_tv));
23306 msg_putchar('\n');
23307 out_flush(); /* output one line at a time */
23308 ui_breakcheck();
23309 }
23310 /* Assume "got_int" was set to truncate the listing. */
23311 got_int = FALSE;
23312
23313#ifdef FEAT_BROWSE_CMD
23314 if (cmdmod.browse)
23315 {
23316 quit_more = FALSE;
23317 nr = prompt_for_number(FALSE);
23318 msg_starthere();
23319 if (nr > 0)
23320 {
23321 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23322 (long)nr);
23323
23324 if (p != NULL)
23325 {
23326 p = expand_env_save(p);
23327 eap->arg = p;
23328 eap->cmdidx = CMD_edit;
23329 cmdmod.browse = FALSE;
23330 do_exedit(eap, NULL);
23331 vim_free(p);
23332 }
23333 }
23334 }
23335#endif
23336 }
23337}
23338
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339#endif /* FEAT_EVAL */
23340
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023342#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343
23344#ifdef WIN3264
23345/*
23346 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23347 */
23348static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23349static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23350static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23351
23352/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023353 * Get the short path (8.3) for the filename in "fnamep".
23354 * Only works for a valid file name.
23355 * When the path gets longer "fnamep" is changed and the allocated buffer
23356 * is put in "bufp".
23357 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23358 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359 */
23360 static int
23361get_short_pathname(fnamep, bufp, fnamelen)
23362 char_u **fnamep;
23363 char_u **bufp;
23364 int *fnamelen;
23365{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023366 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 char_u *newbuf;
23368
23369 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 l = GetShortPathName(*fnamep, *fnamep, len);
23371 if (l > len - 1)
23372 {
23373 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023374 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375 newbuf = vim_strnsave(*fnamep, l);
23376 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378
23379 vim_free(*bufp);
23380 *fnamep = *bufp = newbuf;
23381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023382 /* Really should always succeed, as the buffer is big enough. */
23383 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 }
23385
23386 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023387 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388}
23389
23390/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023391 * Get the short path (8.3) for the filename in "fname". The converted
23392 * path is returned in "bufp".
23393 *
23394 * Some of the directories specified in "fname" may not exist. This function
23395 * will shorten the existing directories at the beginning of the path and then
23396 * append the remaining non-existing path.
23397 *
23398 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023399 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023400 * bufp - Pointer to an allocated buffer for the filename.
23401 * fnamelen - Length of the filename pointed to by fname
23402 *
23403 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 */
23405 static int
23406shortpath_for_invalid_fname(fname, bufp, fnamelen)
23407 char_u **fname;
23408 char_u **bufp;
23409 int *fnamelen;
23410{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023411 char_u *short_fname, *save_fname, *pbuf_unused;
23412 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023414 int old_len, len;
23415 int new_len, sfx_len;
23416 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417
23418 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023419 old_len = *fnamelen;
23420 save_fname = vim_strnsave(*fname, old_len);
23421 pbuf_unused = NULL;
23422 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023424 endp = save_fname + old_len - 1; /* Find the end of the copy */
23425 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023427 /*
23428 * Try shortening the supplied path till it succeeds by removing one
23429 * directory at a time from the tail of the path.
23430 */
23431 len = 0;
23432 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023434 /* go back one path-separator */
23435 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23436 --endp;
23437 if (endp <= save_fname)
23438 break; /* processed the complete path */
23439
23440 /*
23441 * Replace the path separator with a NUL and try to shorten the
23442 * resulting path.
23443 */
23444 ch = *endp;
23445 *endp = 0;
23446 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023447 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023448 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23449 {
23450 retval = FAIL;
23451 goto theend;
23452 }
23453 *endp = ch; /* preserve the string */
23454
23455 if (len > 0)
23456 break; /* successfully shortened the path */
23457
23458 /* failed to shorten the path. Skip the path separator */
23459 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 }
23461
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023462 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023464 /*
23465 * Succeeded in shortening the path. Now concatenate the shortened
23466 * path with the remaining path at the tail.
23467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023469 /* Compute the length of the new path. */
23470 sfx_len = (int)(save_endp - endp) + 1;
23471 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023473 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023475 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023477 /* There is not enough space in the currently allocated string,
23478 * copy it to a buffer big enough. */
23479 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023481 {
23482 retval = FAIL;
23483 goto theend;
23484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 }
23486 else
23487 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023488 /* Transfer short_fname to the main buffer (it's big enough),
23489 * unless get_short_pathname() did its work in-place. */
23490 *fname = *bufp = save_fname;
23491 if (short_fname != save_fname)
23492 vim_strncpy(save_fname, short_fname, len);
23493 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023495
23496 /* concat the not-shortened part of the path */
23497 vim_strncpy(*fname + len, endp, sfx_len);
23498 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023500
23501theend:
23502 vim_free(pbuf_unused);
23503 vim_free(save_fname);
23504
23505 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506}
23507
23508/*
23509 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023510 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 */
23512 static int
23513shortpath_for_partial(fnamep, bufp, fnamelen)
23514 char_u **fnamep;
23515 char_u **bufp;
23516 int *fnamelen;
23517{
23518 int sepcount, len, tflen;
23519 char_u *p;
23520 char_u *pbuf, *tfname;
23521 int hasTilde;
23522
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023523 /* Count up the path separators from the RHS.. so we know which part
23524 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023526 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 if (vim_ispathsep(*p))
23528 ++sepcount;
23529
23530 /* Need full path first (use expand_env() to remove a "~/") */
23531 hasTilde = (**fnamep == '~');
23532 if (hasTilde)
23533 pbuf = tfname = expand_env_save(*fnamep);
23534 else
23535 pbuf = tfname = FullName_save(*fnamep, FALSE);
23536
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023537 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023539 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23540 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541
23542 if (len == 0)
23543 {
23544 /* Don't have a valid filename, so shorten the rest of the
23545 * path if we can. This CAN give us invalid 8.3 filenames, but
23546 * there's not a lot of point in guessing what it might be.
23547 */
23548 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023549 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 }
23552
23553 /* Count the paths backward to find the beginning of the desired string. */
23554 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023555 {
23556#ifdef FEAT_MBYTE
23557 if (has_mbyte)
23558 p -= mb_head_off(tfname, p);
23559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 if (vim_ispathsep(*p))
23561 {
23562 if (sepcount == 0 || (hasTilde && sepcount == 1))
23563 break;
23564 else
23565 sepcount --;
23566 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 if (hasTilde)
23569 {
23570 --p;
23571 if (p >= tfname)
23572 *p = '~';
23573 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 }
23576 else
23577 ++p;
23578
23579 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23580 vim_free(*bufp);
23581 *fnamelen = (int)STRLEN(p);
23582 *bufp = pbuf;
23583 *fnamep = p;
23584
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023585 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586}
23587#endif /* WIN3264 */
23588
23589/*
23590 * Adjust a filename, according to a string of modifiers.
23591 * *fnamep must be NUL terminated when called. When returning, the length is
23592 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023593 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 * When there is an error, *fnamep is set to NULL.
23595 */
23596 int
23597modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23598 char_u *src; /* string with modifiers */
23599 int *usedlen; /* characters after src that are used */
23600 char_u **fnamep; /* file name so far */
23601 char_u **bufp; /* buffer for allocated file name or NULL */
23602 int *fnamelen; /* length of fnamep */
23603{
23604 int valid = 0;
23605 char_u *tail;
23606 char_u *s, *p, *pbuf;
23607 char_u dirname[MAXPATHL];
23608 int c;
23609 int has_fullname = 0;
23610#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023611 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 int has_shortname = 0;
23613#endif
23614
23615repeat:
23616 /* ":p" - full path/file_name */
23617 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23618 {
23619 has_fullname = 1;
23620
23621 valid |= VALID_PATH;
23622 *usedlen += 2;
23623
23624 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23625 if ((*fnamep)[0] == '~'
23626#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23627 && ((*fnamep)[1] == '/'
23628# ifdef BACKSLASH_IN_FILENAME
23629 || (*fnamep)[1] == '\\'
23630# endif
23631 || (*fnamep)[1] == NUL)
23632
23633#endif
23634 )
23635 {
23636 *fnamep = expand_env_save(*fnamep);
23637 vim_free(*bufp); /* free any allocated file name */
23638 *bufp = *fnamep;
23639 if (*fnamep == NULL)
23640 return -1;
23641 }
23642
23643 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023644 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 {
23646 if (vim_ispathsep(*p)
23647 && p[1] == '.'
23648 && (p[2] == NUL
23649 || vim_ispathsep(p[2])
23650 || (p[2] == '.'
23651 && (p[3] == NUL || vim_ispathsep(p[3])))))
23652 break;
23653 }
23654
23655 /* FullName_save() is slow, don't use it when not needed. */
23656 if (*p != NUL || !vim_isAbsName(*fnamep))
23657 {
23658 *fnamep = FullName_save(*fnamep, *p != NUL);
23659 vim_free(*bufp); /* free any allocated file name */
23660 *bufp = *fnamep;
23661 if (*fnamep == NULL)
23662 return -1;
23663 }
23664
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023665#ifdef WIN3264
23666# if _WIN32_WINNT >= 0x0500
23667 if (vim_strchr(*fnamep, '~') != NULL)
23668 {
23669 /* Expand 8.3 filename to full path. Needed to make sure the same
23670 * file does not have two different names.
23671 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23672 p = alloc(_MAX_PATH + 1);
23673 if (p != NULL)
23674 {
23675 if (GetLongPathName(*fnamep, p, MAXPATHL))
23676 {
23677 vim_free(*bufp);
23678 *bufp = *fnamep = p;
23679 }
23680 else
23681 vim_free(p);
23682 }
23683 }
23684# endif
23685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 /* Append a path separator to a directory. */
23687 if (mch_isdir(*fnamep))
23688 {
23689 /* Make room for one or two extra characters. */
23690 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23691 vim_free(*bufp); /* free any allocated file name */
23692 *bufp = *fnamep;
23693 if (*fnamep == NULL)
23694 return -1;
23695 add_pathsep(*fnamep);
23696 }
23697 }
23698
23699 /* ":." - path relative to the current directory */
23700 /* ":~" - path relative to the home directory */
23701 /* ":8" - shortname path - postponed till after */
23702 while (src[*usedlen] == ':'
23703 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23704 {
23705 *usedlen += 2;
23706 if (c == '8')
23707 {
23708#ifdef WIN3264
23709 has_shortname = 1; /* Postpone this. */
23710#endif
23711 continue;
23712 }
23713 pbuf = NULL;
23714 /* Need full path first (use expand_env() to remove a "~/") */
23715 if (!has_fullname)
23716 {
23717 if (c == '.' && **fnamep == '~')
23718 p = pbuf = expand_env_save(*fnamep);
23719 else
23720 p = pbuf = FullName_save(*fnamep, FALSE);
23721 }
23722 else
23723 p = *fnamep;
23724
23725 has_fullname = 0;
23726
23727 if (p != NULL)
23728 {
23729 if (c == '.')
23730 {
23731 mch_dirname(dirname, MAXPATHL);
23732 s = shorten_fname(p, dirname);
23733 if (s != NULL)
23734 {
23735 *fnamep = s;
23736 if (pbuf != NULL)
23737 {
23738 vim_free(*bufp); /* free any allocated file name */
23739 *bufp = pbuf;
23740 pbuf = NULL;
23741 }
23742 }
23743 }
23744 else
23745 {
23746 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23747 /* Only replace it when it starts with '~' */
23748 if (*dirname == '~')
23749 {
23750 s = vim_strsave(dirname);
23751 if (s != NULL)
23752 {
23753 *fnamep = s;
23754 vim_free(*bufp);
23755 *bufp = s;
23756 }
23757 }
23758 }
23759 vim_free(pbuf);
23760 }
23761 }
23762
23763 tail = gettail(*fnamep);
23764 *fnamelen = (int)STRLEN(*fnamep);
23765
23766 /* ":h" - head, remove "/file_name", can be repeated */
23767 /* Don't remove the first "/" or "c:\" */
23768 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23769 {
23770 valid |= VALID_HEAD;
23771 *usedlen += 2;
23772 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023773 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023774 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 *fnamelen = (int)(tail - *fnamep);
23776#ifdef VMS
23777 if (*fnamelen > 0)
23778 *fnamelen += 1; /* the path separator is part of the path */
23779#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023780 if (*fnamelen == 0)
23781 {
23782 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23783 p = vim_strsave((char_u *)".");
23784 if (p == NULL)
23785 return -1;
23786 vim_free(*bufp);
23787 *bufp = *fnamep = tail = p;
23788 *fnamelen = 1;
23789 }
23790 else
23791 {
23792 while (tail > s && !after_pathsep(s, tail))
23793 mb_ptr_back(*fnamep, tail);
23794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 }
23796
23797 /* ":8" - shortname */
23798 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23799 {
23800 *usedlen += 2;
23801#ifdef WIN3264
23802 has_shortname = 1;
23803#endif
23804 }
23805
23806#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023807 /*
23808 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 */
23810 if (has_shortname)
23811 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023812 /* Copy the string if it is shortened by :h and when it wasn't copied
23813 * yet, because we are going to change it in place. Avoids changing
23814 * the buffer name for "%:8". */
23815 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
23817 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023818 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 return -1;
23820 vim_free(*bufp);
23821 *bufp = *fnamep = p;
23822 }
23823
23824 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023825 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 if (!has_fullname && !vim_isAbsName(*fnamep))
23827 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023828 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 return -1;
23830 }
23831 else
23832 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023833 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834
Bram Moolenaardc935552011-08-17 15:23:23 +020023835 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023837 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 return -1;
23839
23840 if (l == 0)
23841 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023842 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023844 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 return -1;
23846 }
23847 *fnamelen = l;
23848 }
23849 }
23850#endif /* WIN3264 */
23851
23852 /* ":t" - tail, just the basename */
23853 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23854 {
23855 *usedlen += 2;
23856 *fnamelen -= (int)(tail - *fnamep);
23857 *fnamep = tail;
23858 }
23859
23860 /* ":e" - extension, can be repeated */
23861 /* ":r" - root, without extension, can be repeated */
23862 while (src[*usedlen] == ':'
23863 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23864 {
23865 /* find a '.' in the tail:
23866 * - for second :e: before the current fname
23867 * - otherwise: The last '.'
23868 */
23869 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23870 s = *fnamep - 2;
23871 else
23872 s = *fnamep + *fnamelen - 1;
23873 for ( ; s > tail; --s)
23874 if (s[0] == '.')
23875 break;
23876 if (src[*usedlen + 1] == 'e') /* :e */
23877 {
23878 if (s > tail)
23879 {
23880 *fnamelen += (int)(*fnamep - (s + 1));
23881 *fnamep = s + 1;
23882#ifdef VMS
23883 /* cut version from the extension */
23884 s = *fnamep + *fnamelen - 1;
23885 for ( ; s > *fnamep; --s)
23886 if (s[0] == ';')
23887 break;
23888 if (s > *fnamep)
23889 *fnamelen = s - *fnamep;
23890#endif
23891 }
23892 else if (*fnamep <= tail)
23893 *fnamelen = 0;
23894 }
23895 else /* :r */
23896 {
23897 if (s > tail) /* remove one extension */
23898 *fnamelen = (int)(s - *fnamep);
23899 }
23900 *usedlen += 2;
23901 }
23902
23903 /* ":s?pat?foo?" - substitute */
23904 /* ":gs?pat?foo?" - global substitute */
23905 if (src[*usedlen] == ':'
23906 && (src[*usedlen + 1] == 's'
23907 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23908 {
23909 char_u *str;
23910 char_u *pat;
23911 char_u *sub;
23912 int sep;
23913 char_u *flags;
23914 int didit = FALSE;
23915
23916 flags = (char_u *)"";
23917 s = src + *usedlen + 2;
23918 if (src[*usedlen + 1] == 'g')
23919 {
23920 flags = (char_u *)"g";
23921 ++s;
23922 }
23923
23924 sep = *s++;
23925 if (sep)
23926 {
23927 /* find end of pattern */
23928 p = vim_strchr(s, sep);
23929 if (p != NULL)
23930 {
23931 pat = vim_strnsave(s, (int)(p - s));
23932 if (pat != NULL)
23933 {
23934 s = p + 1;
23935 /* find end of substitution */
23936 p = vim_strchr(s, sep);
23937 if (p != NULL)
23938 {
23939 sub = vim_strnsave(s, (int)(p - s));
23940 str = vim_strnsave(*fnamep, *fnamelen);
23941 if (sub != NULL && str != NULL)
23942 {
23943 *usedlen = (int)(p + 1 - src);
23944 s = do_string_sub(str, pat, sub, flags);
23945 if (s != NULL)
23946 {
23947 *fnamep = s;
23948 *fnamelen = (int)STRLEN(s);
23949 vim_free(*bufp);
23950 *bufp = s;
23951 didit = TRUE;
23952 }
23953 }
23954 vim_free(sub);
23955 vim_free(str);
23956 }
23957 vim_free(pat);
23958 }
23959 }
23960 /* after using ":s", repeat all the modifiers */
23961 if (didit)
23962 goto repeat;
23963 }
23964 }
23965
23966 return valid;
23967}
23968
23969/*
23970 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23971 * "flags" can be "g" to do a global substitute.
23972 * Returns an allocated string, NULL for error.
23973 */
23974 char_u *
23975do_string_sub(str, pat, sub, flags)
23976 char_u *str;
23977 char_u *pat;
23978 char_u *sub;
23979 char_u *flags;
23980{
23981 int sublen;
23982 regmatch_T regmatch;
23983 int i;
23984 int do_all;
23985 char_u *tail;
23986 garray_T ga;
23987 char_u *ret;
23988 char_u *save_cpo;
23989
23990 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23991 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023992 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993
23994 ga_init2(&ga, 1, 200);
23995
23996 do_all = (flags[0] == 'g');
23997
23998 regmatch.rm_ic = p_ic;
23999 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24000 if (regmatch.regprog != NULL)
24001 {
24002 tail = str;
24003 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24004 {
24005 /*
24006 * Get some space for a temporary buffer to do the substitution
24007 * into. It will contain:
24008 * - The text up to where the match is.
24009 * - The substituted text.
24010 * - The text after the match.
24011 */
24012 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24013 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24014 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24015 {
24016 ga_clear(&ga);
24017 break;
24018 }
24019
24020 /* copy the text up to where the match is */
24021 i = (int)(regmatch.startp[0] - tail);
24022 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24023 /* add the substituted text */
24024 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24025 + ga.ga_len + i, TRUE, TRUE, FALSE);
24026 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 /* avoid getting stuck on a match with an empty string */
24028 if (tail == regmatch.endp[0])
24029 {
24030 if (*tail == NUL)
24031 break;
24032 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24033 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 }
24035 else
24036 {
24037 tail = regmatch.endp[0];
24038 if (*tail == NUL)
24039 break;
24040 }
24041 if (!do_all)
24042 break;
24043 }
24044
24045 if (ga.ga_data != NULL)
24046 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24047
24048 vim_free(regmatch.regprog);
24049 }
24050
24051 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24052 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024053 if (p_cpo == empty_option)
24054 p_cpo = save_cpo;
24055 else
24056 /* Darn, evaluating {sub} expression changed the value. */
24057 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058
24059 return ret;
24060}
24061
24062#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */