blob: 46a1963de94b3176e9f11c1997793738b5edadae [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200460static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100671static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000691static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200692static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000775static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000787static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static 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 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
843
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000844/* Character used as separated in autoload function/variable names. */
845#define AUTOLOAD_CHAR '#'
846
Bram Moolenaar33570922005-01-25 22:26:29 +0000847/*
848 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000849 */
850 void
851eval_init()
852{
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 int i;
854 struct vimvar *p;
855
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200856 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
857 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200858 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000859 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000860 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000861
862 for (i = 0; i < VV_LEN; ++i)
863 {
864 p = &vimvars[i];
865 STRCPY(p->vv_di.di_key, p->vv_name);
866 if (p->vv_flags & VV_RO)
867 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
868 else if (p->vv_flags & VV_RO_SBX)
869 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
870 else
871 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000872
873 /* add to v: scope dict, unless the value is not always available */
874 if (p->vv_type != VAR_UNKNOWN)
875 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000877 /* add to compat scope dict */
878 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000880 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200881 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882
883#ifdef EBCDIC
884 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100885 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886 */
887 sortFunctions();
888#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000889}
890
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000891#if defined(EXITFREE) || defined(PROTO)
892 void
893eval_clear()
894{
895 int i;
896 struct vimvar *p;
897
898 for (i = 0; i < VV_LEN; ++i)
899 {
900 p = &vimvars[i];
901 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000903 vim_free(p->vv_str);
904 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000905 }
906 else if (p->vv_di.di_tv.v_type == VAR_LIST)
907 {
908 list_unref(p->vv_list);
909 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 }
912 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000913 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 hash_clear(&compat_hashtab);
915
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200917 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918
919 /* global variables */
920 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000921
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000922 /* autoloaded script names */
923 ga_clear_strings(&ga_loaded);
924
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200925 /* script-local variables */
926 for (i = 1; i <= ga_scripts.ga_len; ++i)
927 {
928 vars_clear(&SCRIPT_VARS(i));
929 vim_free(SCRIPT_SV(i));
930 }
931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1155 FALSE, FALSE, FALSE, FNE_CHECK_START);
1156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar4f688582007-07-24 12:34:30 +00001561#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1562 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565 * Uses argv[argc] for the function arguments. Only Number and String
1566 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001569 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001570call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 char_u *func;
1572 int argc;
1573 char_u **argv;
1574 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001575 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 long n;
1580 int len;
1581 int i;
1582 int doesrange;
1583 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001586 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 for (i = 0; i < argc; i++)
1591 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001592 /* Pass a NULL or empty argument as an empty string */
1593 if (argv[i] == NULL || *argv[i] == NUL)
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_STRING;
1596 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001597 continue;
1598 }
1599
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001600 if (str_arg_only)
1601 len = 0;
1602 else
1603 /* Recognize a number argument, the others must be strings. */
1604 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (len != 0 && len == (int)STRLEN(argv[i]))
1606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001607 argvars[i].v_type = VAR_NUMBER;
1608 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 else
1611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001612 argvars[i].v_type = VAR_STRING;
1613 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615 }
1616
1617 if (safe)
1618 {
1619 save_funccalp = save_funccal();
1620 ++sandbox;
1621 }
1622
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1624 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (safe)
1628 {
1629 --sandbox;
1630 restore_funccal(save_funccalp);
1631 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 vim_free(argvars);
1633
1634 if (ret == FAIL)
1635 clear_tv(rettv);
1636
1637 return ret;
1638}
1639
Bram Moolenaar4f688582007-07-24 12:34:30 +00001640# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 * Call vimL function "func" and return the result as a string.
1643 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 * Uses argv[argc] for the function arguments.
1645 */
1646 void *
1647call_func_retstr(func, argc, argv, safe)
1648 char_u *func;
1649 int argc;
1650 char_u **argv;
1651 int safe; /* use the sandbox */
1652{
1653 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001654 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001656 /* All arguments are passed as strings, no conversion to number. */
1657 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 return NULL;
1659
1660 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 return retval;
1663}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001664# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665
Bram Moolenaar4f688582007-07-24 12:34:30 +00001666# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001668 * Call vimL function "func" and return the result as a number.
1669 * Returns -1 when calling the function fails.
1670 * Uses argv[argc] for the function arguments.
1671 */
1672 long
1673call_func_retnr(func, argc, argv, safe)
1674 char_u *func;
1675 int argc;
1676 char_u **argv;
1677 int safe; /* use the sandbox */
1678{
1679 typval_T rettv;
1680 long retval;
1681
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001682 /* All arguments are passed as strings, no conversion to number. */
1683 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001684 return -1;
1685
1686 retval = get_tv_number_chk(&rettv, NULL);
1687 clear_tv(&rettv);
1688 return retval;
1689}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001690# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691
1692/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001693 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001695 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 */
1697 void *
1698call_func_retlist(func, argc, argv, safe)
1699 char_u *func;
1700 int argc;
1701 char_u **argv;
1702 int safe; /* use the sandbox */
1703{
1704 typval_T rettv;
1705
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001706 /* All arguments are passed as strings, no conversion to number. */
1707 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 return NULL;
1709
1710 if (rettv.v_type != VAR_LIST)
1711 {
1712 clear_tv(&rettv);
1713 return NULL;
1714 }
1715
1716 return rettv.vval.v_list;
1717}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718#endif
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
1722 * Save the current function call pointer, and set it to NULL.
1723 * Used when executing autocommands and for ":source".
1724 */
1725 void *
1726save_funccal()
1727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 current_funccal = NULL;
1731 return (void *)fc;
1732}
1733
1734 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735restore_funccal(vfc)
1736 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738 funccall_T *fc = (funccall_T *)vfc;
1739
1740 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741}
1742
Bram Moolenaar05159a02005-02-26 23:04:13 +00001743#if defined(FEAT_PROFILE) || defined(PROTO)
1744/*
1745 * Prepare profiling for entering a child or something else that is not
1746 * counted for the script/function itself.
1747 * Should always be called in pair with prof_child_exit().
1748 */
1749 void
1750prof_child_enter(tm)
1751 proftime_T *tm; /* place to store waittime */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 profile_start(&fc->prof_child);
1757 script_prof_save(tm);
1758}
1759
1760/*
1761 * Take care of time spent in a child.
1762 * Should always be called after prof_child_enter().
1763 */
1764 void
1765prof_child_exit(tm)
1766 proftime_T *tm; /* where waittime was stored */
1767{
1768 funccall_T *fc = current_funccal;
1769
1770 if (fc != NULL && fc->func->uf_profiling)
1771 {
1772 profile_end(&fc->prof_child);
1773 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1774 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1775 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1776 }
1777 script_prof_restore(tm);
1778}
1779#endif
1780
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef FEAT_FOLDING
1783/*
1784 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1785 * it in "*cp". Doesn't give error messages.
1786 */
1787 int
1788eval_foldexpr(arg, cp)
1789 char_u *arg;
1790 int *cp;
1791{
Bram Moolenaar33570922005-01-25 22:26:29 +00001792 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 int retval;
1794 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001795 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1796 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001799 if (use_sandbox)
1800 ++sandbox;
1801 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 retval = 0;
1805 else
1806 {
1807 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 if (tv.v_type == VAR_NUMBER)
1809 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001810 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 retval = 0;
1812 else
1813 {
1814 /* If the result is a string, check if there is a non-digit before
1815 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (!VIM_ISDIGIT(*s) && *s != '-')
1818 *cp = *s++;
1819 retval = atol((char *)s);
1820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001824 if (use_sandbox)
1825 --sandbox;
1826 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828 return retval;
1829}
1830#endif
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 * ":let" list all variable values
1834 * ":let var1 var2" list variable values
1835 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 * ":let var += expr" assignment command.
1837 * ":let var -= expr" assignment command.
1838 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 */
1841 void
1842ex_let(eap)
1843 exarg_T *eap;
1844{
1845 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 int var_count = 0;
1850 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001851 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaardb552d602006-03-23 22:59:57 +00001855 argend = skip_var_list(arg, &var_count, &semicolon);
1856 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001858 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1859 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001860 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001863 /*
1864 * ":let" without "=": list variables
1865 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 if (*arg == '[')
1867 EMSG(_(e_invarg));
1868 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 list_glob_vars(&first);
1875 list_buf_vars(&first);
1876 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001879#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 list_script_vars(&first);
1881 list_func_vars(&first);
1882 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 eap->nextcmd = check_nextcmd(arg);
1885 }
1886 else
1887 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 op[0] = '=';
1889 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001890 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 {
1892 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1893 op[0] = expr[-1]; /* +=, -= or .= */
1894 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (eap->skip)
1898 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (eap->skip)
1901 {
1902 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 --emsg_skip;
1905 }
1906 else if (i != FAIL)
1907 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 }
1913}
1914
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915/*
1916 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1917 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 * When "nextchars" is not NULL it points to a string with characters that
1919 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1920 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 * Returns OK or FAIL;
1922 */
1923 static int
1924ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1925 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 int copy; /* copy values from "tv", don't move */
1928 int semicolon; /* from skip_var_list() */
1929 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001930 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931{
1932 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 listitem_T *item;
1936 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937
1938 if (*arg != '[')
1939 {
1940 /*
1941 * ":let var = expr" or ":for var in list"
1942 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 return FAIL;
1945 return OK;
1946 }
1947
1948 /*
1949 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1950 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001951 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 {
1953 EMSG(_(e_listreq));
1954 return FAIL;
1955 }
1956
1957 i = list_len(l);
1958 if (semicolon == 0 && var_count < i)
1959 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001960 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 return FAIL;
1962 }
1963 if (var_count - semicolon > i)
1964 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001965 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 }
1968
1969 item = l->lv_first;
1970 while (*arg != ']')
1971 {
1972 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 item = item->li_next;
1975 if (arg == NULL)
1976 return FAIL;
1977
1978 arg = skipwhite(arg);
1979 if (*arg == ';')
1980 {
1981 /* Put the rest of the list (may be empty) in the var after ';'.
1982 * Create a new list for this. */
1983 l = list_alloc();
1984 if (l == NULL)
1985 return FAIL;
1986 while (item != NULL)
1987 {
1988 list_append_tv(l, &item->li_tv);
1989 item = item->li_next;
1990 }
1991
1992 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001993 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 ltv.vval.v_list = l;
1995 l->lv_refcount = 1;
1996
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1998 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 clear_tv(&ltv);
2000 if (arg == NULL)
2001 return FAIL;
2002 break;
2003 }
2004 else if (*arg != ',' && *arg != ']')
2005 {
2006 EMSG2(_(e_intern2), "ex_let_vars()");
2007 return FAIL;
2008 }
2009 }
2010
2011 return OK;
2012}
2013
2014/*
2015 * Skip over assignable variable "var" or list of variables "[var, var]".
2016 * Used for ":let varvar = expr" and ":for varvar in expr".
2017 * For "[var, var]" increment "*var_count" for each variable.
2018 * for "[var, var; var]" set "semicolon".
2019 * Return NULL for an error.
2020 */
2021 static char_u *
2022skip_var_list(arg, var_count, semicolon)
2023 char_u *arg;
2024 int *var_count;
2025 int *semicolon;
2026{
2027 char_u *p, *s;
2028
2029 if (*arg == '[')
2030 {
2031 /* "[var, var]": find the matching ']'. */
2032 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002033 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 {
2035 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2036 s = skip_var_one(p);
2037 if (s == p)
2038 {
2039 EMSG2(_(e_invarg2), p);
2040 return NULL;
2041 }
2042 ++*var_count;
2043
2044 p = skipwhite(s);
2045 if (*p == ']')
2046 break;
2047 else if (*p == ';')
2048 {
2049 if (*semicolon == 1)
2050 {
2051 EMSG(_("Double ; in list of variables"));
2052 return NULL;
2053 }
2054 *semicolon = 1;
2055 }
2056 else if (*p != ',')
2057 {
2058 EMSG2(_(e_invarg2), p);
2059 return NULL;
2060 }
2061 }
2062 return p + 1;
2063 }
2064 else
2065 return skip_var_one(arg);
2066}
2067
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002069 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002070 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 static char_u *
2073skip_var_one(arg)
2074 char_u *arg;
2075{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002076 if (*arg == '@' && arg[1] != NUL)
2077 return arg + 2;
2078 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2079 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002080}
2081
Bram Moolenaara7043832005-01-21 11:56:39 +00002082/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 * List variables for hashtab "ht" with prefix "prefix".
2084 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092{
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 hashitem_T *hi;
2094 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002095 int todo;
2096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002097 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2099 {
2100 if (!HASHITEM_EMPTY(hi))
2101 {
2102 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 di = HI2DI(hi);
2104 if (empty || di->di_tv.v_type != VAR_STRING
2105 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 }
2108 }
2109}
2110
2111/*
2112 * List global variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_glob_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002119}
2120
2121/*
2122 * List buffer variables.
2123 */
2124 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125list_buf_vars(first)
2126 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128 char_u numbuf[NUMBUFLEN];
2129
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2131 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132
2133 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2135 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136}
2137
2138/*
2139 * List window variables.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_win_vars(first)
2143 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2146 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002147}
2148
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149#ifdef FEAT_WINDOWS
2150/*
2151 * List tab page variables.
2152 */
2153 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_tab_vars(first)
2155 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2158 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159}
2160#endif
2161
Bram Moolenaara7043832005-01-21 11:56:39 +00002162/*
2163 * List Vim variables.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_vim_vars(first)
2167 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170}
2171
2172/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173 * List script-local variables, if there is a script.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_script_vars(first)
2177 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178{
2179 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2181 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182}
2183
2184/*
2185 * List function variables, if there is a function.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_func_vars(first)
2189 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002190{
2191 if (current_funccal != NULL)
2192 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194}
2195
2196/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 * List variables in "arg".
2198 */
2199 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 exarg_T *eap;
2202 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204{
2205 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 char_u *name_start;
2209 char_u *arg_subsc;
2210 char_u *tofree;
2211 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212
2213 while (!ends_excmd(*arg) && !got_int)
2214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002217 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2219 {
2220 emsg_severe = TRUE;
2221 EMSG(_(e_trailing));
2222 break;
2223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 /* get_name_len() takes care of expanding curly braces */
2228 name_start = name = arg;
2229 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2230 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 /* This is mainly to keep test 49 working: when expanding
2233 * curly braces fails overrule the exception error message. */
2234 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 emsg_severe = TRUE;
2237 EMSG2(_(e_invarg2), arg);
2238 break;
2239 }
2240 error = TRUE;
2241 }
2242 else
2243 {
2244 if (tofree != NULL)
2245 name = tofree;
2246 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 else
2249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 /* handle d.key, l[idx], f(expr) */
2251 arg_subsc = arg;
2252 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 case 'g': list_glob_vars(first); break;
2261 case 'b': list_buf_vars(first); break;
2262 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002265#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002266 case 'v': list_vim_vars(first); break;
2267 case 's': list_script_vars(first); break;
2268 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 default:
2270 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 }
2273 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 {
2275 char_u numbuf[NUMBUFLEN];
2276 char_u *tf;
2277 int c;
2278 char_u *s;
2279
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002280 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 c = *arg;
2282 *arg = NUL;
2283 list_one_var_a((char_u *)"",
2284 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002285 tv.v_type,
2286 s == NULL ? (char_u *)"" : s,
2287 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 *arg = c;
2289 vim_free(tf);
2290 }
2291 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
2294 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295
2296 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298
2299 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301
2302 return arg;
2303}
2304
2305/*
2306 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2307 * Returns a pointer to the char just after the var name.
2308 * Returns NULL if there is an error.
2309 */
2310 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002313 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002315 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317{
2318 int c1;
2319 char_u *name;
2320 char_u *p;
2321 char_u *arg_end = NULL;
2322 int len;
2323 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325
2326 /*
2327 * ":let $VAR = expr": Set environment variable.
2328 */
2329 if (*arg == '$')
2330 {
2331 /* Find the end of the name. */
2332 ++arg;
2333 name = arg;
2334 len = get_env_len(&arg);
2335 if (len == 0)
2336 EMSG2(_(e_invarg2), name - 1);
2337 else
2338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 if (op != NULL && (*op == '+' || *op == '-'))
2340 EMSG2(_(e_letwrong), op);
2341 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002344 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 {
2346 c1 = name[len];
2347 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 p = get_tv_string_chk(tv);
2349 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 {
2351 int mustfree = FALSE;
2352 char_u *s = vim_getenv(name, &mustfree);
2353
2354 if (s != NULL)
2355 {
2356 p = tofree = concat_str(s, p);
2357 if (mustfree)
2358 vim_free(s);
2359 }
2360 }
2361 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002364 if (STRICMP(name, "HOME") == 0)
2365 init_homedir();
2366 else if (didset_vim && STRICMP(name, "VIM") == 0)
2367 didset_vim = FALSE;
2368 else if (didset_vimruntime
2369 && STRICMP(name, "VIMRUNTIME") == 0)
2370 didset_vimruntime = FALSE;
2371 arg_end = arg;
2372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 }
2376 }
2377 }
2378
2379 /*
2380 * ":let &option = expr": Set option value.
2381 * ":let &l:option = expr": Set local option value.
2382 * ":let &g:option = expr": Set global option value.
2383 */
2384 else if (*arg == '&')
2385 {
2386 /* Find the end of the name. */
2387 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002388 if (p == NULL || (endchars != NULL
2389 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 EMSG(_(e_letunexp));
2391 else
2392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 long n;
2394 int opt_type;
2395 long numval;
2396 char_u *stringval = NULL;
2397 char_u *s;
2398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 c1 = *p;
2400 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401
2402 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 s = get_tv_string_chk(tv); /* != NULL if number or string */
2404 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 {
2406 opt_type = get_option_value(arg, &numval,
2407 &stringval, opt_flags);
2408 if ((opt_type == 1 && *op == '.')
2409 || (opt_type == 0 && *op != '.'))
2410 EMSG2(_(e_letwrong), op);
2411 else
2412 {
2413 if (opt_type == 1) /* number */
2414 {
2415 if (*op == '+')
2416 n = numval + n;
2417 else
2418 n = numval - n;
2419 }
2420 else if (opt_type == 0 && stringval != NULL) /* string */
2421 {
2422 s = concat_str(stringval, s);
2423 vim_free(stringval);
2424 stringval = s;
2425 }
2426 }
2427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002428 if (s != NULL)
2429 {
2430 set_option_value(arg, n, s, opt_flags);
2431 arg_end = p;
2432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 }
2436 }
2437
2438 /*
2439 * ":let @r = expr": Set register contents.
2440 */
2441 else if (*arg == '@')
2442 {
2443 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (op != NULL && (*op == '+' || *op == '-'))
2445 EMSG2(_(e_letwrong), op);
2446 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 EMSG(_(e_letunexp));
2449 else
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 char_u *s;
2453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 p = get_tv_string_chk(tv);
2455 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002457 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (s != NULL)
2459 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 vim_free(s);
2462 }
2463 }
2464 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 arg_end = arg + 1;
2468 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002469 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 }
2471 }
2472
2473 /*
2474 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002477 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002479 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2485 EMSG(_(e_letunexp));
2486 else
2487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 arg_end = p;
2490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 }
2494
2495 else
2496 EMSG2(_(e_invarg2), arg);
2497
2498 return arg_end;
2499}
2500
2501/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2503 */
2504 static int
2505check_changedtick(arg)
2506 char_u *arg;
2507{
2508 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2509 {
2510 EMSG2(_(e_readonlyvar), arg);
2511 return TRUE;
2512 }
2513 return FALSE;
2514}
2515
2516/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * Get an lval: variable, Dict item or List item that can be assigned a value
2518 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2519 * "name.key", "name.key[expr]" etc.
2520 * Indexing only works if "name" is an existing List or Dictionary.
2521 * "name" points to the start of the name.
2522 * If "rettv" is not NULL it points to the value to be assigned.
2523 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2524 * wrong; must end in space or cmd separator.
2525 *
2526 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002527 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 */
2530 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002531get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 typval_T *rettv;
2534 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 int unlet;
2536 int skip;
2537 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 char_u *expr_start, *expr_end;
2542 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 dictitem_T *v;
2544 typval_T var1;
2545 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554
2555 if (skip)
2556 {
2557 /* When skipping just find the end of the name. */
2558 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002559 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 }
2561
2562 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002563 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (expr_start != NULL)
2565 {
2566 /* Don't expand the name when we already know there is an error. */
2567 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2568 && *p != '[' && *p != '.')
2569 {
2570 EMSG(_(e_trailing));
2571 return NULL;
2572 }
2573
2574 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2575 if (lp->ll_exp_name == NULL)
2576 {
2577 /* Report an invalid expression in braces, unless the
2578 * expression evaluation has been cancelled due to an
2579 * aborting error, an interrupt, or an exception. */
2580 if (!aborting() && !quiet)
2581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002582 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 EMSG2(_(e_invarg2), name);
2584 return NULL;
2585 }
2586 }
2587 lp->ll_name = lp->ll_exp_name;
2588 }
2589 else
2590 lp->ll_name = name;
2591
2592 /* Without [idx] or .key we are done. */
2593 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2594 return p;
2595
2596 cc = *p;
2597 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002598 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (v == NULL && !quiet)
2600 EMSG2(_(e_undefvar), lp->ll_name);
2601 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 if (v == NULL)
2603 return NULL;
2604
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 /*
2606 * Loop until no more [idx] or .key is following.
2607 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002608 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2612 && !(lp->ll_tv->v_type == VAR_DICT
2613 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!quiet)
2616 EMSG(_("E689: Can only index a List or Dictionary"));
2617 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (!quiet)
2622 EMSG(_("E708: [:] must come last"));
2623 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 len = -1;
2627 if (*p == '.')
2628 {
2629 key = p + 1;
2630 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2631 ;
2632 if (len == 0)
2633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_(e_emptykey));
2636 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638 p = key + len;
2639 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 else
2641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 if (*p == ':')
2645 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 else
2647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 empty1 = FALSE;
2649 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 if (get_tv_string_chk(&var1) == NULL)
2652 {
2653 /* not a number or string */
2654 clear_tv(&var1);
2655 return NULL;
2656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658
2659 /* Optionally get the second index [ :expr]. */
2660 if (*p == ':')
2661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002665 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (rettv != NULL && (rettv->v_type != VAR_LIST
2671 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
2674 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (!empty1)
2676 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
2679 p = skipwhite(p + 1);
2680 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 else
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002691 if (get_tv_string_chk(&var2) == NULL)
2692 {
2693 /* not a number or string */
2694 if (!empty1)
2695 clear_tv(&var1);
2696 clear_tv(&var2);
2697 return NULL;
2698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
2708 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715
2716 /* Skip to past ']'. */
2717 ++p;
2718 }
2719
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
2722 if (len == -1)
2723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002725 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (*key == NUL)
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (!quiet)
2729 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 lp->ll_list = NULL;
2735 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002736 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002737
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002738 /* When assigning to a scope dictionary check that a function and
2739 * variable name is valid (only variable name unless it is l: or
2740 * g: dictionary). Disallow overwriting a builtin function. */
2741 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002742 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002743 int prevval;
2744 int wrong;
2745
2746 if (len != -1)
2747 {
2748 prevval = key[len];
2749 key[len] = NUL;
2750 }
2751 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2752 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002753 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002754 || !valid_varname(key);
2755 if (len != -1)
2756 key[len] = prevval;
2757 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 return NULL;
2759 }
2760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 /* Can't add "v:" variable. */
2764 if (lp->ll_dict == &vimvardict)
2765 {
2766 EMSG2(_(e_illvar), name);
2767 return NULL;
2768 }
2769
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002770 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002774 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (len == -1)
2776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (len == -1)
2784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 p = NULL;
2787 break;
2788 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002789 /* existing variable, need to check if it can be changed */
2790 else if (var_check_ro(lp->ll_di->di_flags, name))
2791 return NULL;
2792
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 if (len == -1)
2794 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 }
2797 else
2798 {
2799 /*
2800 * Get the number and item for the only or first index of the List.
2801 */
2802 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 else
2805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002806 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 clear_tv(&var1);
2808 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 lp->ll_list = lp->ll_tv->vval.v_list;
2811 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2812 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002814 if (lp->ll_n1 < 0)
2815 {
2816 lp->ll_n1 = 0;
2817 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2818 }
2819 }
2820 if (lp->ll_li == NULL)
2821 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002824 if (!quiet)
2825 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828
2829 /*
2830 * May need to find the item or absolute index for the second
2831 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 * When no index given: "lp->ll_empty2" is TRUE.
2833 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002837 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002843 {
2844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2852 if (lp->ll_n1 < 0)
2853 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2854 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002855 {
2856 if (!quiet)
2857 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002860 }
2861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return p;
2867}
2868
2869/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 */
2872 static void
2873clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875{
2876 vim_free(lp->ll_exp_name);
2877 vim_free(lp->ll_newkey);
2878}
2879
2880/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 */
2885 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002886set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002889 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892{
2893 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listitem_T *ri;
2895 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896
2897 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 cc = *endp;
2902 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 if (op != NULL && *op != '=')
2904 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002907 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002908 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002909 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 {
2911 if (tv_op(&tv, rettv, op) == OK)
2912 set_var(lp->ll_name, &tv, FALSE);
2913 clear_tv(&tv);
2914 }
2915 }
2916 else
2917 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 else if (tv_check_lock(lp->ll_newkey == NULL
2922 ? lp->ll_tv->v_lock
2923 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2924 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 else if (lp->ll_range)
2926 {
2927 /*
2928 * Assign the List values to the list items.
2929 */
2930 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2934 else
2935 {
2936 clear_tv(&lp->ll_li->li_tv);
2937 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2938 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 ri = ri->li_next;
2940 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2941 break;
2942 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002945 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 ri = NULL;
2948 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002949 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 lp->ll_li = lp->ll_li->li_next;
2952 ++lp->ll_n1;
2953 }
2954 if (ri != NULL)
2955 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 else if (lp->ll_empty2
2957 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 : lp->ll_n1 != lp->ll_n2)
2959 EMSG(_("E711: List value has not enough items"));
2960 }
2961 else
2962 {
2963 /*
2964 * Assign to a List or Dictionary item.
2965 */
2966 if (lp->ll_newkey != NULL)
2967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 if (op != NULL && *op != '=')
2969 {
2970 EMSG2(_(e_letwrong), op);
2971 return;
2972 }
2973
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002975 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 if (di == NULL)
2977 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002978 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2979 {
2980 vim_free(di);
2981 return;
2982 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 else if (op != NULL && *op != '=')
2986 {
2987 tv_op(lp->ll_tv, rettv, op);
2988 return;
2989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002990 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /*
2994 * Assign the value to the variable or list item.
2995 */
2996 if (copy)
2997 copy_tv(rettv, lp->ll_tv);
2998 else
2999 {
3000 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003001 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
3004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003005}
3006
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3009 * Returns OK or FAIL.
3010 */
3011 static int
3012tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003013 typval_T *tv1;
3014 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 char_u *op;
3016{
3017 long n;
3018 char_u numbuf[NUMBUFLEN];
3019 char_u *s;
3020
3021 /* Can't do anything with a Funcref or a Dict on the right. */
3022 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3023 {
3024 switch (tv1->v_type)
3025 {
3026 case VAR_DICT:
3027 case VAR_FUNC:
3028 break;
3029
3030 case VAR_LIST:
3031 if (*op != '+' || tv2->v_type != VAR_LIST)
3032 break;
3033 /* List += List */
3034 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3035 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3036 return OK;
3037
3038 case VAR_NUMBER:
3039 case VAR_STRING:
3040 if (tv2->v_type == VAR_LIST)
3041 break;
3042 if (*op == '+' || *op == '-')
3043 {
3044 /* nr += nr or nr -= nr*/
3045 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046#ifdef FEAT_FLOAT
3047 if (tv2->v_type == VAR_FLOAT)
3048 {
3049 float_T f = n;
3050
3051 if (*op == '+')
3052 f += tv2->vval.v_float;
3053 else
3054 f -= tv2->vval.v_float;
3055 clear_tv(tv1);
3056 tv1->v_type = VAR_FLOAT;
3057 tv1->vval.v_float = f;
3058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#endif
3061 {
3062 if (*op == '+')
3063 n += get_tv_number(tv2);
3064 else
3065 n -= get_tv_number(tv2);
3066 clear_tv(tv1);
3067 tv1->v_type = VAR_NUMBER;
3068 tv1->vval.v_number = n;
3069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 }
3071 else
3072 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 if (tv2->v_type == VAR_FLOAT)
3074 break;
3075
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 /* str .= str */
3077 s = get_tv_string(tv1);
3078 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3079 clear_tv(tv1);
3080 tv1->v_type = VAR_STRING;
3081 tv1->vval.v_string = s;
3082 }
3083 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084
3085#ifdef FEAT_FLOAT
3086 case VAR_FLOAT:
3087 {
3088 float_T f;
3089
3090 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3091 && tv2->v_type != VAR_NUMBER
3092 && tv2->v_type != VAR_STRING))
3093 break;
3094 if (tv2->v_type == VAR_FLOAT)
3095 f = tv2->vval.v_float;
3096 else
3097 f = get_tv_number(tv2);
3098 if (*op == '+')
3099 tv1->vval.v_float += f;
3100 else
3101 tv1->vval.v_float -= f;
3102 }
3103 return OK;
3104#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 }
3106 }
3107
3108 EMSG2(_(e_letwrong), op);
3109 return FAIL;
3110}
3111
3112/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113 * Add a watcher to a list.
3114 */
3115 static void
3116list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 list_T *l;
3118 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119{
3120 lw->lw_next = l->lv_watch;
3121 l->lv_watch = lw;
3122}
3123
3124/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003125 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126 * No warning when it isn't found...
3127 */
3128 static void
3129list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 list_T *l;
3131 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132{
Bram Moolenaar33570922005-01-25 22:26:29 +00003133 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134
3135 lwp = &l->lv_watch;
3136 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3137 {
3138 if (lw == lwrem)
3139 {
3140 *lwp = lw->lw_next;
3141 break;
3142 }
3143 lwp = &lw->lw_next;
3144 }
3145}
3146
3147/*
3148 * Just before removing an item from a list: advance watchers to the next
3149 * item.
3150 */
3151 static void
3152list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003153 list_T *l;
3154 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155{
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157
3158 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3159 if (lw->lw_item == item)
3160 lw->lw_item = item->li_next;
3161}
3162
3163/*
3164 * Evaluate the expression used in a ":for var in expr" command.
3165 * "arg" points to "var".
3166 * Set "*errp" to TRUE for an error, FALSE otherwise;
3167 * Return a pointer that holds the info. Null when there is an error.
3168 */
3169 void *
3170eval_for_line(arg, errp, nextcmdp, skip)
3171 char_u *arg;
3172 int *errp;
3173 char_u **nextcmdp;
3174 int skip;
3175{
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 typval_T tv;
3179 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180
3181 *errp = TRUE; /* default: there is an error */
3182
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 if (fi == NULL)
3185 return NULL;
3186
3187 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3188 if (expr == NULL)
3189 return fi;
3190
3191 expr = skipwhite(expr);
3192 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3193 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003194 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 return fi;
3196 }
3197
3198 if (skip)
3199 ++emsg_skip;
3200 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3201 {
3202 *errp = FALSE;
3203 if (!skip)
3204 {
3205 l = tv.vval.v_list;
3206 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003207 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003209 clear_tv(&tv);
3210 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 else
3212 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003213 /* No need to increment the refcount, it's already set for the
3214 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 fi->fi_list = l;
3216 list_add_watch(l, &fi->fi_lw);
3217 fi->fi_lw.lw_item = l->lv_first;
3218 }
3219 }
3220 }
3221 if (skip)
3222 --emsg_skip;
3223
3224 return fi;
3225}
3226
3227/*
3228 * Use the first item in a ":for" list. Advance to the next.
3229 * Assign the values to the variable (list). "arg" points to the first one.
3230 * Return TRUE when a valid item was found, FALSE when at end of list or
3231 * something wrong.
3232 */
3233 int
3234next_for_item(fi_void, arg)
3235 void *fi_void;
3236 char_u *arg;
3237{
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
3242 item = fi->fi_lw.lw_item;
3243 if (item == NULL)
3244 result = FALSE;
3245 else
3246 {
3247 fi->fi_lw.lw_item = item->li_next;
3248 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3249 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3250 }
3251 return result;
3252}
3253
3254/*
3255 * Free the structure used to store info used by ":for".
3256 */
3257 void
3258free_for_info(fi_void)
3259 void *fi_void;
3260{
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003263 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 list_unref(fi->fi_list);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 vim_free(fi);
3269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3272
3273 void
3274set_context_for_expression(xp, arg, cmdidx)
3275 expand_T *xp;
3276 char_u *arg;
3277 cmdidx_T cmdidx;
3278{
3279 int got_eq = FALSE;
3280 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (cmdidx == CMD_let)
3284 {
3285 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003286 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 {
3288 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003289 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 {
3291 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 if (vim_iswhite(*p))
3294 break;
3295 }
3296 return;
3297 }
3298 }
3299 else
3300 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3301 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 while ((xp->xp_pattern = vim_strpbrk(arg,
3303 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3304 {
3305 c = *xp->xp_pattern;
3306 if (c == '&')
3307 {
3308 c = xp->xp_pattern[1];
3309 if (c == '&')
3310 {
3311 ++xp->xp_pattern;
3312 xp->xp_context = cmdidx != CMD_let || got_eq
3313 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3314 }
3315 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003318 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3319 xp->xp_pattern += 2;
3320
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 else if (c == '$')
3324 {
3325 /* environment variable */
3326 xp->xp_context = EXPAND_ENV_VARS;
3327 }
3328 else if (c == '=')
3329 {
3330 got_eq = TRUE;
3331 xp->xp_context = EXPAND_EXPRESSION;
3332 }
3333 else if (c == '<'
3334 && xp->xp_context == EXPAND_FUNCTIONS
3335 && vim_strchr(xp->xp_pattern, '(') == NULL)
3336 {
3337 /* Function name can start with "<SNR>" */
3338 break;
3339 }
3340 else if (cmdidx != CMD_let || got_eq)
3341 {
3342 if (c == '"') /* string */
3343 {
3344 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3345 if (c == '\\' && xp->xp_pattern[1] != NUL)
3346 ++xp->xp_pattern;
3347 xp->xp_context = EXPAND_NOTHING;
3348 }
3349 else if (c == '\'') /* literal string */
3350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3353 /* skip */ ;
3354 xp->xp_context = EXPAND_NOTHING;
3355 }
3356 else if (c == '|')
3357 {
3358 if (xp->xp_pattern[1] == '|')
3359 {
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_EXPRESSION;
3362 }
3363 else
3364 xp->xp_context = EXPAND_COMMANDS;
3365 }
3366 else
3367 xp->xp_context = EXPAND_EXPRESSION;
3368 }
3369 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 /* Doesn't look like something valid, expand as an expression
3371 * anyway. */
3372 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 arg = xp->xp_pattern;
3374 if (*arg != NUL)
3375 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3376 /* skip */ ;
3377 }
3378 xp->xp_pattern = arg;
3379}
3380
3381#endif /* FEAT_CMDL_COMPL */
3382
3383/*
3384 * ":1,25call func(arg1, arg2)" function call.
3385 */
3386 void
3387ex_call(eap)
3388 exarg_T *eap;
3389{
3390 char_u *arg = eap->arg;
3391 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003393 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 linenr_T lnum;
3397 int doesrange;
3398 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003401 if (eap->skip)
3402 {
3403 /* trans_function_name() doesn't work well when skipping, use eval0()
3404 * instead to skip to any following command, e.g. for:
3405 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003406 ++emsg_skip;
3407 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3408 clear_tv(&rettv);
3409 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003410 return;
3411 }
3412
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003413 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003414 if (fudi.fd_newkey != NULL)
3415 {
3416 /* Still need to give an error message for missing key. */
3417 EMSG2(_(e_dictkey), fudi.fd_newkey);
3418 vim_free(fudi.fd_newkey);
3419 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 if (tofree == NULL)
3421 return;
3422
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003423 /* Increase refcount on dictionary, it could get deleted when evaluating
3424 * the arguments. */
3425 if (fudi.fd_dict != NULL)
3426 ++fudi.fd_dict->dv_refcount;
3427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003429 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003430 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar532c7802005-01-27 14:44:31 +00003432 /* Skip white space to allow ":call func ()". Not good, but required for
3433 * backward compatibility. */
3434 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 if (*startarg != '(')
3438 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003439 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 goto end;
3441 }
3442
3443 /*
3444 * When skipping, evaluate the function once, to find the end of the
3445 * arguments.
3446 * When the function takes a range, this is discovered after the first
3447 * call, and the loop is broken.
3448 */
3449 if (eap->skip)
3450 {
3451 ++emsg_skip;
3452 lnum = eap->line2; /* do it once, also with an invalid range */
3453 }
3454 else
3455 lnum = eap->line1;
3456 for ( ; lnum <= eap->line2; ++lnum)
3457 {
3458 if (!eap->skip && eap->addr_count > 0)
3459 {
3460 curwin->w_cursor.lnum = lnum;
3461 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003462#ifdef FEAT_VIRTUALEDIT
3463 curwin->w_cursor.coladd = 0;
3464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 eap->line1, eap->line2, &doesrange,
3469 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 failed = TRUE;
3472 break;
3473 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003474
3475 /* Handle a function returning a Funcref, Dictionary or List. */
3476 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3477 {
3478 failed = TRUE;
3479 break;
3480 }
3481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (doesrange || eap->skip)
3484 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003487 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003489 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (aborting())
3491 break;
3492 }
3493 if (eap->skip)
3494 --emsg_skip;
3495
3496 if (!failed)
3497 {
3498 /* Check for trailing illegal characters and a following command. */
3499 if (!ends_excmd(*arg))
3500 {
3501 emsg_severe = TRUE;
3502 EMSG(_(e_trailing));
3503 }
3504 else
3505 eap->nextcmd = check_nextcmd(arg);
3506 }
3507
3508end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003509 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003510 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511}
3512
3513/*
3514 * ":unlet[!] var1 ... " command.
3515 */
3516 void
3517ex_unlet(eap)
3518 exarg_T *eap;
3519{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003520 ex_unletlock(eap, eap->arg, 0);
3521}
3522
3523/*
3524 * ":lockvar" and ":unlockvar" commands
3525 */
3526 void
3527ex_lockvar(eap)
3528 exarg_T *eap;
3529{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 int deep = 2;
3532
3533 if (eap->forceit)
3534 deep = -1;
3535 else if (vim_isdigit(*arg))
3536 {
3537 deep = getdigits(&arg);
3538 arg = skipwhite(arg);
3539 }
3540
3541 ex_unletlock(eap, arg, deep);
3542}
3543
3544/*
3545 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3546 */
3547 static void
3548ex_unletlock(eap, argstart, deep)
3549 exarg_T *eap;
3550 char_u *argstart;
3551 int deep;
3552{
3553 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003556 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558 do
3559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003561 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3562 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 if (lv.ll_name == NULL)
3564 error = TRUE; /* error but continue parsing */
3565 if (name_end == NULL || (!vim_iswhite(*name_end)
3566 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (name_end != NULL)
3569 {
3570 emsg_severe = TRUE;
3571 EMSG(_(e_trailing));
3572 }
3573 if (!(eap->skip || error))
3574 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576 }
3577
3578 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 {
3580 if (eap->cmdidx == CMD_unlet)
3581 {
3582 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3583 error = TRUE;
3584 }
3585 else
3586 {
3587 if (do_lock_var(&lv, name_end, deep,
3588 eap->cmdidx == CMD_lockvar) == FAIL)
3589 error = TRUE;
3590 }
3591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 if (!eap->skip)
3594 clear_lval(&lv);
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 arg = skipwhite(name_end);
3597 } while (!ends_excmd(*arg));
3598
3599 eap->nextcmd = check_nextcmd(arg);
3600}
3601
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003605 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 int forceit;
3607{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 int ret = OK;
3609 int cc;
3610
3611 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 cc = *name_end;
3614 *name_end = NUL;
3615
3616 /* Normal name or expanded name. */
3617 if (check_changedtick(lp->ll_name))
3618 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3624 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 else if (lp->ll_range)
3626 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003627 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628
3629 /* Delete a range of List items. */
3630 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3631 {
3632 li = lp->ll_li->li_next;
3633 listitem_remove(lp->ll_list, lp->ll_li);
3634 lp->ll_li = li;
3635 ++lp->ll_n1;
3636 }
3637 }
3638 else
3639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 /* unlet a List item. */
3642 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003645 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 }
3647
3648 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649}
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651/*
3652 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
3655 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 hashtab_T *ht;
3661 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003662 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003663 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 ht = find_var_ht(name, &varname);
3666 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 hi = hash_find(ht, varname);
3669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003671 di = HI2DI(hi);
3672 if (var_check_fixed(di->di_flags, name)
3673 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 return FAIL;
3675 delete_var(ht, hi);
3676 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 if (forceit)
3680 return OK;
3681 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 return FAIL;
3683}
3684
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685/*
3686 * Lock or unlock variable indicated by "lp".
3687 * "deep" is the levels to go (-1 for unlimited);
3688 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3689 */
3690 static int
3691do_lock_var(lp, name_end, deep, lock)
3692 lval_T *lp;
3693 char_u *name_end;
3694 int deep;
3695 int lock;
3696{
3697 int ret = OK;
3698 int cc;
3699 dictitem_T *di;
3700
3701 if (deep == 0) /* nothing to do */
3702 return OK;
3703
3704 if (lp->ll_tv == NULL)
3705 {
3706 cc = *name_end;
3707 *name_end = NUL;
3708
3709 /* Normal name or expanded name. */
3710 if (check_changedtick(lp->ll_name))
3711 ret = FAIL;
3712 else
3713 {
3714 di = find_var(lp->ll_name, NULL);
3715 if (di == NULL)
3716 ret = FAIL;
3717 else
3718 {
3719 if (lock)
3720 di->di_flags |= DI_FLAGS_LOCK;
3721 else
3722 di->di_flags &= ~DI_FLAGS_LOCK;
3723 item_lock(&di->di_tv, deep, lock);
3724 }
3725 }
3726 *name_end = cc;
3727 }
3728 else if (lp->ll_range)
3729 {
3730 listitem_T *li = lp->ll_li;
3731
3732 /* (un)lock a range of List items. */
3733 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3734 {
3735 item_lock(&li->li_tv, deep, lock);
3736 li = li->li_next;
3737 ++lp->ll_n1;
3738 }
3739 }
3740 else if (lp->ll_list != NULL)
3741 /* (un)lock a List item. */
3742 item_lock(&lp->ll_li->li_tv, deep, lock);
3743 else
3744 /* un(lock) a Dictionary item. */
3745 item_lock(&lp->ll_di->di_tv, deep, lock);
3746
3747 return ret;
3748}
3749
3750/*
3751 * Lock or unlock an item. "deep" is nr of levels to go.
3752 */
3753 static void
3754item_lock(tv, deep, lock)
3755 typval_T *tv;
3756 int deep;
3757 int lock;
3758{
3759 static int recurse = 0;
3760 list_T *l;
3761 listitem_T *li;
3762 dict_T *d;
3763 hashitem_T *hi;
3764 int todo;
3765
3766 if (recurse >= DICT_MAXNEST)
3767 {
3768 EMSG(_("E743: variable nested too deep for (un)lock"));
3769 return;
3770 }
3771 if (deep == 0)
3772 return;
3773 ++recurse;
3774
3775 /* lock/unlock the item itself */
3776 if (lock)
3777 tv->v_lock |= VAR_LOCKED;
3778 else
3779 tv->v_lock &= ~VAR_LOCKED;
3780
3781 switch (tv->v_type)
3782 {
3783 case VAR_LIST:
3784 if ((l = tv->vval.v_list) != NULL)
3785 {
3786 if (lock)
3787 l->lv_lock |= VAR_LOCKED;
3788 else
3789 l->lv_lock &= ~VAR_LOCKED;
3790 if (deep < 0 || deep > 1)
3791 /* recursive: lock/unlock the items the List contains */
3792 for (li = l->lv_first; li != NULL; li = li->li_next)
3793 item_lock(&li->li_tv, deep - 1, lock);
3794 }
3795 break;
3796 case VAR_DICT:
3797 if ((d = tv->vval.v_dict) != NULL)
3798 {
3799 if (lock)
3800 d->dv_lock |= VAR_LOCKED;
3801 else
3802 d->dv_lock &= ~VAR_LOCKED;
3803 if (deep < 0 || deep > 1)
3804 {
3805 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003806 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3808 {
3809 if (!HASHITEM_EMPTY(hi))
3810 {
3811 --todo;
3812 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3813 }
3814 }
3815 }
3816 }
3817 }
3818 --recurse;
3819}
3820
Bram Moolenaara40058a2005-07-11 22:42:07 +00003821/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003822 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3823 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003824 */
3825 static int
3826tv_islocked(tv)
3827 typval_T *tv;
3828{
3829 return (tv->v_lock & VAR_LOCKED)
3830 || (tv->v_type == VAR_LIST
3831 && tv->vval.v_list != NULL
3832 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3833 || (tv->v_type == VAR_DICT
3834 && tv->vval.v_dict != NULL
3835 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3836}
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3839/*
3840 * Delete all "menutrans_" variables.
3841 */
3842 void
3843del_menutrans_vars()
3844{
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003849 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 {
3852 if (!HASHITEM_EMPTY(hi))
3853 {
3854 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3856 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 }
3858 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860}
3861#endif
3862
3863#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3864
3865/*
3866 * Local string buffer for the next two functions to store a variable name
3867 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3868 * get_user_var_name().
3869 */
3870
3871static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3872
3873static char_u *varnamebuf = NULL;
3874static int varnamebuflen = 0;
3875
3876/*
3877 * Function to concatenate a prefix and a variable name.
3878 */
3879 static char_u *
3880cat_prefix_varname(prefix, name)
3881 int prefix;
3882 char_u *name;
3883{
3884 int len;
3885
3886 len = (int)STRLEN(name) + 3;
3887 if (len > varnamebuflen)
3888 {
3889 vim_free(varnamebuf);
3890 len += 10; /* some additional space */
3891 varnamebuf = alloc(len);
3892 if (varnamebuf == NULL)
3893 {
3894 varnamebuflen = 0;
3895 return NULL;
3896 }
3897 varnamebuflen = len;
3898 }
3899 *varnamebuf = prefix;
3900 varnamebuf[1] = ':';
3901 STRCPY(varnamebuf + 2, name);
3902 return varnamebuf;
3903}
3904
3905/*
3906 * Function given to ExpandGeneric() to obtain the list of user defined
3907 * (global/buffer/window/built-in) variable names.
3908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 char_u *
3910get_user_var_name(xp, idx)
3911 expand_T *xp;
3912 int idx;
3913{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003914 static long_u gdone;
3915 static long_u bdone;
3916 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917#ifdef FEAT_WINDOWS
3918 static long_u tdone;
3919#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003920 static int vidx;
3921 static hashitem_T *hi;
3922 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003925 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003927#ifdef FEAT_WINDOWS
3928 tdone = 0;
3929#endif
3930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003931
3932 /* Global variables */
3933 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003937 else
3938 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 while (HASHITEM_EMPTY(hi))
3940 ++hi;
3941 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3942 return cat_prefix_varname('g', hi->hi_key);
3943 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* b: variables */
3947 ht = &curbuf->b_vars.dv_hashtab;
3948 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003952 else
3953 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 while (HASHITEM_EMPTY(hi))
3955 ++hi;
3956 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 return (char_u *)"b:changedtick";
3962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963
3964 /* w: variables */
3965 ht = &curwin->w_vars.dv_hashtab;
3966 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003968 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003970 else
3971 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 while (HASHITEM_EMPTY(hi))
3973 ++hi;
3974 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003976
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977#ifdef FEAT_WINDOWS
3978 /* t: variables */
3979 ht = &curtab->tp_vars.dv_hashtab;
3980 if (tdone < ht->ht_used)
3981 {
3982 if (tdone++ == 0)
3983 hi = ht->ht_array;
3984 else
3985 ++hi;
3986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('t', hi->hi_key);
3989 }
3990#endif
3991
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 /* v: variables */
3993 if (vidx < VV_LEN)
3994 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 vim_free(varnamebuf);
3997 varnamebuf = NULL;
3998 varnamebuflen = 0;
3999 return NULL;
4000}
4001
4002#endif /* FEAT_CMDL_COMPL */
4003
4004/*
4005 * types for expressions.
4006 */
4007typedef enum
4008{
4009 TYPE_UNKNOWN = 0
4010 , TYPE_EQUAL /* == */
4011 , TYPE_NEQUAL /* != */
4012 , TYPE_GREATER /* > */
4013 , TYPE_GEQUAL /* >= */
4014 , TYPE_SMALLER /* < */
4015 , TYPE_SEQUAL /* <= */
4016 , TYPE_MATCH /* =~ */
4017 , TYPE_NOMATCH /* !~ */
4018} exptype_T;
4019
4020/*
4021 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4024 */
4025
4026/*
4027 * Handle zero level expression.
4028 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004030 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 * Return OK or FAIL.
4032 */
4033 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 char_u **nextcmd;
4038 int evaluate;
4039{
4040 int ret;
4041 char_u *p;
4042
4043 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (ret == FAIL || !ends_excmd(*p))
4046 {
4047 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 /*
4050 * Report the invalid expression unless the expression evaluation has
4051 * been cancelled due to an aborting error, an interrupt, or an
4052 * exception.
4053 */
4054 if (!aborting())
4055 EMSG2(_(e_invexpr2), arg);
4056 ret = FAIL;
4057 }
4058 if (nextcmd != NULL)
4059 *nextcmd = check_nextcmd(p);
4060
4061 return ret;
4062}
4063
4064/*
4065 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004066 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 *
4068 * "arg" must point to the first non-white of the expression.
4069 * "arg" is advanced to the next non-white after the recognized expression.
4070 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004071 * Note: "rettv.v_lock" is not set.
4072 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 * Return OK or FAIL.
4074 */
4075 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 int evaluate;
4080{
4081 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Get the first variable.
4086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 if ((*arg)[0] == '?')
4091 {
4092 result = FALSE;
4093 if (evaluate)
4094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 int error = FALSE;
4096
4097 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 if (error)
4101 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103
4104 /*
4105 * Get the second variable.
4106 */
4107 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110
4111 /*
4112 * Check for the ":".
4113 */
4114 if ((*arg)[0] != ':')
4115 {
4116 EMSG(_("E109: Missing ':' after '?'"));
4117 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 return FAIL;
4120 }
4121
4122 /*
4123 * Get the third variable.
4124 */
4125 *arg = skipwhite(*arg + 1);
4126 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4127 {
4128 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131 }
4132 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135
4136 return OK;
4137}
4138
4139/*
4140 * Handle first level expression:
4141 * expr2 || expr2 || expr2 logical OR
4142 *
4143 * "arg" must point to the first non-white of the expression.
4144 * "arg" is advanced to the next non-white after the recognized expression.
4145 *
4146 * Return OK or FAIL.
4147 */
4148 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 int evaluate;
4153{
Bram Moolenaar33570922005-01-25 22:26:29 +00004154 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 long result;
4156 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 /*
4160 * Get the first variable.
4161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 return FAIL;
4164
4165 /*
4166 * Repeat until there is no following "||".
4167 */
4168 first = TRUE;
4169 result = FALSE;
4170 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4171 {
4172 if (evaluate && first)
4173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 first = FALSE;
4180 }
4181
4182 /*
4183 * Get the second variable.
4184 */
4185 *arg = skipwhite(*arg + 2);
4186 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4187 return FAIL;
4188
4189 /*
4190 * Compute the result.
4191 */
4192 if (evaluate && !result)
4193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 if (error)
4198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 if (evaluate)
4201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 rettv->v_type = VAR_NUMBER;
4203 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 }
4206
4207 return OK;
4208}
4209
4210/*
4211 * Handle second level expression:
4212 * expr3 && expr3 && expr3 logical AND
4213 *
4214 * "arg" must point to the first non-white of the expression.
4215 * "arg" is advanced to the next non-white after the recognized expression.
4216 *
4217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 int evaluate;
4224{
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 long result;
4227 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004228 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 /*
4231 * Get the first variable.
4232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235
4236 /*
4237 * Repeat until there is no following "&&".
4238 */
4239 first = TRUE;
4240 result = TRUE;
4241 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4242 {
4243 if (evaluate && first)
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (error)
4249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 first = FALSE;
4251 }
4252
4253 /*
4254 * Get the second variable.
4255 */
4256 *arg = skipwhite(*arg + 2);
4257 if (eval4(arg, &var2, evaluate && result) == FAIL)
4258 return FAIL;
4259
4260 /*
4261 * Compute the result.
4262 */
4263 if (evaluate && result)
4264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 if (evaluate)
4272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 rettv->v_type = VAR_NUMBER;
4274 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 }
4277
4278 return OK;
4279}
4280
4281/*
4282 * Handle third level expression:
4283 * var1 == var2
4284 * var1 =~ var2
4285 * var1 != var2
4286 * var1 !~ var2
4287 * var1 > var2
4288 * var1 >= var2
4289 * var1 < var2
4290 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004291 * var1 is var2
4292 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 *
4294 * "arg" must point to the first non-white of the expression.
4295 * "arg" is advanced to the next non-white after the recognized expression.
4296 *
4297 * Return OK or FAIL.
4298 */
4299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int evaluate;
4304{
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u *p;
4307 int i;
4308 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004309 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 int len = 2;
4311 long n1, n2;
4312 char_u *s1, *s2;
4313 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4314 regmatch_T regmatch;
4315 int ic;
4316 char_u *save_cpo;
4317
4318 /*
4319 * Get the first variable.
4320 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 return FAIL;
4323
4324 p = *arg;
4325 switch (p[0])
4326 {
4327 case '=': if (p[1] == '=')
4328 type = TYPE_EQUAL;
4329 else if (p[1] == '~')
4330 type = TYPE_MATCH;
4331 break;
4332 case '!': if (p[1] == '=')
4333 type = TYPE_NEQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_NOMATCH;
4336 break;
4337 case '>': if (p[1] != '=')
4338 {
4339 type = TYPE_GREATER;
4340 len = 1;
4341 }
4342 else
4343 type = TYPE_GEQUAL;
4344 break;
4345 case '<': if (p[1] != '=')
4346 {
4347 type = TYPE_SMALLER;
4348 len = 1;
4349 }
4350 else
4351 type = TYPE_SEQUAL;
4352 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004353 case 'i': if (p[1] == 's')
4354 {
4355 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4356 len = 5;
4357 if (!vim_isIDc(p[len]))
4358 {
4359 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4360 type_is = TRUE;
4361 }
4362 }
4363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365
4366 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004367 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 */
4369 if (type != TYPE_UNKNOWN)
4370 {
4371 /* extra question mark appended: ignore case */
4372 if (p[len] == '?')
4373 {
4374 ic = TRUE;
4375 ++len;
4376 }
4377 /* extra '#' appended: match case */
4378 else if (p[len] == '#')
4379 {
4380 ic = FALSE;
4381 ++len;
4382 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004383 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 else
4385 ic = p_ic;
4386
4387 /*
4388 * Get the second variable.
4389 */
4390 *arg = skipwhite(p + len);
4391 if (eval5(arg, &var2, evaluate) == FAIL)
4392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
4395 }
4396
4397 if (evaluate)
4398 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 if (type_is && rettv->v_type != var2.v_type)
4400 {
4401 /* For "is" a different type always means FALSE, for "notis"
4402 * it means TRUE. */
4403 n1 = (type == TYPE_NEQUAL);
4404 }
4405 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4406 {
4407 if (type_is)
4408 {
4409 n1 = (rettv->v_type == var2.v_type
4410 && rettv->vval.v_list == var2.vval.v_list);
4411 if (type == TYPE_NEQUAL)
4412 n1 = !n1;
4413 }
4414 else if (rettv->v_type != var2.v_type
4415 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4416 {
4417 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004418 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004419 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004420 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004421 clear_tv(rettv);
4422 clear_tv(&var2);
4423 return FAIL;
4424 }
4425 else
4426 {
4427 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004428 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4429 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 if (type == TYPE_NEQUAL)
4431 n1 = !n1;
4432 }
4433 }
4434
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004435 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4436 {
4437 if (type_is)
4438 {
4439 n1 = (rettv->v_type == var2.v_type
4440 && rettv->vval.v_dict == var2.vval.v_dict);
4441 if (type == TYPE_NEQUAL)
4442 n1 = !n1;
4443 }
4444 else if (rettv->v_type != var2.v_type
4445 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4446 {
4447 if (rettv->v_type != var2.v_type)
4448 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4449 else
4450 EMSG(_("E736: Invalid operation for Dictionary"));
4451 clear_tv(rettv);
4452 clear_tv(&var2);
4453 return FAIL;
4454 }
4455 else
4456 {
4457 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004458 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4459 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004460 if (type == TYPE_NEQUAL)
4461 n1 = !n1;
4462 }
4463 }
4464
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4466 {
4467 if (rettv->v_type != var2.v_type
4468 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4469 {
4470 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004471 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004472 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004473 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 clear_tv(rettv);
4475 clear_tv(&var2);
4476 return FAIL;
4477 }
4478 else
4479 {
4480 /* Compare two Funcrefs for being equal or unequal. */
4481 if (rettv->vval.v_string == NULL
4482 || var2.vval.v_string == NULL)
4483 n1 = FALSE;
4484 else
4485 n1 = STRCMP(rettv->vval.v_string,
4486 var2.vval.v_string) == 0;
4487 if (type == TYPE_NEQUAL)
4488 n1 = !n1;
4489 }
4490 }
4491
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004492#ifdef FEAT_FLOAT
4493 /*
4494 * If one of the two variables is a float, compare as a float.
4495 * When using "=~" or "!~", always compare as string.
4496 */
4497 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4498 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4499 {
4500 float_T f1, f2;
4501
4502 if (rettv->v_type == VAR_FLOAT)
4503 f1 = rettv->vval.v_float;
4504 else
4505 f1 = get_tv_number(rettv);
4506 if (var2.v_type == VAR_FLOAT)
4507 f2 = var2.vval.v_float;
4508 else
4509 f2 = get_tv_number(&var2);
4510 n1 = FALSE;
4511 switch (type)
4512 {
4513 case TYPE_EQUAL: n1 = (f1 == f2); break;
4514 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4515 case TYPE_GREATER: n1 = (f1 > f2); break;
4516 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4517 case TYPE_SMALLER: n1 = (f1 < f2); break;
4518 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4519 case TYPE_UNKNOWN:
4520 case TYPE_MATCH:
4521 case TYPE_NOMATCH: break; /* avoid gcc warning */
4522 }
4523 }
4524#endif
4525
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 /*
4527 * If one of the two variables is a number, compare as a number.
4528 * When using "=~" or "!~", always compare as string.
4529 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004530 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533 n1 = get_tv_number(rettv);
4534 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 switch (type)
4536 {
4537 case TYPE_EQUAL: n1 = (n1 == n2); break;
4538 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4539 case TYPE_GREATER: n1 = (n1 > n2); break;
4540 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4541 case TYPE_SMALLER: n1 = (n1 < n2); break;
4542 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4543 case TYPE_UNKNOWN:
4544 case TYPE_MATCH:
4545 case TYPE_NOMATCH: break; /* avoid gcc warning */
4546 }
4547 }
4548 else
4549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004550 s1 = get_tv_string_buf(rettv, buf1);
4551 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4553 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4554 else
4555 i = 0;
4556 n1 = FALSE;
4557 switch (type)
4558 {
4559 case TYPE_EQUAL: n1 = (i == 0); break;
4560 case TYPE_NEQUAL: n1 = (i != 0); break;
4561 case TYPE_GREATER: n1 = (i > 0); break;
4562 case TYPE_GEQUAL: n1 = (i >= 0); break;
4563 case TYPE_SMALLER: n1 = (i < 0); break;
4564 case TYPE_SEQUAL: n1 = (i <= 0); break;
4565
4566 case TYPE_MATCH:
4567 case TYPE_NOMATCH:
4568 /* avoid 'l' flag in 'cpoptions' */
4569 save_cpo = p_cpo;
4570 p_cpo = (char_u *)"";
4571 regmatch.regprog = vim_regcomp(s2,
4572 RE_MAGIC + RE_STRING);
4573 regmatch.rm_ic = ic;
4574 if (regmatch.regprog != NULL)
4575 {
4576 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4577 vim_free(regmatch.regprog);
4578 if (type == TYPE_NOMATCH)
4579 n1 = !n1;
4580 }
4581 p_cpo = save_cpo;
4582 break;
4583
4584 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4585 }
4586 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 clear_tv(rettv);
4588 clear_tv(&var2);
4589 rettv->v_type = VAR_NUMBER;
4590 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 }
4593
4594 return OK;
4595}
4596
4597/*
4598 * Handle fourth level expression:
4599 * + number addition
4600 * - number subtraction
4601 * . string concatenation
4602 *
4603 * "arg" must point to the first non-white of the expression.
4604 * "arg" is advanced to the next non-white after the recognized expression.
4605 *
4606 * Return OK or FAIL.
4607 */
4608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 int evaluate;
4613{
Bram Moolenaar33570922005-01-25 22:26:29 +00004614 typval_T var2;
4615 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 int op;
4617 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618#ifdef FEAT_FLOAT
4619 float_T f1 = 0, f2 = 0;
4620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u *s1, *s2;
4622 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4623 char_u *p;
4624
4625 /*
4626 * Get the first variable.
4627 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004628 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 return FAIL;
4630
4631 /*
4632 * Repeat computing, until no '+', '-' or '.' is following.
4633 */
4634 for (;;)
4635 {
4636 op = **arg;
4637 if (op != '+' && op != '-' && op != '.')
4638 break;
4639
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640 if ((op != '+' || rettv->v_type != VAR_LIST)
4641#ifdef FEAT_FLOAT
4642 && (op == '.' || rettv->v_type != VAR_FLOAT)
4643#endif
4644 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 {
4646 /* For "list + ...", an illegal use of the first operand as
4647 * a number cannot be determined before evaluating the 2nd
4648 * operand: if this is also a list, all is ok.
4649 * For "something . ...", "something - ..." or "non-list + ...",
4650 * we know that the first operand needs to be a string or number
4651 * without evaluating the 2nd operand. So check before to avoid
4652 * side effects after an error. */
4653 if (evaluate && get_tv_string_chk(rettv) == NULL)
4654 {
4655 clear_tv(rettv);
4656 return FAIL;
4657 }
4658 }
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 /*
4661 * Get the second variable.
4662 */
4663 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004664 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FAIL;
4668 }
4669
4670 if (evaluate)
4671 {
4672 /*
4673 * Compute the result.
4674 */
4675 if (op == '.')
4676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4678 s2 = get_tv_string_buf_chk(&var2, buf2);
4679 if (s2 == NULL) /* type error ? */
4680 {
4681 clear_tv(rettv);
4682 clear_tv(&var2);
4683 return FAIL;
4684 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004685 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686 clear_tv(rettv);
4687 rettv->v_type = VAR_STRING;
4688 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004690 else if (op == '+' && rettv->v_type == VAR_LIST
4691 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004692 {
4693 /* concatenate Lists */
4694 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4695 &var3) == FAIL)
4696 {
4697 clear_tv(rettv);
4698 clear_tv(&var2);
4699 return FAIL;
4700 }
4701 clear_tv(rettv);
4702 *rettv = var3;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 else
4705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 int error = FALSE;
4707
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708#ifdef FEAT_FLOAT
4709 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 f1 = rettv->vval.v_float;
4712 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714 else
4715#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 n1 = get_tv_number_chk(rettv, &error);
4718 if (error)
4719 {
4720 /* This can only happen for "list + non-list". For
4721 * "non-list + ..." or "something - ...", we returned
4722 * before evaluating the 2nd operand. */
4723 clear_tv(rettv);
4724 return FAIL;
4725 }
4726#ifdef FEAT_FLOAT
4727 if (var2.v_type == VAR_FLOAT)
4728 f1 = n1;
4729#endif
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 {
4734 f2 = var2.vval.v_float;
4735 n2 = 0;
4736 }
4737 else
4738#endif
4739 {
4740 n2 = get_tv_number_chk(&var2, &error);
4741 if (error)
4742 {
4743 clear_tv(rettv);
4744 clear_tv(&var2);
4745 return FAIL;
4746 }
4747#ifdef FEAT_FLOAT
4748 if (rettv->v_type == VAR_FLOAT)
4749 f2 = n2;
4750#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753
4754#ifdef FEAT_FLOAT
4755 /* If there is a float on either side the result is a float. */
4756 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4757 {
4758 if (op == '+')
4759 f1 = f1 + f2;
4760 else
4761 f1 = f1 - f2;
4762 rettv->v_type = VAR_FLOAT;
4763 rettv->vval.v_float = f1;
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766#endif
4767 {
4768 if (op == '+')
4769 n1 = n1 + n2;
4770 else
4771 n1 = n1 - n2;
4772 rettv->v_type = VAR_NUMBER;
4773 rettv->vval.v_number = n1;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004776 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 }
4779 return OK;
4780}
4781
4782/*
4783 * Handle fifth level expression:
4784 * * number multiplication
4785 * / number division
4786 * % number modulo
4787 *
4788 * "arg" must point to the first non-white of the expression.
4789 * "arg" is advanced to the next non-white after the recognized expression.
4790 *
4791 * Return OK or FAIL.
4792 */
4793 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int op;
4802 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803#ifdef FEAT_FLOAT
4804 int use_float = FALSE;
4805 float_T f1 = 0, f2;
4806#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
4809 /*
4810 * Get the first variable.
4811 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return FAIL;
4814
4815 /*
4816 * Repeat computing, until no '*', '/' or '%' is following.
4817 */
4818 for (;;)
4819 {
4820 op = **arg;
4821 if (op != '*' && op != '/' && op != '%')
4822 break;
4823
4824 if (evaluate)
4825 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826#ifdef FEAT_FLOAT
4827 if (rettv->v_type == VAR_FLOAT)
4828 {
4829 f1 = rettv->vval.v_float;
4830 use_float = TRUE;
4831 n1 = 0;
4832 }
4833 else
4834#endif
4835 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004837 if (error)
4838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 else
4841 n1 = 0;
4842
4843 /*
4844 * Get the second variable.
4845 */
4846 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004847 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return FAIL;
4849
4850 if (evaluate)
4851 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852#ifdef FEAT_FLOAT
4853 if (var2.v_type == VAR_FLOAT)
4854 {
4855 if (!use_float)
4856 {
4857 f1 = n1;
4858 use_float = TRUE;
4859 }
4860 f2 = var2.vval.v_float;
4861 n2 = 0;
4862 }
4863 else
4864#endif
4865 {
4866 n2 = get_tv_number_chk(&var2, &error);
4867 clear_tv(&var2);
4868 if (error)
4869 return FAIL;
4870#ifdef FEAT_FLOAT
4871 if (use_float)
4872 f2 = n2;
4873#endif
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
4876 /*
4877 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#ifdef FEAT_FLOAT
4881 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 if (op == '*')
4884 f1 = f1 * f2;
4885 else if (op == '/')
4886 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887# ifdef VMS
4888 /* VMS crashes on divide by zero, work around it */
4889 if (f2 == 0.0)
4890 {
4891 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004892 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004893 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004894 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004895 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004896 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897 }
4898 else
4899 f1 = f1 / f2;
4900# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 /* We rely on the floating point library to handle divide
4902 * by zero to result in "inf" and not a crash. */
4903 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004904# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004908 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 return FAIL;
4910 }
4911 rettv->v_type = VAR_FLOAT;
4912 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917 if (op == '*')
4918 n1 = n1 * n2;
4919 else if (op == '/')
4920 {
4921 if (n2 == 0) /* give an error message? */
4922 {
4923 if (n1 == 0)
4924 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4925 else if (n1 < 0)
4926 n1 = -0x7fffffffL;
4927 else
4928 n1 = 0x7fffffffL;
4929 }
4930 else
4931 n1 = n1 / n2;
4932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004934 {
4935 if (n2 == 0) /* give an error message? */
4936 n1 = 0;
4937 else
4938 n1 = n1 % n2;
4939 }
4940 rettv->v_type = VAR_NUMBER;
4941 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 }
4945
4946 return OK;
4947}
4948
4949/*
4950 * Handle sixth level expression:
4951 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004952 * "string" string constant
4953 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 * &option-name option value
4955 * @r register contents
4956 * identifier variable value
4957 * function() function call
4958 * $VAR environment variable
4959 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004960 * [expr, expr] List
4961 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *
4963 * Also handle:
4964 * ! in front logical NOT
4965 * - in front unary minus
4966 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 * trailing [] subscript in String or List
4968 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 *
4970 * "arg" must point to the first non-white of the expression.
4971 * "arg" is advanced to the next non-white after the recognized expression.
4972 *
4973 * Return OK or FAIL.
4974 */
4975 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004976eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004980 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 long n;
4983 int len;
4984 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 char_u *start_leader, *end_leader;
4986 int ret = OK;
4987 char_u *alias;
4988
4989 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004991 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 /*
4996 * Skip '!' and '-' characters. They are handled later.
4997 */
4998 start_leader = *arg;
4999 while (**arg == '!' || **arg == '-' || **arg == '+')
5000 *arg = skipwhite(*arg + 1);
5001 end_leader = *arg;
5002
5003 switch (**arg)
5004 {
5005 /*
5006 * Number constant.
5007 */
5008 case '0':
5009 case '1':
5010 case '2':
5011 case '3':
5012 case '4':
5013 case '5':
5014 case '6':
5015 case '7':
5016 case '8':
5017 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 {
5019#ifdef FEAT_FLOAT
5020 char_u *p = skipdigits(*arg + 1);
5021 int get_float = FALSE;
5022
5023 /* We accept a float when the format matches
5024 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005025 * strict to avoid backwards compatibility problems.
5026 * Don't look for a float after the "." operator, so that
5027 * ":let vers = 1.2.3" doesn't fail. */
5028 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 get_float = TRUE;
5031 p = skipdigits(p + 2);
5032 if (*p == 'e' || *p == 'E')
5033 {
5034 ++p;
5035 if (*p == '-' || *p == '+')
5036 ++p;
5037 if (!vim_isdigit(*p))
5038 get_float = FALSE;
5039 else
5040 p = skipdigits(p + 1);
5041 }
5042 if (ASCII_ISALPHA(*p) || *p == '.')
5043 get_float = FALSE;
5044 }
5045 if (get_float)
5046 {
5047 float_T f;
5048
5049 *arg += string2float(*arg, &f);
5050 if (evaluate)
5051 {
5052 rettv->v_type = VAR_FLOAT;
5053 rettv->vval.v_float = f;
5054 }
5055 }
5056 else
5057#endif
5058 {
5059 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5060 *arg += len;
5061 if (evaluate)
5062 {
5063 rettv->v_type = VAR_NUMBER;
5064 rettv->vval.v_number = n;
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * String constant: "string".
5072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 break;
5075
5076 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 break;
5081
5082 /*
5083 * List: [expr, expr]
5084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 * Dictionary: {key: val, key: val}
5090 */
5091 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5092 break;
5093
5094 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005097 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /*
5101 * Environment variable: $VAR.
5102 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 break;
5105
5106 /*
5107 * Register contents: @r.
5108 */
5109 case '@': ++*arg;
5110 if (evaluate)
5111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005113 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 if (**arg != NUL)
5116 ++*arg;
5117 break;
5118
5119 /*
5120 * nested expression: (expression).
5121 */
5122 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (**arg == ')')
5125 ++*arg;
5126 else if (ret == OK)
5127 {
5128 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 ret = FAIL;
5131 }
5132 break;
5133
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137
5138 if (ret == NOTDONE)
5139 {
5140 /*
5141 * Must be a variable or function name.
5142 * Can also be a curly-braces kind of name: {expr}.
5143 */
5144 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005145 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 if (alias != NULL)
5147 s = alias;
5148
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 ret = FAIL;
5151 else
5152 {
5153 if (**arg == '(') /* recursive! */
5154 {
5155 /* If "s" is the name of a variable of type VAR_FUNC
5156 * use its contents. */
5157 s = deref_func_name(s, &len);
5158
5159 /* Invoke the function. */
5160 ret = get_func_tv(s, len, rettv, arg,
5161 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005162 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 /* Stop the expression evaluation when immediately
5164 * aborting on error, or when an interrupt occurred or
5165 * an exception was thrown but not caught. */
5166 if (aborting())
5167 {
5168 if (ret == OK)
5169 clear_tv(rettv);
5170 ret = FAIL;
5171 }
5172 }
5173 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005174 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005175 else
5176 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005178 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 *arg = skipwhite(*arg);
5182
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005183 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5184 * expr(expr). */
5185 if (ret == OK)
5186 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 /*
5189 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5190 */
5191 if (ret == OK && evaluate && end_leader > start_leader)
5192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005194 int val = 0;
5195#ifdef FEAT_FLOAT
5196 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 if (rettv->v_type == VAR_FLOAT)
5199 f = rettv->vval.v_float;
5200 else
5201#endif
5202 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 clear_tv(rettv);
5206 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 else
5209 {
5210 while (end_leader > start_leader)
5211 {
5212 --end_leader;
5213 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005214 {
5215#ifdef FEAT_FLOAT
5216 if (rettv->v_type == VAR_FLOAT)
5217 f = !f;
5218 else
5219#endif
5220 val = !val;
5221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 {
5224#ifdef FEAT_FLOAT
5225 if (rettv->v_type == VAR_FLOAT)
5226 f = -f;
5227 else
5228#endif
5229 val = -val;
5230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005232#ifdef FEAT_FLOAT
5233 if (rettv->v_type == VAR_FLOAT)
5234 {
5235 clear_tv(rettv);
5236 rettv->vval.v_float = f;
5237 }
5238 else
5239#endif
5240 {
5241 clear_tv(rettv);
5242 rettv->v_type = VAR_NUMBER;
5243 rettv->vval.v_number = val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247
5248 return ret;
5249}
5250
5251/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005252 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5253 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5255 */
5256 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
5263 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 long len = -1;
5267 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 if (rettv->v_type == VAR_FUNC
5272#ifdef FEAT_FLOAT
5273 || rettv->v_type == VAR_FLOAT
5274#endif
5275 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 if (verbose)
5278 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 return FAIL;
5280 }
5281
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 /*
5285 * dict.name
5286 */
5287 key = *arg + 1;
5288 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5289 ;
5290 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 }
5294 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 /*
5297 * something[idx]
5298 *
5299 * Get the (first) variable from inside the [].
5300 */
5301 *arg = skipwhite(*arg + 1);
5302 if (**arg == ':')
5303 empty1 = TRUE;
5304 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5305 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5307 {
5308 /* not a number or string */
5309 clear_tv(&var1);
5310 return FAIL;
5311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 /*
5314 * Get the second variable from inside the [:].
5315 */
5316 if (**arg == ':')
5317 {
5318 range = TRUE;
5319 *arg = skipwhite(*arg + 1);
5320 if (**arg == ']')
5321 empty2 = TRUE;
5322 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 if (!empty1)
5325 clear_tv(&var1);
5326 return FAIL;
5327 }
5328 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5329 {
5330 /* not a number or string */
5331 if (!empty1)
5332 clear_tv(&var1);
5333 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 return FAIL;
5335 }
5336 }
5337
5338 /* Check for the ']'. */
5339 if (**arg != ']')
5340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 if (verbose)
5342 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 clear_tv(&var1);
5344 if (range)
5345 clear_tv(&var2);
5346 return FAIL;
5347 }
5348 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350
5351 if (evaluate)
5352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 n1 = 0;
5354 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 n1 = get_tv_number(&var1);
5357 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 if (range)
5360 {
5361 if (empty2)
5362 n2 = -1;
5363 else
5364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 n2 = get_tv_number(&var2);
5366 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 }
5368 }
5369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 {
5372 case VAR_NUMBER:
5373 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 len = (long)STRLEN(s);
5376 if (range)
5377 {
5378 /* The resulting variable is a substring. If the indexes
5379 * are out of range the result is empty. */
5380 if (n1 < 0)
5381 {
5382 n1 = len + n1;
5383 if (n1 < 0)
5384 n1 = 0;
5385 }
5386 if (n2 < 0)
5387 n2 = len + n2;
5388 else if (n2 >= len)
5389 n2 = len;
5390 if (n1 >= len || n2 < 0 || n1 > n2)
5391 s = NULL;
5392 else
5393 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5394 }
5395 else
5396 {
5397 /* The resulting variable is a string of a single
5398 * character. If the index is too big or negative the
5399 * result is empty. */
5400 if (n1 >= len || n1 < 0)
5401 s = NULL;
5402 else
5403 s = vim_strnsave(s + n1, 1);
5404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 clear_tv(rettv);
5406 rettv->v_type = VAR_STRING;
5407 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 break;
5409
5410 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 if (n1 < 0)
5413 n1 = len + n1;
5414 if (!empty1 && (n1 < 0 || n1 >= len))
5415 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005416 /* For a range we allow invalid values and return an empty
5417 * list. A list index out of range is an error. */
5418 if (!range)
5419 {
5420 if (verbose)
5421 EMSGN(_(e_listidx), n1);
5422 return FAIL;
5423 }
5424 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 if (range)
5427 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 list_T *l;
5429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430
5431 if (n2 < 0)
5432 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005433 else if (n2 >= len)
5434 n2 = len - 1;
5435 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005436 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 l = list_alloc();
5438 if (l == NULL)
5439 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 n1 <= n2; ++n1)
5442 {
5443 if (list_append_tv(l, &item->li_tv) == FAIL)
5444 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005445 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 return FAIL;
5447 }
5448 item = item->li_next;
5449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005450 clear_tv(rettv);
5451 rettv->v_type = VAR_LIST;
5452 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005453 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 else
5456 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005457 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 clear_tv(rettv);
5459 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462
5463 case VAR_DICT:
5464 if (range)
5465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 if (verbose)
5467 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 if (len == -1)
5469 clear_tv(&var1);
5470 return FAIL;
5471 }
5472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474
5475 if (len == -1)
5476 {
5477 key = get_tv_string(&var1);
5478 if (*key == NUL)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 clear_tv(&var1);
5483 return FAIL;
5484 }
5485 }
5486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005489 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 if (len == -1)
5492 clear_tv(&var1);
5493 if (item == NULL)
5494 return FAIL;
5495
5496 copy_tv(&item->di_tv, &var1);
5497 clear_tv(rettv);
5498 *rettv = var1;
5499 }
5500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
5502 }
5503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 return OK;
5505}
5506
5507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * Get an option value.
5509 * "arg" points to the '&' or '+' before the option name.
5510 * "arg" is advanced to character after the option name.
5511 * Return OK or FAIL.
5512 */
5513 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 int evaluate;
5518{
5519 char_u *option_end;
5520 long numval;
5521 char_u *stringval;
5522 int opt_type;
5523 int c;
5524 int working = (**arg == '+'); /* has("+option") */
5525 int ret = OK;
5526 int opt_flags;
5527
5528 /*
5529 * Isolate the option name and find its value.
5530 */
5531 option_end = find_option_end(arg, &opt_flags);
5532 if (option_end == NULL)
5533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 EMSG2(_("E112: Option name missing: %s"), *arg);
5536 return FAIL;
5537 }
5538
5539 if (!evaluate)
5540 {
5541 *arg = option_end;
5542 return OK;
5543 }
5544
5545 c = *option_end;
5546 *option_end = NUL;
5547 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 if (opt_type == -3) /* invalid name */
5551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 EMSG2(_("E113: Unknown option: %s"), *arg);
5554 ret = FAIL;
5555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 if (opt_type == -2) /* hidden string option */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 rettv->v_type = VAR_STRING;
5561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else if (opt_type == -1) /* hidden number option */
5564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 rettv->v_type = VAR_NUMBER;
5566 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 else if (opt_type == 1) /* number option */
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 rettv->v_type = VAR_NUMBER;
5571 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 else /* string option */
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv->v_type = VAR_STRING;
5576 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578 }
5579 else if (working && (opt_type == -2 || opt_type == -1))
5580 ret = FAIL;
5581
5582 *option_end = c; /* put back for error messages */
5583 *arg = option_end;
5584
5585 return ret;
5586}
5587
5588/*
5589 * Allocate a variable for a string constant.
5590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *p;
5599 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int extra = 0;
5601
5602 /*
5603 * Find the end of the string, skipping backslashed characters.
5604 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 if (*p == '\\' && p[1] != NUL)
5608 {
5609 ++p;
5610 /* A "\<x>" form occupies at least 4 characters, and produces up
5611 * to 6 characters: reserve space for 2 extra */
5612 if (*p == '<')
5613 extra += 2;
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616
5617 if (*p != '"')
5618 {
5619 EMSG2(_("E114: Missing quote: %s"), *arg);
5620 return FAIL;
5621 }
5622
5623 /* If only parsing, set *arg and return here */
5624 if (!evaluate)
5625 {
5626 *arg = p + 1;
5627 return OK;
5628 }
5629
5630 /*
5631 * Copy the string into allocated memory, handling backslashed
5632 * characters.
5633 */
5634 name = alloc((unsigned)(p - *arg + extra));
5635 if (name == NULL)
5636 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (*p == '\\')
5643 {
5644 switch (*++p)
5645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 case 'b': *name++ = BS; ++p; break;
5647 case 'e': *name++ = ESC; ++p; break;
5648 case 'f': *name++ = FF; ++p; break;
5649 case 'n': *name++ = NL; ++p; break;
5650 case 'r': *name++ = CAR; ++p; break;
5651 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 case 'X': /* hex: "\x1", "\x12" */
5654 case 'x':
5655 case 'u': /* Unicode: "\u0023" */
5656 case 'U':
5657 if (vim_isxdigit(p[1]))
5658 {
5659 int n, nr;
5660 int c = toupper(*p);
5661
5662 if (c == 'X')
5663 n = 2;
5664 else
5665 n = 4;
5666 nr = 0;
5667 while (--n >= 0 && vim_isxdigit(p[1]))
5668 {
5669 ++p;
5670 nr = (nr << 4) + hex2nr(*p);
5671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#ifdef FEAT_MBYTE
5674 /* For "\u" store the number according to
5675 * 'encoding'. */
5676 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683
5684 /* octal: "\1", "\12", "\123" */
5685 case '0':
5686 case '1':
5687 case '2':
5688 case '3':
5689 case '4':
5690 case '5':
5691 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 case '7': *name = *p++ - '0';
5693 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 *name = (*name << 3) + *p++ - '0';
5696 if (*p >= '0' && *p <= '7')
5697 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 break;
5701
5702 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (extra != 0)
5705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708 }
5709 /* FALLTHROUGH */
5710
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713 }
5714 }
5715 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 *arg = p + 1;
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 return OK;
5723}
5724
5725/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 * Return OK or FAIL.
5728 */
5729 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int evaluate;
5734{
5735 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 int reduce = 0;
5738
5739 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 */
5742 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 break;
5748 ++reduce;
5749 ++p;
5750 }
5751 }
5752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 return FAIL;
5757 }
5758
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 if (!evaluate)
5761 {
5762 *arg = p + 1;
5763 return OK;
5764 }
5765
5766 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 */
5769 str = alloc((unsigned)((p - *arg) - reduce));
5770 if (str == NULL)
5771 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 rettv->v_type = VAR_STRING;
5773 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 break;
5781 ++p;
5782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 *arg = p + 1;
5787
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 return OK;
5789}
5790
5791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 * Allocate a variable for a List and fill it from "*arg".
5793 * Return OK or FAIL.
5794 */
5795 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 int evaluate;
5800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l = NULL;
5802 typval_T tv;
5803 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804
5805 if (evaluate)
5806 {
5807 l = list_alloc();
5808 if (l == NULL)
5809 return FAIL;
5810 }
5811
5812 *arg = skipwhite(*arg + 1);
5813 while (**arg != ']' && **arg != NUL)
5814 {
5815 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5816 goto failret;
5817 if (evaluate)
5818 {
5819 item = listitem_alloc();
5820 if (item != NULL)
5821 {
5822 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005823 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 list_append(l, item);
5825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005826 else
5827 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 }
5829
5830 if (**arg == ']')
5831 break;
5832 if (**arg != ',')
5833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 goto failret;
5836 }
5837 *arg = skipwhite(*arg + 1);
5838 }
5839
5840 if (**arg != ']')
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843failret:
5844 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005845 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 return FAIL;
5847 }
5848
5849 *arg = skipwhite(*arg + 1);
5850 if (evaluate)
5851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005852 rettv->v_type = VAR_LIST;
5853 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 ++l->lv_refcount;
5855 }
5856
5857 return OK;
5858}
5859
5860/*
5861 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005862 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005864 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865list_alloc()
5866{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005867 list_T *l;
5868
5869 l = (list_T *)alloc_clear(sizeof(list_T));
5870 if (l != NULL)
5871 {
5872 /* Prepend the list to the list of lists for garbage collection. */
5873 if (first_list != NULL)
5874 first_list->lv_used_prev = l;
5875 l->lv_used_prev = NULL;
5876 l->lv_used_next = first_list;
5877 first_list = l;
5878 }
5879 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880}
5881
5882/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005883 * Allocate an empty list for a return value.
5884 * Returns OK or FAIL.
5885 */
5886 static int
5887rettv_list_alloc(rettv)
5888 typval_T *rettv;
5889{
5890 list_T *l = list_alloc();
5891
5892 if (l == NULL)
5893 return FAIL;
5894
5895 rettv->vval.v_list = l;
5896 rettv->v_type = VAR_LIST;
5897 ++l->lv_refcount;
5898 return OK;
5899}
5900
5901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 * Unreference a list: decrement the reference count and free it when it
5903 * becomes zero.
5904 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005905 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005909 if (l != NULL && --l->lv_refcount <= 0)
5910 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911}
5912
5913/*
5914 * Free a list, including all items it points to.
5915 * Ignores the reference count.
5916 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005917 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005918list_free(l, recurse)
5919 list_T *l;
5920 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924 /* Remove the list from the list of lists for garbage collection. */
5925 if (l->lv_used_prev == NULL)
5926 first_list = l->lv_used_next;
5927 else
5928 l->lv_used_prev->lv_used_next = l->lv_used_next;
5929 if (l->lv_used_next != NULL)
5930 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5931
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 /* Remove the item before deleting it. */
5935 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005936 if (recurse || (item->li_tv.v_type != VAR_LIST
5937 && item->li_tv.v_type != VAR_DICT))
5938 clear_tv(&item->li_tv);
5939 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941 vim_free(l);
5942}
5943
5944/*
5945 * Allocate a list item.
5946 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948listitem_alloc()
5949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951}
5952
5953/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005954 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
5956 static void
5957listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 vim_free(item);
5962}
5963
5964/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005965 * Remove a list item from a List and free it. Also clears the value.
5966 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005967 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005968listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 list_T *l;
5970 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005971{
5972 list_remove(l, item, item);
5973 listitem_free(item);
5974}
5975
5976/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 * Get the number of items in a list.
5978 */
5979 static long
5980list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 if (l == NULL)
5984 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005985 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 * Return TRUE when two lists have exactly the same values.
5990 */
5991 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l1;
5994 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997{
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006000 if (l1 == NULL || l2 == NULL)
6001 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006002 if (l1 == l2)
6003 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004 if (list_len(l1) != list_len(l2))
6005 return FALSE;
6006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 for (item1 = l1->lv_first, item2 = l2->lv_first;
6008 item1 != NULL && item2 != NULL;
6009 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 return FALSE;
6012 return item1 == NULL && item2 == NULL;
6013}
6014
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006015#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6016 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017/*
6018 * Return the dictitem that an entry in a hashtable points to.
6019 */
6020 dictitem_T *
6021dict_lookup(hi)
6022 hashitem_T *hi;
6023{
6024 return HI2DI(hi);
6025}
6026#endif
6027
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 * Return TRUE when two dictionaries have exactly the same key/values.
6030 */
6031 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 dict_T *d1;
6034 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 hashitem_T *hi;
6039 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006042 if (d1 == NULL || d2 == NULL)
6043 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006044 if (d1 == d2)
6045 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046 if (dict_len(d1) != dict_len(d2))
6047 return FALSE;
6048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006049 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006052 if (!HASHITEM_EMPTY(hi))
6053 {
6054 item2 = dict_find(d2, hi->hi_key, -1);
6055 if (item2 == NULL)
6056 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 return FALSE;
6059 --todo;
6060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061 }
6062 return TRUE;
6063}
6064
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065static int tv_equal_recurse_limit;
6066
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006069 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006070 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 */
6072 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 typval_T *tv1;
6075 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 int ic; /* ignore case */
6077 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078{
6079 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006080 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006082 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006084 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 * recursiveness to a limit. We guess they are equal then.
6089 * A fixed limit has the problem of still taking an awful long time.
6090 * Reduce the limit every time running into it. That should work fine for
6091 * deeply linked structures that are not recursively linked and catch
6092 * recursiveness quickly. */
6093 if (!recursive)
6094 tv_equal_recurse_limit = 1000;
6095 if (recursive_cnt >= tv_equal_recurse_limit)
6096 {
6097 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100
6101 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 ++recursive_cnt;
6105 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6106 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006107 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 ++recursive_cnt;
6111 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6112 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006113 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 case VAR_FUNC:
6116 return (tv1->vval.v_string != NULL
6117 && tv2->vval.v_string != NULL
6118 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6119
6120 case VAR_NUMBER:
6121 return tv1->vval.v_number == tv2->vval.v_number;
6122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006123#ifdef FEAT_FLOAT
6124 case VAR_FLOAT:
6125 return tv1->vval.v_float == tv2->vval.v_float;
6126#endif
6127
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 case VAR_STRING:
6129 s1 = get_tv_string_buf(tv1, buf1);
6130 s2 = get_tv_string_buf(tv2, buf2);
6131 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 return TRUE;
6136}
6137
6138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 * Locate item with index "n" in list "l" and return it.
6140 * A negative index is counted from the end; -1 is the last item.
6141 * Returns NULL when "n" is out of range.
6142 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006143 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 long n;
6147{
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 long idx;
6150
6151 if (l == NULL)
6152 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153
6154 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006156 n = l->lv_len + n;
6157
6158 /* Check for index out of range. */
6159 if (n < 0 || n >= l->lv_len)
6160 return NULL;
6161
6162 /* When there is a cached index may start search from there. */
6163 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165 if (n < l->lv_idx / 2)
6166 {
6167 /* closest to the start of the list */
6168 item = l->lv_first;
6169 idx = 0;
6170 }
6171 else if (n > (l->lv_idx + l->lv_len) / 2)
6172 {
6173 /* closest to the end of the list */
6174 item = l->lv_last;
6175 idx = l->lv_len - 1;
6176 }
6177 else
6178 {
6179 /* closest to the cached index */
6180 item = l->lv_idx_item;
6181 idx = l->lv_idx;
6182 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 }
6184 else
6185 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006186 if (n < l->lv_len / 2)
6187 {
6188 /* closest to the start of the list */
6189 item = l->lv_first;
6190 idx = 0;
6191 }
6192 else
6193 {
6194 /* closest to the end of the list */
6195 item = l->lv_last;
6196 idx = l->lv_len - 1;
6197 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006199
6200 while (n > idx)
6201 {
6202 /* search forward */
6203 item = item->li_next;
6204 ++idx;
6205 }
6206 while (n < idx)
6207 {
6208 /* search backward */
6209 item = item->li_prev;
6210 --idx;
6211 }
6212
6213 /* cache the used index */
6214 l->lv_idx = idx;
6215 l->lv_idx_item = item;
6216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 return item;
6218}
6219
6220/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006221 * Get list item "l[idx]" as a number.
6222 */
6223 static long
6224list_find_nr(l, idx, errorp)
6225 list_T *l;
6226 long idx;
6227 int *errorp; /* set to TRUE when something wrong */
6228{
6229 listitem_T *li;
6230
6231 li = list_find(l, idx);
6232 if (li == NULL)
6233 {
6234 if (errorp != NULL)
6235 *errorp = TRUE;
6236 return -1L;
6237 }
6238 return get_tv_number_chk(&li->li_tv, errorp);
6239}
6240
6241/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006242 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6243 */
6244 char_u *
6245list_find_str(l, idx)
6246 list_T *l;
6247 long idx;
6248{
6249 listitem_T *li;
6250
6251 li = list_find(l, idx - 1);
6252 if (li == NULL)
6253 {
6254 EMSGN(_(e_listidx), idx);
6255 return NULL;
6256 }
6257 return get_tv_string(&li->li_tv);
6258}
6259
6260/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006261 * Locate "item" list "l" and return its index.
6262 * Returns -1 when "item" is not in the list.
6263 */
6264 static long
6265list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268{
6269 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006271
6272 if (l == NULL)
6273 return -1;
6274 idx = 0;
6275 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6276 ++idx;
6277 if (li == NULL)
6278 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006279 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280}
6281
6282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 * Append item "item" to the end of list "l".
6284 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 list_T *l;
6288 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289{
6290 if (l->lv_last == NULL)
6291 {
6292 /* empty list */
6293 l->lv_first = item;
6294 l->lv_last = item;
6295 item->li_prev = NULL;
6296 }
6297 else
6298 {
6299 l->lv_last->li_next = item;
6300 item->li_prev = l->lv_last;
6301 l->lv_last = item;
6302 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006304 item->li_next = NULL;
6305}
6306
6307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006311 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 list_T *l;
6314 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 copy_tv(tv, &li->li_tv);
6321 list_append(l, li);
6322 return OK;
6323}
6324
6325/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006326 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 * Return FAIL when out of memory.
6328 */
6329 int
6330list_append_dict(list, dict)
6331 list_T *list;
6332 dict_T *dict;
6333{
6334 listitem_T *li = listitem_alloc();
6335
6336 if (li == NULL)
6337 return FAIL;
6338 li->li_tv.v_type = VAR_DICT;
6339 li->li_tv.v_lock = 0;
6340 li->li_tv.vval.v_dict = dict;
6341 list_append(list, li);
6342 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 return OK;
6344}
6345
6346/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006348 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006349 * Returns FAIL when out of memory.
6350 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353 list_T *l;
6354 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356{
6357 listitem_T *li = listitem_alloc();
6358
6359 if (li == NULL)
6360 return FAIL;
6361 list_append(l, li);
6362 li->li_tv.v_type = VAR_STRING;
6363 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006364 if (str == NULL)
6365 li->li_tv.vval.v_string = NULL;
6366 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006367 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 return FAIL;
6369 return OK;
6370}
6371
6372/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006373 * Append "n" to list "l".
6374 * Returns FAIL when out of memory.
6375 */
6376 static int
6377list_append_number(l, n)
6378 list_T *l;
6379 varnumber_T n;
6380{
6381 listitem_T *li;
6382
6383 li = listitem_alloc();
6384 if (li == NULL)
6385 return FAIL;
6386 li->li_tv.v_type = VAR_NUMBER;
6387 li->li_tv.v_lock = 0;
6388 li->li_tv.vval.v_number = n;
6389 list_append(l, li);
6390 return OK;
6391}
6392
6393/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 * If "item" is NULL append at the end.
6396 * Return FAIL when out of memory.
6397 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 list_T *l;
6401 typval_T *tv;
6402 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403{
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405
6406 if (ni == NULL)
6407 return FAIL;
6408 copy_tv(tv, &ni->li_tv);
6409 if (item == NULL)
6410 /* Append new item at end of list. */
6411 list_append(l, ni);
6412 else
6413 {
6414 /* Insert new item before existing item. */
6415 ni->li_prev = item->li_prev;
6416 ni->li_next = item;
6417 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 ++l->lv_idx;
6421 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 l->lv_idx_item = NULL;
6426 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 }
6430 return OK;
6431}
6432
6433/*
6434 * Extend "l1" with "l2".
6435 * If "bef" is NULL append at the end, otherwise insert before this item.
6436 * Returns FAIL when out of memory.
6437 */
6438 static int
6439list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l1;
6441 list_T *l2;
6442 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006447 /* We also quit the loop when we have inserted the original item count of
6448 * the list, avoid a hang when we extend a list with itself. */
6449 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6451 return FAIL;
6452 return OK;
6453}
6454
6455/*
6456 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6457 * Return FAIL when out of memory.
6458 */
6459 static int
6460list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 list_T *l1;
6462 list_T *l2;
6463 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464{
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006467 if (l1 == NULL || l2 == NULL)
6468 return FAIL;
6469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 if (l == NULL)
6473 return FAIL;
6474 tv->v_type = VAR_LIST;
6475 tv->vval.v_list = l;
6476
6477 /* append all items from the second list */
6478 return list_extend(l, l2, NULL);
6479}
6480
6481/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 * Returns NULL when out of memory.
6486 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *copy;
6494 listitem_T *item;
6495 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496
6497 if (orig == NULL)
6498 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499
6500 copy = list_alloc();
6501 if (copy != NULL)
6502 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 if (copyID != 0)
6504 {
6505 /* Do this before adding the items, because one of the items may
6506 * refer back to this list. */
6507 orig->lv_copyID = copyID;
6508 orig->lv_copylist = copy;
6509 }
6510 for (item = orig->lv_first; item != NULL && !got_int;
6511 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 {
6513 ni = listitem_alloc();
6514 if (ni == NULL)
6515 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 {
6518 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6519 {
6520 vim_free(ni);
6521 break;
6522 }
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 list_append(copy, ni);
6527 }
6528 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (item != NULL)
6530 {
6531 list_unref(copy);
6532 copy = NULL;
6533 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 }
6535
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 return copy;
6537}
6538
6539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006541 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006543 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006544list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 list_T *l;
6546 listitem_T *item;
6547 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548{
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 /* notify watchers */
6552 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006554 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 list_fix_watch(l, ip);
6556 if (ip == item2)
6557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
6560 if (item2->li_next == NULL)
6561 l->lv_last = item->li_prev;
6562 else
6563 item2->li_next->li_prev = item->li_prev;
6564 if (item->li_prev == NULL)
6565 l->lv_first = item2->li_next;
6566 else
6567 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569}
6570
6571/*
6572 * Return an allocated string with the string representation of a list.
6573 * May return NULL.
6574 */
6575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579{
6580 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581
6582 if (tv->vval.v_list == NULL)
6583 return NULL;
6584 ga_init2(&ga, (int)sizeof(char), 80);
6585 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 {
6588 vim_free(ga.ga_data);
6589 return NULL;
6590 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 ga_append(&ga, ']');
6592 ga_append(&ga, NUL);
6593 return (char_u *)ga.ga_data;
6594}
6595
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006596typedef struct join_S {
6597 char_u *s;
6598 char_u *tofree;
6599} join_T;
6600
6601 static int
6602list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6603 garray_T *gap; /* to store the result in */
6604 list_T *l;
6605 char_u *sep;
6606 int echo_style;
6607 int copyID;
6608 garray_T *join_gap; /* to keep each list item string */
6609{
6610 int i;
6611 join_T *p;
6612 int len;
6613 int sumlen = 0;
6614 int first = TRUE;
6615 char_u *tofree;
6616 char_u numbuf[NUMBUFLEN];
6617 listitem_T *item;
6618 char_u *s;
6619
6620 /* Stringify each item in the list. */
6621 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6622 {
6623 if (echo_style)
6624 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6625 else
6626 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6627 if (s == NULL)
6628 return FAIL;
6629
6630 len = (int)STRLEN(s);
6631 sumlen += len;
6632
6633 ga_grow(join_gap, 1);
6634 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6635 if (tofree != NULL || s != numbuf)
6636 {
6637 p->s = s;
6638 p->tofree = tofree;
6639 }
6640 else
6641 {
6642 p->s = vim_strnsave(s, len);
6643 p->tofree = p->s;
6644 }
6645
6646 line_breakcheck();
6647 }
6648
6649 /* Allocate result buffer with its total size, avoid re-allocation and
6650 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6651 if (join_gap->ga_len >= 2)
6652 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6653 if (ga_grow(gap, sumlen + 2) == FAIL)
6654 return FAIL;
6655
6656 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6657 {
6658 if (first)
6659 first = FALSE;
6660 else
6661 ga_concat(gap, sep);
6662 p = ((join_T *)join_gap->ga_data) + i;
6663
6664 if (p->s != NULL)
6665 ga_concat(gap, p->s);
6666 line_breakcheck();
6667 }
6668
6669 return OK;
6670}
6671
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006674 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006683 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006685 garray_T join_ga;
6686 int retval;
6687 join_T *p;
6688 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006690 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6691 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6692
6693 /* Dispose each item in join_ga. */
6694 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696 p = (join_T *)join_ga.ga_data;
6697 for (i = 0; i < join_ga.ga_len; ++i)
6698 {
6699 vim_free(p->tofree);
6700 ++p;
6701 }
6702 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704
6705 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706}
6707
6708/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 * Garbage collection for lists and dictionaries.
6710 *
6711 * We use reference counts to be able to free most items right away when they
6712 * are no longer used. But for composite items it's possible that it becomes
6713 * unused while the reference count is > 0: When there is a recursive
6714 * reference. Example:
6715 * :let l = [1, 2, 3]
6716 * :let d = {9: l}
6717 * :let l[1] = d
6718 *
6719 * Since this is quite unusual we handle this with garbage collection: every
6720 * once in a while find out which lists and dicts are not referenced from any
6721 * variable.
6722 *
6723 * Here is a good reference text about garbage collection (refers to Python
6724 * but it applies to all reference-counting mechanisms):
6725 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006726 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727
6728/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006729 * Do garbage collection for lists and dicts.
6730 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 int
6733garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006735 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 buf_T *buf;
6737 win_T *wp;
6738 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006739 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 int did_free;
6741 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742#ifdef FEAT_WINDOWS
6743 tabpage_T *tp;
6744#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006746 /* Only do this once. */
6747 want_garbage_collect = FALSE;
6748 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006749 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006750
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006751 /* We advance by two because we add one for items referenced through
6752 * previous_funccal. */
6753 current_copyID += COPYID_INC;
6754 copyID = current_copyID;
6755
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 /*
6757 * 1. Go through all accessible variables and mark all lists and dicts
6758 * with copyID.
6759 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760
6761 /* Don't free variables in the previous_funccal list unless they are only
6762 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006763 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6765 {
6766 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6767 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6768 }
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /* script-local variables */
6771 for (i = 1; i <= ga_scripts.ga_len; ++i)
6772 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6773
6774 /* buffer-local variables */
6775 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6776 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6777
6778 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6781
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 /* tabpage-local variables */
6784 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6785 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6786#endif
6787
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788 /* global variables */
6789 set_ref_in_ht(&globvarht, copyID);
6790
6791 /* function-local variables */
6792 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6796 }
6797
Bram Moolenaard812df62008-11-09 12:46:09 +00006798 /* v: vars */
6799 set_ref_in_ht(&vimvarht, copyID);
6800
Bram Moolenaar1dced572012-04-05 16:54:08 +02006801#ifdef FEAT_LUA
6802 set_ref_in_lua(copyID);
6803#endif
6804
Bram Moolenaardb913952012-06-29 12:54:53 +02006805#ifdef FEAT_PYTHON
6806 set_ref_in_python(copyID);
6807#endif
6808
6809#ifdef FEAT_PYTHON3
6810 set_ref_in_python3(copyID);
6811#endif
6812
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006813 /*
6814 * 2. Free lists and dictionaries that are not referenced.
6815 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006816 did_free = free_unref_items(copyID);
6817
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006818 /*
6819 * 3. Check if any funccal can be freed now.
6820 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006821 for (pfc = &previous_funccal; *pfc != NULL; )
6822 {
6823 if (can_free_funccal(*pfc, copyID))
6824 {
6825 fc = *pfc;
6826 *pfc = fc->caller;
6827 free_funccal(fc, TRUE);
6828 did_free = TRUE;
6829 did_free_funccal = TRUE;
6830 }
6831 else
6832 pfc = &(*pfc)->caller;
6833 }
6834 if (did_free_funccal)
6835 /* When a funccal was freed some more items might be garbage
6836 * collected, so run again. */
6837 (void)garbage_collect();
6838
6839 return did_free;
6840}
6841
6842/*
6843 * Free lists and dictionaries that are no longer referenced.
6844 */
6845 static int
6846free_unref_items(copyID)
6847 int copyID;
6848{
6849 dict_T *dd;
6850 list_T *ll;
6851 int did_free = FALSE;
6852
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 */
6856 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006859 /* Free the Dictionary and ordinary items it contains, but don't
6860 * recurse into Lists and Dictionaries, they will be in the list
6861 * of dicts or list of lists. */
6862 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863 did_free = TRUE;
6864
6865 /* restart, next dict may also have been freed */
6866 dd = first_dict;
6867 }
6868 else
6869 dd = dd->dv_used_next;
6870
6871 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 * Go through the list of lists and free items without the copyID.
6873 * But don't free a list that has a watcher (used in a for loop), these
6874 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 */
6876 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6878 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006880 /* Free the List and ordinary items it contains, but don't recurse
6881 * into Lists and Dictionaries, they will be in the list of dicts
6882 * or list of lists. */
6883 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 did_free = TRUE;
6885
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006886 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 ll = first_list;
6888 }
6889 else
6890 ll = ll->lv_used_next;
6891
6892 return did_free;
6893}
6894
6895/*
6896 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6897 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006898 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899set_ref_in_ht(ht, copyID)
6900 hashtab_T *ht;
6901 int copyID;
6902{
6903 int todo;
6904 hashitem_T *hi;
6905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006906 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 for (hi = ht->ht_array; todo > 0; ++hi)
6908 if (!HASHITEM_EMPTY(hi))
6909 {
6910 --todo;
6911 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6912 }
6913}
6914
6915/*
6916 * Mark all lists and dicts referenced through list "l" with "copyID".
6917 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006918 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919set_ref_in_list(l, copyID)
6920 list_T *l;
6921 int copyID;
6922{
6923 listitem_T *li;
6924
6925 for (li = l->lv_first; li != NULL; li = li->li_next)
6926 set_ref_in_item(&li->li_tv, copyID);
6927}
6928
6929/*
6930 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6931 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006932 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933set_ref_in_item(tv, copyID)
6934 typval_T *tv;
6935 int copyID;
6936{
6937 dict_T *dd;
6938 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939
6940 switch (tv->v_type)
6941 {
6942 case VAR_DICT:
6943 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006944 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006946 /* Didn't see this dict yet. */
6947 dd->dv_copyID = copyID;
6948 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951
6952 case VAR_LIST:
6953 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006954 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 /* Didn't see this list yet. */
6957 ll->lv_copyID = copyID;
6958 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963}
6964
6965/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 * Allocate an empty header for a dictionary.
6967 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006968 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969dict_alloc()
6970{
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006975 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006976 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 if (first_dict != NULL)
6978 first_dict->dv_used_prev = d;
6979 d->dv_used_next = first_dict;
6980 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006985 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006987 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006989 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990}
6991
6992/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006993 * Allocate an empty dict for a return value.
6994 * Returns OK or FAIL.
6995 */
6996 static int
6997rettv_dict_alloc(rettv)
6998 typval_T *rettv;
6999{
7000 dict_T *d = dict_alloc();
7001
7002 if (d == NULL)
7003 return FAIL;
7004
7005 rettv->vval.v_dict = d;
7006 rettv->v_type = VAR_DICT;
7007 ++d->dv_refcount;
7008 return OK;
7009}
7010
7011
7012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013 * Unreference a Dictionary: decrement the reference count and free it when it
7014 * becomes zero.
7015 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007016 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 if (d != NULL && --d->dv_refcount <= 0)
7021 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
7025 * Free a Dictionary, including all items it contains.
7026 * Ignores the reference count.
7027 */
7028 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007029dict_free(d, recurse)
7030 dict_T *d;
7031 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007035 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 /* Remove the dict from the list of dicts for garbage collection. */
7038 if (d->dv_used_prev == NULL)
7039 first_dict = d->dv_used_next;
7040 else
7041 d->dv_used_prev->dv_used_next = d->dv_used_next;
7042 if (d->dv_used_next != NULL)
7043 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7044
7045 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007047 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if (!HASHITEM_EMPTY(hi))
7051 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052 /* Remove the item before deleting it, just in case there is
7053 * something recursive causing trouble. */
7054 di = HI2DI(hi);
7055 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007056 if (recurse || (di->di_tv.v_type != VAR_LIST
7057 && di->di_tv.v_type != VAR_DICT))
7058 clear_tv(&di->di_tv);
7059 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007060 --todo;
7061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064 vim_free(d);
7065}
7066
7067/*
7068 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 * The "key" is copied to the new item.
7070 * Note that the value of the item "di_tv" still needs to be initialized!
7071 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007073 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074dictitem_alloc(key)
7075 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076{
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 di->di_flags = 0;
7084 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086}
7087
7088/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 * Make a copy of a Dictionary item.
7090 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094{
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007097 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7098 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099 if (di != NULL)
7100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007102 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 copy_tv(&org->di_tv, &di->di_tv);
7104 }
7105 return di;
7106}
7107
7108/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 * Remove item "item" from Dictionary "dict" and free it.
7110 */
7111 static void
7112dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dict_T *dict;
7114 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115{
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 if (HASHITEM_EMPTY(hi))
7120 EMSG2(_(e_intern2), "dictitem_remove()");
7121 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 dictitem_free(item);
7124}
7125
7126/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127 * Free a dict item. Also clears the value.
7128 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007129 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 clear_tv(&item->di_tv);
7134 vim_free(item);
7135}
7136
7137/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7139 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007140 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 * Returns NULL when out of memory.
7142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148{
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *copy;
7150 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153
7154 if (orig == NULL)
7155 return NULL;
7156
7157 copy = dict_alloc();
7158 if (copy != NULL)
7159 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007160 if (copyID != 0)
7161 {
7162 orig->dv_copyID = copyID;
7163 orig->dv_copydict = copy;
7164 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007165 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170 --todo;
7171
7172 di = dictitem_alloc(hi->hi_key);
7173 if (di == NULL)
7174 break;
7175 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176 {
7177 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7178 copyID) == FAIL)
7179 {
7180 vim_free(di);
7181 break;
7182 }
7183 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 else
7185 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7186 if (dict_add(copy, di) == FAIL)
7187 {
7188 dictitem_free(di);
7189 break;
7190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195 if (todo > 0)
7196 {
7197 dict_unref(copy);
7198 copy = NULL;
7199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 }
7201
7202 return copy;
7203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007207 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007209 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
7212 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215}
7216
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007218 * Add a number or string entry to dictionary "d".
7219 * When "str" is NULL use number "nr", otherwise use "str".
7220 * Returns FAIL when out of memory and when key already exists.
7221 */
7222 int
7223dict_add_nr_str(d, key, nr, str)
7224 dict_T *d;
7225 char *key;
7226 long nr;
7227 char_u *str;
7228{
7229 dictitem_T *item;
7230
7231 item = dictitem_alloc((char_u *)key);
7232 if (item == NULL)
7233 return FAIL;
7234 item->di_tv.v_lock = 0;
7235 if (str == NULL)
7236 {
7237 item->di_tv.v_type = VAR_NUMBER;
7238 item->di_tv.vval.v_number = nr;
7239 }
7240 else
7241 {
7242 item->di_tv.v_type = VAR_STRING;
7243 item->di_tv.vval.v_string = vim_strsave(str);
7244 }
7245 if (dict_add(d, item) == FAIL)
7246 {
7247 dictitem_free(item);
7248 return FAIL;
7249 }
7250 return OK;
7251}
7252
7253/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007254 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007255 * Returns FAIL when out of memory and when key already exists.
7256 */
7257 int
7258dict_add_list(d, key, list)
7259 dict_T *d;
7260 char *key;
7261 list_T *list;
7262{
7263 dictitem_T *item;
7264
7265 item = dictitem_alloc((char_u *)key);
7266 if (item == NULL)
7267 return FAIL;
7268 item->di_tv.v_lock = 0;
7269 item->di_tv.v_type = VAR_LIST;
7270 item->di_tv.vval.v_list = list;
7271 if (dict_add(d, item) == FAIL)
7272 {
7273 dictitem_free(item);
7274 return FAIL;
7275 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007276 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007277 return OK;
7278}
7279
7280/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 * Get the number of items in a Dictionary.
7282 */
7283 static long
7284dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 if (d == NULL)
7288 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290}
7291
7292/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 * Find item "key[len]" in Dictionary "d".
7294 * If "len" is negative use strlen(key).
7295 * Returns NULL when not found.
7296 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007297 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 char_u *key;
7301 int len;
7302{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303#define AKEYLEN 200
7304 char_u buf[AKEYLEN];
7305 char_u *akey;
7306 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 if (len < 0)
7310 akey = key;
7311 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 tofree = akey = vim_strnsave(key, len);
7314 if (akey == NULL)
7315 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 else
7318 {
7319 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007320 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 akey = buf;
7322 }
7323
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 vim_free(tofree);
7326 if (HASHITEM_EMPTY(hi))
7327 return NULL;
7328 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329}
7330
7331/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007332 * Get a string item from a dictionary.
7333 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007334 * Returns NULL if the entry doesn't exist or out of memory.
7335 */
7336 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007337get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338 dict_T *d;
7339 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007341{
7342 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007344
7345 di = dict_find(d, key, -1);
7346 if (di == NULL)
7347 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348 s = get_tv_string(&di->di_tv);
7349 if (save && s != NULL)
7350 s = vim_strsave(s);
7351 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352}
7353
7354/*
7355 * Get a number item from a dictionary.
7356 * Returns 0 if the entry doesn't exist or out of memory.
7357 */
7358 long
7359get_dict_number(d, key)
7360 dict_T *d;
7361 char_u *key;
7362{
7363 dictitem_T *di;
7364
7365 di = dict_find(d, key, -1);
7366 if (di == NULL)
7367 return 0;
7368 return get_tv_number(&di->di_tv);
7369}
7370
7371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 * Return an allocated string with the string representation of a Dictionary.
7373 * May return NULL.
7374 */
7375 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007376dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007378 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379{
7380 garray_T ga;
7381 int first = TRUE;
7382 char_u *tofree;
7383 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 return NULL;
7391 ga_init2(&ga, (int)sizeof(char), 80);
7392 ga_append(&ga, '{');
7393
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007394 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 --todo;
7400
7401 if (first)
7402 first = FALSE;
7403 else
7404 ga_concat(&ga, (char_u *)", ");
7405
7406 tofree = string_quote(hi->hi_key, FALSE);
7407 if (tofree != NULL)
7408 {
7409 ga_concat(&ga, tofree);
7410 vim_free(tofree);
7411 }
7412 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 if (s != NULL)
7415 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (s == NULL)
7418 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 if (todo > 0)
7422 {
7423 vim_free(ga.ga_data);
7424 return NULL;
7425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426
7427 ga_append(&ga, '}');
7428 ga_append(&ga, NUL);
7429 return (char_u *)ga.ga_data;
7430}
7431
7432/*
7433 * Allocate a variable for a Dictionary and fill it from "*arg".
7434 * Return OK or FAIL. Returns NOTDONE for {expr}.
7435 */
7436 static int
7437get_dict_tv(arg, rettv, evaluate)
7438 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 int evaluate;
7441{
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 dict_T *d = NULL;
7443 typval_T tvkey;
7444 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007445 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449
7450 /*
7451 * First check if it's not a curly-braces thing: {expr}.
7452 * Must do this without evaluating, otherwise a function may be called
7453 * twice. Unfortunately this means we need to call eval1() twice for the
7454 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 if (*start != '}')
7458 {
7459 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7460 return FAIL;
7461 if (*start == '}')
7462 return NOTDONE;
7463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464
7465 if (evaluate)
7466 {
7467 d = dict_alloc();
7468 if (d == NULL)
7469 return FAIL;
7470 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007471 tvkey.v_type = VAR_UNKNOWN;
7472 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473
7474 *arg = skipwhite(*arg + 1);
7475 while (**arg != '}' && **arg != NUL)
7476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 goto failret;
7479 if (**arg != ':')
7480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007481 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 goto failret;
7484 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007485 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007487 key = get_tv_string_buf_chk(&tvkey, buf);
7488 if (key == NULL || *key == NUL)
7489 {
7490 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7491 if (key != NULL)
7492 EMSG(_(e_emptykey));
7493 clear_tv(&tvkey);
7494 goto failret;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
7498 *arg = skipwhite(*arg + 1);
7499 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7500 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 if (evaluate)
7502 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 goto failret;
7504 }
7505 if (evaluate)
7506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 if (item != NULL)
7509 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007510 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 clear_tv(&tv);
7513 goto failret;
7514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 item = dictitem_alloc(key);
7516 clear_tv(&tvkey);
7517 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007520 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (dict_add(d, item) == FAIL)
7522 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 }
7524 }
7525
7526 if (**arg == '}')
7527 break;
7528 if (**arg != ',')
7529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007530 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 goto failret;
7532 }
7533 *arg = skipwhite(*arg + 1);
7534 }
7535
7536 if (**arg != '}')
7537 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007538 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539failret:
7540 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007541 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 return FAIL;
7543 }
7544
7545 *arg = skipwhite(*arg + 1);
7546 if (evaluate)
7547 {
7548 rettv->v_type = VAR_DICT;
7549 rettv->vval.v_dict = d;
7550 ++d->dv_refcount;
7551 }
7552
7553 return OK;
7554}
7555
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007557 * Return a string with the string representation of a variable.
7558 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007559 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007560 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007561 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007562 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 */
7564 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007568 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 static int recurse = 0;
7572 char_u *r = NULL;
7573
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007576 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007577 *tofree = NULL;
7578 return NULL;
7579 }
7580 ++recurse;
7581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 switch (tv->v_type)
7583 {
7584 case VAR_FUNC:
7585 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 r = tv->vval.v_string;
7587 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590 if (tv->vval.v_list == NULL)
7591 {
7592 *tofree = NULL;
7593 r = NULL;
7594 }
7595 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7596 {
7597 *tofree = NULL;
7598 r = (char_u *)"[...]";
7599 }
7600 else
7601 {
7602 tv->vval.v_list->lv_copyID = copyID;
7603 *tofree = list2string(tv, copyID);
7604 r = *tofree;
7605 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007606 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609 if (tv->vval.v_dict == NULL)
7610 {
7611 *tofree = NULL;
7612 r = NULL;
7613 }
7614 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7615 {
7616 *tofree = NULL;
7617 r = (char_u *)"{...}";
7618 }
7619 else
7620 {
7621 tv->vval.v_dict->dv_copyID = copyID;
7622 *tofree = dict2string(tv, copyID);
7623 r = *tofree;
7624 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 case VAR_STRING:
7628 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 *tofree = NULL;
7630 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007633#ifdef FEAT_FLOAT
7634 case VAR_FLOAT:
7635 *tofree = NULL;
7636 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7637 r = numbuf;
7638 break;
7639#endif
7640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645
7646 --recurse;
7647 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648}
7649
7650/*
7651 * Return a string with the string representation of a variable.
7652 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7653 * "numbuf" is used for a number.
7654 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007655 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 */
7657 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 char_u **tofree;
7661 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007663{
7664 switch (tv->v_type)
7665 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666 case VAR_FUNC:
7667 *tofree = string_quote(tv->vval.v_string, TRUE);
7668 return *tofree;
7669 case VAR_STRING:
7670 *tofree = string_quote(tv->vval.v_string, FALSE);
7671 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 case VAR_FLOAT:
7674 *tofree = NULL;
7675 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7676 return numbuf;
7677#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007684 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007685 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686}
7687
7688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 * Return string "str" in ' quotes, doubling ' characters.
7690 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 */
7693 static char_u *
7694string_quote(str, function)
7695 char_u *str;
7696 int function;
7697{
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007699 char_u *p, *r, *s;
7700
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 len = (function ? 13 : 3);
7702 if (str != NULL)
7703 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007704 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 for (p = str; *p != NUL; mb_ptr_adv(p))
7706 if (*p == '\'')
7707 ++len;
7708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007709 s = r = alloc(len);
7710 if (r != NULL)
7711 {
7712 if (function)
7713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 r += 10;
7716 }
7717 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 if (str != NULL)
7720 for (p = str; *p != NUL; )
7721 {
7722 if (*p == '\'')
7723 *r++ = '\'';
7724 MB_COPY_CHAR(p, r);
7725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 if (function)
7728 *r++ = ')';
7729 *r++ = NUL;
7730 }
7731 return s;
7732}
7733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735/*
7736 * Convert the string "text" to a floating point number.
7737 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7738 * this always uses a decimal point.
7739 * Returns the length of the text that was consumed.
7740 */
7741 static int
7742string2float(text, value)
7743 char_u *text;
7744 float_T *value; /* result stored here */
7745{
7746 char *s = (char *)text;
7747 float_T f;
7748
7749 f = strtod(s, &s);
7750 *value = f;
7751 return (int)((char_u *)s - text);
7752}
7753#endif
7754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * Get the value of an environment variable.
7757 * "arg" is pointing to the '$'. It is advanced to after the name.
7758 * If the environment variable was not set, silently assume it is empty.
7759 * Always return OK.
7760 */
7761 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 int evaluate;
7766{
7767 char_u *string = NULL;
7768 int len;
7769 int cc;
7770 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007771 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772
7773 ++*arg;
7774 name = *arg;
7775 len = get_env_len(arg);
7776 if (evaluate)
7777 {
7778 if (len != 0)
7779 {
7780 cc = name[len];
7781 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 /* first try vim_getenv(), fast for normal environment vars */
7783 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 {
7786 if (!mustfree)
7787 string = vim_strsave(string);
7788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 else
7790 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007791 if (mustfree)
7792 vim_free(string);
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 /* next try expanding things like $VIM and ${HOME} */
7795 string = expand_env_save(name - 1);
7796 if (string != NULL && *string == '$')
7797 {
7798 vim_free(string);
7799 string = NULL;
7800 }
7801 }
7802 name[len] = cc;
7803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->v_type = VAR_STRING;
7805 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807
7808 return OK;
7809}
7810
7811/*
7812 * Array with names and number of arguments of all internal functions
7813 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7814 */
7815static struct fst
7816{
7817 char *f_name; /* function name */
7818 char f_min_argc; /* minimal number of arguments */
7819 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007821 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822} functions[] =
7823{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#ifdef FEAT_FLOAT
7825 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007826 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007828 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007829 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"append", 2, 2, f_append},
7831 {"argc", 0, 0, f_argc},
7832 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007833 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007834#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007835 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007840 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"bufexists", 1, 1, f_bufexists},
7842 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7843 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7844 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7845 {"buflisted", 1, 1, f_buflisted},
7846 {"bufloaded", 1, 1, f_bufloaded},
7847 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007848 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"bufwinnr", 1, 1, f_bufwinnr},
7850 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007851 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007853#ifdef FEAT_FLOAT
7854 {"ceil", 1, 1, f_ceil},
7855#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007856 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007857 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007861#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007862 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007863 {"complete_add", 1, 1, f_complete_add},
7864 {"complete_check", 0, 0, f_complete_check},
7865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#ifdef FEAT_FLOAT
7869 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007870 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007874 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007875 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"delete", 1, 1, f_delete},
7877 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007878 {"diff_filler", 1, 1, f_diff_filler},
7879 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007880 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"eventhandler", 0, 0, f_eventhandler},
7884 {"executable", 1, 1, f_executable},
7885 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007886#ifdef FEAT_FLOAT
7887 {"exp", 1, 1, f_exp},
7888#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007889 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007891 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7893 {"filereadable", 1, 1, f_filereadable},
7894 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007896 {"finddir", 1, 3, f_finddir},
7897 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898#ifdef FEAT_FLOAT
7899 {"float2nr", 1, 1, f_float2nr},
7900 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007901 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007903 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"fnamemodify", 2, 2, f_fnamemodify},
7905 {"foldclosed", 1, 1, f_foldclosed},
7906 {"foldclosedend", 1, 1, f_foldclosedend},
7907 {"foldlevel", 1, 1, f_foldlevel},
7908 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007909 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007912 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007913 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007914 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"getbufvar", 2, 2, f_getbufvar},
7916 {"getchar", 0, 1, f_getchar},
7917 {"getcharmod", 0, 0, f_getcharmod},
7918 {"getcmdline", 0, 0, f_getcmdline},
7919 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007920 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007922 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007923 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getfsize", 1, 1, f_getfsize},
7925 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007926 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007928 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007929 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007930 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007931 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007932 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007933 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007935 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007936 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"getwinposx", 0, 0, f_getwinposx},
7938 {"getwinposy", 0, 0, f_getwinposy},
7939 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007940 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007941 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007944 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007945 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7947 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7948 {"histadd", 2, 2, f_histadd},
7949 {"histdel", 1, 2, f_histdel},
7950 {"histget", 1, 2, f_histget},
7951 {"histnr", 1, 1, f_histnr},
7952 {"hlID", 1, 1, f_hlID},
7953 {"hlexists", 1, 1, f_hlexists},
7954 {"hostname", 0, 0, f_hostname},
7955 {"iconv", 3, 3, f_iconv},
7956 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007958 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007960 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"inputrestore", 0, 0, f_inputrestore},
7962 {"inputsave", 0, 0, f_inputsave},
7963 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007965 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007967 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"libcall", 3, 3, f_libcall},
7974 {"libcallnr", 3, 3, f_libcallnr},
7975 {"line", 1, 1, f_line},
7976 {"line2byte", 1, 1, f_line2byte},
7977 {"lispindent", 1, 1, f_lispindent},
7978 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007979#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007980 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007981 {"log10", 1, 1, f_log10},
7982#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007983#ifdef FEAT_LUA
7984 {"luaeval", 1, 2, f_luaeval},
7985#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007987 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007988 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007989 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007990 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007991 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007992 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007994 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007995 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007996 {"max", 1, 1, f_max},
7997 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007998#ifdef vim_mkdir
7999 {"mkdir", 1, 3, f_mkdir},
8000#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008001 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008002#ifdef FEAT_MZSCHEME
8003 {"mzeval", 1, 1, f_mzeval},
8004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008006 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008007 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008008 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008009#ifdef FEAT_FLOAT
8010 {"pow", 2, 2, f_pow},
8011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008013 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008014 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008015#ifdef FEAT_PYTHON3
8016 {"py3eval", 1, 1, f_py3eval},
8017#endif
8018#ifdef FEAT_PYTHON
8019 {"pyeval", 1, 1, f_pyeval},
8020#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008022 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008023 {"reltime", 0, 2, f_reltime},
8024 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"remote_expr", 2, 3, f_remote_expr},
8026 {"remote_foreground", 1, 1, f_remote_foreground},
8027 {"remote_peek", 1, 2, f_remote_peek},
8028 {"remote_read", 1, 1, f_remote_read},
8029 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008030 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008032 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008034 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035#ifdef FEAT_FLOAT
8036 {"round", 1, 1, f_round},
8037#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008038 {"screencol", 0, 0, f_screencol},
8039 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008040 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008041 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008042 {"searchpair", 3, 7, f_searchpair},
8043 {"searchpairpos", 3, 7, f_searchpairpos},
8044 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"server2client", 2, 2, f_server2client},
8046 {"serverlist", 0, 0, f_serverlist},
8047 {"setbufvar", 3, 3, f_setbufvar},
8048 {"setcmdpos", 1, 1, f_setcmdpos},
8049 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008050 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008051 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008052 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008053 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008055 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008056 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008058 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008059 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008061#ifdef FEAT_FLOAT
8062 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008063 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008065 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008066 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008067 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008068 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008069 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"sqrt", 1, 1, f_sqrt},
8072 {"str2float", 1, 1, f_str2float},
8073#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008074 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008075 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008076 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077#ifdef HAVE_STRFTIME
8078 {"strftime", 1, 2, f_strftime},
8079#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"strlen", 1, 1, f_strlen},
8083 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008084 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008086 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"submatch", 1, 1, f_submatch},
8088 {"substitute", 4, 4, f_substitute},
8089 {"synID", 3, 3, f_synID},
8090 {"synIDattr", 2, 3, f_synIDattr},
8091 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008092 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008093 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008094 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008095 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008096 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008097 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008098 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008099 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008100#ifdef FEAT_FLOAT
8101 {"tan", 1, 1, f_tan},
8102 {"tanh", 1, 1, f_tanh},
8103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008105 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"tolower", 1, 1, f_tolower},
8107 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008108 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#ifdef FEAT_FLOAT
8110 {"trunc", 1, 1, f_trunc},
8111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008113 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008114 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008115 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"virtcol", 1, 1, f_virtcol},
8117 {"visualmode", 0, 1, f_visualmode},
8118 {"winbufnr", 1, 1, f_winbufnr},
8119 {"wincol", 0, 0, f_wincol},
8120 {"winheight", 1, 1, f_winheight},
8121 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008122 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008124 {"winrestview", 1, 1, f_winrestview},
8125 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008127 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008128 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129};
8130
8131#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8132
8133/*
8134 * Function given to ExpandGeneric() to obtain the list of internal
8135 * or user defined function names.
8136 */
8137 char_u *
8138get_function_name(xp, idx)
8139 expand_T *xp;
8140 int idx;
8141{
8142 static int intidx = -1;
8143 char_u *name;
8144
8145 if (idx == 0)
8146 intidx = -1;
8147 if (intidx < 0)
8148 {
8149 name = get_user_func_name(xp, idx);
8150 if (name != NULL)
8151 return name;
8152 }
8153 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8154 {
8155 STRCPY(IObuff, functions[intidx].f_name);
8156 STRCAT(IObuff, "(");
8157 if (functions[intidx].f_max_argc == 0)
8158 STRCAT(IObuff, ")");
8159 return IObuff;
8160 }
8161
8162 return NULL;
8163}
8164
8165/*
8166 * Function given to ExpandGeneric() to obtain the list of internal or
8167 * user defined variable or function names.
8168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 char_u *
8170get_expr_name(xp, idx)
8171 expand_T *xp;
8172 int idx;
8173{
8174 static int intidx = -1;
8175 char_u *name;
8176
8177 if (idx == 0)
8178 intidx = -1;
8179 if (intidx < 0)
8180 {
8181 name = get_function_name(xp, idx);
8182 if (name != NULL)
8183 return name;
8184 }
8185 return get_user_var_name(xp, ++intidx);
8186}
8187
8188#endif /* FEAT_CMDL_COMPL */
8189
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008190#if defined(EBCDIC) || defined(PROTO)
8191/*
8192 * Compare struct fst by function name.
8193 */
8194 static int
8195compare_func_name(s1, s2)
8196 const void *s1;
8197 const void *s2;
8198{
8199 struct fst *p1 = (struct fst *)s1;
8200 struct fst *p2 = (struct fst *)s2;
8201
8202 return STRCMP(p1->f_name, p2->f_name);
8203}
8204
8205/*
8206 * Sort the function table by function name.
8207 * The sorting of the table above is ASCII dependant.
8208 * On machines using EBCDIC we have to sort it.
8209 */
8210 static void
8211sortFunctions()
8212{
8213 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8214
8215 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8216}
8217#endif
8218
8219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220/*
8221 * Find internal function in table above.
8222 * Return index, or -1 if not found
8223 */
8224 static int
8225find_internal_func(name)
8226 char_u *name; /* name of the function */
8227{
8228 int first = 0;
8229 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8230 int cmp;
8231 int x;
8232
8233 /*
8234 * Find the function name in the table. Binary search.
8235 */
8236 while (first <= last)
8237 {
8238 x = first + ((unsigned)(last - first) >> 1);
8239 cmp = STRCMP(name, functions[x].f_name);
8240 if (cmp < 0)
8241 last = x - 1;
8242 else if (cmp > 0)
8243 first = x + 1;
8244 else
8245 return x;
8246 }
8247 return -1;
8248}
8249
8250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008251 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8252 * name it contains, otherwise return "name".
8253 */
8254 static char_u *
8255deref_func_name(name, lenp)
8256 char_u *name;
8257 int *lenp;
8258{
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008260 int cc;
8261
8262 cc = name[*lenp];
8263 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008264 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008266 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008267 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 {
8270 *lenp = 0;
8271 return (char_u *)""; /* just in case */
8272 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008273 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 }
8276
8277 return name;
8278}
8279
8280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 * Allocate a variable for the result of a function.
8282 * Return OK or FAIL.
8283 */
8284 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8286 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 char_u *name; /* name of the function */
8288 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 char_u **arg; /* argument, pointing to the '(' */
8291 linenr_T firstline; /* first line of range */
8292 linenr_T lastline; /* last line of range */
8293 int *doesrange; /* return: function handled range */
8294 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
8297 char_u *argp;
8298 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008299 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 int argcount = 0; /* number of arguments found */
8301
8302 /*
8303 * Get the arguments.
8304 */
8305 argp = *arg;
8306 while (argcount < MAX_FUNC_ARGS)
8307 {
8308 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8309 if (*argp == ')' || *argp == ',' || *argp == NUL)
8310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8312 {
8313 ret = FAIL;
8314 break;
8315 }
8316 ++argcount;
8317 if (*argp != ',')
8318 break;
8319 }
8320 if (*argp == ')')
8321 ++argp;
8322 else
8323 ret = FAIL;
8324
8325 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008327 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 {
8330 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008331 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008333 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
8336 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
8339 *arg = skipwhite(argp);
8340 return ret;
8341}
8342
8343
8344/*
8345 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008346 * Return OK when the function can't be called, FAIL otherwise.
8347 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 */
8349 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008350call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008351 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008352 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008356 typval_T *argvars; /* vars for arguments, must have "argcount"
8357 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 linenr_T firstline; /* first line of range */
8359 linenr_T lastline; /* last line of range */
8360 int *doesrange; /* return: function handled range */
8361 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#define ERROR_UNKNOWN 0
8366#define ERROR_TOOMANY 1
8367#define ERROR_TOOFEW 2
8368#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369#define ERROR_DICT 4
8370#define ERROR_NONE 5
8371#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int error = ERROR_NONE;
8373 int i;
8374 int llen;
8375 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376#define FLEN_FIXED 40
8377 char_u fname_buf[FLEN_FIXED + 1];
8378 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008379 char_u *name;
8380
8381 /* Make a copy of the name, if it comes from a funcref variable it could
8382 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008383 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008384 if (name == NULL)
8385 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387 /*
8388 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8389 * Change <SNR>123_name() to K_SNR 123_name().
8390 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 llen = eval_fname_script(name);
8393 if (llen > 0)
8394 {
8395 fname_buf[0] = K_SPECIAL;
8396 fname_buf[1] = KS_EXTRA;
8397 fname_buf[2] = (int)KE_SNR;
8398 i = 3;
8399 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8400 {
8401 if (current_SID <= 0)
8402 error = ERROR_SCRIPT;
8403 else
8404 {
8405 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8406 i = (int)STRLEN(fname_buf);
8407 }
8408 }
8409 if (i + STRLEN(name + llen) < FLEN_FIXED)
8410 {
8411 STRCPY(fname_buf + i, name + llen);
8412 fname = fname_buf;
8413 }
8414 else
8415 {
8416 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8417 if (fname == NULL)
8418 error = ERROR_OTHER;
8419 else
8420 {
8421 mch_memmove(fname, fname_buf, (size_t)i);
8422 STRCPY(fname + i, name + llen);
8423 }
8424 }
8425 }
8426 else
8427 fname = name;
8428
8429 *doesrange = FALSE;
8430
8431
8432 /* execute the function if no errors detected and executing */
8433 if (evaluate && error == ERROR_NONE)
8434 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008435 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8436 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 error = ERROR_UNKNOWN;
8438
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008439 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
8441 /*
8442 * User defined function.
8443 */
8444 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008445
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008447 /* Trigger FuncUndefined event, may load the function. */
8448 if (fp == NULL
8449 && apply_autocmds(EVENT_FUNCUNDEFINED,
8450 fname, fname, TRUE, NULL)
8451 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 fp = find_func(fname);
8455 }
8456#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008458 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008459 {
8460 /* loaded a package, search for the function again */
8461 fp = find_func(fname);
8462 }
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 if (fp != NULL)
8465 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008466 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008468 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008470 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008472 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 else
8475 {
8476 /*
8477 * Call the user function.
8478 * Save and restore search patterns, script variables and
8479 * redo buffer.
8480 */
8481 save_search_patterns();
8482 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008485 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8487 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8488 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008489 /* Function was unreferenced while being used, free it
8490 * now. */
8491 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 restoreRedobuff();
8493 restore_search_patterns();
8494 error = ERROR_NONE;
8495 }
8496 }
8497 }
8498 else
8499 {
8500 /*
8501 * Find the function name in the table, call its implementation.
8502 */
8503 i = find_internal_func(fname);
8504 if (i >= 0)
8505 {
8506 if (argcount < functions[i].f_min_argc)
8507 error = ERROR_TOOFEW;
8508 else if (argcount > functions[i].f_max_argc)
8509 error = ERROR_TOOMANY;
8510 else
8511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 error = ERROR_NONE;
8515 }
8516 }
8517 }
8518 /*
8519 * The function call (or "FuncUndefined" autocommand sequence) might
8520 * have been aborted by an error, an interrupt, or an explicitly thrown
8521 * exception that has not been caught so far. This situation can be
8522 * tested for by calling aborting(). For an error in an internal
8523 * function or for the "E132" error in call_user_func(), however, the
8524 * throw point at which the "force_abort" flag (temporarily reset by
8525 * emsg()) is normally updated has not been reached yet. We need to
8526 * update that flag first to make aborting() reliable.
8527 */
8528 update_force_abort();
8529 }
8530 if (error == ERROR_NONE)
8531 ret = OK;
8532
8533 /*
8534 * Report an error unless the argument evaluation or function call has been
8535 * cancelled due to an aborting error, an interrupt, or an exception.
8536 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008537 if (!aborting())
8538 {
8539 switch (error)
8540 {
8541 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008542 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008543 break;
8544 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008545 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008546 break;
8547 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 name);
8550 break;
8551 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 name);
8554 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008555 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008556 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 name);
8558 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 }
8560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (fname != name && fname != fname_buf)
8563 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008564 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566 return ret;
8567}
8568
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008569/*
8570 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008571 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008572 */
8573 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008574emsg_funcname(ermsg, name)
8575 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008576 char_u *name;
8577{
8578 char_u *p;
8579
8580 if (*name == K_SPECIAL)
8581 p = concat_str((char_u *)"<SNR>", name + 3);
8582 else
8583 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008584 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008585 if (p != name)
8586 vim_free(p);
8587}
8588
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008589/*
8590 * Return TRUE for a non-zero Number and a non-empty String.
8591 */
8592 static int
8593non_zero_arg(argvars)
8594 typval_T *argvars;
8595{
8596 return ((argvars[0].v_type == VAR_NUMBER
8597 && argvars[0].vval.v_number != 0)
8598 || (argvars[0].v_type == VAR_STRING
8599 && argvars[0].vval.v_string != NULL
8600 && *argvars[0].vval.v_string != NUL));
8601}
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603/*********************************************
8604 * Implementation of the built-in functions
8605 */
8606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008608static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8609
8610/*
8611 * Get the float value of "argvars[0]" into "f".
8612 * Returns FAIL when the argument is not a Number or Float.
8613 */
8614 static int
8615get_float_arg(argvars, f)
8616 typval_T *argvars;
8617 float_T *f;
8618{
8619 if (argvars[0].v_type == VAR_FLOAT)
8620 {
8621 *f = argvars[0].vval.v_float;
8622 return OK;
8623 }
8624 if (argvars[0].v_type == VAR_NUMBER)
8625 {
8626 *f = (float_T)argvars[0].vval.v_number;
8627 return OK;
8628 }
8629 EMSG(_("E808: Number or Float required"));
8630 return FAIL;
8631}
8632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008633/*
8634 * "abs(expr)" function
8635 */
8636 static void
8637f_abs(argvars, rettv)
8638 typval_T *argvars;
8639 typval_T *rettv;
8640{
8641 if (argvars[0].v_type == VAR_FLOAT)
8642 {
8643 rettv->v_type = VAR_FLOAT;
8644 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8645 }
8646 else
8647 {
8648 varnumber_T n;
8649 int error = FALSE;
8650
8651 n = get_tv_number_chk(&argvars[0], &error);
8652 if (error)
8653 rettv->vval.v_number = -1;
8654 else if (n > 0)
8655 rettv->vval.v_number = n;
8656 else
8657 rettv->vval.v_number = -n;
8658 }
8659}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008660
8661/*
8662 * "acos()" function
8663 */
8664 static void
8665f_acos(argvars, rettv)
8666 typval_T *argvars;
8667 typval_T *rettv;
8668{
8669 float_T f;
8670
8671 rettv->v_type = VAR_FLOAT;
8672 if (get_float_arg(argvars, &f) == OK)
8673 rettv->vval.v_float = acos(f);
8674 else
8675 rettv->vval.v_float = 0.0;
8676}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677#endif
8678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008680 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 */
8682 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008683f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 typval_T *argvars;
8685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008690 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008692 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008693 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008694 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 }
8697 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 EMSG(_(e_listreq));
8699}
8700
8701/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008702 * "and(expr, expr)" function
8703 */
8704 static void
8705f_and(argvars, rettv)
8706 typval_T *argvars;
8707 typval_T *rettv;
8708{
8709 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8710 & get_tv_number_chk(&argvars[1], NULL);
8711}
8712
8713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008714 * "append(lnum, string/list)" function
8715 */
8716 static void
8717f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 typval_T *argvars;
8719 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720{
8721 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 list_T *l = NULL;
8724 listitem_T *li = NULL;
8725 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726 long added = 0;
8727
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728 lnum = get_tv_lnum(argvars);
8729 if (lnum >= 0
8730 && lnum <= curbuf->b_ml.ml_line_count
8731 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008732 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008733 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008734 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008735 l = argvars[1].vval.v_list;
8736 if (l == NULL)
8737 return;
8738 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008739 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008740 for (;;)
8741 {
8742 if (l == NULL)
8743 tv = &argvars[1]; /* append a string */
8744 else if (li == NULL)
8745 break; /* end of list */
8746 else
8747 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 line = get_tv_string_chk(tv);
8749 if (line == NULL) /* type error */
8750 {
8751 rettv->vval.v_number = 1; /* Failed */
8752 break;
8753 }
8754 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008755 ++added;
8756 if (l == NULL)
8757 break;
8758 li = li->li_next;
8759 }
8760
8761 appended_lines_mark(lnum, added);
8762 if (curwin->w_cursor.lnum > lnum)
8763 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 else
8766 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767}
8768
8769/*
8770 * "argc()" function
8771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008774 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
8780/*
8781 * "argidx()" function
8782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789}
8790
8791/*
8792 * "argv(nr)" function
8793 */
8794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *argvars;
8797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 int idx;
8800
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008801 if (argvars[0].v_type != VAR_UNKNOWN)
8802 {
8803 idx = get_tv_number_chk(&argvars[0], NULL);
8804 if (idx >= 0 && idx < ARGCOUNT)
8805 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8806 else
8807 rettv->vval.v_string = NULL;
8808 rettv->v_type = VAR_STRING;
8809 }
8810 else if (rettv_list_alloc(rettv) == OK)
8811 for (idx = 0; idx < ARGCOUNT; ++idx)
8812 list_append_string(rettv->vval.v_list,
8813 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008816#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008817/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008818 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008819 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008820 static void
8821f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008823 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008824{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008825 float_T f;
8826
8827 rettv->v_type = VAR_FLOAT;
8828 if (get_float_arg(argvars, &f) == OK)
8829 rettv->vval.v_float = asin(f);
8830 else
8831 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832}
8833
8834/*
8835 * "atan()" function
8836 */
8837 static void
8838f_atan(argvars, rettv)
8839 typval_T *argvars;
8840 typval_T *rettv;
8841{
8842 float_T f;
8843
8844 rettv->v_type = VAR_FLOAT;
8845 if (get_float_arg(argvars, &f) == OK)
8846 rettv->vval.v_float = atan(f);
8847 else
8848 rettv->vval.v_float = 0.0;
8849}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008850
8851/*
8852 * "atan2()" function
8853 */
8854 static void
8855f_atan2(argvars, rettv)
8856 typval_T *argvars;
8857 typval_T *rettv;
8858{
8859 float_T fx, fy;
8860
8861 rettv->v_type = VAR_FLOAT;
8862 if (get_float_arg(argvars, &fx) == OK
8863 && get_float_arg(&argvars[1], &fy) == OK)
8864 rettv->vval.v_float = atan2(fx, fy);
8865 else
8866 rettv->vval.v_float = 0.0;
8867}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008868#endif
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870/*
8871 * "browse(save, title, initdir, default)" function
8872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008875 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878#ifdef FEAT_BROWSE
8879 int save;
8880 char_u *title;
8881 char_u *initdir;
8882 char_u *defname;
8883 char_u buf[NUMBUFLEN];
8884 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 save = get_tv_number_chk(&argvars[0], &error);
8888 title = get_tv_string_chk(&argvars[1]);
8889 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8890 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008892 if (error || title == NULL || initdir == NULL || defname == NULL)
8893 rettv->vval.v_string = NULL;
8894 else
8895 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008896 do_browse(save ? BROWSE_SAVE : 0,
8897 title, defname, NULL, initdir, NULL, curbuf);
8898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008902}
8903
8904/*
8905 * "browsedir(title, initdir)" function
8906 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008909 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008910 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008911{
8912#ifdef FEAT_BROWSE
8913 char_u *title;
8914 char_u *initdir;
8915 char_u buf[NUMBUFLEN];
8916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 title = get_tv_string_chk(&argvars[0]);
8918 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 if (title == NULL || initdir == NULL)
8921 rettv->vval.v_string = NULL;
8922 else
8923 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008924 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929}
8930
Bram Moolenaar33570922005-01-25 22:26:29 +00008931static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933/*
8934 * Find a buffer by number or exact name.
8935 */
8936 static buf_T *
8937find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008942 if (avar->v_type == VAR_NUMBER)
8943 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008944 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008946 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008947 if (buf == NULL)
8948 {
8949 /* No full path name match, try a match with a URL or a "nofile"
8950 * buffer, these don't use the full path. */
8951 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8952 if (buf->b_fname != NULL
8953 && (path_with_url(buf->b_fname)
8954#ifdef FEAT_QUICKFIX
8955 || bt_nofile(buf)
8956#endif
8957 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008958 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008959 break;
8960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962 return buf;
8963}
8964
8965/*
8966 * "bufexists(expr)" function
8967 */
8968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 typval_T *argvars;
8971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974}
8975
8976/*
8977 * "buflisted(expr)" function
8978 */
8979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 buf_T *buf;
8985
8986 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988}
8989
8990/*
8991 * "bufloaded(expr)" function
8992 */
8993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *argvars;
8996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 buf_T *buf;
8999
9000 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002}
9003
Bram Moolenaar33570922005-01-25 22:26:29 +00009004static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009005
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006/*
9007 * Get buffer by number or pattern.
9008 */
9009 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 int save_magic;
9015 char_u *save_cpo;
9016 buf_T *buf;
9017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 if (tv->v_type == VAR_NUMBER)
9019 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009020 if (tv->v_type != VAR_STRING)
9021 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 if (name == NULL || *name == NUL)
9023 return curbuf;
9024 if (name[0] == '$' && name[1] == NUL)
9025 return lastbuf;
9026
9027 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9028 save_magic = p_magic;
9029 p_magic = TRUE;
9030 save_cpo = p_cpo;
9031 p_cpo = (char_u *)"";
9032
9033 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9034 TRUE, FALSE));
9035
9036 p_magic = save_magic;
9037 p_cpo = save_cpo;
9038
9039 /* If not found, try expanding the name, like done for bufexists(). */
9040 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
9043 return buf;
9044}
9045
9046/*
9047 * "bufname(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009056 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 buf = get_buf_tv(&argvars[0]);
9059 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 --emsg_off;
9065}
9066
9067/*
9068 * "bufnr(expr)" function
9069 */
9070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009072 typval_T *argvars;
9073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
9075 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009076 int error = FALSE;
9077 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009082 --emsg_off;
9083
9084 /* If the buffer isn't found and the second argument is not zero create a
9085 * new buffer. */
9086 if (buf == NULL
9087 && argvars[1].v_type != VAR_UNKNOWN
9088 && get_tv_number_chk(&argvars[1], &error) != 0
9089 && !error
9090 && (name = get_tv_string_chk(&argvars[0])) != NULL
9091 && !error)
9092 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9093
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100/*
9101 * "bufwinnr(nr)" function
9102 */
9103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 typval_T *argvars;
9106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108#ifdef FEAT_WINDOWS
9109 win_T *wp;
9110 int winnr = 0;
9111#endif
9112 buf_T *buf;
9113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117#ifdef FEAT_WINDOWS
9118 for (wp = firstwin; wp; wp = wp->w_next)
9119 {
9120 ++winnr;
9121 if (wp->w_buffer == buf)
9122 break;
9123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127#endif
9128 --emsg_off;
9129}
9130
9131/*
9132 * "byte2line(byte)" function
9133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141#else
9142 long boff = 0;
9143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 (linenr_T)0, &boff);
9150#endif
9151}
9152
9153/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009154 * "byteidx()" function
9155 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009158 typval_T *argvars;
9159 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009160{
9161#ifdef FEAT_MBYTE
9162 char_u *t;
9163#endif
9164 char_u *str;
9165 long idx;
9166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 str = get_tv_string_chk(&argvars[0]);
9168 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009171 return;
9172
9173#ifdef FEAT_MBYTE
9174 t = str;
9175 for ( ; idx > 0; idx--)
9176 {
9177 if (*t == NUL) /* EOL reached */
9178 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009179 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009180 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009181 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009182#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009183 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009185#endif
9186}
9187
Bram Moolenaardb913952012-06-29 12:54:53 +02009188 int
9189func_call(name, args, selfdict, rettv)
9190 char_u *name;
9191 typval_T *args;
9192 dict_T *selfdict;
9193 typval_T *rettv;
9194{
9195 listitem_T *item;
9196 typval_T argv[MAX_FUNC_ARGS + 1];
9197 int argc = 0;
9198 int dummy;
9199 int r = 0;
9200
9201 for (item = args->vval.v_list->lv_first; item != NULL;
9202 item = item->li_next)
9203 {
9204 if (argc == MAX_FUNC_ARGS)
9205 {
9206 EMSG(_("E699: Too many arguments"));
9207 break;
9208 }
9209 /* Make a copy of each argument. This is needed to be able to set
9210 * v_lock to VAR_FIXED in the copy without changing the original list.
9211 */
9212 copy_tv(&item->li_tv, &argv[argc++]);
9213 }
9214
9215 if (item == NULL)
9216 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9217 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9218 &dummy, TRUE, selfdict);
9219
9220 /* Free the arguments. */
9221 while (argc > 0)
9222 clear_tv(&argv[--argc]);
9223
9224 return r;
9225}
9226
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009227/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009228 * "call(func, arglist)" function
9229 */
9230 static void
9231f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009234{
9235 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009236 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009237
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009238 if (argvars[1].v_type != VAR_LIST)
9239 {
9240 EMSG(_(e_listreq));
9241 return;
9242 }
9243 if (argvars[1].vval.v_list == NULL)
9244 return;
9245
9246 if (argvars[0].v_type == VAR_FUNC)
9247 func = argvars[0].vval.v_string;
9248 else
9249 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 if (*func == NUL)
9251 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009252
Bram Moolenaare9a41262005-01-15 22:18:47 +00009253 if (argvars[2].v_type != VAR_UNKNOWN)
9254 {
9255 if (argvars[2].v_type != VAR_DICT)
9256 {
9257 EMSG(_(e_dictreq));
9258 return;
9259 }
9260 selfdict = argvars[2].vval.v_dict;
9261 }
9262
Bram Moolenaardb913952012-06-29 12:54:53 +02009263 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009264}
9265
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009266#ifdef FEAT_FLOAT
9267/*
9268 * "ceil({float})" function
9269 */
9270 static void
9271f_ceil(argvars, rettv)
9272 typval_T *argvars;
9273 typval_T *rettv;
9274{
9275 float_T f;
9276
9277 rettv->v_type = VAR_FLOAT;
9278 if (get_float_arg(argvars, &f) == OK)
9279 rettv->vval.v_float = ceil(f);
9280 else
9281 rettv->vval.v_float = 0.0;
9282}
9283#endif
9284
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009285/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009286 * "changenr()" function
9287 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009288 static void
9289f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009290 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009291 typval_T *rettv;
9292{
9293 rettv->vval.v_number = curbuf->b_u_seq_cur;
9294}
9295
9296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 * "char2nr(string)" function
9298 */
9299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009301 typval_T *argvars;
9302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304#ifdef FEAT_MBYTE
9305 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009306 {
9307 int utf8 = 0;
9308
9309 if (argvars[1].v_type != VAR_UNKNOWN)
9310 utf8 = get_tv_number_chk(&argvars[1], NULL);
9311
9312 if (utf8)
9313 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9314 else
9315 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 else
9318#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * "cindent(lnum)" function
9324 */
9325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *argvars;
9328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330#ifdef FEAT_CINDENT
9331 pos_T pos;
9332 linenr_T lnum;
9333
9334 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9337 {
9338 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 curwin->w_cursor = pos;
9341 }
9342 else
9343#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009348 * "clearmatches()" function
9349 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009350 static void
9351f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009352 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009353 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009354{
9355#ifdef FEAT_SEARCH_EXTRA
9356 clear_matches(curwin);
9357#endif
9358}
9359
9360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 * "col(string)" function
9362 */
9363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 typval_T *argvars;
9366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368 colnr_T col = 0;
9369 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009370 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009372 fp = var2fpos(&argvars[0], FALSE, &fnum);
9373 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
9375 if (fp->col == MAXCOL)
9376 {
9377 /* '> can be MAXCOL, get the length of the line then */
9378 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009379 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 else
9381 col = MAXCOL;
9382 }
9383 else
9384 {
9385 col = fp->col + 1;
9386#ifdef FEAT_VIRTUALEDIT
9387 /* col(".") when the cursor is on the NUL at the end of the line
9388 * because of "coladd" can be seen as an extra column. */
9389 if (virtual_active() && fp == &curwin->w_cursor)
9390 {
9391 char_u *p = ml_get_cursor();
9392
9393 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9394 curwin->w_virtcol - curwin->w_cursor.coladd))
9395 {
9396# ifdef FEAT_MBYTE
9397 int l;
9398
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009399 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 col += l;
9401# else
9402 if (*p != NUL && p[1] == NUL)
9403 ++col;
9404# endif
9405 }
9406 }
9407#endif
9408 }
9409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411}
9412
Bram Moolenaar572cb562005-08-05 21:35:02 +00009413#if defined(FEAT_INS_EXPAND)
9414/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009415 * "complete()" function
9416 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009417 static void
9418f_complete(argvars, rettv)
9419 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009420 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009421{
9422 int startcol;
9423
9424 if ((State & INSERT) == 0)
9425 {
9426 EMSG(_("E785: complete() can only be used in Insert mode"));
9427 return;
9428 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009429
9430 /* Check for undo allowed here, because if something was already inserted
9431 * the line was already saved for undo and this check isn't done. */
9432 if (!undo_allowed())
9433 return;
9434
Bram Moolenaarade00832006-03-10 21:46:58 +00009435 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9436 {
9437 EMSG(_(e_invarg));
9438 return;
9439 }
9440
9441 startcol = get_tv_number_chk(&argvars[0], NULL);
9442 if (startcol <= 0)
9443 return;
9444
9445 set_completion(startcol - 1, argvars[1].vval.v_list);
9446}
9447
9448/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009449 * "complete_add()" function
9450 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009451 static void
9452f_complete_add(argvars, rettv)
9453 typval_T *argvars;
9454 typval_T *rettv;
9455{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009456 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009457}
9458
9459/*
9460 * "complete_check()" function
9461 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009462 static void
9463f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009464 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009465 typval_T *rettv;
9466{
9467 int saved = RedrawingDisabled;
9468
9469 RedrawingDisabled = 0;
9470 ins_compl_check_keys(0);
9471 rettv->vval.v_number = compl_interrupted;
9472 RedrawingDisabled = saved;
9473}
9474#endif
9475
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476/*
9477 * "confirm(message, buttons[, default [, type]])" function
9478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009481 typval_T *argvars UNUSED;
9482 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9485 char_u *message;
9486 char_u *buttons = NULL;
9487 char_u buf[NUMBUFLEN];
9488 char_u buf2[NUMBUFLEN];
9489 int def = 1;
9490 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009491 char_u *typestr;
9492 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 message = get_tv_string_chk(&argvars[0]);
9495 if (message == NULL)
9496 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009497 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9500 if (buttons == NULL)
9501 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009504 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9508 if (typestr == NULL)
9509 error = TRUE;
9510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 switch (TOUPPER_ASC(*typestr))
9513 {
9514 case 'E': type = VIM_ERROR; break;
9515 case 'Q': type = VIM_QUESTION; break;
9516 case 'I': type = VIM_INFO; break;
9517 case 'W': type = VIM_WARNING; break;
9518 case 'G': type = VIM_GENERIC; break;
9519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 }
9521 }
9522 }
9523 }
9524
9525 if (buttons == NULL || *buttons == NUL)
9526 buttons = (char_u *)_("&Ok");
9527
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009528 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009529 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009530 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531#endif
9532}
9533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009534/*
9535 * "copy()" function
9536 */
9537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009539 typval_T *argvars;
9540 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009542 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009545#ifdef FEAT_FLOAT
9546/*
9547 * "cos()" function
9548 */
9549 static void
9550f_cos(argvars, rettv)
9551 typval_T *argvars;
9552 typval_T *rettv;
9553{
9554 float_T f;
9555
9556 rettv->v_type = VAR_FLOAT;
9557 if (get_float_arg(argvars, &f) == OK)
9558 rettv->vval.v_float = cos(f);
9559 else
9560 rettv->vval.v_float = 0.0;
9561}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009562
9563/*
9564 * "cosh()" function
9565 */
9566 static void
9567f_cosh(argvars, rettv)
9568 typval_T *argvars;
9569 typval_T *rettv;
9570{
9571 float_T f;
9572
9573 rettv->v_type = VAR_FLOAT;
9574 if (get_float_arg(argvars, &f) == OK)
9575 rettv->vval.v_float = cosh(f);
9576 else
9577 rettv->vval.v_float = 0.0;
9578}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009579#endif
9580
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009582 * "count()" function
9583 */
9584 static void
9585f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009588{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009589 long n = 0;
9590 int ic = FALSE;
9591
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009593 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 listitem_T *li;
9595 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009597
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 if ((l = argvars[0].vval.v_list) != NULL)
9599 {
9600 li = l->lv_first;
9601 if (argvars[2].v_type != VAR_UNKNOWN)
9602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009603 int error = FALSE;
9604
9605 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 if (argvars[3].v_type != VAR_UNKNOWN)
9607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 idx = get_tv_number_chk(&argvars[3], &error);
9609 if (!error)
9610 {
9611 li = list_find(l, idx);
9612 if (li == NULL)
9613 EMSGN(_(e_listidx), idx);
9614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 if (error)
9617 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 }
9619
9620 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009621 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 ++n;
9623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009624 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 else if (argvars[0].v_type == VAR_DICT)
9626 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 int todo;
9628 dict_T *d;
9629 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009630
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009631 if ((d = argvars[0].vval.v_dict) != NULL)
9632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009633 int error = FALSE;
9634
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 if (argvars[2].v_type != VAR_UNKNOWN)
9636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009637 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 if (argvars[3].v_type != VAR_UNKNOWN)
9639 EMSG(_(e_invarg));
9640 }
9641
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009642 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009644 {
9645 if (!HASHITEM_EMPTY(hi))
9646 {
9647 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009648 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009649 ++n;
9650 }
9651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 }
9653 }
9654 else
9655 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009656 rettv->vval.v_number = n;
9657}
9658
9659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9661 *
9662 * Checks the existence of a cscope connection.
9663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009666 typval_T *argvars UNUSED;
9667 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669#ifdef FEAT_CSCOPE
9670 int num = 0;
9671 char_u *dbpath = NULL;
9672 char_u *prepend = NULL;
9673 char_u buf[NUMBUFLEN];
9674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009675 if (argvars[0].v_type != VAR_UNKNOWN
9676 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 num = (int)get_tv_number(&argvars[0]);
9679 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
9683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#endif
9686}
9687
9688/*
9689 * "cursor(lnum, col)" function
9690 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009691 * Moves the cursor to the specified line and column.
9692 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009696 typval_T *argvars;
9697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009700#ifdef FEAT_VIRTUALEDIT
9701 long coladd = 0;
9702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009704 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009705 if (argvars[1].v_type == VAR_UNKNOWN)
9706 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009707 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009708
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009709 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009710 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009711 line = pos.lnum;
9712 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009713#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009715#endif
9716 }
9717 else
9718 {
9719 line = get_tv_lnum(argvars);
9720 col = get_tv_number_chk(&argvars[1], NULL);
9721#ifdef FEAT_VIRTUALEDIT
9722 if (argvars[2].v_type != VAR_UNKNOWN)
9723 coladd = get_tv_number_chk(&argvars[2], NULL);
9724#endif
9725 }
9726 if (line < 0 || col < 0
9727#ifdef FEAT_VIRTUALEDIT
9728 || coladd < 0
9729#endif
9730 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (line > 0)
9733 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 if (col > 0)
9735 curwin->w_cursor.col = col - 1;
9736#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009737 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#endif
9739
9740 /* Make sure the cursor is in a valid position. */
9741 check_cursor();
9742#ifdef FEAT_MBYTE
9743 /* Correct cursor for multi-byte character. */
9744 if (has_mbyte)
9745 mb_adjust_cursor();
9746#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009747
9748 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009749 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750}
9751
9752/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009753 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 */
9755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009757 typval_T *argvars;
9758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009760 int noref = 0;
9761
9762 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009764 if (noref < 0 || noref > 1)
9765 EMSG(_(e_invarg));
9766 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009767 {
9768 current_copyID += COPYID_INC;
9769 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773/*
9774 * "delete()" function
9775 */
9776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009778 typval_T *argvars;
9779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785}
9786
9787/*
9788 * "did_filetype()" function
9789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009792 typval_T *argvars UNUSED;
9793 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797#endif
9798}
9799
9800/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009801 * "diff_filler()" function
9802 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009805 typval_T *argvars UNUSED;
9806 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009807{
9808#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810#endif
9811}
9812
9813/*
9814 * "diff_hlID()" function
9815 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009820{
9821#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009823 static linenr_T prev_lnum = 0;
9824 static int changedtick = 0;
9825 static int fnum = 0;
9826 static int change_start = 0;
9827 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009828 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829 int filler_lines;
9830 int col;
9831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 if (lnum < 0) /* ignore type error in {lnum} arg */
9833 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 if (lnum != prev_lnum
9835 || changedtick != curbuf->b_changedtick
9836 || fnum != curbuf->b_fnum)
9837 {
9838 /* New line, buffer, change: need to get the values. */
9839 filler_lines = diff_check(curwin, lnum);
9840 if (filler_lines < 0)
9841 {
9842 if (filler_lines == -1)
9843 {
9844 change_start = MAXCOL;
9845 change_end = -1;
9846 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9847 hlID = HLF_ADD; /* added line */
9848 else
9849 hlID = HLF_CHD; /* changed line */
9850 }
9851 else
9852 hlID = HLF_ADD; /* added line */
9853 }
9854 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009855 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856 prev_lnum = lnum;
9857 changedtick = curbuf->b_changedtick;
9858 fnum = curbuf->b_fnum;
9859 }
9860
9861 if (hlID == HLF_CHD || hlID == HLF_TXD)
9862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009864 if (col >= change_start && col <= change_end)
9865 hlID = HLF_TXD; /* changed text */
9866 else
9867 hlID = HLF_CHD; /* changed line */
9868 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009869 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009870#endif
9871}
9872
9873/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009874 * "empty({expr})" function
9875 */
9876 static void
9877f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009878 typval_T *argvars;
9879 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009880{
9881 int n;
9882
9883 switch (argvars[0].v_type)
9884 {
9885 case VAR_STRING:
9886 case VAR_FUNC:
9887 n = argvars[0].vval.v_string == NULL
9888 || *argvars[0].vval.v_string == NUL;
9889 break;
9890 case VAR_NUMBER:
9891 n = argvars[0].vval.v_number == 0;
9892 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009893#ifdef FEAT_FLOAT
9894 case VAR_FLOAT:
9895 n = argvars[0].vval.v_float == 0.0;
9896 break;
9897#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009898 case VAR_LIST:
9899 n = argvars[0].vval.v_list == NULL
9900 || argvars[0].vval.v_list->lv_first == NULL;
9901 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 case VAR_DICT:
9903 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009906 default:
9907 EMSG2(_(e_intern2), "f_empty()");
9908 n = 0;
9909 }
9910
9911 rettv->vval.v_number = n;
9912}
9913
9914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 * "escape({string}, {chars})" function
9916 */
9917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 typval_T *argvars;
9920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 char_u buf[NUMBUFLEN];
9923
Bram Moolenaar758711c2005-02-02 23:11:38 +00009924 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9925 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927}
9928
9929/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009930 * "eval()" function
9931 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932 static void
9933f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009934 typval_T *argvars;
9935 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009936{
9937 char_u *s;
9938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 s = get_tv_string_chk(&argvars[0]);
9940 if (s != NULL)
9941 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9944 {
9945 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009946 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948 else if (*s != NUL)
9949 EMSG(_(e_trailing));
9950}
9951
9952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 * "eventhandler()" function
9954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961}
9962
9963/*
9964 * "executable()" function
9965 */
9966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 typval_T *argvars;
9969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974/*
9975 * "exists()" function
9976 */
9977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009979 typval_T *argvars;
9980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
9982 char_u *p;
9983 char_u *name;
9984 int n = FALSE;
9985 int len = 0;
9986
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009987 no_autoload = TRUE;
9988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 if (*p == '$') /* environment variable */
9991 {
9992 /* first try "normal" environment variables (fast) */
9993 if (mch_getenv(p + 1) != NULL)
9994 n = TRUE;
9995 else
9996 {
9997 /* try expanding things like $VIM and ${HOME} */
9998 p = expand_env_save(p);
9999 if (p != NULL && *p != '$')
10000 n = TRUE;
10001 vim_free(p);
10002 }
10003 }
10004 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010007 if (*skipwhite(p) != NUL)
10008 n = FALSE; /* trailing garbage */
10009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 else if (*p == '*') /* internal or user defined function */
10011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
10014 else if (*p == ':')
10015 {
10016 n = cmd_exists(p + 1);
10017 }
10018 else if (*p == '#')
10019 {
10020#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010021 if (p[1] == '#')
10022 n = autocmd_supported(p + 2);
10023 else
10024 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025#endif
10026 }
10027 else /* internal variable */
10028 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010029 char_u *tofree;
10030 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010032 /* get_name_len() takes care of expanding curly braces */
10033 name = p;
10034 len = get_name_len(&p, &tofree, TRUE, FALSE);
10035 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010037 if (tofree != NULL)
10038 name = tofree;
10039 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10040 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010042 /* handle d.key, l[idx], f(expr) */
10043 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10044 if (n)
10045 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
10047 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010048 if (*p != NUL)
10049 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010051 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010055
10056 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010059#ifdef FEAT_FLOAT
10060/*
10061 * "exp()" function
10062 */
10063 static void
10064f_exp(argvars, rettv)
10065 typval_T *argvars;
10066 typval_T *rettv;
10067{
10068 float_T f;
10069
10070 rettv->v_type = VAR_FLOAT;
10071 if (get_float_arg(argvars, &f) == OK)
10072 rettv->vval.v_float = exp(f);
10073 else
10074 rettv->vval.v_float = 0.0;
10075}
10076#endif
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078/*
10079 * "expand()" function
10080 */
10081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010083 typval_T *argvars;
10084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086 char_u *s;
10087 int len;
10088 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010089 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010092 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010095 if (argvars[1].v_type != VAR_UNKNOWN
10096 && argvars[2].v_type != VAR_UNKNOWN
10097 && get_tv_number_chk(&argvars[2], &error)
10098 && !error)
10099 {
10100 rettv->v_type = VAR_LIST;
10101 rettv->vval.v_list = NULL;
10102 }
10103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 if (*s == '%' || *s == '#' || *s == '<')
10106 {
10107 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010108 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010110 if (rettv->v_type == VAR_LIST)
10111 {
10112 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10113 list_append_string(rettv->vval.v_list, result, -1);
10114 else
10115 vim_free(result);
10116 }
10117 else
10118 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 }
10120 else
10121 {
10122 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010123 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 if (argvars[1].v_type != VAR_UNKNOWN
10125 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010126 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 if (!error)
10128 {
10129 ExpandInit(&xpc);
10130 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010131 if (p_wic)
10132 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010133 if (rettv->v_type == VAR_STRING)
10134 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10135 options, WILD_ALL);
10136 else if (rettv_list_alloc(rettv) != FAIL)
10137 {
10138 int i;
10139
10140 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10141 for (i = 0; i < xpc.xp_numfiles; i++)
10142 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10143 ExpandCleanup(&xpc);
10144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 }
10146 else
10147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149}
10150
10151/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010152 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010154 */
10155 static void
10156f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010159{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010160 char *arg_errmsg = N_("extend() argument");
10161
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010164 list_T *l1, *l2;
10165 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010166 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010168
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 l1 = argvars[0].vval.v_list;
10170 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010171 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010172 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 {
10174 if (argvars[2].v_type != VAR_UNKNOWN)
10175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 before = get_tv_number_chk(&argvars[2], &error);
10177 if (error)
10178 return; /* type error; errmsg already given */
10179
Bram Moolenaar758711c2005-02-02 23:11:38 +000010180 if (before == l1->lv_len)
10181 item = NULL;
10182 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010184 item = list_find(l1, before);
10185 if (item == NULL)
10186 {
10187 EMSGN(_(e_listidx), before);
10188 return;
10189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010190 }
10191 }
10192 else
10193 item = NULL;
10194 list_extend(l1, l2, item);
10195
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 copy_tv(&argvars[0], rettv);
10197 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 dict_T *d1, *d2;
10202 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 char_u *action;
10204 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010205 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010206 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010207
10208 d1 = argvars[0].vval.v_dict;
10209 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010210 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010211 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 {
10213 /* Check the third argument. */
10214 if (argvars[2].v_type != VAR_UNKNOWN)
10215 {
10216 static char *(av[]) = {"keep", "force", "error"};
10217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 action = get_tv_string_chk(&argvars[2]);
10219 if (action == NULL)
10220 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 for (i = 0; i < 3; ++i)
10222 if (STRCMP(action, av[i]) == 0)
10223 break;
10224 if (i == 3)
10225 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010226 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 return;
10228 }
10229 }
10230 else
10231 action = (char_u *)"force";
10232
10233 /* Go over all entries in the second dict and add them to the
10234 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010235 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010238 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010240 --todo;
10241 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010242 if (d1->dv_scope != 0)
10243 {
10244 /* Disallow replacing a builtin function in l: and g:.
10245 * Check the key to be valid when adding to any
10246 * scope. */
10247 if (d1->dv_scope == VAR_DEF_SCOPE
10248 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10249 && var_check_func_name(hi2->hi_key,
10250 di1 == NULL))
10251 break;
10252 if (!valid_varname(hi2->hi_key))
10253 break;
10254 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010255 if (di1 == NULL)
10256 {
10257 di1 = dictitem_copy(HI2DI(hi2));
10258 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10259 dictitem_free(di1);
10260 }
10261 else if (*action == 'e')
10262 {
10263 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10264 break;
10265 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010266 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010267 {
10268 clear_tv(&di1->di_tv);
10269 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 }
10272 }
10273
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 copy_tv(&argvars[0], rettv);
10275 }
10276 }
10277 else
10278 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010279}
10280
10281/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010282 * "feedkeys()" function
10283 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010284 static void
10285f_feedkeys(argvars, rettv)
10286 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010287 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010288{
10289 int remap = TRUE;
10290 char_u *keys, *flags;
10291 char_u nbuf[NUMBUFLEN];
10292 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010293 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010294
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010295 /* This is not allowed in the sandbox. If the commands would still be
10296 * executed in the sandbox it would be OK, but it probably happens later,
10297 * when "sandbox" is no longer set. */
10298 if (check_secure())
10299 return;
10300
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010301 keys = get_tv_string(&argvars[0]);
10302 if (*keys != NUL)
10303 {
10304 if (argvars[1].v_type != VAR_UNKNOWN)
10305 {
10306 flags = get_tv_string_buf(&argvars[1], nbuf);
10307 for ( ; *flags != NUL; ++flags)
10308 {
10309 switch (*flags)
10310 {
10311 case 'n': remap = FALSE; break;
10312 case 'm': remap = TRUE; break;
10313 case 't': typed = TRUE; break;
10314 }
10315 }
10316 }
10317
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010318 /* Need to escape K_SPECIAL and CSI before putting the string in the
10319 * typeahead buffer. */
10320 keys_esc = vim_strsave_escape_csi(keys);
10321 if (keys_esc != NULL)
10322 {
10323 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010324 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010325 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010326 if (vgetc_busy)
10327 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010328 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010329 }
10330}
10331
10332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 * "filereadable()" function
10334 */
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010340 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 char_u *p;
10342 int n;
10343
Bram Moolenaarc236c162008-07-13 17:41:49 +000010344#ifndef O_NONBLOCK
10345# define O_NONBLOCK 0
10346#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010348 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10349 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 {
10351 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010352 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354 else
10355 n = FALSE;
10356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358}
10359
10360/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010361 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 * rights to write into.
10363 */
10364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010366 typval_T *argvars;
10367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010369 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370}
10371
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010372static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010373
10374 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010375findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010378 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010379{
10380#ifdef FEAT_SEARCHPATH
10381 char_u *fname;
10382 char_u *fresult = NULL;
10383 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10384 char_u *p;
10385 char_u pathbuf[NUMBUFLEN];
10386 int count = 1;
10387 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010388 int error = FALSE;
10389#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010390
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010391 rettv->vval.v_string = NULL;
10392 rettv->v_type = VAR_STRING;
10393
10394#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010396
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010397 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010399 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10400 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010401 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 else
10403 {
10404 if (*p != NUL)
10405 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010408 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010409 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010410 }
10411
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010412 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10413 error = TRUE;
10414
10415 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 do
10418 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010419 if (rettv->v_type == VAR_STRING)
10420 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 fresult = find_file_in_path_option(first ? fname : NULL,
10422 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010423 0, first, path,
10424 find_what,
10425 curbuf->b_ffname,
10426 find_what == FINDFILE_DIR
10427 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010429
10430 if (fresult != NULL && rettv->v_type == VAR_LIST)
10431 list_append_string(rettv->vval.v_list, fresult, -1);
10432
10433 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010435
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010436 if (rettv->v_type == VAR_STRING)
10437 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010439}
10440
Bram Moolenaar33570922005-01-25 22:26:29 +000010441static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10442static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010443
10444/*
10445 * Implementation of map() and filter().
10446 */
10447 static void
10448filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010449 typval_T *argvars;
10450 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451 int map;
10452{
10453 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 listitem_T *li, *nli;
10456 list_T *l = NULL;
10457 dictitem_T *di;
10458 hashtab_T *ht;
10459 hashitem_T *hi;
10460 dict_T *d = NULL;
10461 typval_T save_val;
10462 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010463 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010464 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010465 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10466 char *arg_errmsg = (map ? N_("map() argument")
10467 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010468 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010469 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010470
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010472 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010473 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010474 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010475 return;
10476 }
10477 else if (argvars[0].v_type == VAR_DICT)
10478 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010479 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010480 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 return;
10482 }
10483 else
10484 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010485 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 return;
10487 }
10488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 expr = get_tv_string_buf_chk(&argvars[1], buf);
10490 /* On type errors, the preceding call has already displayed an error
10491 * message. Avoid a misleading error message for an empty string that
10492 * was not passed as argument. */
10493 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 prepare_vimvar(VV_VAL, &save_val);
10496 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010497
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010498 /* We reset "did_emsg" to be able to detect whether an error
10499 * occurred during evaluation of the expression. */
10500 save_did_emsg = did_emsg;
10501 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010502
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010503 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 vimvars[VV_KEY].vv_type = VAR_STRING;
10507
10508 ht = &d->dv_hashtab;
10509 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010510 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 if (!HASHITEM_EMPTY(hi))
10514 {
10515 --todo;
10516 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010517 if (tv_check_lock(di->di_tv.v_lock,
10518 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 break;
10520 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010521 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010522 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 break;
10524 if (!map && rem)
10525 dictitem_remove(d, di);
10526 clear_tv(&vimvars[VV_KEY].vv_tv);
10527 }
10528 }
10529 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 }
10531 else
10532 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010533 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 for (li = l->lv_first; li != NULL; li = nli)
10536 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010537 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010538 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010540 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010541 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010542 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010543 break;
10544 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010546 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010547 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010549
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010550 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010552
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010553 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555
10556 copy_tv(&argvars[0], rettv);
10557}
10558
10559 static int
10560filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 char_u *expr;
10563 int map;
10564 int *remp;
10565{
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010568 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 s = expr;
10572 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010573 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 if (*s != NUL) /* check for trailing chars after expr */
10575 {
10576 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010577 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 }
10579 if (map)
10580 {
10581 /* map(): replace the list item value */
10582 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010583 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 *tv = rettv;
10585 }
10586 else
10587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010588 int error = FALSE;
10589
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 /* On type error, nothing has been removed; return FAIL to stop the
10594 * loop. The error message was given by get_tv_number_chk(). */
10595 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010596 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010598 retval = OK;
10599theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010601 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010602}
10603
10604/*
10605 * "filter()" function
10606 */
10607 static void
10608f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010609 typval_T *argvars;
10610 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611{
10612 filter_map(argvars, rettv, FALSE);
10613}
10614
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010615/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010616 * "finddir({fname}[, {path}[, {count}]])" function
10617 */
10618 static void
10619f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010620 typval_T *argvars;
10621 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010622{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010623 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010624}
10625
10626/*
10627 * "findfile({fname}[, {path}[, {count}]])" function
10628 */
10629 static void
10630f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010633{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010634 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635}
10636
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010637#ifdef FEAT_FLOAT
10638/*
10639 * "float2nr({float})" function
10640 */
10641 static void
10642f_float2nr(argvars, rettv)
10643 typval_T *argvars;
10644 typval_T *rettv;
10645{
10646 float_T f;
10647
10648 if (get_float_arg(argvars, &f) == OK)
10649 {
10650 if (f < -0x7fffffff)
10651 rettv->vval.v_number = -0x7fffffff;
10652 else if (f > 0x7fffffff)
10653 rettv->vval.v_number = 0x7fffffff;
10654 else
10655 rettv->vval.v_number = (varnumber_T)f;
10656 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010657}
10658
10659/*
10660 * "floor({float})" function
10661 */
10662 static void
10663f_floor(argvars, rettv)
10664 typval_T *argvars;
10665 typval_T *rettv;
10666{
10667 float_T f;
10668
10669 rettv->v_type = VAR_FLOAT;
10670 if (get_float_arg(argvars, &f) == OK)
10671 rettv->vval.v_float = floor(f);
10672 else
10673 rettv->vval.v_float = 0.0;
10674}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010675
10676/*
10677 * "fmod()" function
10678 */
10679 static void
10680f_fmod(argvars, rettv)
10681 typval_T *argvars;
10682 typval_T *rettv;
10683{
10684 float_T fx, fy;
10685
10686 rettv->v_type = VAR_FLOAT;
10687 if (get_float_arg(argvars, &fx) == OK
10688 && get_float_arg(&argvars[1], &fy) == OK)
10689 rettv->vval.v_float = fmod(fx, fy);
10690 else
10691 rettv->vval.v_float = 0.0;
10692}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010693#endif
10694
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010696 * "fnameescape({string})" function
10697 */
10698 static void
10699f_fnameescape(argvars, rettv)
10700 typval_T *argvars;
10701 typval_T *rettv;
10702{
10703 rettv->vval.v_string = vim_strsave_fnameescape(
10704 get_tv_string(&argvars[0]), FALSE);
10705 rettv->v_type = VAR_STRING;
10706}
10707
10708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 * "fnamemodify({fname}, {mods})" function
10710 */
10711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
10716 char_u *fname;
10717 char_u *mods;
10718 int usedlen = 0;
10719 int len;
10720 char_u *fbuf = NULL;
10721 char_u buf[NUMBUFLEN];
10722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 fname = get_tv_string_chk(&argvars[0]);
10724 mods = get_tv_string_buf_chk(&argvars[1], buf);
10725 if (fname == NULL || mods == NULL)
10726 fname = NULL;
10727 else
10728 {
10729 len = (int)STRLEN(fname);
10730 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 vim_free(fbuf);
10739}
10740
Bram Moolenaar33570922005-01-25 22:26:29 +000010741static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742
10743/*
10744 * "foldclosed()" function
10745 */
10746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *argvars;
10749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 int end;
10751{
10752#ifdef FEAT_FOLDING
10753 linenr_T lnum;
10754 linenr_T first, last;
10755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10758 {
10759 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10760 {
10761 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 return;
10766 }
10767 }
10768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770}
10771
10772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010773 * "foldclosed()" function
10774 */
10775 static void
10776f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 typval_T *argvars;
10778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010779{
10780 foldclosed_both(argvars, rettv, FALSE);
10781}
10782
10783/*
10784 * "foldclosedend()" function
10785 */
10786 static void
10787f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010790{
10791 foldclosed_both(argvars, rettv, TRUE);
10792}
10793
10794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 * "foldlevel()" function
10796 */
10797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 typval_T *argvars;
10800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802#ifdef FEAT_FOLDING
10803 linenr_T lnum;
10804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809}
10810
10811/*
10812 * "foldtext()" function
10813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
10819#ifdef FEAT_FOLDING
10820 linenr_T lnum;
10821 char_u *s;
10822 char_u *r;
10823 int len;
10824 char *txt;
10825#endif
10826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 rettv->v_type = VAR_STRING;
10828 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10831 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10832 <= curbuf->b_ml.ml_line_count
10833 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 {
10835 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10837 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
10839 if (!linewhite(lnum))
10840 break;
10841 ++lnum;
10842 }
10843
10844 /* Find interesting text in this line. */
10845 s = skipwhite(ml_get(lnum));
10846 /* skip C comment-start */
10847 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010850 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010852 {
10853 s = skipwhite(ml_get(lnum + 1));
10854 if (*s == '*')
10855 s = skipwhite(s + 1);
10856 }
10857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 txt = _("+-%s%3ld lines: ");
10859 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 + 20 /* for %3ld */
10862 + STRLEN(s))); /* concatenated */
10863 if (r != NULL)
10864 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010865 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10866 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10867 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 len = (int)STRLEN(r);
10869 STRCAT(r, s);
10870 /* remove 'foldmarker' and 'commentstring' */
10871 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 }
10874 }
10875#endif
10876}
10877
10878/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010879 * "foldtextresult(lnum)" function
10880 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010885{
10886#ifdef FEAT_FOLDING
10887 linenr_T lnum;
10888 char_u *text;
10889 char_u buf[51];
10890 foldinfo_T foldinfo;
10891 int fold_count;
10892#endif
10893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->v_type = VAR_STRING;
10895 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010896#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010898 /* treat illegal types and illegal string values for {lnum} the same */
10899 if (lnum < 0)
10900 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010901 fold_count = foldedCount(curwin, lnum, &foldinfo);
10902 if (fold_count > 0)
10903 {
10904 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10905 &foldinfo, buf);
10906 if (text == buf)
10907 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010909 }
10910#endif
10911}
10912
10913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 * "foreground()" function
10915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010918 typval_T *argvars UNUSED;
10919 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#ifdef FEAT_GUI
10922 if (gui.in_use)
10923 gui_mch_set_foreground();
10924#else
10925# ifdef WIN32
10926 win32_set_foreground();
10927# endif
10928#endif
10929}
10930
10931/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010932 * "function()" function
10933 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *argvars;
10937 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938{
10939 char_u *s;
10940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010942 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010944 /* Don't check an autoload name for existence here. */
10945 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010946 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 else
10948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = vim_strsave(s);
10950 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 }
10952}
10953
10954/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010955 * "garbagecollect()" function
10956 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010957 static void
10958f_garbagecollect(argvars, rettv)
10959 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010960 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010961{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010962 /* This is postponed until we are back at the toplevel, because we may be
10963 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10964 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010965
10966 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10967 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010968}
10969
10970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971 * "get()" function
10972 */
10973 static void
10974f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010975 typval_T *argvars;
10976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010977{
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 listitem_T *li;
10979 list_T *l;
10980 dictitem_T *di;
10981 dict_T *d;
10982 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010983
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010985 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 int error = FALSE;
10989
10990 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10991 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 else if (argvars[0].v_type == VAR_DICT)
10996 {
10997 if ((d = argvars[0].vval.v_dict) != NULL)
10998 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010999 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 if (di != NULL)
11001 tv = &di->di_tv;
11002 }
11003 }
11004 else
11005 EMSG2(_(e_listdictarg), "get()");
11006
11007 if (tv == NULL)
11008 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 copy_tv(&argvars[2], rettv);
11011 }
11012 else
11013 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014}
11015
Bram Moolenaar342337a2005-07-21 21:11:17 +000011016static 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 +000011017
11018/*
11019 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011020 * Return a range (from start to end) of lines in rettv from the specified
11021 * buffer.
11022 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011023 */
11024 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011025get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011026 buf_T *buf;
11027 linenr_T start;
11028 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011029 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011030 typval_T *rettv;
11031{
11032 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011033
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011034 if (retlist && rettv_list_alloc(rettv) == FAIL)
11035 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011036
11037 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11038 return;
11039
11040 if (!retlist)
11041 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011042 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11043 p = ml_get_buf(buf, start, FALSE);
11044 else
11045 p = (char_u *)"";
11046
11047 rettv->v_type = VAR_STRING;
11048 rettv->vval.v_string = vim_strsave(p);
11049 }
11050 else
11051 {
11052 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011053 return;
11054
11055 if (start < 1)
11056 start = 1;
11057 if (end > buf->b_ml.ml_line_count)
11058 end = buf->b_ml.ml_line_count;
11059 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011060 if (list_append_string(rettv->vval.v_list,
11061 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011062 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011063 }
11064}
11065
11066/*
11067 * "getbufline()" function
11068 */
11069 static void
11070f_getbufline(argvars, rettv)
11071 typval_T *argvars;
11072 typval_T *rettv;
11073{
11074 linenr_T lnum;
11075 linenr_T end;
11076 buf_T *buf;
11077
11078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11079 ++emsg_off;
11080 buf = get_buf_tv(&argvars[0]);
11081 --emsg_off;
11082
Bram Moolenaar661b1822005-07-28 22:36:45 +000011083 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011084 if (argvars[2].v_type == VAR_UNKNOWN)
11085 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011086 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011087 end = get_tv_lnum_buf(&argvars[2], buf);
11088
Bram Moolenaar342337a2005-07-21 21:11:17 +000011089 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011090}
11091
Bram Moolenaar0d660222005-01-07 21:51:51 +000011092/*
11093 * "getbufvar()" function
11094 */
11095 static void
11096f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 typval_T *argvars;
11098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011099{
11100 buf_T *buf;
11101 buf_T *save_curbuf;
11102 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11106 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011107 ++emsg_off;
11108 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109
11110 rettv->v_type = VAR_STRING;
11111 rettv->vval.v_string = NULL;
11112
11113 if (buf != NULL && varname != NULL)
11114 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011115 /* set curbuf to be our buf, temporarily */
11116 save_curbuf = curbuf;
11117 curbuf = buf;
11118
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011120 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011121 else if (STRCMP(varname, "changedtick") == 0)
11122 {
11123 rettv->v_type = VAR_NUMBER;
11124 rettv->vval.v_number = curbuf->b_changedtick;
11125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 else
11127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 if (*varname == NUL)
11129 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11130 * scope prefix before the NUL byte is required by
11131 * find_var_in_ht(). */
11132 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011134 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011137 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011138
11139 /* restore previous notion of curbuf */
11140 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141 }
11142
11143 --emsg_off;
11144}
11145
11146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 * "getchar()" function
11148 */
11149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011151 typval_T *argvars;
11152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153{
11154 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011157 /* Position the cursor. Needed after a message that ends in a space. */
11158 windgoto(msg_row, msg_col);
11159
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 ++no_mapping;
11161 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011162 for (;;)
11163 {
11164 if (argvars[0].v_type == VAR_UNKNOWN)
11165 /* getchar(): blocking wait. */
11166 n = safe_vgetc();
11167 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11168 /* getchar(1): only check if char avail */
11169 n = vpeekc();
11170 else if (error || vpeekc() == NUL)
11171 /* illegal argument or getchar(0) and no char avail: return zero */
11172 n = 0;
11173 else
11174 /* getchar(0) and char avail: return char */
11175 n = safe_vgetc();
11176 if (n == K_IGNORE)
11177 continue;
11178 break;
11179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 --no_mapping;
11181 --allow_keys;
11182
Bram Moolenaar219b8702006-11-01 14:32:36 +000011183 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11184 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11185 vimvars[VV_MOUSE_COL].vv_nr = 0;
11186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 if (IS_SPECIAL(n) || mod_mask != 0)
11189 {
11190 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11191 int i = 0;
11192
11193 /* Turn a special key into three bytes, plus modifier. */
11194 if (mod_mask != 0)
11195 {
11196 temp[i++] = K_SPECIAL;
11197 temp[i++] = KS_MODIFIER;
11198 temp[i++] = mod_mask;
11199 }
11200 if (IS_SPECIAL(n))
11201 {
11202 temp[i++] = K_SPECIAL;
11203 temp[i++] = K_SECOND(n);
11204 temp[i++] = K_THIRD(n);
11205 }
11206#ifdef FEAT_MBYTE
11207 else if (has_mbyte)
11208 i += (*mb_char2bytes)(n, temp + i);
11209#endif
11210 else
11211 temp[i++] = n;
11212 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011215
11216#ifdef FEAT_MOUSE
11217 if (n == K_LEFTMOUSE
11218 || n == K_LEFTMOUSE_NM
11219 || n == K_LEFTDRAG
11220 || n == K_LEFTRELEASE
11221 || n == K_LEFTRELEASE_NM
11222 || n == K_MIDDLEMOUSE
11223 || n == K_MIDDLEDRAG
11224 || n == K_MIDDLERELEASE
11225 || n == K_RIGHTMOUSE
11226 || n == K_RIGHTDRAG
11227 || n == K_RIGHTRELEASE
11228 || n == K_X1MOUSE
11229 || n == K_X1DRAG
11230 || n == K_X1RELEASE
11231 || n == K_X2MOUSE
11232 || n == K_X2DRAG
11233 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011234 || n == K_MOUSELEFT
11235 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011236 || n == K_MOUSEDOWN
11237 || n == K_MOUSEUP)
11238 {
11239 int row = mouse_row;
11240 int col = mouse_col;
11241 win_T *win;
11242 linenr_T lnum;
11243# ifdef FEAT_WINDOWS
11244 win_T *wp;
11245# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011246 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011247
11248 if (row >= 0 && col >= 0)
11249 {
11250 /* Find the window at the mouse coordinates and compute the
11251 * text position. */
11252 win = mouse_find_win(&row, &col);
11253 (void)mouse_comp_pos(win, &row, &col, &lnum);
11254# ifdef FEAT_WINDOWS
11255 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011256 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011257# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011258 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011259 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11260 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11261 }
11262 }
11263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 }
11265}
11266
11267/*
11268 * "getcharmod()" function
11269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011272 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
11279 * "getcmdline()" function
11280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011283 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->v_type = VAR_STRING;
11287 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288}
11289
11290/*
11291 * "getcmdpos()" function
11292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011302 * "getcmdtype()" function
11303 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011304 static void
11305f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011306 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011307 typval_T *rettv;
11308{
11309 rettv->v_type = VAR_STRING;
11310 rettv->vval.v_string = alloc(2);
11311 if (rettv->vval.v_string != NULL)
11312 {
11313 rettv->vval.v_string[0] = get_cmdline_type();
11314 rettv->vval.v_string[1] = NUL;
11315 }
11316}
11317
11318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 * "getcwd()" function
11320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011326 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011329 rettv->vval.v_string = NULL;
11330 cwd = alloc(MAXPATHL);
11331 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011333 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11334 {
11335 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011337 if (rettv->vval.v_string != NULL)
11338 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011340 }
11341 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343}
11344
11345/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011346 * "getfontname()" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011350 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->v_type = VAR_STRING;
11354 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011355#ifdef FEAT_GUI
11356 if (gui.in_use)
11357 {
11358 GuiFont font;
11359 char_u *name = NULL;
11360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011361 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011362 {
11363 /* Get the "Normal" font. Either the name saved by
11364 * hl_set_font_name() or from the font ID. */
11365 font = gui.norm_font;
11366 name = hl_get_font_name();
11367 }
11368 else
11369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011371 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11372 return;
11373 font = gui_mch_get_font(name, FALSE);
11374 if (font == NOFONT)
11375 return; /* Invalid font name, return empty string. */
11376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011379 gui_mch_free_font(font);
11380 }
11381#endif
11382}
11383
11384/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011385 * "getfperm({fname})" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391{
11392 char_u *fname;
11393 struct stat st;
11394 char_u *perm = NULL;
11395 char_u flags[] = "rwx";
11396 int i;
11397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011401 if (mch_stat((char *)fname, &st) >= 0)
11402 {
11403 perm = vim_strsave((char_u *)"---------");
11404 if (perm != NULL)
11405 {
11406 for (i = 0; i < 9; i++)
11407 {
11408 if (st.st_mode & (1 << (8 - i)))
11409 perm[i] = flags[i % 3];
11410 }
11411 }
11412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011414}
11415
11416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 * "getfsize({fname})" function
11418 */
11419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011421 typval_T *argvars;
11422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424 char_u *fname;
11425 struct stat st;
11426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
11431 if (mch_stat((char *)fname, &st) >= 0)
11432 {
11433 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011438
11439 /* non-perfect check for overflow */
11440 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11441 rettv->vval.v_number = -2;
11442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 }
11444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
11449 * "getftime({fname})" function
11450 */
11451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
11456 char_u *fname;
11457 struct stat st;
11458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
11461 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011468 * "getftype({fname})" function
11469 */
11470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011474{
11475 char_u *fname;
11476 struct stat st;
11477 char_u *type = NULL;
11478 char *t;
11479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011483 if (mch_lstat((char *)fname, &st) >= 0)
11484 {
11485#ifdef S_ISREG
11486 if (S_ISREG(st.st_mode))
11487 t = "file";
11488 else if (S_ISDIR(st.st_mode))
11489 t = "dir";
11490# ifdef S_ISLNK
11491 else if (S_ISLNK(st.st_mode))
11492 t = "link";
11493# endif
11494# ifdef S_ISBLK
11495 else if (S_ISBLK(st.st_mode))
11496 t = "bdev";
11497# endif
11498# ifdef S_ISCHR
11499 else if (S_ISCHR(st.st_mode))
11500 t = "cdev";
11501# endif
11502# ifdef S_ISFIFO
11503 else if (S_ISFIFO(st.st_mode))
11504 t = "fifo";
11505# endif
11506# ifdef S_ISSOCK
11507 else if (S_ISSOCK(st.st_mode))
11508 t = "fifo";
11509# endif
11510 else
11511 t = "other";
11512#else
11513# ifdef S_IFMT
11514 switch (st.st_mode & S_IFMT)
11515 {
11516 case S_IFREG: t = "file"; break;
11517 case S_IFDIR: t = "dir"; break;
11518# ifdef S_IFLNK
11519 case S_IFLNK: t = "link"; break;
11520# endif
11521# ifdef S_IFBLK
11522 case S_IFBLK: t = "bdev"; break;
11523# endif
11524# ifdef S_IFCHR
11525 case S_IFCHR: t = "cdev"; break;
11526# endif
11527# ifdef S_IFIFO
11528 case S_IFIFO: t = "fifo"; break;
11529# endif
11530# ifdef S_IFSOCK
11531 case S_IFSOCK: t = "socket"; break;
11532# endif
11533 default: t = "other";
11534 }
11535# else
11536 if (mch_isdir(fname))
11537 t = "dir";
11538 else
11539 t = "file";
11540# endif
11541#endif
11542 type = vim_strsave((char_u *)t);
11543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011545}
11546
11547/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011548 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549 */
11550 static void
11551f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554{
11555 linenr_T lnum;
11556 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011557 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558
11559 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011560 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011561 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011563 retlist = FALSE;
11564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011568 retlist = TRUE;
11569 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011570
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572}
11573
11574/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011575 * "getmatches()" function
11576 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011577 static void
11578f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011579 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011580 typval_T *rettv;
11581{
11582#ifdef FEAT_SEARCH_EXTRA
11583 dict_T *dict;
11584 matchitem_T *cur = curwin->w_match_head;
11585
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011586 if (rettv_list_alloc(rettv) == OK)
11587 {
11588 while (cur != NULL)
11589 {
11590 dict = dict_alloc();
11591 if (dict == NULL)
11592 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011593 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11594 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11595 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11596 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11597 list_append_dict(rettv->vval.v_list, dict);
11598 cur = cur->next;
11599 }
11600 }
11601#endif
11602}
11603
11604/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011605 * "getpid()" function
11606 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011607 static void
11608f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011609 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011610 typval_T *rettv;
11611{
11612 rettv->vval.v_number = mch_get_pid();
11613}
11614
11615/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011616 * "getpos(string)" function
11617 */
11618 static void
11619f_getpos(argvars, rettv)
11620 typval_T *argvars;
11621 typval_T *rettv;
11622{
11623 pos_T *fp;
11624 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011625 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011626
11627 if (rettv_list_alloc(rettv) == OK)
11628 {
11629 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011630 fp = var2fpos(&argvars[0], TRUE, &fnum);
11631 if (fnum != -1)
11632 list_append_number(l, (varnumber_T)fnum);
11633 else
11634 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011635 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11636 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011637 list_append_number(l, (fp != NULL)
11638 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011639 : (varnumber_T)0);
11640 list_append_number(l,
11641#ifdef FEAT_VIRTUALEDIT
11642 (fp != NULL) ? (varnumber_T)fp->coladd :
11643#endif
11644 (varnumber_T)0);
11645 }
11646 else
11647 rettv->vval.v_number = FALSE;
11648}
11649
11650/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011651 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011652 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011653 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011654f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011655 typval_T *argvars UNUSED;
11656 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011657{
11658#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011659 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011660#endif
11661
Bram Moolenaar2641f772005-03-25 21:58:17 +000011662#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011665 wp = NULL;
11666 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11667 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011668 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011669 if (wp == NULL)
11670 return;
11671 }
11672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011673 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011674 }
11675#endif
11676}
11677
11678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 * "getreg()" function
11680 */
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 char_u *strregname;
11687 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011688 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011691 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 strregname = get_tv_string_chk(&argvars[0]);
11694 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 regname = (strregname == NULL ? '"' : *strregname);
11701 if (regname == 0)
11702 regname = '"';
11703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 rettv->vval.v_string = error ? NULL :
11706 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708
11709/*
11710 * "getregtype()" function
11711 */
11712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011714 typval_T *argvars;
11715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 char_u *strregname;
11718 int regname;
11719 char_u buf[NUMBUFLEN + 2];
11720 long reglen = 0;
11721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011722 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011723 {
11724 strregname = get_tv_string_chk(&argvars[0]);
11725 if (strregname == NULL) /* type error; errmsg already given */
11726 {
11727 rettv->v_type = VAR_STRING;
11728 rettv->vval.v_string = NULL;
11729 return;
11730 }
11731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 else
11733 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
11736 regname = (strregname == NULL ? '"' : *strregname);
11737 if (regname == 0)
11738 regname = '"';
11739
11740 buf[0] = NUL;
11741 buf[1] = NUL;
11742 switch (get_reg_type(regname, &reglen))
11743 {
11744 case MLINE: buf[0] = 'V'; break;
11745 case MCHAR: buf[0] = 'v'; break;
11746#ifdef FEAT_VISUAL
11747 case MBLOCK:
11748 buf[0] = Ctrl_V;
11749 sprintf((char *)buf + 1, "%ld", reglen + 1);
11750 break;
11751#endif
11752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755}
11756
11757/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011758 * "gettabvar()" function
11759 */
11760 static void
11761f_gettabvar(argvars, rettv)
11762 typval_T *argvars;
11763 typval_T *rettv;
11764{
11765 tabpage_T *tp;
11766 dictitem_T *v;
11767 char_u *varname;
11768
11769 rettv->v_type = VAR_STRING;
11770 rettv->vval.v_string = NULL;
11771
11772 varname = get_tv_string_chk(&argvars[1]);
11773 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11774 if (tp != NULL && varname != NULL)
11775 {
11776 /* look up the variable */
11777 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11778 if (v != NULL)
11779 copy_tv(&v->di_tv, rettv);
11780 }
11781}
11782
11783/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011784 * "gettabwinvar()" function
11785 */
11786 static void
11787f_gettabwinvar(argvars, rettv)
11788 typval_T *argvars;
11789 typval_T *rettv;
11790{
11791 getwinvar(argvars, rettv, 1);
11792}
11793
11794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 * "getwinposx()" function
11796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803#ifdef FEAT_GUI
11804 if (gui.in_use)
11805 {
11806 int x, y;
11807
11808 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 }
11811#endif
11812}
11813
11814/*
11815 * "getwinposy()" function
11816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823#ifdef FEAT_GUI
11824 if (gui.in_use)
11825 {
11826 int x, y;
11827
11828 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 }
11831#endif
11832}
11833
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011834/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011835 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011836 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011837 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011838find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011839 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011840 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011841{
11842#ifdef FEAT_WINDOWS
11843 win_T *wp;
11844#endif
11845 int nr;
11846
11847 nr = get_tv_number_chk(vp, NULL);
11848
11849#ifdef FEAT_WINDOWS
11850 if (nr < 0)
11851 return NULL;
11852 if (nr == 0)
11853 return curwin;
11854
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011855 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11856 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011857 if (--nr <= 0)
11858 break;
11859 return wp;
11860#else
11861 if (nr == 0 || nr == 1)
11862 return curwin;
11863 return NULL;
11864#endif
11865}
11866
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867/*
11868 * "getwinvar()" function
11869 */
11870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 typval_T *argvars;
11873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011875 getwinvar(argvars, rettv, 0);
11876}
11877
11878/*
11879 * getwinvar() and gettabwinvar()
11880 */
11881 static void
11882getwinvar(argvars, rettv, off)
11883 typval_T *argvars;
11884 typval_T *rettv;
11885 int off; /* 1 for gettabwinvar() */
11886{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 win_T *win, *oldcurwin;
11888 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011889 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011890 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011892#ifdef FEAT_WINDOWS
11893 if (off == 1)
11894 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11895 else
11896 tp = curtab;
11897#endif
11898 win = find_win_by_nr(&argvars[off], tp);
11899 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 rettv->v_type = VAR_STRING;
11903 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904
11905 if (win != NULL && varname != NULL)
11906 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011907 /* Set curwin to be our win, temporarily. Also set curbuf, so
11908 * that we can get buffer-local options. */
11909 oldcurwin = curwin;
11910 curwin = win;
11911 curbuf = win->w_buffer;
11912
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 else
11916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 if (*varname == NUL)
11918 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11919 * scope prefix before the NUL byte is required by
11920 * find_var_in_ht(). */
11921 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011923 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011925 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011927
11928 /* restore previous notion of curwin */
11929 curwin = oldcurwin;
11930 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 }
11932
11933 --emsg_off;
11934}
11935
11936/*
11937 * "glob()" function
11938 */
11939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 typval_T *argvars;
11942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011944 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011946 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011948 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011949 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011951 if (argvars[1].v_type != VAR_UNKNOWN)
11952 {
11953 if (get_tv_number_chk(&argvars[1], &error))
11954 options |= WILD_KEEP_ALL;
11955 if (argvars[2].v_type != VAR_UNKNOWN
11956 && get_tv_number_chk(&argvars[2], &error))
11957 {
11958 rettv->v_type = VAR_LIST;
11959 rettv->vval.v_list = NULL;
11960 }
11961 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011962 if (!error)
11963 {
11964 ExpandInit(&xpc);
11965 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011966 if (p_wic)
11967 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011968 if (rettv->v_type == VAR_STRING)
11969 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011970 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011971 else if (rettv_list_alloc(rettv) != FAIL)
11972 {
11973 int i;
11974
11975 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11976 NULL, options, WILD_ALL_KEEP);
11977 for (i = 0; i < xpc.xp_numfiles; i++)
11978 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11979
11980 ExpandCleanup(&xpc);
11981 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011982 }
11983 else
11984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985}
11986
11987/*
11988 * "globpath()" function
11989 */
11990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011992 typval_T *argvars;
11993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011995 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011998 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012000 /* When the optional second argument is non-zero, don't remove matches
12001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12002 if (argvars[2].v_type != VAR_UNKNOWN
12003 && get_tv_number_chk(&argvars[2], &error))
12004 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012007 rettv->vval.v_string = NULL;
12008 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12010 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011}
12012
12013/*
12014 * "has()" function
12015 */
12016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012018 typval_T *argvars;
12019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020{
12021 int i;
12022 char_u *name;
12023 int n = FALSE;
12024 static char *(has_list[]) =
12025 {
12026#ifdef AMIGA
12027 "amiga",
12028# ifdef FEAT_ARP
12029 "arp",
12030# endif
12031#endif
12032#ifdef __BEOS__
12033 "beos",
12034#endif
12035#ifdef MSDOS
12036# ifdef DJGPP
12037 "dos32",
12038# else
12039 "dos16",
12040# endif
12041#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012042#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 "mac",
12044#endif
12045#if defined(MACOS_X_UNIX)
12046 "macunix",
12047#endif
12048#ifdef OS2
12049 "os2",
12050#endif
12051#ifdef __QNX__
12052 "qnx",
12053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054#ifdef UNIX
12055 "unix",
12056#endif
12057#ifdef VMS
12058 "vms",
12059#endif
12060#ifdef WIN16
12061 "win16",
12062#endif
12063#ifdef WIN32
12064 "win32",
12065#endif
12066#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12067 "win32unix",
12068#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012069#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 "win64",
12071#endif
12072#ifdef EBCDIC
12073 "ebcdic",
12074#endif
12075#ifndef CASE_INSENSITIVE_FILENAME
12076 "fname_case",
12077#endif
12078#ifdef FEAT_ARABIC
12079 "arabic",
12080#endif
12081#ifdef FEAT_AUTOCMD
12082 "autocmd",
12083#endif
12084#ifdef FEAT_BEVAL
12085 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12087 "balloon_multiline",
12088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089#endif
12090#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12091 "builtin_terms",
12092# ifdef ALL_BUILTIN_TCAPS
12093 "all_builtin_terms",
12094# endif
12095#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012096#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12097 || defined(FEAT_GUI_W32) \
12098 || defined(FEAT_GUI_MOTIF))
12099 "browsefilter",
12100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101#ifdef FEAT_BYTEOFF
12102 "byte_offset",
12103#endif
12104#ifdef FEAT_CINDENT
12105 "cindent",
12106#endif
12107#ifdef FEAT_CLIENTSERVER
12108 "clientserver",
12109#endif
12110#ifdef FEAT_CLIPBOARD
12111 "clipboard",
12112#endif
12113#ifdef FEAT_CMDL_COMPL
12114 "cmdline_compl",
12115#endif
12116#ifdef FEAT_CMDHIST
12117 "cmdline_hist",
12118#endif
12119#ifdef FEAT_COMMENTS
12120 "comments",
12121#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012122#ifdef FEAT_CONCEAL
12123 "conceal",
12124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125#ifdef FEAT_CRYPT
12126 "cryptv",
12127#endif
12128#ifdef FEAT_CSCOPE
12129 "cscope",
12130#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012131#ifdef FEAT_CURSORBIND
12132 "cursorbind",
12133#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012134#ifdef CURSOR_SHAPE
12135 "cursorshape",
12136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137#ifdef DEBUG
12138 "debug",
12139#endif
12140#ifdef FEAT_CON_DIALOG
12141 "dialog_con",
12142#endif
12143#ifdef FEAT_GUI_DIALOG
12144 "dialog_gui",
12145#endif
12146#ifdef FEAT_DIFF
12147 "diff",
12148#endif
12149#ifdef FEAT_DIGRAPHS
12150 "digraphs",
12151#endif
12152#ifdef FEAT_DND
12153 "dnd",
12154#endif
12155#ifdef FEAT_EMACS_TAGS
12156 "emacs_tags",
12157#endif
12158 "eval", /* always present, of course! */
12159#ifdef FEAT_EX_EXTRA
12160 "ex_extra",
12161#endif
12162#ifdef FEAT_SEARCH_EXTRA
12163 "extra_search",
12164#endif
12165#ifdef FEAT_FKMAP
12166 "farsi",
12167#endif
12168#ifdef FEAT_SEARCHPATH
12169 "file_in_path",
12170#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012171#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012172 "filterpipe",
12173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174#ifdef FEAT_FIND_ID
12175 "find_in_path",
12176#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012177#ifdef FEAT_FLOAT
12178 "float",
12179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180#ifdef FEAT_FOLDING
12181 "folding",
12182#endif
12183#ifdef FEAT_FOOTER
12184 "footer",
12185#endif
12186#if !defined(USE_SYSTEM) && defined(UNIX)
12187 "fork",
12188#endif
12189#ifdef FEAT_GETTEXT
12190 "gettext",
12191#endif
12192#ifdef FEAT_GUI
12193 "gui",
12194#endif
12195#ifdef FEAT_GUI_ATHENA
12196# ifdef FEAT_GUI_NEXTAW
12197 "gui_neXtaw",
12198# else
12199 "gui_athena",
12200# endif
12201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202#ifdef FEAT_GUI_GTK
12203 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012206#ifdef FEAT_GUI_GNOME
12207 "gui_gnome",
12208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209#ifdef FEAT_GUI_MAC
12210 "gui_mac",
12211#endif
12212#ifdef FEAT_GUI_MOTIF
12213 "gui_motif",
12214#endif
12215#ifdef FEAT_GUI_PHOTON
12216 "gui_photon",
12217#endif
12218#ifdef FEAT_GUI_W16
12219 "gui_win16",
12220#endif
12221#ifdef FEAT_GUI_W32
12222 "gui_win32",
12223#endif
12224#ifdef FEAT_HANGULIN
12225 "hangul_input",
12226#endif
12227#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12228 "iconv",
12229#endif
12230#ifdef FEAT_INS_EXPAND
12231 "insert_expand",
12232#endif
12233#ifdef FEAT_JUMPLIST
12234 "jumplist",
12235#endif
12236#ifdef FEAT_KEYMAP
12237 "keymap",
12238#endif
12239#ifdef FEAT_LANGMAP
12240 "langmap",
12241#endif
12242#ifdef FEAT_LIBCALL
12243 "libcall",
12244#endif
12245#ifdef FEAT_LINEBREAK
12246 "linebreak",
12247#endif
12248#ifdef FEAT_LISP
12249 "lispindent",
12250#endif
12251#ifdef FEAT_LISTCMDS
12252 "listcmds",
12253#endif
12254#ifdef FEAT_LOCALMAP
12255 "localmap",
12256#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012257#ifdef FEAT_LUA
12258# ifndef DYNAMIC_LUA
12259 "lua",
12260# endif
12261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262#ifdef FEAT_MENU
12263 "menu",
12264#endif
12265#ifdef FEAT_SESSION
12266 "mksession",
12267#endif
12268#ifdef FEAT_MODIFY_FNAME
12269 "modify_fname",
12270#endif
12271#ifdef FEAT_MOUSE
12272 "mouse",
12273#endif
12274#ifdef FEAT_MOUSESHAPE
12275 "mouseshape",
12276#endif
12277#if defined(UNIX) || defined(VMS)
12278# ifdef FEAT_MOUSE_DEC
12279 "mouse_dec",
12280# endif
12281# ifdef FEAT_MOUSE_GPM
12282 "mouse_gpm",
12283# endif
12284# ifdef FEAT_MOUSE_JSB
12285 "mouse_jsbterm",
12286# endif
12287# ifdef FEAT_MOUSE_NET
12288 "mouse_netterm",
12289# endif
12290# ifdef FEAT_MOUSE_PTERM
12291 "mouse_pterm",
12292# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012293# ifdef FEAT_MOUSE_SGR
12294 "mouse_sgr",
12295# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012296# ifdef FEAT_SYSMOUSE
12297 "mouse_sysmouse",
12298# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012299# ifdef FEAT_MOUSE_URXVT
12300 "mouse_urxvt",
12301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302# ifdef FEAT_MOUSE_XTERM
12303 "mouse_xterm",
12304# endif
12305#endif
12306#ifdef FEAT_MBYTE
12307 "multi_byte",
12308#endif
12309#ifdef FEAT_MBYTE_IME
12310 "multi_byte_ime",
12311#endif
12312#ifdef FEAT_MULTI_LANG
12313 "multi_lang",
12314#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012315#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012316#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012317 "mzscheme",
12318#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320#ifdef FEAT_OLE
12321 "ole",
12322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323#ifdef FEAT_PATH_EXTRA
12324 "path_extra",
12325#endif
12326#ifdef FEAT_PERL
12327#ifndef DYNAMIC_PERL
12328 "perl",
12329#endif
12330#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012331#ifdef FEAT_PERSISTENT_UNDO
12332 "persistent_undo",
12333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_PYTHON
12335#ifndef DYNAMIC_PYTHON
12336 "python",
12337#endif
12338#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012339#ifdef FEAT_PYTHON3
12340#ifndef DYNAMIC_PYTHON3
12341 "python3",
12342#endif
12343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344#ifdef FEAT_POSTSCRIPT
12345 "postscript",
12346#endif
12347#ifdef FEAT_PRINTER
12348 "printer",
12349#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012350#ifdef FEAT_PROFILE
12351 "profile",
12352#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012353#ifdef FEAT_RELTIME
12354 "reltime",
12355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356#ifdef FEAT_QUICKFIX
12357 "quickfix",
12358#endif
12359#ifdef FEAT_RIGHTLEFT
12360 "rightleft",
12361#endif
12362#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12363 "ruby",
12364#endif
12365#ifdef FEAT_SCROLLBIND
12366 "scrollbind",
12367#endif
12368#ifdef FEAT_CMDL_INFO
12369 "showcmd",
12370 "cmdline_info",
12371#endif
12372#ifdef FEAT_SIGNS
12373 "signs",
12374#endif
12375#ifdef FEAT_SMARTINDENT
12376 "smartindent",
12377#endif
12378#ifdef FEAT_SNIFF
12379 "sniff",
12380#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012381#ifdef STARTUPTIME
12382 "startuptime",
12383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384#ifdef FEAT_STL_OPT
12385 "statusline",
12386#endif
12387#ifdef FEAT_SUN_WORKSHOP
12388 "sun_workshop",
12389#endif
12390#ifdef FEAT_NETBEANS_INTG
12391 "netbeans_intg",
12392#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012393#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012394 "spell",
12395#endif
12396#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 "syntax",
12398#endif
12399#if defined(USE_SYSTEM) || !defined(UNIX)
12400 "system",
12401#endif
12402#ifdef FEAT_TAG_BINS
12403 "tag_binary",
12404#endif
12405#ifdef FEAT_TAG_OLDSTATIC
12406 "tag_old_static",
12407#endif
12408#ifdef FEAT_TAG_ANYWHITE
12409 "tag_any_white",
12410#endif
12411#ifdef FEAT_TCL
12412# ifndef DYNAMIC_TCL
12413 "tcl",
12414# endif
12415#endif
12416#ifdef TERMINFO
12417 "terminfo",
12418#endif
12419#ifdef FEAT_TERMRESPONSE
12420 "termresponse",
12421#endif
12422#ifdef FEAT_TEXTOBJ
12423 "textobjects",
12424#endif
12425#ifdef HAVE_TGETENT
12426 "tgetent",
12427#endif
12428#ifdef FEAT_TITLE
12429 "title",
12430#endif
12431#ifdef FEAT_TOOLBAR
12432 "toolbar",
12433#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012434#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12435 "unnamedplus",
12436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#ifdef FEAT_USR_CMDS
12438 "user-commands", /* was accidentally included in 5.4 */
12439 "user_commands",
12440#endif
12441#ifdef FEAT_VIMINFO
12442 "viminfo",
12443#endif
12444#ifdef FEAT_VERTSPLIT
12445 "vertsplit",
12446#endif
12447#ifdef FEAT_VIRTUALEDIT
12448 "virtualedit",
12449#endif
12450#ifdef FEAT_VISUAL
12451 "visual",
12452#endif
12453#ifdef FEAT_VISUALEXTRA
12454 "visualextra",
12455#endif
12456#ifdef FEAT_VREPLACE
12457 "vreplace",
12458#endif
12459#ifdef FEAT_WILDIGN
12460 "wildignore",
12461#endif
12462#ifdef FEAT_WILDMENU
12463 "wildmenu",
12464#endif
12465#ifdef FEAT_WINDOWS
12466 "windows",
12467#endif
12468#ifdef FEAT_WAK
12469 "winaltkeys",
12470#endif
12471#ifdef FEAT_WRITEBACKUP
12472 "writebackup",
12473#endif
12474#ifdef FEAT_XIM
12475 "xim",
12476#endif
12477#ifdef FEAT_XFONTSET
12478 "xfontset",
12479#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012480#ifdef FEAT_XPM_W32
12481 "xpm_w32",
12482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483#ifdef USE_XSMP
12484 "xsmp",
12485#endif
12486#ifdef USE_XSMP_INTERACT
12487 "xsmp_interact",
12488#endif
12489#ifdef FEAT_XCLIPBOARD
12490 "xterm_clipboard",
12491#endif
12492#ifdef FEAT_XTERM_SAVE
12493 "xterm_save",
12494#endif
12495#if defined(UNIX) && defined(FEAT_X11)
12496 "X11",
12497#endif
12498 NULL
12499 };
12500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 for (i = 0; has_list[i] != NULL; ++i)
12503 if (STRICMP(name, has_list[i]) == 0)
12504 {
12505 n = TRUE;
12506 break;
12507 }
12508
12509 if (n == FALSE)
12510 {
12511 if (STRNICMP(name, "patch", 5) == 0)
12512 n = has_patch(atoi((char *)name + 5));
12513 else if (STRICMP(name, "vim_starting") == 0)
12514 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012515#ifdef FEAT_MBYTE
12516 else if (STRICMP(name, "multi_byte_encoding") == 0)
12517 n = has_mbyte;
12518#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012519#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12520 else if (STRICMP(name, "balloon_multiline") == 0)
12521 n = multiline_balloon_available();
12522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523#ifdef DYNAMIC_TCL
12524 else if (STRICMP(name, "tcl") == 0)
12525 n = tcl_enabled(FALSE);
12526#endif
12527#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12528 else if (STRICMP(name, "iconv") == 0)
12529 n = iconv_enabled(FALSE);
12530#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012531#ifdef DYNAMIC_LUA
12532 else if (STRICMP(name, "lua") == 0)
12533 n = lua_enabled(FALSE);
12534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012535#ifdef DYNAMIC_MZSCHEME
12536 else if (STRICMP(name, "mzscheme") == 0)
12537 n = mzscheme_enabled(FALSE);
12538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539#ifdef DYNAMIC_RUBY
12540 else if (STRICMP(name, "ruby") == 0)
12541 n = ruby_enabled(FALSE);
12542#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012543#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544#ifdef DYNAMIC_PYTHON
12545 else if (STRICMP(name, "python") == 0)
12546 n = python_enabled(FALSE);
12547#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012548#endif
12549#ifdef FEAT_PYTHON3
12550#ifdef DYNAMIC_PYTHON3
12551 else if (STRICMP(name, "python3") == 0)
12552 n = python3_enabled(FALSE);
12553#endif
12554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555#ifdef DYNAMIC_PERL
12556 else if (STRICMP(name, "perl") == 0)
12557 n = perl_enabled(FALSE);
12558#endif
12559#ifdef FEAT_GUI
12560 else if (STRICMP(name, "gui_running") == 0)
12561 n = (gui.in_use || gui.starting);
12562# ifdef FEAT_GUI_W32
12563 else if (STRICMP(name, "gui_win32s") == 0)
12564 n = gui_is_win32s();
12565# endif
12566# ifdef FEAT_BROWSE
12567 else if (STRICMP(name, "browse") == 0)
12568 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12569# endif
12570#endif
12571#ifdef FEAT_SYN_HL
12572 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012573 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574#endif
12575#if defined(WIN3264)
12576 else if (STRICMP(name, "win95") == 0)
12577 n = mch_windows95();
12578#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012579#ifdef FEAT_NETBEANS_INTG
12580 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012581 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 }
12584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586}
12587
12588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012589 * "has_key()" function
12590 */
12591 static void
12592f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012595{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012596 if (argvars[0].v_type != VAR_DICT)
12597 {
12598 EMSG(_(e_dictreq));
12599 return;
12600 }
12601 if (argvars[0].vval.v_dict == NULL)
12602 return;
12603
12604 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012605 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012606}
12607
12608/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012609 * "haslocaldir()" function
12610 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012611 static void
12612f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012613 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012614 typval_T *rettv;
12615{
12616 rettv->vval.v_number = (curwin->w_localdir != NULL);
12617}
12618
12619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 * "hasmapto()" function
12621 */
12622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012624 typval_T *argvars;
12625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626{
12627 char_u *name;
12628 char_u *mode;
12629 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012630 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012633 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 mode = (char_u *)"nvo";
12635 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012638 if (argvars[2].v_type != VAR_UNKNOWN)
12639 abbr = get_tv_number(&argvars[2]);
12640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641
Bram Moolenaar2c932302006-03-18 21:42:09 +000012642 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646}
12647
12648/*
12649 * "histadd()" function
12650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655{
12656#ifdef FEAT_CMDHIST
12657 int histype;
12658 char_u *str;
12659 char_u buf[NUMBUFLEN];
12660#endif
12661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 if (check_restricted() || check_secure())
12664 return;
12665#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12667 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 if (histype >= 0)
12669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 if (*str != NUL)
12672 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012673 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 return;
12677 }
12678 }
12679#endif
12680}
12681
12682/*
12683 * "histdel()" function
12684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012687 typval_T *argvars UNUSED;
12688 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
12690#ifdef FEAT_CMDHIST
12691 int n;
12692 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12696 if (str == NULL)
12697 n = 0;
12698 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 else
12706 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708 get_tv_string_buf(&argvars[1], buf));
12709 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#endif
12711}
12712
12713/*
12714 * "histget()" function
12715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012718 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
12721#ifdef FEAT_CMDHIST
12722 int type;
12723 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12727 if (str == NULL)
12728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 {
12731 type = get_histtype(str);
12732 if (argvars[1].v_type == VAR_UNKNOWN)
12733 idx = get_history_idx(type);
12734 else
12735 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12736 /* -1 on type error */
12737 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
12746 * "histnr()" function
12747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752{
12753 int i;
12754
12755#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 char_u *history = get_tv_string_chk(&argvars[0]);
12757
12758 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 if (i >= HIST_CMD && i < HIST_COUNT)
12760 i = get_history_idx(i);
12761 else
12762#endif
12763 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765}
12766
12767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 * "highlightID(name)" function
12769 */
12770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 typval_T *argvars;
12773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776}
12777
12778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012779 * "highlight_exists()" function
12780 */
12781 static void
12782f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *argvars;
12784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785{
12786 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12787}
12788
12789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 * "hostname()" function
12791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012794 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796{
12797 char_u hostname[256];
12798
12799 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->v_type = VAR_STRING;
12801 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802}
12803
12804/*
12805 * iconv() function
12806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811{
12812#ifdef FEAT_MBYTE
12813 char_u buf1[NUMBUFLEN];
12814 char_u buf2[NUMBUFLEN];
12815 char_u *from, *to, *str;
12816 vimconv_T vimconv;
12817#endif
12818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->v_type = VAR_STRING;
12820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821
12822#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 str = get_tv_string(&argvars[0]);
12824 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12825 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 vimconv.vc_type = CONV_NONE;
12827 convert_setup(&vimconv, from, to);
12828
12829 /* If the encodings are equal, no conversion needed. */
12830 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834
12835 convert_setup(&vimconv, NULL, NULL);
12836 vim_free(from);
12837 vim_free(to);
12838#endif
12839}
12840
12841/*
12842 * "indent()" function
12843 */
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 linenr_T lnum;
12850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856}
12857
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012858/*
12859 * "index()" function
12860 */
12861 static void
12862f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *argvars;
12864 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012865{
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 list_T *l;
12867 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012868 long idx = 0;
12869 int ic = FALSE;
12870
12871 rettv->vval.v_number = -1;
12872 if (argvars[0].v_type != VAR_LIST)
12873 {
12874 EMSG(_(e_listreq));
12875 return;
12876 }
12877 l = argvars[0].vval.v_list;
12878 if (l != NULL)
12879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012880 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012881 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012883 int error = FALSE;
12884
Bram Moolenaar758711c2005-02-02 23:11:38 +000012885 /* Start at specified item. Use the cached index that list_find()
12886 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012887 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012888 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012889 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012890 ic = get_tv_number_chk(&argvars[3], &error);
12891 if (error)
12892 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012893 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012894
Bram Moolenaar758711c2005-02-02 23:11:38 +000012895 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012896 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012897 {
12898 rettv->vval.v_number = idx;
12899 break;
12900 }
12901 }
12902}
12903
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904static int inputsecret_flag = 0;
12905
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012906static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12907
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012909 * This function is used by f_input() and f_inputdialog() functions. The third
12910 * argument to f_input() specifies the type of completion to use at the
12911 * prompt. The third argument to f_inputdialog() specifies the value to return
12912 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 */
12914 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012915get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012918 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 char_u *p = NULL;
12922 int c;
12923 char_u buf[NUMBUFLEN];
12924 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012925 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012926 int xp_type = EXPAND_NOTHING;
12927 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931
12932#ifdef NO_CONSOLE_INPUT
12933 /* While starting up, there is no place to enter text. */
12934 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936#endif
12937
12938 cmd_silent = FALSE; /* Want to see the prompt. */
12939 if (prompt != NULL)
12940 {
12941 /* Only the part of the message after the last NL is considered as
12942 * prompt for the command line */
12943 p = vim_strrchr(prompt, '\n');
12944 if (p == NULL)
12945 p = prompt;
12946 else
12947 {
12948 ++p;
12949 c = *p;
12950 *p = NUL;
12951 msg_start();
12952 msg_clr_eos();
12953 msg_puts_attr(prompt, echo_attr);
12954 msg_didout = FALSE;
12955 msg_starthere();
12956 *p = c;
12957 }
12958 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012960 if (argvars[1].v_type != VAR_UNKNOWN)
12961 {
12962 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12963 if (defstr != NULL)
12964 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012966 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012967 {
12968 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012969 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012970 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012971
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012972 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012973 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012974
Bram Moolenaar4463f292005-09-25 22:20:24 +000012975 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12976 if (xp_name == NULL)
12977 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012979 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012980
Bram Moolenaar4463f292005-09-25 22:20:24 +000012981 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12982 &xp_arg) == FAIL)
12983 return;
12984 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012985 }
12986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 if (defstr != NULL)
12988 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012989 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12990 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020012991 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012992 && argvars[1].v_type != VAR_UNKNOWN
12993 && argvars[2].v_type != VAR_UNKNOWN)
12994 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
12995 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012996
12997 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999 /* since the user typed this, no need to wait for return */
13000 need_wait_return = FALSE;
13001 msg_didout = FALSE;
13002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 cmd_silent = cmd_silent_save;
13004}
13005
13006/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013007 * "input()" function
13008 * Also handles inputsecret() when inputsecret is set.
13009 */
13010 static void
13011f_input(argvars, rettv)
13012 typval_T *argvars;
13013 typval_T *rettv;
13014{
13015 get_user_input(argvars, rettv, FALSE);
13016}
13017
13018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 * "inputdialog()" function
13020 */
13021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *argvars;
13024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
13026#if defined(FEAT_GUI_TEXTDIALOG)
13027 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13028 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13029 {
13030 char_u *message;
13031 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 message = get_tv_string_chk(&argvars[0]);
13035 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013036 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013037 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 else
13039 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 if (message != NULL && defstr != NULL
13041 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013042 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 else
13045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 if (message != NULL && defstr != NULL
13047 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013048 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->vval.v_string = vim_strsave(
13050 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 }
13056 else
13057#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013058 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059}
13060
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013061/*
13062 * "inputlist()" function
13063 */
13064 static void
13065f_inputlist(argvars, rettv)
13066 typval_T *argvars;
13067 typval_T *rettv;
13068{
13069 listitem_T *li;
13070 int selected;
13071 int mouse_used;
13072
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013073#ifdef NO_CONSOLE_INPUT
13074 /* While starting up, there is no place to enter text. */
13075 if (no_console_input())
13076 return;
13077#endif
13078 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13079 {
13080 EMSG2(_(e_listarg), "inputlist()");
13081 return;
13082 }
13083
13084 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013085 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013086 lines_left = Rows; /* avoid more prompt */
13087 msg_scroll = TRUE;
13088 msg_clr_eos();
13089
13090 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13091 {
13092 msg_puts(get_tv_string(&li->li_tv));
13093 msg_putchar('\n');
13094 }
13095
13096 /* Ask for choice. */
13097 selected = prompt_for_number(&mouse_used);
13098 if (mouse_used)
13099 selected -= lines_left;
13100
13101 rettv->vval.v_number = selected;
13102}
13103
13104
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13106
13107/*
13108 * "inputrestore()" function
13109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_inputrestore(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{
13115 if (ga_userinput.ga_len > 0)
13116 {
13117 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13119 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013120 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 }
13122 else if (p_verbose > 1)
13123 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013124 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 }
13127}
13128
13129/*
13130 * "inputsave()" function
13131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013137 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 if (ga_grow(&ga_userinput, 1) == OK)
13139 {
13140 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13141 + ga_userinput.ga_len);
13142 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013143 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 }
13145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147}
13148
13149/*
13150 * "inputsecret()" function
13151 */
13152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013154 typval_T *argvars;
13155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156{
13157 ++cmdline_star;
13158 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 --cmdline_star;
13161 --inputsecret_flag;
13162}
13163
13164/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013165 * "insert()" function
13166 */
13167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013169 typval_T *argvars;
13170 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013171{
13172 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013173 listitem_T *item;
13174 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176
13177 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013179 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013180 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013181 {
13182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183 before = get_tv_number_chk(&argvars[2], &error);
13184 if (error)
13185 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013186
Bram Moolenaar758711c2005-02-02 23:11:38 +000013187 if (before == l->lv_len)
13188 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189 else
13190 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013191 item = list_find(l, before);
13192 if (item == NULL)
13193 {
13194 EMSGN(_(e_listidx), before);
13195 l = NULL;
13196 }
13197 }
13198 if (l != NULL)
13199 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013200 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013201 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202 }
13203 }
13204}
13205
13206/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013207 * "invert(expr)" function
13208 */
13209 static void
13210f_invert(argvars, rettv)
13211 typval_T *argvars;
13212 typval_T *rettv;
13213{
13214 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13215}
13216
13217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 * "isdirectory()" function
13219 */
13220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226}
13227
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013228/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013229 * "islocked()" function
13230 */
13231 static void
13232f_islocked(argvars, rettv)
13233 typval_T *argvars;
13234 typval_T *rettv;
13235{
13236 lval_T lv;
13237 char_u *end;
13238 dictitem_T *di;
13239
13240 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013241 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13242 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013243 if (end != NULL && lv.ll_name != NULL)
13244 {
13245 if (*end != NUL)
13246 EMSG(_(e_trailing));
13247 else
13248 {
13249 if (lv.ll_tv == NULL)
13250 {
13251 if (check_changedtick(lv.ll_name))
13252 rettv->vval.v_number = 1; /* always locked */
13253 else
13254 {
13255 di = find_var(lv.ll_name, NULL);
13256 if (di != NULL)
13257 {
13258 /* Consider a variable locked when:
13259 * 1. the variable itself is locked
13260 * 2. the value of the variable is locked.
13261 * 3. the List or Dict value is locked.
13262 */
13263 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13264 || tv_islocked(&di->di_tv));
13265 }
13266 }
13267 }
13268 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013269 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013270 else if (lv.ll_newkey != NULL)
13271 EMSG2(_(e_dictkey), lv.ll_newkey);
13272 else if (lv.ll_list != NULL)
13273 /* List item. */
13274 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13275 else
13276 /* Dictionary item. */
13277 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13278 }
13279 }
13280
13281 clear_lval(&lv);
13282}
13283
Bram Moolenaar33570922005-01-25 22:26:29 +000013284static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013285
13286/*
13287 * Turn a dict into a list:
13288 * "what" == 0: list of keys
13289 * "what" == 1: list of values
13290 * "what" == 2: list of items
13291 */
13292 static void
13293dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 typval_T *argvars;
13295 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013296 int what;
13297{
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 list_T *l2;
13299 dictitem_T *di;
13300 hashitem_T *hi;
13301 listitem_T *li;
13302 listitem_T *li2;
13303 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013304 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305
Bram Moolenaar8c711452005-01-14 21:53:12 +000013306 if (argvars[0].v_type != VAR_DICT)
13307 {
13308 EMSG(_(e_dictreq));
13309 return;
13310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013312 return;
13313
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013314 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013315 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013316
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013317 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013318 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013320 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013322 --todo;
13323 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013324
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013325 li = listitem_alloc();
13326 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013327 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013328 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013329
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013330 if (what == 0)
13331 {
13332 /* keys() */
13333 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013334 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013335 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13336 }
13337 else if (what == 1)
13338 {
13339 /* values() */
13340 copy_tv(&di->di_tv, &li->li_tv);
13341 }
13342 else
13343 {
13344 /* items() */
13345 l2 = list_alloc();
13346 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013347 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013348 li->li_tv.vval.v_list = l2;
13349 if (l2 == NULL)
13350 break;
13351 ++l2->lv_refcount;
13352
13353 li2 = listitem_alloc();
13354 if (li2 == NULL)
13355 break;
13356 list_append(l2, li2);
13357 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013358 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013359 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13360
13361 li2 = listitem_alloc();
13362 if (li2 == NULL)
13363 break;
13364 list_append(l2, li2);
13365 copy_tv(&di->di_tv, &li2->li_tv);
13366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013367 }
13368 }
13369}
13370
13371/*
13372 * "items(dict)" function
13373 */
13374 static void
13375f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *argvars;
13377 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013378{
13379 dict_list(argvars, rettv, 2);
13380}
13381
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013383 * "join()" function
13384 */
13385 static void
13386f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013389{
13390 garray_T ga;
13391 char_u *sep;
13392
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013393 if (argvars[0].v_type != VAR_LIST)
13394 {
13395 EMSG(_(e_listreq));
13396 return;
13397 }
13398 if (argvars[0].vval.v_list == NULL)
13399 return;
13400 if (argvars[1].v_type == VAR_UNKNOWN)
13401 sep = (char_u *)" ";
13402 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404
13405 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013406
13407 if (sep != NULL)
13408 {
13409 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013410 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 ga_append(&ga, NUL);
13412 rettv->vval.v_string = (char_u *)ga.ga_data;
13413 }
13414 else
13415 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013416}
13417
13418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013419 * "keys()" function
13420 */
13421 static void
13422f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425{
13426 dict_list(argvars, rettv, 0);
13427}
13428
13429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 * "last_buffer_nr()" function.
13431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013434 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436{
13437 int n = 0;
13438 buf_T *buf;
13439
13440 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13441 if (n < buf->b_fnum)
13442 n = buf->b_fnum;
13443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013445}
13446
13447/*
13448 * "len()" function
13449 */
13450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013451f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013452 typval_T *argvars;
13453 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013454{
13455 switch (argvars[0].v_type)
13456 {
13457 case VAR_STRING:
13458 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_number = (varnumber_T)STRLEN(
13460 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013461 break;
13462 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013464 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013465 case VAR_DICT:
13466 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013468 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013469 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 break;
13471 }
13472}
13473
Bram Moolenaar33570922005-01-25 22:26:29 +000013474static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475
13476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013480 int type;
13481{
13482#ifdef FEAT_LIBCALL
13483 char_u *string_in;
13484 char_u **string_result;
13485 int nr_result;
13486#endif
13487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013489 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491
13492 if (check_restricted() || check_secure())
13493 return;
13494
13495#ifdef FEAT_LIBCALL
13496 /* The first two args must be strings, otherwise its meaningless */
13497 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13498 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013499 string_in = NULL;
13500 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013501 string_in = argvars[2].vval.v_string;
13502 if (type == VAR_NUMBER)
13503 string_result = NULL;
13504 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013506 if (mch_libcall(argvars[0].vval.v_string,
13507 argvars[1].vval.v_string,
13508 string_in,
13509 argvars[2].vval.v_number,
13510 string_result,
13511 &nr_result) == OK
13512 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514 }
13515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516}
13517
13518/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013519 * "libcall()" function
13520 */
13521 static void
13522f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *argvars;
13524 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525{
13526 libcall_common(argvars, rettv, VAR_STRING);
13527}
13528
13529/*
13530 * "libcallnr()" function
13531 */
13532 static void
13533f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 libcall_common(argvars, rettv, VAR_NUMBER);
13538}
13539
13540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 * "line(string)" function
13542 */
13543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013545 typval_T *argvars;
13546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547{
13548 linenr_T lnum = 0;
13549 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013550 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013552 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 if (fp != NULL)
13554 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
13558/*
13559 * "line2byte(lnum)" function
13560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565{
13566#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568#else
13569 linenr_T lnum;
13570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13576 if (rettv->vval.v_number >= 0)
13577 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578#endif
13579}
13580
13581/*
13582 * "lispindent(lnum)" function
13583 */
13584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
13587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588{
13589#ifdef FEAT_LISP
13590 pos_T pos;
13591 linenr_T lnum;
13592
13593 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13596 {
13597 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 curwin->w_cursor = pos;
13600 }
13601 else
13602#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604}
13605
13606/*
13607 * "localtime()" function
13608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013611 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
Bram Moolenaar33570922005-01-25 22:26:29 +000013617static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618
13619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *argvars;
13622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 int exact;
13624{
13625 char_u *keys;
13626 char_u *which;
13627 char_u buf[NUMBUFLEN];
13628 char_u *keys_buf = NULL;
13629 char_u *rhs;
13630 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013631 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013632 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013633 mapblock_T *mp;
13634 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
13636 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->v_type = VAR_STRING;
13638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 if (*keys == NUL)
13642 return;
13643
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013646 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013648 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013649 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013650 if (argvars[3].v_type != VAR_UNKNOWN)
13651 get_dict = get_tv_number(&argvars[3]);
13652 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 else
13655 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 if (which == NULL)
13657 return;
13658
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 mode = get_map_mode(&which, 0);
13660
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013661 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013662 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013664
13665 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013667 /* Return a string. */
13668 if (rhs != NULL)
13669 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
Bram Moolenaarbd743252010-10-20 21:23:33 +020013671 }
13672 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13673 {
13674 /* Return a dictionary. */
13675 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13676 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13677 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678
Bram Moolenaarbd743252010-10-20 21:23:33 +020013679 dict_add_nr_str(dict, "lhs", 0L, lhs);
13680 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13681 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13682 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13683 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13684 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13685 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13686 dict_add_nr_str(dict, "mode", 0L, mapmode);
13687
13688 vim_free(lhs);
13689 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 }
13691}
13692
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013693#ifdef FEAT_FLOAT
13694/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013695 * "log()" function
13696 */
13697 static void
13698f_log(argvars, rettv)
13699 typval_T *argvars;
13700 typval_T *rettv;
13701{
13702 float_T f;
13703
13704 rettv->v_type = VAR_FLOAT;
13705 if (get_float_arg(argvars, &f) == OK)
13706 rettv->vval.v_float = log(f);
13707 else
13708 rettv->vval.v_float = 0.0;
13709}
13710
13711/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013712 * "log10()" function
13713 */
13714 static void
13715f_log10(argvars, rettv)
13716 typval_T *argvars;
13717 typval_T *rettv;
13718{
13719 float_T f;
13720
13721 rettv->v_type = VAR_FLOAT;
13722 if (get_float_arg(argvars, &f) == OK)
13723 rettv->vval.v_float = log10(f);
13724 else
13725 rettv->vval.v_float = 0.0;
13726}
13727#endif
13728
Bram Moolenaar1dced572012-04-05 16:54:08 +020013729#ifdef FEAT_LUA
13730/*
13731 * "luaeval()" function
13732 */
13733 static void
13734f_luaeval(argvars, rettv)
13735 typval_T *argvars;
13736 typval_T *rettv;
13737{
13738 char_u *str;
13739 char_u buf[NUMBUFLEN];
13740
13741 str = get_tv_string_buf(&argvars[0], buf);
13742 do_luaeval(str, argvars + 1, rettv);
13743}
13744#endif
13745
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013747 * "map()" function
13748 */
13749 static void
13750f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013751 typval_T *argvars;
13752 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013753{
13754 filter_map(argvars, rettv, TRUE);
13755}
13756
13757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 */
13760 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 typval_T *argvars;
13763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013765 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766}
13767
13768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 */
13771 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013776 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777}
13778
Bram Moolenaar33570922005-01-25 22:26:29 +000013779static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
13781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013783 typval_T *argvars;
13784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 int type;
13786{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013787 char_u *str = NULL;
13788 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 char_u *pat;
13790 regmatch_T regmatch;
13791 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013792 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 char_u *save_cpo;
13794 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013795 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013796 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013797 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 list_T *l = NULL;
13799 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013800 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013801 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
13803 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13804 save_cpo = p_cpo;
13805 p_cpo = (char_u *)"";
13806
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013807 rettv->vval.v_number = -1;
13808 if (type == 3)
13809 {
13810 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013811 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013812 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013813 }
13814 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816 rettv->v_type = VAR_STRING;
13817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 if (argvars[0].v_type == VAR_LIST)
13821 {
13822 if ((l = argvars[0].vval.v_list) == NULL)
13823 goto theend;
13824 li = l->lv_first;
13825 }
13826 else
13827 expr = str = get_tv_string(&argvars[0]);
13828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13830 if (pat == NULL)
13831 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013832
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 int error = FALSE;
13836
13837 start = get_tv_number_chk(&argvars[2], &error);
13838 if (error)
13839 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013840 if (l != NULL)
13841 {
13842 li = list_find(l, start);
13843 if (li == NULL)
13844 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013845 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846 }
13847 else
13848 {
13849 if (start < 0)
13850 start = 0;
13851 if (start > (long)STRLEN(str))
13852 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013853 /* When "count" argument is there ignore matches before "start",
13854 * otherwise skip part of the string. Differs when pattern is "^"
13855 * or "\<". */
13856 if (argvars[3].v_type != VAR_UNKNOWN)
13857 startcol = start;
13858 else
13859 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013861
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013862 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 nth = get_tv_number_chk(&argvars[3], &error);
13864 if (error)
13865 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 }
13867
13868 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13869 if (regmatch.regprog != NULL)
13870 {
13871 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013872
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013873 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013874 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013875 if (l != NULL)
13876 {
13877 if (li == NULL)
13878 {
13879 match = FALSE;
13880 break;
13881 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013882 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013883 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013884 if (str == NULL)
13885 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 }
13887
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013888 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013889
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013890 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013891 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892 if (l == NULL && !match)
13893 break;
13894
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013895 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013896 if (l != NULL)
13897 {
13898 li = li->li_next;
13899 ++idx;
13900 }
13901 else
13902 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013903#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013904 startcol = (colnr_T)(regmatch.startp[0]
13905 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013906#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013907 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013908#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013910 }
13911
13912 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013914 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013916 int i;
13917
13918 /* return list with matched string and submatches */
13919 for (i = 0; i < NSUBEXP; ++i)
13920 {
13921 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013922 {
13923 if (list_append_string(rettv->vval.v_list,
13924 (char_u *)"", 0) == FAIL)
13925 break;
13926 }
13927 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013928 regmatch.startp[i],
13929 (int)(regmatch.endp[i] - regmatch.startp[i]))
13930 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013931 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013932 }
13933 }
13934 else if (type == 2)
13935 {
13936 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937 if (l != NULL)
13938 copy_tv(&li->li_tv, rettv);
13939 else
13940 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013942 }
13943 else if (l != NULL)
13944 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 else
13946 {
13947 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 (varnumber_T)(regmatch.startp[0] - str);
13950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013953 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 }
13955 }
13956 vim_free(regmatch.regprog);
13957 }
13958
13959theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013960 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 p_cpo = save_cpo;
13962}
13963
13964/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965 * "match()" function
13966 */
13967 static void
13968f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971{
13972 find_some_match(argvars, rettv, 1);
13973}
13974
13975/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013976 * "matchadd()" function
13977 */
13978 static void
13979f_matchadd(argvars, rettv)
13980 typval_T *argvars;
13981 typval_T *rettv;
13982{
13983#ifdef FEAT_SEARCH_EXTRA
13984 char_u buf[NUMBUFLEN];
13985 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13986 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13987 int prio = 10; /* default priority */
13988 int id = -1;
13989 int error = FALSE;
13990
13991 rettv->vval.v_number = -1;
13992
13993 if (grp == NULL || pat == NULL)
13994 return;
13995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013996 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013997 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013998 if (argvars[3].v_type != VAR_UNKNOWN)
13999 id = get_tv_number_chk(&argvars[3], &error);
14000 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014001 if (error == TRUE)
14002 return;
14003 if (id >= 1 && id <= 3)
14004 {
14005 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14006 return;
14007 }
14008
14009 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14010#endif
14011}
14012
14013/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014014 * "matcharg()" function
14015 */
14016 static void
14017f_matcharg(argvars, rettv)
14018 typval_T *argvars;
14019 typval_T *rettv;
14020{
14021 if (rettv_list_alloc(rettv) == OK)
14022 {
14023#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014024 int id = get_tv_number(&argvars[0]);
14025 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014026
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014027 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014028 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014029 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14030 {
14031 list_append_string(rettv->vval.v_list,
14032 syn_id2name(m->hlg_id), -1);
14033 list_append_string(rettv->vval.v_list, m->pattern, -1);
14034 }
14035 else
14036 {
14037 list_append_string(rettv->vval.v_list, NUL, -1);
14038 list_append_string(rettv->vval.v_list, NUL, -1);
14039 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014040 }
14041#endif
14042 }
14043}
14044
14045/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014046 * "matchdelete()" function
14047 */
14048 static void
14049f_matchdelete(argvars, rettv)
14050 typval_T *argvars;
14051 typval_T *rettv;
14052{
14053#ifdef FEAT_SEARCH_EXTRA
14054 rettv->vval.v_number = match_delete(curwin,
14055 (int)get_tv_number(&argvars[0]), TRUE);
14056#endif
14057}
14058
14059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060 * "matchend()" function
14061 */
14062 static void
14063f_matchend(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, 0);
14068}
14069
14070/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014071 * "matchlist()" function
14072 */
14073 static void
14074f_matchlist(argvars, rettv)
14075 typval_T *argvars;
14076 typval_T *rettv;
14077{
14078 find_some_match(argvars, rettv, 3);
14079}
14080
14081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 * "matchstr()" function
14083 */
14084 static void
14085f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088{
14089 find_some_match(argvars, rettv, 2);
14090}
14091
Bram Moolenaar33570922005-01-25 22:26:29 +000014092static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014093
14094 static void
14095max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 typval_T *argvars;
14097 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014098 int domax;
14099{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014100 long n = 0;
14101 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014102 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014103
14104 if (argvars[0].v_type == VAR_LIST)
14105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014106 list_T *l;
14107 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014108
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014109 l = argvars[0].vval.v_list;
14110 if (l != NULL)
14111 {
14112 li = l->lv_first;
14113 if (li != NULL)
14114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014116 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014117 {
14118 li = li->li_next;
14119 if (li == NULL)
14120 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014121 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014122 if (domax ? i > n : i < n)
14123 n = i;
14124 }
14125 }
14126 }
14127 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014128 else if (argvars[0].v_type == VAR_DICT)
14129 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014134
14135 d = argvars[0].vval.v_dict;
14136 if (d != NULL)
14137 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014138 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014141 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014143 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014145 if (first)
14146 {
14147 n = i;
14148 first = FALSE;
14149 }
14150 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014151 n = i;
14152 }
14153 }
14154 }
14155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014156 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014157 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014159}
14160
14161/*
14162 * "max()" function
14163 */
14164 static void
14165f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *argvars;
14167 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014168{
14169 max_min(argvars, rettv, TRUE);
14170}
14171
14172/*
14173 * "min()" function
14174 */
14175 static void
14176f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 typval_T *argvars;
14178 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014179{
14180 max_min(argvars, rettv, FALSE);
14181}
14182
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014183static int mkdir_recurse __ARGS((char_u *dir, int prot));
14184
14185/*
14186 * Create the directory in which "dir" is located, and higher levels when
14187 * needed.
14188 */
14189 static int
14190mkdir_recurse(dir, prot)
14191 char_u *dir;
14192 int prot;
14193{
14194 char_u *p;
14195 char_u *updir;
14196 int r = FAIL;
14197
14198 /* Get end of directory name in "dir".
14199 * We're done when it's "/" or "c:/". */
14200 p = gettail_sep(dir);
14201 if (p <= get_past_head(dir))
14202 return OK;
14203
14204 /* If the directory exists we're done. Otherwise: create it.*/
14205 updir = vim_strnsave(dir, (int)(p - dir));
14206 if (updir == NULL)
14207 return FAIL;
14208 if (mch_isdir(updir))
14209 r = OK;
14210 else if (mkdir_recurse(updir, prot) == OK)
14211 r = vim_mkdir_emsg(updir, prot);
14212 vim_free(updir);
14213 return r;
14214}
14215
14216#ifdef vim_mkdir
14217/*
14218 * "mkdir()" function
14219 */
14220 static void
14221f_mkdir(argvars, rettv)
14222 typval_T *argvars;
14223 typval_T *rettv;
14224{
14225 char_u *dir;
14226 char_u buf[NUMBUFLEN];
14227 int prot = 0755;
14228
14229 rettv->vval.v_number = FAIL;
14230 if (check_restricted() || check_secure())
14231 return;
14232
14233 dir = get_tv_string_buf(&argvars[0], buf);
14234 if (argvars[1].v_type != VAR_UNKNOWN)
14235 {
14236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 prot = get_tv_number_chk(&argvars[2], NULL);
14238 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014239 mkdir_recurse(dir, prot);
14240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014242}
14243#endif
14244
Bram Moolenaar0d660222005-01-07 21:51:51 +000014245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 * "mode()" function
14247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 typval_T *argvars;
14251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014253 char_u buf[3];
14254
14255 buf[1] = NUL;
14256 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257
14258#ifdef FEAT_VISUAL
14259 if (VIsual_active)
14260 {
14261 if (VIsual_select)
14262 buf[0] = VIsual_mode + 's' - 'v';
14263 else
14264 buf[0] = VIsual_mode;
14265 }
14266 else
14267#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014268 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14269 || State == CONFIRM)
14270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014272 if (State == ASKMORE)
14273 buf[1] = 'm';
14274 else if (State == CONFIRM)
14275 buf[1] = '?';
14276 }
14277 else if (State == EXTERNCMD)
14278 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 else if (State & INSERT)
14280 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014281#ifdef FEAT_VREPLACE
14282 if (State & VREPLACE_FLAG)
14283 {
14284 buf[0] = 'R';
14285 buf[1] = 'v';
14286 }
14287 else
14288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 if (State & REPLACE_FLAG)
14290 buf[0] = 'R';
14291 else
14292 buf[0] = 'i';
14293 }
14294 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014297 if (exmode_active)
14298 buf[1] = 'v';
14299 }
14300 else if (exmode_active)
14301 {
14302 buf[0] = 'c';
14303 buf[1] = 'e';
14304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014308 if (finish_op)
14309 buf[1] = 'o';
14310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014312 /* Clear out the minor mode when the argument is not a non-zero number or
14313 * non-empty string. */
14314 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014315 buf[1] = NUL;
14316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_string = vim_strsave(buf);
14318 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319}
14320
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014321#ifdef FEAT_MZSCHEME
14322/*
14323 * "mzeval()" function
14324 */
14325 static void
14326f_mzeval(argvars, rettv)
14327 typval_T *argvars;
14328 typval_T *rettv;
14329{
14330 char_u *str;
14331 char_u buf[NUMBUFLEN];
14332
14333 str = get_tv_string_buf(&argvars[0], buf);
14334 do_mzeval(str, rettv);
14335}
14336#endif
14337
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014339 * "nextnonblank()" function
14340 */
14341 static void
14342f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014343 typval_T *argvars;
14344 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345{
14346 linenr_T lnum;
14347
14348 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014351 {
14352 lnum = 0;
14353 break;
14354 }
14355 if (*skipwhite(ml_get(lnum)) != NUL)
14356 break;
14357 }
14358 rettv->vval.v_number = lnum;
14359}
14360
14361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 * "nr2char()" function
14363 */
14364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *argvars;
14367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368{
14369 char_u buf[NUMBUFLEN];
14370
14371#ifdef FEAT_MBYTE
14372 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014373 {
14374 int utf8 = 0;
14375
14376 if (argvars[1].v_type != VAR_UNKNOWN)
14377 utf8 = get_tv_number_chk(&argvars[1], NULL);
14378 if (utf8)
14379 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14380 else
14381 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 else
14384#endif
14385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 buf[1] = NUL;
14388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->v_type = VAR_STRING;
14390 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014391}
14392
14393/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014394 * "or(expr, expr)" function
14395 */
14396 static void
14397f_or(argvars, rettv)
14398 typval_T *argvars;
14399 typval_T *rettv;
14400{
14401 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14402 | get_tv_number_chk(&argvars[1], NULL);
14403}
14404
14405/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014406 * "pathshorten()" function
14407 */
14408 static void
14409f_pathshorten(argvars, rettv)
14410 typval_T *argvars;
14411 typval_T *rettv;
14412{
14413 char_u *p;
14414
14415 rettv->v_type = VAR_STRING;
14416 p = get_tv_string_chk(&argvars[0]);
14417 if (p == NULL)
14418 rettv->vval.v_string = NULL;
14419 else
14420 {
14421 p = vim_strsave(p);
14422 rettv->vval.v_string = p;
14423 if (p != NULL)
14424 shorten_dir(p);
14425 }
14426}
14427
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014428#ifdef FEAT_FLOAT
14429/*
14430 * "pow()" function
14431 */
14432 static void
14433f_pow(argvars, rettv)
14434 typval_T *argvars;
14435 typval_T *rettv;
14436{
14437 float_T fx, fy;
14438
14439 rettv->v_type = VAR_FLOAT;
14440 if (get_float_arg(argvars, &fx) == OK
14441 && get_float_arg(&argvars[1], &fy) == OK)
14442 rettv->vval.v_float = pow(fx, fy);
14443 else
14444 rettv->vval.v_float = 0.0;
14445}
14446#endif
14447
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014449 * "prevnonblank()" function
14450 */
14451 static void
14452f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *argvars;
14454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455{
14456 linenr_T lnum;
14457
14458 lnum = get_tv_lnum(argvars);
14459 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14460 lnum = 0;
14461 else
14462 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14463 --lnum;
14464 rettv->vval.v_number = lnum;
14465}
14466
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014467#ifdef HAVE_STDARG_H
14468/* This dummy va_list is here because:
14469 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14470 * - locally in the function results in a "used before set" warning
14471 * - using va_start() to initialize it gives "function with fixed args" error */
14472static va_list ap;
14473#endif
14474
Bram Moolenaar8c711452005-01-14 21:53:12 +000014475/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014476 * "printf()" function
14477 */
14478 static void
14479f_printf(argvars, rettv)
14480 typval_T *argvars;
14481 typval_T *rettv;
14482{
14483 rettv->v_type = VAR_STRING;
14484 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014485#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014486 {
14487 char_u buf[NUMBUFLEN];
14488 int len;
14489 char_u *s;
14490 int saved_did_emsg = did_emsg;
14491 char *fmt;
14492
14493 /* Get the required length, allocate the buffer and do it for real. */
14494 did_emsg = FALSE;
14495 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014496 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014497 if (!did_emsg)
14498 {
14499 s = alloc(len + 1);
14500 if (s != NULL)
14501 {
14502 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014503 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014504 }
14505 }
14506 did_emsg |= saved_did_emsg;
14507 }
14508#endif
14509}
14510
14511/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014512 * "pumvisible()" function
14513 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014514 static void
14515f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014516 typval_T *argvars UNUSED;
14517 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014518{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014519#ifdef FEAT_INS_EXPAND
14520 if (pum_visible())
14521 rettv->vval.v_number = 1;
14522#endif
14523}
14524
Bram Moolenaardb913952012-06-29 12:54:53 +020014525#ifdef FEAT_PYTHON3
14526/*
14527 * "py3eval()" function
14528 */
14529 static void
14530f_py3eval(argvars, rettv)
14531 typval_T *argvars;
14532 typval_T *rettv;
14533{
14534 char_u *str;
14535 char_u buf[NUMBUFLEN];
14536
14537 str = get_tv_string_buf(&argvars[0], buf);
14538 do_py3eval(str, rettv);
14539}
14540#endif
14541
14542#ifdef FEAT_PYTHON
14543/*
14544 * "pyeval()" function
14545 */
14546 static void
14547f_pyeval(argvars, rettv)
14548 typval_T *argvars;
14549 typval_T *rettv;
14550{
14551 char_u *str;
14552 char_u buf[NUMBUFLEN];
14553
14554 str = get_tv_string_buf(&argvars[0], buf);
14555 do_pyeval(str, rettv);
14556}
14557#endif
14558
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014560 * "range()" function
14561 */
14562 static void
14563f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014564 typval_T *argvars;
14565 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014566{
14567 long start;
14568 long end;
14569 long stride = 1;
14570 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014571 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014574 if (argvars[1].v_type == VAR_UNKNOWN)
14575 {
14576 end = start - 1;
14577 start = 0;
14578 }
14579 else
14580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014581 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014583 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014584 }
14585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 if (error)
14587 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014588 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014589 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014590 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014591 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014592 else
14593 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014594 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014595 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014596 if (list_append_number(rettv->vval.v_list,
14597 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014598 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014599 }
14600}
14601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014602/*
14603 * "readfile()" function
14604 */
14605 static void
14606f_readfile(argvars, rettv)
14607 typval_T *argvars;
14608 typval_T *rettv;
14609{
14610 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014611 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014612 char_u *fname;
14613 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014614 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14615 int io_size = sizeof(buf);
14616 int readlen; /* size of last fread() */
14617 char_u *prev = NULL; /* previously read bytes, if any */
14618 long prevlen = 0; /* length of data in prev */
14619 long prevsize = 0; /* size of prev buffer */
14620 long maxline = MAXLNUM;
14621 long cnt = 0;
14622 char_u *p; /* position in buf */
14623 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014624
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014625 if (argvars[1].v_type != VAR_UNKNOWN)
14626 {
14627 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14628 binary = TRUE;
14629 if (argvars[2].v_type != VAR_UNKNOWN)
14630 maxline = get_tv_number(&argvars[2]);
14631 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014632
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014633 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014634 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014635
14636 /* Always open the file in binary mode, library functions have a mind of
14637 * their own about CR-LF conversion. */
14638 fname = get_tv_string(&argvars[0]);
14639 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14640 {
14641 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14642 return;
14643 }
14644
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014645 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014646 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014647 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014648
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014649 /* This for loop processes what was read, but is also entered at end
14650 * of file so that either:
14651 * - an incomplete line gets written
14652 * - a "binary" file gets an empty line at the end if it ends in a
14653 * newline. */
14654 for (p = buf, start = buf;
14655 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14656 ++p)
14657 {
14658 if (*p == '\n' || readlen <= 0)
14659 {
14660 listitem_T *li;
14661 char_u *s = NULL;
14662 long_u len = p - start;
14663
14664 /* Finished a line. Remove CRs before NL. */
14665 if (readlen > 0 && !binary)
14666 {
14667 while (len > 0 && start[len - 1] == '\r')
14668 --len;
14669 /* removal may cross back to the "prev" string */
14670 if (len == 0)
14671 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14672 --prevlen;
14673 }
14674 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014675 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676 else
14677 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014678 /* Change "prev" buffer to be the right size. This way
14679 * the bytes are only copied once, and very long lines are
14680 * allocated only once. */
14681 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014682 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014683 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014684 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014685 prev = NULL; /* the list will own the string */
14686 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014687 }
14688 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014689 if (s == NULL)
14690 {
14691 do_outofmem_msg((long_u) prevlen + len + 1);
14692 failed = TRUE;
14693 break;
14694 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014695
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014696 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014697 {
14698 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014699 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014700 break;
14701 }
14702 li->li_tv.v_type = VAR_STRING;
14703 li->li_tv.v_lock = 0;
14704 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014705 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014707 start = p + 1; /* step over newline */
14708 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709 break;
14710 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014711 else if (*p == NUL)
14712 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014713#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014714 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14715 * when finding the BF and check the previous two bytes. */
14716 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014718 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14719 * + 1, these may be in the "prev" string. */
14720 char_u back1 = p >= buf + 1 ? p[-1]
14721 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14722 char_u back2 = p >= buf + 2 ? p[-2]
14723 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14724 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014726 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014728 char_u *dest = p - 2;
14729
14730 /* Usually a BOM is at the beginning of a file, and so at
14731 * the beginning of a line; then we can just step over it.
14732 */
14733 if (start == dest)
14734 start = p + 1;
14735 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014736 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014737 /* have to shuffle buf to close gap */
14738 int adjust_prevlen = 0;
14739
14740 if (dest < buf)
14741 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014742 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 dest = buf;
14744 }
14745 if (readlen > p - buf + 1)
14746 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14747 readlen -= 3 - adjust_prevlen;
14748 prevlen -= adjust_prevlen;
14749 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014750 }
14751 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753#endif
14754 } /* for */
14755
14756 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14757 break;
14758 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 /* There's part of a line in buf, store it in "prev". */
14761 if (p - start + prevlen >= prevsize)
14762 {
14763 /* need bigger "prev" buffer */
14764 char_u *newprev;
14765
14766 /* A common use case is ordinary text files and "prev" gets a
14767 * fragment of a line, so the first allocation is made
14768 * small, to avoid repeatedly 'allocing' large and
14769 * 'reallocing' small. */
14770 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014771 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014772 else
14773 {
14774 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014775 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014776 prevsize = grow50pc > growmin ? grow50pc : growmin;
14777 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014778 newprev = prev == NULL ? alloc(prevsize)
14779 : vim_realloc(prev, prevsize);
14780 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014781 {
14782 do_outofmem_msg((long_u)prevsize);
14783 failed = TRUE;
14784 break;
14785 }
14786 prev = newprev;
14787 }
14788 /* Add the line part to end of "prev". */
14789 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014790 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014792 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014793
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014794 /*
14795 * For a negative line count use only the lines at the end of the file,
14796 * free the rest.
14797 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014798 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014799 while (cnt > -maxline)
14800 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014801 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014802 --cnt;
14803 }
14804
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014805 if (failed)
14806 {
14807 list_free(rettv->vval.v_list, TRUE);
14808 /* readfile doc says an empty list is returned on error */
14809 rettv->vval.v_list = list_alloc();
14810 }
14811
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014812 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014813 fclose(fd);
14814}
14815
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014816#if defined(FEAT_RELTIME)
14817static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14818
14819/*
14820 * Convert a List to proftime_T.
14821 * Return FAIL when there is something wrong.
14822 */
14823 static int
14824list2proftime(arg, tm)
14825 typval_T *arg;
14826 proftime_T *tm;
14827{
14828 long n1, n2;
14829 int error = FALSE;
14830
14831 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14832 || arg->vval.v_list->lv_len != 2)
14833 return FAIL;
14834 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14835 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14836# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014837 tm->HighPart = n1;
14838 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014839# else
14840 tm->tv_sec = n1;
14841 tm->tv_usec = n2;
14842# endif
14843 return error ? FAIL : OK;
14844}
14845#endif /* FEAT_RELTIME */
14846
14847/*
14848 * "reltime()" function
14849 */
14850 static void
14851f_reltime(argvars, rettv)
14852 typval_T *argvars;
14853 typval_T *rettv;
14854{
14855#ifdef FEAT_RELTIME
14856 proftime_T res;
14857 proftime_T start;
14858
14859 if (argvars[0].v_type == VAR_UNKNOWN)
14860 {
14861 /* No arguments: get current time. */
14862 profile_start(&res);
14863 }
14864 else if (argvars[1].v_type == VAR_UNKNOWN)
14865 {
14866 if (list2proftime(&argvars[0], &res) == FAIL)
14867 return;
14868 profile_end(&res);
14869 }
14870 else
14871 {
14872 /* Two arguments: compute the difference. */
14873 if (list2proftime(&argvars[0], &start) == FAIL
14874 || list2proftime(&argvars[1], &res) == FAIL)
14875 return;
14876 profile_sub(&res, &start);
14877 }
14878
14879 if (rettv_list_alloc(rettv) == OK)
14880 {
14881 long n1, n2;
14882
14883# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014884 n1 = res.HighPart;
14885 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014886# else
14887 n1 = res.tv_sec;
14888 n2 = res.tv_usec;
14889# endif
14890 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14891 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14892 }
14893#endif
14894}
14895
14896/*
14897 * "reltimestr()" function
14898 */
14899 static void
14900f_reltimestr(argvars, rettv)
14901 typval_T *argvars;
14902 typval_T *rettv;
14903{
14904#ifdef FEAT_RELTIME
14905 proftime_T tm;
14906#endif
14907
14908 rettv->v_type = VAR_STRING;
14909 rettv->vval.v_string = NULL;
14910#ifdef FEAT_RELTIME
14911 if (list2proftime(&argvars[0], &tm) == OK)
14912 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14913#endif
14914}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014915
Bram Moolenaar0d660222005-01-07 21:51:51 +000014916#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14917static void make_connection __ARGS((void));
14918static int check_connection __ARGS((void));
14919
14920 static void
14921make_connection()
14922{
14923 if (X_DISPLAY == NULL
14924# ifdef FEAT_GUI
14925 && !gui.in_use
14926# endif
14927 )
14928 {
14929 x_force_connect = TRUE;
14930 setup_term_clip();
14931 x_force_connect = FALSE;
14932 }
14933}
14934
14935 static int
14936check_connection()
14937{
14938 make_connection();
14939 if (X_DISPLAY == NULL)
14940 {
14941 EMSG(_("E240: No connection to Vim server"));
14942 return FAIL;
14943 }
14944 return OK;
14945}
14946#endif
14947
14948#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014949static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950
14951 static void
14952remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
14954 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 int expr;
14956{
14957 char_u *server_name;
14958 char_u *keys;
14959 char_u *r = NULL;
14960 char_u buf[NUMBUFLEN];
14961# ifdef WIN32
14962 HWND w;
14963# else
14964 Window w;
14965# endif
14966
14967 if (check_restricted() || check_secure())
14968 return;
14969
14970# ifdef FEAT_X11
14971 if (check_connection() == FAIL)
14972 return;
14973# endif
14974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014975 server_name = get_tv_string_chk(&argvars[0]);
14976 if (server_name == NULL)
14977 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 keys = get_tv_string_buf(&argvars[1], buf);
14979# ifdef WIN32
14980 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14981# else
14982 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14983 < 0)
14984# endif
14985 {
14986 if (r != NULL)
14987 EMSG(r); /* sending worked but evaluation failed */
14988 else
14989 EMSG2(_("E241: Unable to send to %s"), server_name);
14990 return;
14991 }
14992
14993 rettv->vval.v_string = r;
14994
14995 if (argvars[2].v_type != VAR_UNKNOWN)
14996 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014998 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015001 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015002 v.di_tv.v_type = VAR_STRING;
15003 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004 idvar = get_tv_string_chk(&argvars[2]);
15005 if (idvar != NULL)
15006 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015007 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008 }
15009}
15010#endif
15011
15012/*
15013 * "remote_expr()" function
15014 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015 static void
15016f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015017 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019{
15020 rettv->v_type = VAR_STRING;
15021 rettv->vval.v_string = NULL;
15022#ifdef FEAT_CLIENTSERVER
15023 remote_common(argvars, rettv, TRUE);
15024#endif
15025}
15026
15027/*
15028 * "remote_foreground()" function
15029 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030 static void
15031f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015032 typval_T *argvars UNUSED;
15033 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035#ifdef FEAT_CLIENTSERVER
15036# ifdef WIN32
15037 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015038 {
15039 char_u *server_name = get_tv_string_chk(&argvars[0]);
15040
15041 if (server_name != NULL)
15042 serverForeground(server_name);
15043 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044# else
15045 /* Send a foreground() expression to the server. */
15046 argvars[1].v_type = VAR_STRING;
15047 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15048 argvars[2].v_type = VAR_UNKNOWN;
15049 remote_common(argvars, rettv, TRUE);
15050 vim_free(argvars[1].vval.v_string);
15051# endif
15052#endif
15053}
15054
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 static void
15056f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059{
15060#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015061 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015062 char_u *s = NULL;
15063# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015064 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015066 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067
15068 if (check_restricted() || check_secure())
15069 {
15070 rettv->vval.v_number = -1;
15071 return;
15072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073 serverid = get_tv_string_chk(&argvars[0]);
15074 if (serverid == NULL)
15075 {
15076 rettv->vval.v_number = -1;
15077 return; /* type error; errmsg already given */
15078 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015079# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015080 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081 if (n == 0)
15082 rettv->vval.v_number = -1;
15083 else
15084 {
15085 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15086 rettv->vval.v_number = (s != NULL);
15087 }
15088# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 if (check_connection() == FAIL)
15090 return;
15091
15092 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094# endif
15095
15096 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 char_u *retvar;
15099
Bram Moolenaar33570922005-01-25 22:26:29 +000015100 v.di_tv.v_type = VAR_STRING;
15101 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015102 retvar = get_tv_string_chk(&argvars[1]);
15103 if (retvar != NULL)
15104 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 }
15107#else
15108 rettv->vval.v_number = -1;
15109#endif
15110}
15111
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 static void
15113f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116{
15117 char_u *r = NULL;
15118
15119#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 char_u *serverid = get_tv_string_chk(&argvars[0]);
15121
15122 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 {
15124# ifdef WIN32
15125 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015126 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015128 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 if (n != 0)
15130 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15131 if (r == NULL)
15132# else
15133 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135# endif
15136 EMSG(_("E277: Unable to read a server reply"));
15137 }
15138#endif
15139 rettv->v_type = VAR_STRING;
15140 rettv->vval.v_string = r;
15141}
15142
15143/*
15144 * "remote_send()" function
15145 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146 static void
15147f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015148 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015149 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150{
15151 rettv->v_type = VAR_STRING;
15152 rettv->vval.v_string = NULL;
15153#ifdef FEAT_CLIENTSERVER
15154 remote_common(argvars, rettv, FALSE);
15155#endif
15156}
15157
15158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015159 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015160 */
15161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015162f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 typval_T *argvars;
15164 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015165{
Bram Moolenaar33570922005-01-25 22:26:29 +000015166 list_T *l;
15167 listitem_T *item, *item2;
15168 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015169 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015170 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015171 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015172 dict_T *d;
15173 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015174 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015175
Bram Moolenaar8c711452005-01-14 21:53:12 +000015176 if (argvars[0].v_type == VAR_DICT)
15177 {
15178 if (argvars[2].v_type != VAR_UNKNOWN)
15179 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015180 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015181 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 key = get_tv_string_chk(&argvars[1]);
15184 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 di = dict_find(d, key, -1);
15187 if (di == NULL)
15188 EMSG2(_(e_dictkey), key);
15189 else
15190 {
15191 *rettv = di->di_tv;
15192 init_tv(&di->di_tv);
15193 dictitem_remove(d, di);
15194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015196 }
15197 }
15198 else if (argvars[0].v_type != VAR_LIST)
15199 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015200 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015201 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 int error = FALSE;
15204
15205 idx = get_tv_number_chk(&argvars[1], &error);
15206 if (error)
15207 ; /* type error: do nothing, errmsg already given */
15208 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015209 EMSGN(_(e_listidx), idx);
15210 else
15211 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015212 if (argvars[2].v_type == VAR_UNKNOWN)
15213 {
15214 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015215 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015216 *rettv = item->li_tv;
15217 vim_free(item);
15218 }
15219 else
15220 {
15221 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 end = get_tv_number_chk(&argvars[2], &error);
15223 if (error)
15224 ; /* type error: do nothing */
15225 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015226 EMSGN(_(e_listidx), end);
15227 else
15228 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015229 int cnt = 0;
15230
15231 for (li = item; li != NULL; li = li->li_next)
15232 {
15233 ++cnt;
15234 if (li == item2)
15235 break;
15236 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015237 if (li == NULL) /* didn't find "item2" after "item" */
15238 EMSG(_(e_invrange));
15239 else
15240 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015241 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015242 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015243 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015244 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015245 l->lv_first = item;
15246 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015247 item->li_prev = NULL;
15248 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015249 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015250 }
15251 }
15252 }
15253 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015254 }
15255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256}
15257
15258/*
15259 * "rename({from}, {to})" function
15260 */
15261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 typval_T *argvars;
15264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265{
15266 char_u buf[NUMBUFLEN];
15267
15268 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15272 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273}
15274
15275/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015276 * "repeat()" function
15277 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015278 static void
15279f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *argvars;
15281 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015282{
15283 char_u *p;
15284 int n;
15285 int slen;
15286 int len;
15287 char_u *r;
15288 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015289
15290 n = get_tv_number(&argvars[1]);
15291 if (argvars[0].v_type == VAR_LIST)
15292 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015293 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015294 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015295 if (list_extend(rettv->vval.v_list,
15296 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015297 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015298 }
15299 else
15300 {
15301 p = get_tv_string(&argvars[0]);
15302 rettv->v_type = VAR_STRING;
15303 rettv->vval.v_string = NULL;
15304
15305 slen = (int)STRLEN(p);
15306 len = slen * n;
15307 if (len <= 0)
15308 return;
15309
15310 r = alloc(len + 1);
15311 if (r != NULL)
15312 {
15313 for (i = 0; i < n; i++)
15314 mch_memmove(r + i * slen, p, (size_t)slen);
15315 r[len] = NUL;
15316 }
15317
15318 rettv->vval.v_string = r;
15319 }
15320}
15321
15322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 * "resolve()" function
15324 */
15325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015326f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015327 typval_T *argvars;
15328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329{
15330 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015331#ifdef HAVE_READLINK
15332 char_u *buf = NULL;
15333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336#ifdef FEAT_SHORTCUT
15337 {
15338 char_u *v = NULL;
15339
15340 v = mch_resolve_shortcut(p);
15341 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 }
15346#else
15347# ifdef HAVE_READLINK
15348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 char_u *cpy;
15350 int len;
15351 char_u *remain = NULL;
15352 char_u *q;
15353 int is_relative_to_current = FALSE;
15354 int has_trailing_pathsep = FALSE;
15355 int limit = 100;
15356
15357 p = vim_strsave(p);
15358
15359 if (p[0] == '.' && (vim_ispathsep(p[1])
15360 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15361 is_relative_to_current = TRUE;
15362
15363 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015364 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015367 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369
15370 q = getnextcomp(p);
15371 if (*q != NUL)
15372 {
15373 /* Separate the first path component in "p", and keep the
15374 * remainder (beginning with the path separator). */
15375 remain = vim_strsave(q - 1);
15376 q[-1] = NUL;
15377 }
15378
Bram Moolenaard9462e32011-04-11 21:35:11 +020015379 buf = alloc(MAXPATHL + 1);
15380 if (buf == NULL)
15381 goto fail;
15382
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 for (;;)
15384 {
15385 for (;;)
15386 {
15387 len = readlink((char *)p, (char *)buf, MAXPATHL);
15388 if (len <= 0)
15389 break;
15390 buf[len] = NUL;
15391
15392 if (limit-- == 0)
15393 {
15394 vim_free(p);
15395 vim_free(remain);
15396 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 goto fail;
15399 }
15400
15401 /* Ensure that the result will have a trailing path separator
15402 * if the argument has one. */
15403 if (remain == NULL && has_trailing_pathsep)
15404 add_pathsep(buf);
15405
15406 /* Separate the first path component in the link value and
15407 * concatenate the remainders. */
15408 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15409 if (*q != NUL)
15410 {
15411 if (remain == NULL)
15412 remain = vim_strsave(q - 1);
15413 else
15414 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015415 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 if (cpy != NULL)
15417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 vim_free(remain);
15419 remain = cpy;
15420 }
15421 }
15422 q[-1] = NUL;
15423 }
15424
15425 q = gettail(p);
15426 if (q > p && *q == NUL)
15427 {
15428 /* Ignore trailing path separator. */
15429 q[-1] = NUL;
15430 q = gettail(p);
15431 }
15432 if (q > p && !mch_isFullName(buf))
15433 {
15434 /* symlink is relative to directory of argument */
15435 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15436 if (cpy != NULL)
15437 {
15438 STRCPY(cpy, p);
15439 STRCPY(gettail(cpy), buf);
15440 vim_free(p);
15441 p = cpy;
15442 }
15443 }
15444 else
15445 {
15446 vim_free(p);
15447 p = vim_strsave(buf);
15448 }
15449 }
15450
15451 if (remain == NULL)
15452 break;
15453
15454 /* Append the first path component of "remain" to "p". */
15455 q = getnextcomp(remain + 1);
15456 len = q - remain - (*q != NUL);
15457 cpy = vim_strnsave(p, STRLEN(p) + len);
15458 if (cpy != NULL)
15459 {
15460 STRNCAT(cpy, remain, len);
15461 vim_free(p);
15462 p = cpy;
15463 }
15464 /* Shorten "remain". */
15465 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015466 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 else
15468 {
15469 vim_free(remain);
15470 remain = NULL;
15471 }
15472 }
15473
15474 /* If the result is a relative path name, make it explicitly relative to
15475 * the current directory if and only if the argument had this form. */
15476 if (!vim_ispathsep(*p))
15477 {
15478 if (is_relative_to_current
15479 && *p != NUL
15480 && !(p[0] == '.'
15481 && (p[1] == NUL
15482 || vim_ispathsep(p[1])
15483 || (p[1] == '.'
15484 && (p[2] == NUL
15485 || vim_ispathsep(p[2]))))))
15486 {
15487 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015488 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 if (cpy != NULL)
15490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 vim_free(p);
15492 p = cpy;
15493 }
15494 }
15495 else if (!is_relative_to_current)
15496 {
15497 /* Strip leading "./". */
15498 q = p;
15499 while (q[0] == '.' && vim_ispathsep(q[1]))
15500 q += 2;
15501 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015502 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 }
15504 }
15505
15506 /* Ensure that the result will have no trailing path separator
15507 * if the argument had none. But keep "/" or "//". */
15508 if (!has_trailing_pathsep)
15509 {
15510 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015511 if (after_pathsep(p, q))
15512 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 }
15514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 }
15517# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519# endif
15520#endif
15521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
15524#ifdef HAVE_READLINK
15525fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015526 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529}
15530
15531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015532 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 */
15534 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015535f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015536 typval_T *argvars;
15537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538{
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 list_T *l;
15540 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541
Bram Moolenaar0d660222005-01-07 21:51:51 +000015542 if (argvars[0].v_type != VAR_LIST)
15543 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015544 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015545 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015546 {
15547 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015548 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015549 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015550 while (li != NULL)
15551 {
15552 ni = li->li_prev;
15553 list_append(l, li);
15554 li = ni;
15555 }
15556 rettv->vval.v_list = l;
15557 rettv->v_type = VAR_LIST;
15558 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015559 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561}
15562
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015563#define SP_NOMOVE 0x01 /* don't move cursor */
15564#define SP_REPEAT 0x02 /* repeat to find outer pair */
15565#define SP_RETCOUNT 0x04 /* return matchcount */
15566#define SP_SETPCMARK 0x08 /* set previous context mark */
15567#define SP_START 0x10 /* accept match at start position */
15568#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15569#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015570
Bram Moolenaar33570922005-01-25 22:26:29 +000015571static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572
15573/*
15574 * Get flags for a search function.
15575 * Possibly sets "p_ws".
15576 * Returns BACKWARD, FORWARD or zero (for an error).
15577 */
15578 static int
15579get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015580 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581 int *flagsp;
15582{
15583 int dir = FORWARD;
15584 char_u *flags;
15585 char_u nbuf[NUMBUFLEN];
15586 int mask;
15587
15588 if (varp->v_type != VAR_UNKNOWN)
15589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015590 flags = get_tv_string_buf_chk(varp, nbuf);
15591 if (flags == NULL)
15592 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 while (*flags != NUL)
15594 {
15595 switch (*flags)
15596 {
15597 case 'b': dir = BACKWARD; break;
15598 case 'w': p_ws = TRUE; break;
15599 case 'W': p_ws = FALSE; break;
15600 default: mask = 0;
15601 if (flagsp != NULL)
15602 switch (*flags)
15603 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015604 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015605 case 'e': mask = SP_END; break;
15606 case 'm': mask = SP_RETCOUNT; break;
15607 case 'n': mask = SP_NOMOVE; break;
15608 case 'p': mask = SP_SUBPAT; break;
15609 case 'r': mask = SP_REPEAT; break;
15610 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 }
15612 if (mask == 0)
15613 {
15614 EMSG2(_(e_invarg2), flags);
15615 dir = 0;
15616 }
15617 else
15618 *flagsp |= mask;
15619 }
15620 if (dir == 0)
15621 break;
15622 ++flags;
15623 }
15624 }
15625 return dir;
15626}
15627
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015629 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015631 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015632search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015633 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015634 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015635 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015637 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 char_u *pat;
15639 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015640 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 int save_p_ws = p_ws;
15642 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015643 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015644 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015645 proftime_T tm;
15646#ifdef FEAT_RELTIME
15647 long time_limit = 0;
15648#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015649 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015650 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015653 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015654 if (dir == 0)
15655 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015656 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015657 if (flags & SP_START)
15658 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015659 if (flags & SP_END)
15660 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015661
Bram Moolenaar76929292008-01-06 19:07:36 +000015662 /* Optional arguments: line number to stop searching and timeout. */
15663 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015664 {
15665 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15666 if (lnum_stop < 0)
15667 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015668#ifdef FEAT_RELTIME
15669 if (argvars[3].v_type != VAR_UNKNOWN)
15670 {
15671 time_limit = get_tv_number_chk(&argvars[3], NULL);
15672 if (time_limit < 0)
15673 goto theend;
15674 }
15675#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015676 }
15677
Bram Moolenaar76929292008-01-06 19:07:36 +000015678#ifdef FEAT_RELTIME
15679 /* Set the time limit, if there is one. */
15680 profile_setlimit(time_limit, &tm);
15681#endif
15682
Bram Moolenaar231334e2005-07-25 20:46:57 +000015683 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015684 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015685 * Check to make sure only those flags are set.
15686 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15687 * flags cannot be set. Check for that condition also.
15688 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015689 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015690 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015693 goto theend;
15694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015696 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015697 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015698 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015699 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015701 if (flags & SP_SUBPAT)
15702 retval = subpatnum;
15703 else
15704 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015705 if (flags & SP_SETPCMARK)
15706 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015708 if (match_pos != NULL)
15709 {
15710 /* Store the match cursor position */
15711 match_pos->lnum = pos.lnum;
15712 match_pos->col = pos.col + 1;
15713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 /* "/$" will put the cursor after the end of the line, may need to
15715 * correct that here */
15716 check_cursor();
15717 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015718
15719 /* If 'n' flag is used: restore cursor position. */
15720 if (flags & SP_NOMOVE)
15721 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015722 else
15723 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015724theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015726
15727 return retval;
15728}
15729
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015730#ifdef FEAT_FLOAT
15731/*
15732 * "round({float})" function
15733 */
15734 static void
15735f_round(argvars, rettv)
15736 typval_T *argvars;
15737 typval_T *rettv;
15738{
15739 float_T f;
15740
15741 rettv->v_type = VAR_FLOAT;
15742 if (get_float_arg(argvars, &f) == OK)
15743 /* round() is not in C90, use ceil() or floor() instead. */
15744 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15745 else
15746 rettv->vval.v_float = 0.0;
15747}
15748#endif
15749
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015750/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015751 * "screencol()" function
15752 *
15753 * First column is 1 to be consistent with virtcol().
15754 */
15755 static void
15756f_screencol(argvars, rettv)
15757 typval_T *argvars UNUSED;
15758 typval_T *rettv;
15759{
15760 rettv->vval.v_number = screen_screencol() + 1;
15761}
15762
15763/*
15764 * "screenrow()" function
15765 */
15766 static void
15767f_screenrow(argvars, rettv)
15768 typval_T *argvars UNUSED;
15769 typval_T *rettv;
15770{
15771 rettv->vval.v_number = screen_screenrow() + 1;
15772}
15773
15774/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015775 * "search()" function
15776 */
15777 static void
15778f_search(argvars, rettv)
15779 typval_T *argvars;
15780 typval_T *rettv;
15781{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015782 int flags = 0;
15783
15784 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785}
15786
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015788 * "searchdecl()" function
15789 */
15790 static void
15791f_searchdecl(argvars, rettv)
15792 typval_T *argvars;
15793 typval_T *rettv;
15794{
15795 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015796 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015797 int error = FALSE;
15798 char_u *name;
15799
15800 rettv->vval.v_number = 1; /* default: FAIL */
15801
15802 name = get_tv_string_chk(&argvars[0]);
15803 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015804 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015805 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015806 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15807 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15808 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015809 if (!error && name != NULL)
15810 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015811 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015812}
15813
15814/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015815 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015817 static int
15818searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015820 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821{
15822 char_u *spat, *mpat, *epat;
15823 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825 int dir;
15826 int flags = 0;
15827 char_u nbuf1[NUMBUFLEN];
15828 char_u nbuf2[NUMBUFLEN];
15829 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015830 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015831 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015832 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 spat = get_tv_string_chk(&argvars[0]);
15836 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15837 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15838 if (spat == NULL || mpat == NULL || epat == NULL)
15839 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 /* Handle the optional fourth argument: flags */
15842 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015843 if (dir == 0)
15844 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015845
15846 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015847 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15848 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015849 if ((flags & (SP_END | SP_SUBPAT)) != 0
15850 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015851 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015852 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015853 goto theend;
15854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015856 /* Using 'r' implies 'W', otherwise it doesn't work. */
15857 if (flags & SP_REPEAT)
15858 p_ws = FALSE;
15859
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015860 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015861 if (argvars[3].v_type == VAR_UNKNOWN
15862 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 skip = (char_u *)"";
15864 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015867 if (argvars[5].v_type != VAR_UNKNOWN)
15868 {
15869 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15870 if (lnum_stop < 0)
15871 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015872#ifdef FEAT_RELTIME
15873 if (argvars[6].v_type != VAR_UNKNOWN)
15874 {
15875 time_limit = get_tv_number_chk(&argvars[6], NULL);
15876 if (time_limit < 0)
15877 goto theend;
15878 }
15879#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015880 }
15881 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015882 if (skip == NULL)
15883 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015885 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015886 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015887
15888theend:
15889 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015890
15891 return retval;
15892}
15893
15894/*
15895 * "searchpair()" function
15896 */
15897 static void
15898f_searchpair(argvars, rettv)
15899 typval_T *argvars;
15900 typval_T *rettv;
15901{
15902 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15903}
15904
15905/*
15906 * "searchpairpos()" function
15907 */
15908 static void
15909f_searchpairpos(argvars, rettv)
15910 typval_T *argvars;
15911 typval_T *rettv;
15912{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015913 pos_T match_pos;
15914 int lnum = 0;
15915 int col = 0;
15916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015917 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015918 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015919
15920 if (searchpair_cmn(argvars, &match_pos) > 0)
15921 {
15922 lnum = match_pos.lnum;
15923 col = match_pos.col;
15924 }
15925
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15927 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015928}
15929
15930/*
15931 * Search for a start/middle/end thing.
15932 * Used by searchpair(), see its documentation for the details.
15933 * Returns 0 or -1 for no match,
15934 */
15935 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015936do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15937 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015938 char_u *spat; /* start pattern */
15939 char_u *mpat; /* middle pattern */
15940 char_u *epat; /* end pattern */
15941 int dir; /* BACKWARD or FORWARD */
15942 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015943 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015944 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015945 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015946 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015947{
15948 char_u *save_cpo;
15949 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15950 long retval = 0;
15951 pos_T pos;
15952 pos_T firstpos;
15953 pos_T foundpos;
15954 pos_T save_cursor;
15955 pos_T save_pos;
15956 int n;
15957 int r;
15958 int nest = 1;
15959 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015960 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015961 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015962
15963 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15964 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015965 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015966
Bram Moolenaar76929292008-01-06 19:07:36 +000015967#ifdef FEAT_RELTIME
15968 /* Set the time limit, if there is one. */
15969 profile_setlimit(time_limit, &tm);
15970#endif
15971
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015972 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15973 * start/middle/end (pat3, for the top pair). */
15974 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15975 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15976 if (pat2 == NULL || pat3 == NULL)
15977 goto theend;
15978 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15979 if (*mpat == NUL)
15980 STRCPY(pat3, pat2);
15981 else
15982 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15983 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015984 if (flags & SP_START)
15985 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015986
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 save_cursor = curwin->w_cursor;
15988 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015989 clearpos(&firstpos);
15990 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 pat = pat3;
15992 for (;;)
15993 {
15994 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015995 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15997 /* didn't find it or found the first match again: FAIL */
15998 break;
15999
16000 if (firstpos.lnum == 0)
16001 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016002 if (equalpos(pos, foundpos))
16003 {
16004 /* Found the same position again. Can happen with a pattern that
16005 * has "\zs" at the end and searching backwards. Advance one
16006 * character and try again. */
16007 if (dir == BACKWARD)
16008 decl(&pos);
16009 else
16010 incl(&pos);
16011 }
16012 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016014 /* clear the start flag to avoid getting stuck here */
16015 options &= ~SEARCH_START;
16016
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 /* If the skip pattern matches, ignore this match. */
16018 if (*skip != NUL)
16019 {
16020 save_pos = curwin->w_cursor;
16021 curwin->w_cursor = pos;
16022 r = eval_to_bool(skip, &err, NULL, FALSE);
16023 curwin->w_cursor = save_pos;
16024 if (err)
16025 {
16026 /* Evaluating {skip} caused an error, break here. */
16027 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016028 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 break;
16030 }
16031 if (r)
16032 continue;
16033 }
16034
16035 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16036 {
16037 /* Found end when searching backwards or start when searching
16038 * forward: nested pair. */
16039 ++nest;
16040 pat = pat2; /* nested, don't search for middle */
16041 }
16042 else
16043 {
16044 /* Found end when searching forward or start when searching
16045 * backward: end of (nested) pair; or found middle in outer pair. */
16046 if (--nest == 1)
16047 pat = pat3; /* outer level, search for middle */
16048 }
16049
16050 if (nest == 0)
16051 {
16052 /* Found the match: return matchcount or line number. */
16053 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016054 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016056 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016057 if (flags & SP_SETPCMARK)
16058 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 curwin->w_cursor = pos;
16060 if (!(flags & SP_REPEAT))
16061 break;
16062 nest = 1; /* search for next unmatched */
16063 }
16064 }
16065
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016066 if (match_pos != NULL)
16067 {
16068 /* Store the match cursor position */
16069 match_pos->lnum = curwin->w_cursor.lnum;
16070 match_pos->col = curwin->w_cursor.col + 1;
16071 }
16072
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016074 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 curwin->w_cursor = save_cursor;
16076
16077theend:
16078 vim_free(pat2);
16079 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016080 if (p_cpo == empty_option)
16081 p_cpo = save_cpo;
16082 else
16083 /* Darn, evaluating the {skip} expression changed the value. */
16084 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016085
16086 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087}
16088
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016089/*
16090 * "searchpos()" function
16091 */
16092 static void
16093f_searchpos(argvars, rettv)
16094 typval_T *argvars;
16095 typval_T *rettv;
16096{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016097 pos_T match_pos;
16098 int lnum = 0;
16099 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016100 int n;
16101 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016102
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016103 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016104 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016105
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016106 n = search_cmn(argvars, &match_pos, &flags);
16107 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016108 {
16109 lnum = match_pos.lnum;
16110 col = match_pos.col;
16111 }
16112
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016113 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16114 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016115 if (flags & SP_SUBPAT)
16116 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016117}
16118
16119
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120 static void
16121f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016122 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125#ifdef FEAT_CLIENTSERVER
16126 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016127 char_u *server = get_tv_string_chk(&argvars[0]);
16128 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129
Bram Moolenaar0d660222005-01-07 21:51:51 +000016130 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016131 if (server == NULL || reply == NULL)
16132 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016133 if (check_restricted() || check_secure())
16134 return;
16135# ifdef FEAT_X11
16136 if (check_connection() == FAIL)
16137 return;
16138# endif
16139
16140 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016142 EMSG(_("E258: Unable to send to client"));
16143 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016145 rettv->vval.v_number = 0;
16146#else
16147 rettv->vval.v_number = -1;
16148#endif
16149}
16150
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151 static void
16152f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016155{
16156 char_u *r = NULL;
16157
16158#ifdef FEAT_CLIENTSERVER
16159# ifdef WIN32
16160 r = serverGetVimNames();
16161# else
16162 make_connection();
16163 if (X_DISPLAY != NULL)
16164 r = serverGetVimNames(X_DISPLAY);
16165# endif
16166#endif
16167 rettv->v_type = VAR_STRING;
16168 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169}
16170
16171/*
16172 * "setbufvar()" function
16173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016177 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178{
16179 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016182 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 char_u nbuf[NUMBUFLEN];
16184
16185 if (check_restricted() || check_secure())
16186 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016187 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16188 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016189 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 varp = &argvars[2];
16191
16192 if (buf != NULL && varname != NULL && varp != NULL)
16193 {
16194 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196
16197 if (*varname == '&')
16198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016199 long numval;
16200 char_u *strval;
16201 int error = FALSE;
16202
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016204 numval = get_tv_number_chk(varp, &error);
16205 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016206 if (!error && strval != NULL)
16207 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 }
16209 else
16210 {
16211 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16212 if (bufvarname != NULL)
16213 {
16214 STRCPY(bufvarname, "b:");
16215 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016216 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 vim_free(bufvarname);
16218 }
16219 }
16220
16221 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224}
16225
16226/*
16227 * "setcmdpos()" function
16228 */
16229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016230f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016231 typval_T *argvars;
16232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 int pos = (int)get_tv_number(&argvars[0]) - 1;
16235
16236 if (pos >= 0)
16237 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238}
16239
16240/*
16241 * "setline()" function
16242 */
16243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016244f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016245 typval_T *argvars;
16246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247{
16248 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016249 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016250 list_T *l = NULL;
16251 listitem_T *li = NULL;
16252 long added = 0;
16253 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016255 lnum = get_tv_lnum(&argvars[0]);
16256 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016258 l = argvars[1].vval.v_list;
16259 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016261 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016263
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016264 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016265 for (;;)
16266 {
16267 if (l != NULL)
16268 {
16269 /* list argument, get next string */
16270 if (li == NULL)
16271 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016272 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016273 li = li->li_next;
16274 }
16275
16276 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016277 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016278 break;
16279 if (lnum <= curbuf->b_ml.ml_line_count)
16280 {
16281 /* existing line, replace it */
16282 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16283 {
16284 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016285 if (lnum == curwin->w_cursor.lnum)
16286 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016287 rettv->vval.v_number = 0; /* OK */
16288 }
16289 }
16290 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16291 {
16292 /* lnum is one past the last line, append the line */
16293 ++added;
16294 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16295 rettv->vval.v_number = 0; /* OK */
16296 }
16297
16298 if (l == NULL) /* only one string argument */
16299 break;
16300 ++lnum;
16301 }
16302
16303 if (added > 0)
16304 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305}
16306
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016307static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16308
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016310 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016311 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016312 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016313set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016314 win_T *wp UNUSED;
16315 typval_T *list_arg UNUSED;
16316 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016317 typval_T *rettv;
16318{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016319#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016320 char_u *act;
16321 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016322#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016323
Bram Moolenaar2641f772005-03-25 21:58:17 +000016324 rettv->vval.v_number = -1;
16325
16326#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016327 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016328 EMSG(_(e_listreq));
16329 else
16330 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016331 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016332
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016333 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016334 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016335 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016336 if (act == NULL)
16337 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016338 if (*act == 'a' || *act == 'r')
16339 action = *act;
16340 }
16341
Bram Moolenaar81484f42012-12-05 15:16:47 +010016342 if (l != NULL && set_errorlist(wp, l, action,
16343 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016344 rettv->vval.v_number = 0;
16345 }
16346#endif
16347}
16348
16349/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016350 * "setloclist()" function
16351 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016352 static void
16353f_setloclist(argvars, rettv)
16354 typval_T *argvars;
16355 typval_T *rettv;
16356{
16357 win_T *win;
16358
16359 rettv->vval.v_number = -1;
16360
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016361 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016362 if (win != NULL)
16363 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16364}
16365
16366/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016367 * "setmatches()" function
16368 */
16369 static void
16370f_setmatches(argvars, rettv)
16371 typval_T *argvars;
16372 typval_T *rettv;
16373{
16374#ifdef FEAT_SEARCH_EXTRA
16375 list_T *l;
16376 listitem_T *li;
16377 dict_T *d;
16378
16379 rettv->vval.v_number = -1;
16380 if (argvars[0].v_type != VAR_LIST)
16381 {
16382 EMSG(_(e_listreq));
16383 return;
16384 }
16385 if ((l = argvars[0].vval.v_list) != NULL)
16386 {
16387
16388 /* To some extent make sure that we are dealing with a list from
16389 * "getmatches()". */
16390 li = l->lv_first;
16391 while (li != NULL)
16392 {
16393 if (li->li_tv.v_type != VAR_DICT
16394 || (d = li->li_tv.vval.v_dict) == NULL)
16395 {
16396 EMSG(_(e_invarg));
16397 return;
16398 }
16399 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16400 && dict_find(d, (char_u *)"pattern", -1) != NULL
16401 && dict_find(d, (char_u *)"priority", -1) != NULL
16402 && dict_find(d, (char_u *)"id", -1) != NULL))
16403 {
16404 EMSG(_(e_invarg));
16405 return;
16406 }
16407 li = li->li_next;
16408 }
16409
16410 clear_matches(curwin);
16411 li = l->lv_first;
16412 while (li != NULL)
16413 {
16414 d = li->li_tv.vval.v_dict;
16415 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16416 get_dict_string(d, (char_u *)"pattern", FALSE),
16417 (int)get_dict_number(d, (char_u *)"priority"),
16418 (int)get_dict_number(d, (char_u *)"id"));
16419 li = li->li_next;
16420 }
16421 rettv->vval.v_number = 0;
16422 }
16423#endif
16424}
16425
16426/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016427 * "setpos()" function
16428 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016429 static void
16430f_setpos(argvars, rettv)
16431 typval_T *argvars;
16432 typval_T *rettv;
16433{
16434 pos_T pos;
16435 int fnum;
16436 char_u *name;
16437
Bram Moolenaar08250432008-02-13 11:42:46 +000016438 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016439 name = get_tv_string_chk(argvars);
16440 if (name != NULL)
16441 {
16442 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16443 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016444 if (--pos.col < 0)
16445 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016446 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016447 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016448 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016449 if (fnum == curbuf->b_fnum)
16450 {
16451 curwin->w_cursor = pos;
16452 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016453 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016454 }
16455 else
16456 EMSG(_(e_invarg));
16457 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016458 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16459 {
16460 /* set mark */
16461 if (setmark_pos(name[1], &pos, fnum) == OK)
16462 rettv->vval.v_number = 0;
16463 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016464 else
16465 EMSG(_(e_invarg));
16466 }
16467 }
16468}
16469
16470/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016471 * "setqflist()" function
16472 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016473 static void
16474f_setqflist(argvars, rettv)
16475 typval_T *argvars;
16476 typval_T *rettv;
16477{
16478 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16479}
16480
16481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 * "setreg()" function
16483 */
16484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 typval_T *argvars;
16487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488{
16489 int regname;
16490 char_u *strregname;
16491 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 int append;
16494 char_u yank_type;
16495 long block_len;
16496
16497 block_len = -1;
16498 yank_type = MAUTO;
16499 append = FALSE;
16500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016501 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016504 if (strregname == NULL)
16505 return; /* type error; errmsg already given */
16506 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 if (regname == 0 || regname == '@')
16508 regname = '"';
16509 else if (regname == '=')
16510 return;
16511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016512 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016514 stropt = get_tv_string_chk(&argvars[2]);
16515 if (stropt == NULL)
16516 return; /* type error */
16517 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 switch (*stropt)
16519 {
16520 case 'a': case 'A': /* append */
16521 append = TRUE;
16522 break;
16523 case 'v': case 'c': /* character-wise selection */
16524 yank_type = MCHAR;
16525 break;
16526 case 'V': case 'l': /* line-wise selection */
16527 yank_type = MLINE;
16528 break;
16529#ifdef FEAT_VISUAL
16530 case 'b': case Ctrl_V: /* block-wise selection */
16531 yank_type = MBLOCK;
16532 if (VIM_ISDIGIT(stropt[1]))
16533 {
16534 ++stropt;
16535 block_len = getdigits(&stropt) - 1;
16536 --stropt;
16537 }
16538 break;
16539#endif
16540 }
16541 }
16542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 strval = get_tv_string_chk(&argvars[1]);
16544 if (strval != NULL)
16545 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548}
16549
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016550/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016551 * "settabvar()" function
16552 */
16553 static void
16554f_settabvar(argvars, rettv)
16555 typval_T *argvars;
16556 typval_T *rettv;
16557{
16558 tabpage_T *save_curtab;
16559 char_u *varname, *tabvarname;
16560 typval_T *varp;
16561 tabpage_T *tp;
16562
16563 rettv->vval.v_number = 0;
16564
16565 if (check_restricted() || check_secure())
16566 return;
16567
16568 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16569 varname = get_tv_string_chk(&argvars[1]);
16570 varp = &argvars[2];
16571
16572 if (tp != NULL && varname != NULL && varp != NULL)
16573 {
16574 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016575 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016576
16577 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16578 if (tabvarname != NULL)
16579 {
16580 STRCPY(tabvarname, "t:");
16581 STRCPY(tabvarname + 2, varname);
16582 set_var(tabvarname, varp, TRUE);
16583 vim_free(tabvarname);
16584 }
16585
16586 /* Restore current tabpage */
16587 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016588 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016589 }
16590}
16591
16592/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016593 * "settabwinvar()" function
16594 */
16595 static void
16596f_settabwinvar(argvars, rettv)
16597 typval_T *argvars;
16598 typval_T *rettv;
16599{
16600 setwinvar(argvars, rettv, 1);
16601}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
16603/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016604 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016607f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016608 typval_T *argvars;
16609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016611 setwinvar(argvars, rettv, 0);
16612}
16613
16614/*
16615 * "setwinvar()" and "settabwinvar()" functions
16616 */
16617 static void
16618setwinvar(argvars, rettv, off)
16619 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016620 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016621 int off;
16622{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 win_T *win;
16624#ifdef FEAT_WINDOWS
16625 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016626 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627#endif
16628 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016629 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016631 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632
16633 if (check_restricted() || check_secure())
16634 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016635
16636#ifdef FEAT_WINDOWS
16637 if (off == 1)
16638 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16639 else
16640 tp = curtab;
16641#endif
16642 win = find_win_by_nr(&argvars[off], tp);
16643 varname = get_tv_string_chk(&argvars[off + 1]);
16644 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645
16646 if (win != NULL && varname != NULL && varp != NULL)
16647 {
16648#ifdef FEAT_WINDOWS
16649 /* set curwin to be our win, temporarily */
16650 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016651 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016652 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016653 if (!win_valid(win))
16654 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 curwin = win;
16656 curbuf = curwin->w_buffer;
16657#endif
16658
16659 if (*varname == '&')
16660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016661 long numval;
16662 char_u *strval;
16663 int error = FALSE;
16664
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016666 numval = get_tv_number_chk(varp, &error);
16667 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016668 if (!error && strval != NULL)
16669 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 }
16671 else
16672 {
16673 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16674 if (winvarname != NULL)
16675 {
16676 STRCPY(winvarname, "w:");
16677 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016678 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 vim_free(winvarname);
16680 }
16681 }
16682
16683#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016684 /* Restore current tabpage and window, if still valid (autocomands can
16685 * make them invalid). */
16686 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016687 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 if (win_valid(save_curwin))
16689 {
16690 curwin = save_curwin;
16691 curbuf = curwin->w_buffer;
16692 }
16693#endif
16694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695}
16696
16697/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016698 * "shellescape({string})" function
16699 */
16700 static void
16701f_shellescape(argvars, rettv)
16702 typval_T *argvars;
16703 typval_T *rettv;
16704{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016705 rettv->vval.v_string = vim_strsave_shellescape(
16706 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016707 rettv->v_type = VAR_STRING;
16708}
16709
16710/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016711 * shiftwidth() function
16712 */
16713 static void
16714f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016715 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016716 typval_T *rettv;
16717{
16718 rettv->vval.v_number = get_sw_value();
16719}
16720
16721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016722 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 */
16724 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016725f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 typval_T *argvars;
16727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730
Bram Moolenaar0d660222005-01-07 21:51:51 +000016731 p = get_tv_string(&argvars[0]);
16732 rettv->vval.v_string = vim_strsave(p);
16733 simplify_filename(rettv->vval.v_string); /* simplify in place */
16734 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735}
16736
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016737#ifdef FEAT_FLOAT
16738/*
16739 * "sin()" function
16740 */
16741 static void
16742f_sin(argvars, rettv)
16743 typval_T *argvars;
16744 typval_T *rettv;
16745{
16746 float_T f;
16747
16748 rettv->v_type = VAR_FLOAT;
16749 if (get_float_arg(argvars, &f) == OK)
16750 rettv->vval.v_float = sin(f);
16751 else
16752 rettv->vval.v_float = 0.0;
16753}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016754
16755/*
16756 * "sinh()" function
16757 */
16758 static void
16759f_sinh(argvars, rettv)
16760 typval_T *argvars;
16761 typval_T *rettv;
16762{
16763 float_T f;
16764
16765 rettv->v_type = VAR_FLOAT;
16766 if (get_float_arg(argvars, &f) == OK)
16767 rettv->vval.v_float = sinh(f);
16768 else
16769 rettv->vval.v_float = 0.0;
16770}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016771#endif
16772
Bram Moolenaar0d660222005-01-07 21:51:51 +000016773static int
16774#ifdef __BORLANDC__
16775 _RTLENTRYF
16776#endif
16777 item_compare __ARGS((const void *s1, const void *s2));
16778static int
16779#ifdef __BORLANDC__
16780 _RTLENTRYF
16781#endif
16782 item_compare2 __ARGS((const void *s1, const void *s2));
16783
16784static int item_compare_ic;
16785static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016786static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016788#define ITEM_COMPARE_FAIL 999
16789
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016791 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016793 static int
16794#ifdef __BORLANDC__
16795_RTLENTRYF
16796#endif
16797item_compare(s1, s2)
16798 const void *s1;
16799 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 char_u *p1, *p2;
16802 char_u *tofree1, *tofree2;
16803 int res;
16804 char_u numbuf1[NUMBUFLEN];
16805 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016807 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16808 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016809 if (p1 == NULL)
16810 p1 = (char_u *)"";
16811 if (p2 == NULL)
16812 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813 if (item_compare_ic)
16814 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816 res = STRCMP(p1, p2);
16817 vim_free(tofree1);
16818 vim_free(tofree2);
16819 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820}
16821
16822 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823#ifdef __BORLANDC__
16824_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016826item_compare2(s1, s2)
16827 const void *s1;
16828 const void *s2;
16829{
16830 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016831 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016832 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016835 /* shortcut after failure in previous call; compare all items equal */
16836 if (item_compare_func_err)
16837 return 0;
16838
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016839 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16840 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16842 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843
16844 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016845 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016846 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16847 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016848 clear_tv(&argv[0]);
16849 clear_tv(&argv[1]);
16850
16851 if (res == FAIL)
16852 res = ITEM_COMPARE_FAIL;
16853 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16855 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016856 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857 clear_tv(&rettv);
16858 return res;
16859}
16860
16861/*
16862 * "sort({list})" function
16863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016866 typval_T *argvars;
16867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868{
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 list_T *l;
16870 listitem_T *li;
16871 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872 long len;
16873 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875 if (argvars[0].v_type != VAR_LIST)
16876 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 else
16878 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016880 if (l == NULL || tv_check_lock(l->lv_lock,
16881 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 return;
16883 rettv->vval.v_list = l;
16884 rettv->v_type = VAR_LIST;
16885 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 len = list_len(l);
16888 if (len <= 1)
16889 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 item_compare_ic = FALSE;
16892 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016893 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894 if (argvars[1].v_type != VAR_UNKNOWN)
16895 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016896 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016898 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899 else
16900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 int error = FALSE;
16902
16903 i = get_tv_number_chk(&argvars[1], &error);
16904 if (error)
16905 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 if (i == 1)
16907 item_compare_ic = TRUE;
16908 else
16909 item_compare_func = get_tv_string(&argvars[1]);
16910 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016911
16912 if (argvars[2].v_type != VAR_UNKNOWN)
16913 {
16914 /* optional third argument: {dict} */
16915 if (argvars[2].v_type != VAR_DICT)
16916 {
16917 EMSG(_(e_dictreq));
16918 return;
16919 }
16920 item_compare_selfdict = argvars[2].vval.v_dict;
16921 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016925 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926 if (ptrs == NULL)
16927 return;
16928 i = 0;
16929 for (li = l->lv_first; li != NULL; li = li->li_next)
16930 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016932 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 /* test the compare function */
16934 if (item_compare_func != NULL
16935 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16936 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016937 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 {
16940 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016941 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 item_compare_func == NULL ? item_compare : item_compare2);
16943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016944 if (!item_compare_func_err)
16945 {
16946 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016947 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 l->lv_len = 0;
16949 for (i = 0; i < len; ++i)
16950 list_append(l, ptrs[i]);
16951 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 }
16953
16954 vim_free(ptrs);
16955 }
16956}
16957
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016958/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016959 * "soundfold({word})" function
16960 */
16961 static void
16962f_soundfold(argvars, rettv)
16963 typval_T *argvars;
16964 typval_T *rettv;
16965{
16966 char_u *s;
16967
16968 rettv->v_type = VAR_STRING;
16969 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016970#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016971 rettv->vval.v_string = eval_soundfold(s);
16972#else
16973 rettv->vval.v_string = vim_strsave(s);
16974#endif
16975}
16976
16977/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016978 * "spellbadword()" function
16979 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016980 static void
16981f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016982 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016983 typval_T *rettv;
16984{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016985 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016986 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016987 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016989 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016990 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016991
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016992#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016993 if (argvars[0].v_type == VAR_UNKNOWN)
16994 {
16995 /* Find the start and length of the badly spelled word. */
16996 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16997 if (len != 0)
16998 word = ml_get_cursor();
16999 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017000 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017001 {
17002 char_u *str = get_tv_string_chk(&argvars[0]);
17003 int capcol = -1;
17004
17005 if (str != NULL)
17006 {
17007 /* Check the argument for spelling. */
17008 while (*str != NUL)
17009 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017010 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017011 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017012 {
17013 word = str;
17014 break;
17015 }
17016 str += len;
17017 }
17018 }
17019 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017020#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017021
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017022 list_append_string(rettv->vval.v_list, word, len);
17023 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017024 attr == HLF_SPB ? "bad" :
17025 attr == HLF_SPR ? "rare" :
17026 attr == HLF_SPL ? "local" :
17027 attr == HLF_SPC ? "caps" :
17028 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017029}
17030
17031/*
17032 * "spellsuggest()" function
17033 */
17034 static void
17035f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017036 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017037 typval_T *rettv;
17038{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017039#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017040 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017041 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017042 int maxcount;
17043 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017044 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017045 listitem_T *li;
17046 int need_capital = FALSE;
17047#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017048
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017049 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017050 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017051
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017052#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017053 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017054 {
17055 str = get_tv_string(&argvars[0]);
17056 if (argvars[1].v_type != VAR_UNKNOWN)
17057 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017058 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017059 if (maxcount <= 0)
17060 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017061 if (argvars[2].v_type != VAR_UNKNOWN)
17062 {
17063 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17064 if (typeerr)
17065 return;
17066 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017067 }
17068 else
17069 maxcount = 25;
17070
Bram Moolenaar4770d092006-01-12 23:22:24 +000017071 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017072
17073 for (i = 0; i < ga.ga_len; ++i)
17074 {
17075 str = ((char_u **)ga.ga_data)[i];
17076
17077 li = listitem_alloc();
17078 if (li == NULL)
17079 vim_free(str);
17080 else
17081 {
17082 li->li_tv.v_type = VAR_STRING;
17083 li->li_tv.v_lock = 0;
17084 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017085 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017086 }
17087 }
17088 ga_clear(&ga);
17089 }
17090#endif
17091}
17092
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017094f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 typval_T *argvars;
17096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097{
17098 char_u *str;
17099 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017100 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 regmatch_T regmatch;
17102 char_u patbuf[NUMBUFLEN];
17103 char_u *save_cpo;
17104 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017106 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017107 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108
17109 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17110 save_cpo = p_cpo;
17111 p_cpo = (char_u *)"";
17112
17113 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017114 if (argvars[1].v_type != VAR_UNKNOWN)
17115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017116 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17117 if (pat == NULL)
17118 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017119 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017121 }
17122 if (pat == NULL || *pat == NUL)
17123 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017127 if (typeerr)
17128 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17131 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017134 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017136 if (*str == NUL)
17137 match = FALSE; /* empty item at the end */
17138 else
17139 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140 if (match)
17141 end = regmatch.startp[0];
17142 else
17143 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17145 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017147 if (list_append_string(rettv->vval.v_list, str,
17148 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 }
17151 if (!match)
17152 break;
17153 /* Advance to just after the match. */
17154 if (regmatch.endp[0] > str)
17155 col = 0;
17156 else
17157 {
17158 /* Don't get stuck at the same match. */
17159#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017160 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161#else
17162 col = 1;
17163#endif
17164 }
17165 str = regmatch.endp[0];
17166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172}
17173
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017174#ifdef FEAT_FLOAT
17175/*
17176 * "sqrt()" function
17177 */
17178 static void
17179f_sqrt(argvars, rettv)
17180 typval_T *argvars;
17181 typval_T *rettv;
17182{
17183 float_T f;
17184
17185 rettv->v_type = VAR_FLOAT;
17186 if (get_float_arg(argvars, &f) == OK)
17187 rettv->vval.v_float = sqrt(f);
17188 else
17189 rettv->vval.v_float = 0.0;
17190}
17191
17192/*
17193 * "str2float()" function
17194 */
17195 static void
17196f_str2float(argvars, rettv)
17197 typval_T *argvars;
17198 typval_T *rettv;
17199{
17200 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17201
17202 if (*p == '+')
17203 p = skipwhite(p + 1);
17204 (void)string2float(p, &rettv->vval.v_float);
17205 rettv->v_type = VAR_FLOAT;
17206}
17207#endif
17208
Bram Moolenaar2c932302006-03-18 21:42:09 +000017209/*
17210 * "str2nr()" function
17211 */
17212 static void
17213f_str2nr(argvars, rettv)
17214 typval_T *argvars;
17215 typval_T *rettv;
17216{
17217 int base = 10;
17218 char_u *p;
17219 long n;
17220
17221 if (argvars[1].v_type != VAR_UNKNOWN)
17222 {
17223 base = get_tv_number(&argvars[1]);
17224 if (base != 8 && base != 10 && base != 16)
17225 {
17226 EMSG(_(e_invarg));
17227 return;
17228 }
17229 }
17230
17231 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017232 if (*p == '+')
17233 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017234 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17235 rettv->vval.v_number = n;
17236}
17237
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238#ifdef HAVE_STRFTIME
17239/*
17240 * "strftime({format}[, {time}])" function
17241 */
17242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017244 typval_T *argvars;
17245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246{
17247 char_u result_buf[256];
17248 struct tm *curtime;
17249 time_t seconds;
17250 char_u *p;
17251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017252 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017254 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 seconds = time(NULL);
17257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017258 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 curtime = localtime(&seconds);
17260 /* MSVC returns NULL for an invalid value of seconds. */
17261 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017262 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 else
17264 {
17265# ifdef FEAT_MBYTE
17266 vimconv_T conv;
17267 char_u *enc;
17268
17269 conv.vc_type = CONV_NONE;
17270 enc = enc_locale();
17271 convert_setup(&conv, p_enc, enc);
17272 if (conv.vc_type != CONV_NONE)
17273 p = string_convert(&conv, p, NULL);
17274# endif
17275 if (p != NULL)
17276 (void)strftime((char *)result_buf, sizeof(result_buf),
17277 (char *)p, curtime);
17278 else
17279 result_buf[0] = NUL;
17280
17281# ifdef FEAT_MBYTE
17282 if (conv.vc_type != CONV_NONE)
17283 vim_free(p);
17284 convert_setup(&conv, enc, p_enc);
17285 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 else
17288# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290
17291# ifdef FEAT_MBYTE
17292 /* Release conversion descriptors */
17293 convert_setup(&conv, NULL, NULL);
17294 vim_free(enc);
17295# endif
17296 }
17297}
17298#endif
17299
17300/*
17301 * "stridx()" function
17302 */
17303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017304f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 typval_T *argvars;
17306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307{
17308 char_u buf[NUMBUFLEN];
17309 char_u *needle;
17310 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017315 needle = get_tv_string_chk(&argvars[1]);
17316 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017318 if (needle == NULL || haystack == NULL)
17319 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320
Bram Moolenaar33570922005-01-25 22:26:29 +000017321 if (argvars[2].v_type != VAR_UNKNOWN)
17322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017323 int error = FALSE;
17324
17325 start_idx = get_tv_number_chk(&argvars[2], &error);
17326 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017328 if (start_idx >= 0)
17329 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 }
17331
17332 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17333 if (pos != NULL)
17334 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335}
17336
17337/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017338 * "string()" function
17339 */
17340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017342 typval_T *argvars;
17343 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017344{
17345 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017346 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017349 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017350 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017351 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017352 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353}
17354
17355/*
17356 * "strlen()" function
17357 */
17358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 typval_T *argvars;
17361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363 rettv->vval.v_number = (varnumber_T)(STRLEN(
17364 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365}
17366
17367/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017368 * "strchars()" function
17369 */
17370 static void
17371f_strchars(argvars, rettv)
17372 typval_T *argvars;
17373 typval_T *rettv;
17374{
17375 char_u *s = get_tv_string(&argvars[0]);
17376#ifdef FEAT_MBYTE
17377 varnumber_T len = 0;
17378
17379 while (*s != NUL)
17380 {
17381 mb_cptr2char_adv(&s);
17382 ++len;
17383 }
17384 rettv->vval.v_number = len;
17385#else
17386 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17387#endif
17388}
17389
17390/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017391 * "strdisplaywidth()" function
17392 */
17393 static void
17394f_strdisplaywidth(argvars, rettv)
17395 typval_T *argvars;
17396 typval_T *rettv;
17397{
17398 char_u *s = get_tv_string(&argvars[0]);
17399 int col = 0;
17400
17401 if (argvars[1].v_type != VAR_UNKNOWN)
17402 col = get_tv_number(&argvars[1]);
17403
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017404 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017405}
17406
17407/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017408 * "strwidth()" function
17409 */
17410 static void
17411f_strwidth(argvars, rettv)
17412 typval_T *argvars;
17413 typval_T *rettv;
17414{
17415 char_u *s = get_tv_string(&argvars[0]);
17416
17417 rettv->vval.v_number = (varnumber_T)(
17418#ifdef FEAT_MBYTE
17419 mb_string2cells(s, -1)
17420#else
17421 STRLEN(s)
17422#endif
17423 );
17424}
17425
17426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 * "strpart()" function
17428 */
17429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017430f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017431 typval_T *argvars;
17432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433{
17434 char_u *p;
17435 int n;
17436 int len;
17437 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017438 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017440 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 slen = (int)STRLEN(p);
17442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017443 n = get_tv_number_chk(&argvars[1], &error);
17444 if (error)
17445 len = 0;
17446 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017447 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 else
17449 len = slen - n; /* default len: all bytes that are available. */
17450
17451 /*
17452 * Only return the overlap between the specified part and the actual
17453 * string.
17454 */
17455 if (n < 0)
17456 {
17457 len += n;
17458 n = 0;
17459 }
17460 else if (n > slen)
17461 n = slen;
17462 if (len < 0)
17463 len = 0;
17464 else if (n + len > slen)
17465 len = slen - n;
17466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017467 rettv->v_type = VAR_STRING;
17468 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469}
17470
17471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017472 * "strridx()" function
17473 */
17474 static void
17475f_strridx(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 char_u buf[NUMBUFLEN];
17480 char_u *needle;
17481 char_u *haystack;
17482 char_u *rest;
17483 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017484 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 needle = get_tv_string_chk(&argvars[1]);
17487 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017488
17489 rettv->vval.v_number = -1;
17490 if (needle == NULL || haystack == NULL)
17491 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017492
17493 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017494 if (argvars[2].v_type != VAR_UNKNOWN)
17495 {
17496 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017497 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017498 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017499 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017500 }
17501 else
17502 end_idx = haystack_len;
17503
Bram Moolenaar0d660222005-01-07 21:51:51 +000017504 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017505 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017507 lastmatch = haystack + end_idx;
17508 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017509 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017510 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017511 for (rest = haystack; *rest != '\0'; ++rest)
17512 {
17513 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017514 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017515 break;
17516 lastmatch = rest;
17517 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017518 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017519
17520 if (lastmatch == NULL)
17521 rettv->vval.v_number = -1;
17522 else
17523 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17524}
17525
17526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 * "strtrans()" function
17528 */
17529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017530f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 typval_T *argvars;
17532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 rettv->v_type = VAR_STRING;
17535 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536}
17537
17538/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539 * "submatch()" function
17540 */
17541 static void
17542f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 typval_T *argvars;
17544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545{
17546 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017547 rettv->vval.v_string =
17548 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017549}
17550
17551/*
17552 * "substitute()" function
17553 */
17554 static void
17555f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 typval_T *argvars;
17557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017558{
17559 char_u patbuf[NUMBUFLEN];
17560 char_u subbuf[NUMBUFLEN];
17561 char_u flagsbuf[NUMBUFLEN];
17562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017563 char_u *str = get_tv_string_chk(&argvars[0]);
17564 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17565 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17566 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17567
Bram Moolenaar0d660222005-01-07 21:51:51 +000017568 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017569 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17570 rettv->vval.v_string = NULL;
17571 else
17572 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573}
17574
17575/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017576 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017580 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
17583 int id = 0;
17584#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017585 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 long col;
17587 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017588 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017590 lnum = get_tv_lnum(argvars); /* -1 on type error */
17591 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17592 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017594 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017595 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017596 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597#endif
17598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600}
17601
17602/*
17603 * "synIDattr(id, what [, mode])" function
17604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609{
17610 char_u *p = NULL;
17611#ifdef FEAT_SYN_HL
17612 int id;
17613 char_u *what;
17614 char_u *mode;
17615 char_u modebuf[NUMBUFLEN];
17616 int modec;
17617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618 id = get_tv_number(&argvars[0]);
17619 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017624 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 modec = 0; /* replace invalid with current */
17626 }
17627 else
17628 {
17629#ifdef FEAT_GUI
17630 if (gui.in_use)
17631 modec = 'g';
17632 else
17633#endif
17634 if (t_colors > 1)
17635 modec = 'c';
17636 else
17637 modec = 't';
17638 }
17639
17640
17641 switch (TOLOWER_ASC(what[0]))
17642 {
17643 case 'b':
17644 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17645 p = highlight_color(id, what, modec);
17646 else /* bold */
17647 p = highlight_has_attr(id, HL_BOLD, modec);
17648 break;
17649
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017650 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 p = highlight_color(id, what, modec);
17652 break;
17653
17654 case 'i':
17655 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17656 p = highlight_has_attr(id, HL_INVERSE, modec);
17657 else /* italic */
17658 p = highlight_has_attr(id, HL_ITALIC, modec);
17659 break;
17660
17661 case 'n': /* name */
17662 p = get_highlight_name(NULL, id - 1);
17663 break;
17664
17665 case 'r': /* reverse */
17666 p = highlight_has_attr(id, HL_INVERSE, modec);
17667 break;
17668
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017669 case 's':
17670 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17671 p = highlight_color(id, what, modec);
17672 else /* standout */
17673 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 break;
17675
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017676 case 'u':
17677 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17678 /* underline */
17679 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17680 else
17681 /* undercurl */
17682 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 break;
17684 }
17685
17686 if (p != NULL)
17687 p = vim_strsave(p);
17688#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689 rettv->v_type = VAR_STRING;
17690 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691}
17692
17693/*
17694 * "synIDtrans(id)" function
17695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017698 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700{
17701 int id;
17702
17703#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705
17706 if (id > 0)
17707 id = syn_get_final_id(id);
17708 else
17709#endif
17710 id = 0;
17711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017712 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713}
17714
17715/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017716 * "synconcealed(lnum, col)" function
17717 */
17718 static void
17719f_synconcealed(argvars, rettv)
17720 typval_T *argvars UNUSED;
17721 typval_T *rettv;
17722{
17723#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17724 long lnum;
17725 long col;
17726 int syntax_flags = 0;
17727 int cchar;
17728 int matchid = 0;
17729 char_u str[NUMBUFLEN];
17730#endif
17731
17732 rettv->v_type = VAR_LIST;
17733 rettv->vval.v_list = NULL;
17734
17735#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17736 lnum = get_tv_lnum(argvars); /* -1 on type error */
17737 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17738
17739 vim_memset(str, NUL, sizeof(str));
17740
17741 if (rettv_list_alloc(rettv) != FAIL)
17742 {
17743 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17744 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17745 && curwin->w_p_cole > 0)
17746 {
17747 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17748 syntax_flags = get_syntax_info(&matchid);
17749
17750 /* get the conceal character */
17751 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17752 {
17753 cchar = syn_get_sub_char();
17754 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17755 cchar = lcs_conceal;
17756 if (cchar != NUL)
17757 {
17758# ifdef FEAT_MBYTE
17759 if (has_mbyte)
17760 (*mb_char2bytes)(cchar, str);
17761 else
17762# endif
17763 str[0] = cchar;
17764 }
17765 }
17766 }
17767
17768 list_append_number(rettv->vval.v_list,
17769 (syntax_flags & HL_CONCEAL) != 0);
17770 /* -1 to auto-determine strlen */
17771 list_append_string(rettv->vval.v_list, str, -1);
17772 list_append_number(rettv->vval.v_list, matchid);
17773 }
17774#endif
17775}
17776
17777/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017778 * "synstack(lnum, col)" function
17779 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017780 static void
17781f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017782 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017783 typval_T *rettv;
17784{
17785#ifdef FEAT_SYN_HL
17786 long lnum;
17787 long col;
17788 int i;
17789 int id;
17790#endif
17791
17792 rettv->v_type = VAR_LIST;
17793 rettv->vval.v_list = NULL;
17794
17795#ifdef FEAT_SYN_HL
17796 lnum = get_tv_lnum(argvars); /* -1 on type error */
17797 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17798
17799 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017800 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017801 && rettv_list_alloc(rettv) != FAIL)
17802 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017803 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017804 for (i = 0; ; ++i)
17805 {
17806 id = syn_get_stack_item(i);
17807 if (id < 0)
17808 break;
17809 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17810 break;
17811 }
17812 }
17813#endif
17814}
17815
17816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 * "system()" function
17818 */
17819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017820f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017821 typval_T *argvars;
17822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017824 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017826 char_u *infile = NULL;
17827 char_u buf[NUMBUFLEN];
17828 int err = FALSE;
17829 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017831 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017832 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017833
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017835 {
17836 /*
17837 * Write the string to a temp file, to be used for input of the shell
17838 * command.
17839 */
17840 if ((infile = vim_tempname('i')) == NULL)
17841 {
17842 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017843 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017844 }
17845
17846 fd = mch_fopen((char *)infile, WRITEBIN);
17847 if (fd == NULL)
17848 {
17849 EMSG2(_(e_notopen), infile);
17850 goto done;
17851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017852 p = get_tv_string_buf_chk(&argvars[1], buf);
17853 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017854 {
17855 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017856 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017857 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017858 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17859 err = TRUE;
17860 if (fclose(fd) != 0)
17861 err = TRUE;
17862 if (err)
17863 {
17864 EMSG(_("E677: Error writing temp file"));
17865 goto done;
17866 }
17867 }
17868
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017869 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17870 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017871
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872#ifdef USE_CR
17873 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017874 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 {
17876 char_u *s;
17877
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017878 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 {
17880 if (*s == CAR)
17881 *s = NL;
17882 }
17883 }
17884#else
17885# ifdef USE_CRNL
17886 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017887 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 {
17889 char_u *s, *d;
17890
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017891 d = res;
17892 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 {
17894 if (s[0] == CAR && s[1] == NL)
17895 ++s;
17896 *d++ = *s;
17897 }
17898 *d = NUL;
17899 }
17900# endif
17901#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017902
17903done:
17904 if (infile != NULL)
17905 {
17906 mch_remove(infile);
17907 vim_free(infile);
17908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017909 rettv->v_type = VAR_STRING;
17910 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911}
17912
17913/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017914 * "tabpagebuflist()" function
17915 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017916 static void
17917f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017918 typval_T *argvars UNUSED;
17919 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017920{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017921#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017922 tabpage_T *tp;
17923 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017924
17925 if (argvars[0].v_type == VAR_UNKNOWN)
17926 wp = firstwin;
17927 else
17928 {
17929 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17930 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017931 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017932 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017933 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017934 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017935 for (; wp != NULL; wp = wp->w_next)
17936 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017937 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017938 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017939 }
17940#endif
17941}
17942
17943
17944/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017945 * "tabpagenr()" function
17946 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017947 static void
17948f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017949 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017950 typval_T *rettv;
17951{
17952 int nr = 1;
17953#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017954 char_u *arg;
17955
17956 if (argvars[0].v_type != VAR_UNKNOWN)
17957 {
17958 arg = get_tv_string_chk(&argvars[0]);
17959 nr = 0;
17960 if (arg != NULL)
17961 {
17962 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017963 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017964 else
17965 EMSG2(_(e_invexpr2), arg);
17966 }
17967 }
17968 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017969 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017970#endif
17971 rettv->vval.v_number = nr;
17972}
17973
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017974
17975#ifdef FEAT_WINDOWS
17976static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17977
17978/*
17979 * Common code for tabpagewinnr() and winnr().
17980 */
17981 static int
17982get_winnr(tp, argvar)
17983 tabpage_T *tp;
17984 typval_T *argvar;
17985{
17986 win_T *twin;
17987 int nr = 1;
17988 win_T *wp;
17989 char_u *arg;
17990
17991 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17992 if (argvar->v_type != VAR_UNKNOWN)
17993 {
17994 arg = get_tv_string_chk(argvar);
17995 if (arg == NULL)
17996 nr = 0; /* type error; errmsg already given */
17997 else if (STRCMP(arg, "$") == 0)
17998 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17999 else if (STRCMP(arg, "#") == 0)
18000 {
18001 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18002 if (twin == NULL)
18003 nr = 0;
18004 }
18005 else
18006 {
18007 EMSG2(_(e_invexpr2), arg);
18008 nr = 0;
18009 }
18010 }
18011
18012 if (nr > 0)
18013 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18014 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018015 {
18016 if (wp == NULL)
18017 {
18018 /* didn't find it in this tabpage */
18019 nr = 0;
18020 break;
18021 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018022 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018023 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018024 return nr;
18025}
18026#endif
18027
18028/*
18029 * "tabpagewinnr()" function
18030 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018031 static void
18032f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018033 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018034 typval_T *rettv;
18035{
18036 int nr = 1;
18037#ifdef FEAT_WINDOWS
18038 tabpage_T *tp;
18039
18040 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18041 if (tp == NULL)
18042 nr = 0;
18043 else
18044 nr = get_winnr(tp, &argvars[1]);
18045#endif
18046 rettv->vval.v_number = nr;
18047}
18048
18049
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018050/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018051 * "tagfiles()" function
18052 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018053 static void
18054f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018055 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018056 typval_T *rettv;
18057{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018058 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018059 tagname_T tn;
18060 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018062 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018063 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018064 fname = alloc(MAXPATHL);
18065 if (fname == NULL)
18066 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018068 for (first = TRUE; ; first = FALSE)
18069 if (get_tagfname(&tn, first, fname) == FAIL
18070 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018071 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018072 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018073 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018074}
18075
18076/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018077 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018078 */
18079 static void
18080f_taglist(argvars, rettv)
18081 typval_T *argvars;
18082 typval_T *rettv;
18083{
18084 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018085
18086 tag_pattern = get_tv_string(&argvars[0]);
18087
18088 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 if (*tag_pattern == NUL)
18090 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018091
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018092 if (rettv_list_alloc(rettv) == OK)
18093 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018094}
18095
18096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 * "tempname()" function
18098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018101 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103{
18104 static int x = 'A';
18105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018106 rettv->v_type = VAR_STRING;
18107 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108
18109 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18110 * names. Skip 'I' and 'O', they are used for shell redirection. */
18111 do
18112 {
18113 if (x == 'Z')
18114 x = '0';
18115 else if (x == '9')
18116 x = 'A';
18117 else
18118 {
18119#ifdef EBCDIC
18120 if (x == 'I')
18121 x = 'J';
18122 else if (x == 'R')
18123 x = 'S';
18124 else
18125#endif
18126 ++x;
18127 }
18128 } while (x == 'I' || x == 'O');
18129}
18130
18131/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018132 * "test(list)" function: Just checking the walls...
18133 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018134 static void
18135f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018136 typval_T *argvars UNUSED;
18137 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018138{
18139 /* Used for unit testing. Change the code below to your liking. */
18140#if 0
18141 listitem_T *li;
18142 list_T *l;
18143 char_u *bad, *good;
18144
18145 if (argvars[0].v_type != VAR_LIST)
18146 return;
18147 l = argvars[0].vval.v_list;
18148 if (l == NULL)
18149 return;
18150 li = l->lv_first;
18151 if (li == NULL)
18152 return;
18153 bad = get_tv_string(&li->li_tv);
18154 li = li->li_next;
18155 if (li == NULL)
18156 return;
18157 good = get_tv_string(&li->li_tv);
18158 rettv->vval.v_number = test_edit_score(bad, good);
18159#endif
18160}
18161
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018162#ifdef FEAT_FLOAT
18163/*
18164 * "tan()" function
18165 */
18166 static void
18167f_tan(argvars, rettv)
18168 typval_T *argvars;
18169 typval_T *rettv;
18170{
18171 float_T f;
18172
18173 rettv->v_type = VAR_FLOAT;
18174 if (get_float_arg(argvars, &f) == OK)
18175 rettv->vval.v_float = tan(f);
18176 else
18177 rettv->vval.v_float = 0.0;
18178}
18179
18180/*
18181 * "tanh()" function
18182 */
18183 static void
18184f_tanh(argvars, rettv)
18185 typval_T *argvars;
18186 typval_T *rettv;
18187{
18188 float_T f;
18189
18190 rettv->v_type = VAR_FLOAT;
18191 if (get_float_arg(argvars, &f) == OK)
18192 rettv->vval.v_float = tanh(f);
18193 else
18194 rettv->vval.v_float = 0.0;
18195}
18196#endif
18197
Bram Moolenaard52d9742005-08-21 22:20:28 +000018198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 * "tolower(string)" function
18200 */
18201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018203 typval_T *argvars;
18204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205{
18206 char_u *p;
18207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018208 p = vim_strsave(get_tv_string(&argvars[0]));
18209 rettv->v_type = VAR_STRING;
18210 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211
18212 if (p != NULL)
18213 while (*p != NUL)
18214 {
18215#ifdef FEAT_MBYTE
18216 int l;
18217
18218 if (enc_utf8)
18219 {
18220 int c, lc;
18221
18222 c = utf_ptr2char(p);
18223 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018224 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 /* TODO: reallocate string when byte count changes. */
18226 if (utf_char2len(lc) == l)
18227 utf_char2bytes(lc, p);
18228 p += l;
18229 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018230 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 p += l; /* skip multi-byte character */
18232 else
18233#endif
18234 {
18235 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18236 ++p;
18237 }
18238 }
18239}
18240
18241/*
18242 * "toupper(string)" function
18243 */
18244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018245f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018246 typval_T *argvars;
18247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018249 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251}
18252
18253/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018254 * "tr(string, fromstr, tostr)" function
18255 */
18256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018258 typval_T *argvars;
18259 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018260{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018261 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018262 char_u *fromstr;
18263 char_u *tostr;
18264 char_u *p;
18265#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018266 int inlen;
18267 int fromlen;
18268 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018269 int idx;
18270 char_u *cpstr;
18271 int cplen;
18272 int first = TRUE;
18273#endif
18274 char_u buf[NUMBUFLEN];
18275 char_u buf2[NUMBUFLEN];
18276 garray_T ga;
18277
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018278 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018279 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18280 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018281
18282 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283 rettv->v_type = VAR_STRING;
18284 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018285 if (fromstr == NULL || tostr == NULL)
18286 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018287 ga_init2(&ga, (int)sizeof(char), 80);
18288
18289#ifdef FEAT_MBYTE
18290 if (!has_mbyte)
18291#endif
18292 /* not multi-byte: fromstr and tostr must be the same length */
18293 if (STRLEN(fromstr) != STRLEN(tostr))
18294 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018295#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018296error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018297#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018298 EMSG2(_(e_invarg2), fromstr);
18299 ga_clear(&ga);
18300 return;
18301 }
18302
18303 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018304 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018305 {
18306#ifdef FEAT_MBYTE
18307 if (has_mbyte)
18308 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018309 inlen = (*mb_ptr2len)(in_str);
18310 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018311 cplen = inlen;
18312 idx = 0;
18313 for (p = fromstr; *p != NUL; p += fromlen)
18314 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018315 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018316 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018317 {
18318 for (p = tostr; *p != NUL; p += tolen)
18319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018320 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018321 if (idx-- == 0)
18322 {
18323 cplen = tolen;
18324 cpstr = p;
18325 break;
18326 }
18327 }
18328 if (*p == NUL) /* tostr is shorter than fromstr */
18329 goto error;
18330 break;
18331 }
18332 ++idx;
18333 }
18334
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018335 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018336 {
18337 /* Check that fromstr and tostr have the same number of
18338 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018339 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018340 first = FALSE;
18341 for (p = tostr; *p != NUL; p += tolen)
18342 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018343 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018344 --idx;
18345 }
18346 if (idx != 0)
18347 goto error;
18348 }
18349
18350 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018351 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018352 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018353
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018354 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018355 }
18356 else
18357#endif
18358 {
18359 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018360 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018361 if (p != NULL)
18362 ga_append(&ga, tostr[p - fromstr]);
18363 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018364 ga_append(&ga, *in_str);
18365 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018366 }
18367 }
18368
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018369 /* add a terminating NUL */
18370 ga_grow(&ga, 1);
18371 ga_append(&ga, NUL);
18372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018373 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018374}
18375
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018376#ifdef FEAT_FLOAT
18377/*
18378 * "trunc({float})" function
18379 */
18380 static void
18381f_trunc(argvars, rettv)
18382 typval_T *argvars;
18383 typval_T *rettv;
18384{
18385 float_T f;
18386
18387 rettv->v_type = VAR_FLOAT;
18388 if (get_float_arg(argvars, &f) == OK)
18389 /* trunc() is not in C90, use floor() or ceil() instead. */
18390 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18391 else
18392 rettv->vval.v_float = 0.0;
18393}
18394#endif
18395
Bram Moolenaar8299df92004-07-10 09:47:34 +000018396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 * "type(expr)" function
18398 */
18399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018400f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018401 typval_T *argvars;
18402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018404 int n;
18405
18406 switch (argvars[0].v_type)
18407 {
18408 case VAR_NUMBER: n = 0; break;
18409 case VAR_STRING: n = 1; break;
18410 case VAR_FUNC: n = 2; break;
18411 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018412 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018413#ifdef FEAT_FLOAT
18414 case VAR_FLOAT: n = 5; break;
18415#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018416 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18417 }
18418 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419}
18420
18421/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018422 * "undofile(name)" function
18423 */
18424 static void
18425f_undofile(argvars, rettv)
18426 typval_T *argvars;
18427 typval_T *rettv;
18428{
18429 rettv->v_type = VAR_STRING;
18430#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018431 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018432 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018433
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018434 if (*fname == NUL)
18435 {
18436 /* If there is no file name there will be no undo file. */
18437 rettv->vval.v_string = NULL;
18438 }
18439 else
18440 {
18441 char_u *ffname = FullName_save(fname, FALSE);
18442
18443 if (ffname != NULL)
18444 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18445 vim_free(ffname);
18446 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018447 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018448#else
18449 rettv->vval.v_string = NULL;
18450#endif
18451}
18452
18453/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018454 * "undotree()" function
18455 */
18456 static void
18457f_undotree(argvars, rettv)
18458 typval_T *argvars UNUSED;
18459 typval_T *rettv;
18460{
18461 if (rettv_dict_alloc(rettv) == OK)
18462 {
18463 dict_T *dict = rettv->vval.v_dict;
18464 list_T *list;
18465
Bram Moolenaar730cde92010-06-27 05:18:54 +020018466 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018467 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018468 dict_add_nr_str(dict, "save_last",
18469 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018470 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18471 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018472 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018473
18474 list = list_alloc();
18475 if (list != NULL)
18476 {
18477 u_eval_tree(curbuf->b_u_oldhead, list);
18478 dict_add_list(dict, "entries", list);
18479 }
18480 }
18481}
18482
18483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018484 * "values(dict)" function
18485 */
18486 static void
18487f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018488 typval_T *argvars;
18489 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018490{
18491 dict_list(argvars, rettv, 1);
18492}
18493
18494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 * "virtcol(string)" function
18496 */
18497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018498f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018499 typval_T *argvars;
18500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501{
18502 colnr_T vcol = 0;
18503 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018504 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018506 fp = var2fpos(&argvars[0], FALSE, &fnum);
18507 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18508 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 {
18510 getvvcol(curwin, fp, NULL, NULL, &vcol);
18511 ++vcol;
18512 }
18513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018514 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515}
18516
18517/*
18518 * "visualmode()" function
18519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018522 typval_T *argvars UNUSED;
18523 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524{
18525#ifdef FEAT_VISUAL
18526 char_u str[2];
18527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 str[0] = curbuf->b_visual_mode_eval;
18530 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018531 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532
18533 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018534 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536#endif
18537}
18538
18539/*
18540 * "winbufnr(nr)" function
18541 */
18542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018543f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T *argvars;
18545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546{
18547 win_T *wp;
18548
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018549 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554}
18555
18556/*
18557 * "wincol()" function
18558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563{
18564 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018565 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566}
18567
18568/*
18569 * "winheight(nr)" function
18570 */
18571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018573 typval_T *argvars;
18574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575{
18576 win_T *wp;
18577
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018578 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018580 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583}
18584
18585/*
18586 * "winline()" function
18587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592{
18593 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595}
18596
18597/*
18598 * "winnr()" function
18599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018601f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018602 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604{
18605 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018606
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018608 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611}
18612
18613/*
18614 * "winrestcmd()" function
18615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620{
18621#ifdef FEAT_WINDOWS
18622 win_T *wp;
18623 int winnr = 1;
18624 garray_T ga;
18625 char_u buf[50];
18626
18627 ga_init2(&ga, (int)sizeof(char), 70);
18628 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18629 {
18630 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18631 ga_concat(&ga, buf);
18632# ifdef FEAT_VERTSPLIT
18633 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18634 ga_concat(&ga, buf);
18635# endif
18636 ++winnr;
18637 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018638 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645}
18646
18647/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018648 * "winrestview()" function
18649 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018650 static void
18651f_winrestview(argvars, rettv)
18652 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018653 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018654{
18655 dict_T *dict;
18656
18657 if (argvars[0].v_type != VAR_DICT
18658 || (dict = argvars[0].vval.v_dict) == NULL)
18659 EMSG(_(e_invarg));
18660 else
18661 {
18662 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18663 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18664#ifdef FEAT_VIRTUALEDIT
18665 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18666#endif
18667 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018668 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018669
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018670 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018671#ifdef FEAT_DIFF
18672 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18673#endif
18674 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18675 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18676
18677 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018678 win_new_height(curwin, curwin->w_height);
18679# ifdef FEAT_VERTSPLIT
18680 win_new_width(curwin, W_WIDTH(curwin));
18681# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018682 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018683
18684 if (curwin->w_topline == 0)
18685 curwin->w_topline = 1;
18686 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18687 curwin->w_topline = curbuf->b_ml.ml_line_count;
18688#ifdef FEAT_DIFF
18689 check_topfill(curwin, TRUE);
18690#endif
18691 }
18692}
18693
18694/*
18695 * "winsaveview()" function
18696 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018697 static void
18698f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018699 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018700 typval_T *rettv;
18701{
18702 dict_T *dict;
18703
Bram Moolenaara800b422010-06-27 01:15:55 +020018704 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018705 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018706 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018707
18708 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18709 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18710#ifdef FEAT_VIRTUALEDIT
18711 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18712#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018713 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018714 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18715
18716 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18717#ifdef FEAT_DIFF
18718 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18719#endif
18720 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18721 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18722}
18723
18724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 * "winwidth(nr)" function
18726 */
18727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018729 typval_T *argvars;
18730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731{
18732 win_T *wp;
18733
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018734 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 else
18738#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742#endif
18743}
18744
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018746 * "writefile()" function
18747 */
18748 static void
18749f_writefile(argvars, rettv)
18750 typval_T *argvars;
18751 typval_T *rettv;
18752{
18753 int binary = FALSE;
18754 char_u *fname;
18755 FILE *fd;
18756 listitem_T *li;
18757 char_u *s;
18758 int ret = 0;
18759 int c;
18760
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018761 if (check_restricted() || check_secure())
18762 return;
18763
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018764 if (argvars[0].v_type != VAR_LIST)
18765 {
18766 EMSG2(_(e_listarg), "writefile()");
18767 return;
18768 }
18769 if (argvars[0].vval.v_list == NULL)
18770 return;
18771
18772 if (argvars[2].v_type != VAR_UNKNOWN
18773 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18774 binary = TRUE;
18775
18776 /* Always open the file in binary mode, library functions have a mind of
18777 * their own about CR-LF conversion. */
18778 fname = get_tv_string(&argvars[1]);
18779 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18780 {
18781 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18782 ret = -1;
18783 }
18784 else
18785 {
18786 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18787 li = li->li_next)
18788 {
18789 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18790 {
18791 if (*s == '\n')
18792 c = putc(NUL, fd);
18793 else
18794 c = putc(*s, fd);
18795 if (c == EOF)
18796 {
18797 ret = -1;
18798 break;
18799 }
18800 }
18801 if (!binary || li->li_next != NULL)
18802 if (putc('\n', fd) == EOF)
18803 {
18804 ret = -1;
18805 break;
18806 }
18807 if (ret < 0)
18808 {
18809 EMSG(_(e_write));
18810 break;
18811 }
18812 }
18813 fclose(fd);
18814 }
18815
18816 rettv->vval.v_number = ret;
18817}
18818
18819/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018820 * "xor(expr, expr)" function
18821 */
18822 static void
18823f_xor(argvars, rettv)
18824 typval_T *argvars;
18825 typval_T *rettv;
18826{
18827 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18828 ^ get_tv_number_chk(&argvars[1], NULL);
18829}
18830
18831
18832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018834 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 */
18836 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018837var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018839 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018840 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018842 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018844 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845
Bram Moolenaara5525202006-03-02 22:52:09 +000018846 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018847 if (varp->v_type == VAR_LIST)
18848 {
18849 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018850 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018851 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018852 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018853
18854 l = varp->vval.v_list;
18855 if (l == NULL)
18856 return NULL;
18857
18858 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018859 pos.lnum = list_find_nr(l, 0L, &error);
18860 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018861 return NULL; /* invalid line number */
18862
18863 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018864 pos.col = list_find_nr(l, 1L, &error);
18865 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018866 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018867 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018868
18869 /* We accept "$" for the column number: last column. */
18870 li = list_find(l, 1L);
18871 if (li != NULL && li->li_tv.v_type == VAR_STRING
18872 && li->li_tv.vval.v_string != NULL
18873 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18874 pos.col = len + 1;
18875
Bram Moolenaara5525202006-03-02 22:52:09 +000018876 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018877 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018878 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018879 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018880
Bram Moolenaara5525202006-03-02 22:52:09 +000018881#ifdef FEAT_VIRTUALEDIT
18882 /* Get the virtual offset. Defaults to zero. */
18883 pos.coladd = list_find_nr(l, 2L, &error);
18884 if (error)
18885 pos.coladd = 0;
18886#endif
18887
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018888 return &pos;
18889 }
18890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018891 name = get_tv_string_chk(varp);
18892 if (name == NULL)
18893 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018894 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018896#ifdef FEAT_VISUAL
18897 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18898 {
18899 if (VIsual_active)
18900 return &VIsual;
18901 return &curwin->w_cursor;
18902 }
18903#endif
18904 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018906 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18908 return NULL;
18909 return pp;
18910 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018911
18912#ifdef FEAT_VIRTUALEDIT
18913 pos.coladd = 0;
18914#endif
18915
Bram Moolenaar477933c2007-07-17 14:32:23 +000018916 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018917 {
18918 pos.col = 0;
18919 if (name[1] == '0') /* "w0": first visible line */
18920 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018921 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018922 pos.lnum = curwin->w_topline;
18923 return &pos;
18924 }
18925 else if (name[1] == '$') /* "w$": last visible line */
18926 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018927 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018928 pos.lnum = curwin->w_botline - 1;
18929 return &pos;
18930 }
18931 }
18932 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018934 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 {
18936 pos.lnum = curbuf->b_ml.ml_line_count;
18937 pos.col = 0;
18938 }
18939 else
18940 {
18941 pos.lnum = curwin->w_cursor.lnum;
18942 pos.col = (colnr_T)STRLEN(ml_get_curline());
18943 }
18944 return &pos;
18945 }
18946 return NULL;
18947}
18948
18949/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018950 * Convert list in "arg" into a position and optional file number.
18951 * When "fnump" is NULL there is no file number, only 3 items.
18952 * Note that the column is passed on as-is, the caller may want to decrement
18953 * it to use 1 for the first column.
18954 * Return FAIL when conversion is not possible, doesn't check the position for
18955 * validity.
18956 */
18957 static int
18958list2fpos(arg, posp, fnump)
18959 typval_T *arg;
18960 pos_T *posp;
18961 int *fnump;
18962{
18963 list_T *l = arg->vval.v_list;
18964 long i = 0;
18965 long n;
18966
Bram Moolenaarbde35262006-07-23 20:12:24 +000018967 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18968 * when "fnump" isn't NULL and "coladd" is optional. */
18969 if (arg->v_type != VAR_LIST
18970 || l == NULL
18971 || l->lv_len < (fnump == NULL ? 2 : 3)
18972 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018973 return FAIL;
18974
18975 if (fnump != NULL)
18976 {
18977 n = list_find_nr(l, i++, NULL); /* fnum */
18978 if (n < 0)
18979 return FAIL;
18980 if (n == 0)
18981 n = curbuf->b_fnum; /* current buffer */
18982 *fnump = n;
18983 }
18984
18985 n = list_find_nr(l, i++, NULL); /* lnum */
18986 if (n < 0)
18987 return FAIL;
18988 posp->lnum = n;
18989
18990 n = list_find_nr(l, i++, NULL); /* col */
18991 if (n < 0)
18992 return FAIL;
18993 posp->col = n;
18994
18995#ifdef FEAT_VIRTUALEDIT
18996 n = list_find_nr(l, i, NULL);
18997 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018998 posp->coladd = 0;
18999 else
19000 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019001#endif
19002
19003 return OK;
19004}
19005
19006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 * Get the length of an environment variable name.
19008 * Advance "arg" to the first character after the name.
19009 * Return 0 for error.
19010 */
19011 static int
19012get_env_len(arg)
19013 char_u **arg;
19014{
19015 char_u *p;
19016 int len;
19017
19018 for (p = *arg; vim_isIDc(*p); ++p)
19019 ;
19020 if (p == *arg) /* no name found */
19021 return 0;
19022
19023 len = (int)(p - *arg);
19024 *arg = p;
19025 return len;
19026}
19027
19028/*
19029 * Get the length of the name of a function or internal variable.
19030 * "arg" is advanced to the first non-white character after the name.
19031 * Return 0 if something is wrong.
19032 */
19033 static int
19034get_id_len(arg)
19035 char_u **arg;
19036{
19037 char_u *p;
19038 int len;
19039
19040 /* Find the end of the name. */
19041 for (p = *arg; eval_isnamec(*p); ++p)
19042 ;
19043 if (p == *arg) /* no name found */
19044 return 0;
19045
19046 len = (int)(p - *arg);
19047 *arg = skipwhite(p);
19048
19049 return len;
19050}
19051
19052/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019053 * Get the length of the name of a variable or function.
19054 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019056 * Return -1 if curly braces expansion failed.
19057 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 * If the name contains 'magic' {}'s, expand them and return the
19059 * expanded name in an allocated string via 'alias' - caller must free.
19060 */
19061 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019062get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 char_u **arg;
19064 char_u **alias;
19065 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019066 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067{
19068 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 char_u *p;
19070 char_u *expr_start;
19071 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072
19073 *alias = NULL; /* default to no alias */
19074
19075 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19076 && (*arg)[2] == (int)KE_SNR)
19077 {
19078 /* hard coded <SNR>, already translated */
19079 *arg += 3;
19080 return get_id_len(arg) + 3;
19081 }
19082 len = eval_fname_script(*arg);
19083 if (len > 0)
19084 {
19085 /* literal "<SID>", "s:" or "<SNR>" */
19086 *arg += len;
19087 }
19088
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019090 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019092 p = find_name_end(*arg, &expr_start, &expr_end,
19093 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 if (expr_start != NULL)
19095 {
19096 char_u *temp_string;
19097
19098 if (!evaluate)
19099 {
19100 len += (int)(p - *arg);
19101 *arg = skipwhite(p);
19102 return len;
19103 }
19104
19105 /*
19106 * Include any <SID> etc in the expanded string:
19107 * Thus the -len here.
19108 */
19109 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19110 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019111 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 *alias = temp_string;
19113 *arg = skipwhite(p);
19114 return (int)STRLEN(temp_string);
19115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116
19117 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019118 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 EMSG2(_(e_invexpr2), *arg);
19120
19121 return len;
19122}
19123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124/*
19125 * Find the end of a variable or function name, taking care of magic braces.
19126 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19127 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019128 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129 * Return a pointer to just after the name. Equal to "arg" if there is no
19130 * valid name.
19131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019133find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 char_u *arg;
19135 char_u **expr_start;
19136 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019137 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 int mb_nest = 0;
19140 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 char_u *p;
19142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019145 *expr_start = NULL;
19146 *expr_end = NULL;
19147 }
19148
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019149 /* Quick check for valid starting character. */
19150 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19151 return arg;
19152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153 for (p = arg; *p != NUL
19154 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019155 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019156 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019157 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019158 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019160 if (*p == '\'')
19161 {
19162 /* skip over 'string' to avoid counting [ and ] inside it. */
19163 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19164 ;
19165 if (*p == NUL)
19166 break;
19167 }
19168 else if (*p == '"')
19169 {
19170 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19171 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19172 if (*p == '\\' && p[1] != NUL)
19173 ++p;
19174 if (*p == NUL)
19175 break;
19176 }
19177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180 if (*p == '[')
19181 ++br_nest;
19182 else if (*p == ']')
19183 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019188 if (*p == '{')
19189 {
19190 mb_nest++;
19191 if (expr_start != NULL && *expr_start == NULL)
19192 *expr_start = p;
19193 }
19194 else if (*p == '}')
19195 {
19196 mb_nest--;
19197 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19198 *expr_end = p;
19199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 }
19202
19203 return p;
19204}
19205
19206/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019207 * Expands out the 'magic' {}'s in a variable/function name.
19208 * Note that this can call itself recursively, to deal with
19209 * constructs like foo{bar}{baz}{bam}
19210 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19211 * "in_start" ^
19212 * "expr_start" ^
19213 * "expr_end" ^
19214 * "in_end" ^
19215 *
19216 * Returns a new allocated string, which the caller must free.
19217 * Returns NULL for failure.
19218 */
19219 static char_u *
19220make_expanded_name(in_start, expr_start, expr_end, in_end)
19221 char_u *in_start;
19222 char_u *expr_start;
19223 char_u *expr_end;
19224 char_u *in_end;
19225{
19226 char_u c1;
19227 char_u *retval = NULL;
19228 char_u *temp_result;
19229 char_u *nextcmd = NULL;
19230
19231 if (expr_end == NULL || in_end == NULL)
19232 return NULL;
19233 *expr_start = NUL;
19234 *expr_end = NUL;
19235 c1 = *in_end;
19236 *in_end = NUL;
19237
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019238 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019239 if (temp_result != NULL && nextcmd == NULL)
19240 {
19241 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19242 + (in_end - expr_end) + 1));
19243 if (retval != NULL)
19244 {
19245 STRCPY(retval, in_start);
19246 STRCAT(retval, temp_result);
19247 STRCAT(retval, expr_end + 1);
19248 }
19249 }
19250 vim_free(temp_result);
19251
19252 *in_end = c1; /* put char back for error messages */
19253 *expr_start = '{';
19254 *expr_end = '}';
19255
19256 if (retval != NULL)
19257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019258 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019259 if (expr_start != NULL)
19260 {
19261 /* Further expansion! */
19262 temp_result = make_expanded_name(retval, expr_start,
19263 expr_end, temp_result);
19264 vim_free(retval);
19265 retval = temp_result;
19266 }
19267 }
19268
19269 return retval;
19270}
19271
19272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019274 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 */
19276 static int
19277eval_isnamec(c)
19278 int c;
19279{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019280 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19281}
19282
19283/*
19284 * Return TRUE if character "c" can be used as the first character in a
19285 * variable or function name (excluding '{' and '}').
19286 */
19287 static int
19288eval_isnamec1(c)
19289 int c;
19290{
19291 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292}
19293
19294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 * Set number v: variable to "val".
19296 */
19297 void
19298set_vim_var_nr(idx, val)
19299 int idx;
19300 long val;
19301{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019302 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303}
19304
19305/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019306 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 */
19308 long
19309get_vim_var_nr(idx)
19310 int idx;
19311{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019312 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313}
19314
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019315/*
19316 * Get string v: variable value. Uses a static buffer, can only be used once.
19317 */
19318 char_u *
19319get_vim_var_str(idx)
19320 int idx;
19321{
19322 return get_tv_string(&vimvars[idx].vv_tv);
19323}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019324
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019326 * Get List v: variable value. Caller must take care of reference count when
19327 * needed.
19328 */
19329 list_T *
19330get_vim_var_list(idx)
19331 int idx;
19332{
19333 return vimvars[idx].vv_list;
19334}
19335
19336/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019337 * Set v:char to character "c".
19338 */
19339 void
19340set_vim_var_char(c)
19341 int c;
19342{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019343 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019344
19345#ifdef FEAT_MBYTE
19346 if (has_mbyte)
19347 buf[(*mb_char2bytes)(c, buf)] = NUL;
19348 else
19349#endif
19350 {
19351 buf[0] = c;
19352 buf[1] = NUL;
19353 }
19354 set_vim_var_string(VV_CHAR, buf, -1);
19355}
19356
19357/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019358 * Set v:count to "count" and v:count1 to "count1".
19359 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 */
19361 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019362set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 long count;
19364 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019365 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019367 if (set_prevcount)
19368 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019369 vimvars[VV_COUNT].vv_nr = count;
19370 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371}
19372
19373/*
19374 * Set string v: variable to a copy of "val".
19375 */
19376 void
19377set_vim_var_string(idx, val, len)
19378 int idx;
19379 char_u *val;
19380 int len; /* length of "val" to use or -1 (whole string) */
19381{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019382 /* Need to do this (at least) once, since we can't initialize a union.
19383 * Will always be invoked when "v:progname" is set. */
19384 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19385
Bram Moolenaare9a41262005-01-15 22:18:47 +000019386 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019388 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019390 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019392 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393}
19394
19395/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019396 * Set List v: variable to "val".
19397 */
19398 void
19399set_vim_var_list(idx, val)
19400 int idx;
19401 list_T *val;
19402{
19403 list_unref(vimvars[idx].vv_list);
19404 vimvars[idx].vv_list = val;
19405 if (val != NULL)
19406 ++val->lv_refcount;
19407}
19408
19409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 * Set v:register if needed.
19411 */
19412 void
19413set_reg_var(c)
19414 int c;
19415{
19416 char_u regname;
19417
19418 if (c == 0 || c == ' ')
19419 regname = '"';
19420 else
19421 regname = c;
19422 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019423 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 set_vim_var_string(VV_REG, &regname, 1);
19425}
19426
19427/*
19428 * Get or set v:exception. If "oldval" == NULL, return the current value.
19429 * Otherwise, restore the value to "oldval" and return NULL.
19430 * Must always be called in pairs to save and restore v:exception! Does not
19431 * take care of memory allocations.
19432 */
19433 char_u *
19434v_exception(oldval)
19435 char_u *oldval;
19436{
19437 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019438 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
Bram Moolenaare9a41262005-01-15 22:18:47 +000019440 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 return NULL;
19442}
19443
19444/*
19445 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19446 * Otherwise, restore the value to "oldval" and return NULL.
19447 * Must always be called in pairs to save and restore v:throwpoint! Does not
19448 * take care of memory allocations.
19449 */
19450 char_u *
19451v_throwpoint(oldval)
19452 char_u *oldval;
19453{
19454 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019455 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456
Bram Moolenaare9a41262005-01-15 22:18:47 +000019457 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 return NULL;
19459}
19460
19461#if defined(FEAT_AUTOCMD) || defined(PROTO)
19462/*
19463 * Set v:cmdarg.
19464 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19465 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19466 * Must always be called in pairs!
19467 */
19468 char_u *
19469set_cmdarg(eap, oldarg)
19470 exarg_T *eap;
19471 char_u *oldarg;
19472{
19473 char_u *oldval;
19474 char_u *newval;
19475 unsigned len;
19476
Bram Moolenaare9a41262005-01-15 22:18:47 +000019477 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019478 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019480 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019481 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019482 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 }
19484
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019485 if (eap->force_bin == FORCE_BIN)
19486 len = 6;
19487 else if (eap->force_bin == FORCE_NOBIN)
19488 len = 8;
19489 else
19490 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019491
19492 if (eap->read_edit)
19493 len += 7;
19494
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019495 if (eap->force_ff != 0)
19496 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19497# ifdef FEAT_MBYTE
19498 if (eap->force_enc != 0)
19499 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019500 if (eap->bad_char != 0)
19501 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019502# endif
19503
19504 newval = alloc(len + 1);
19505 if (newval == NULL)
19506 return NULL;
19507
19508 if (eap->force_bin == FORCE_BIN)
19509 sprintf((char *)newval, " ++bin");
19510 else if (eap->force_bin == FORCE_NOBIN)
19511 sprintf((char *)newval, " ++nobin");
19512 else
19513 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019514
19515 if (eap->read_edit)
19516 STRCAT(newval, " ++edit");
19517
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019518 if (eap->force_ff != 0)
19519 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19520 eap->cmd + eap->force_ff);
19521# ifdef FEAT_MBYTE
19522 if (eap->force_enc != 0)
19523 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19524 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019525 if (eap->bad_char == BAD_KEEP)
19526 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19527 else if (eap->bad_char == BAD_DROP)
19528 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19529 else if (eap->bad_char != 0)
19530 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019531# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019532 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019533 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534}
19535#endif
19536
19537/*
19538 * Get the value of internal variable "name".
19539 * Return OK or FAIL.
19540 */
19541 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019542get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 char_u *name;
19544 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019545 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019546 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547{
19548 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 typval_T *tv = NULL;
19550 typval_T atv;
19551 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553
19554 /* truncate the name, so that we can use strcmp() */
19555 cc = name[len];
19556 name[len] = NUL;
19557
19558 /*
19559 * Check for "b:changedtick".
19560 */
19561 if (STRCMP(name, "b:changedtick") == 0)
19562 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019563 atv.v_type = VAR_NUMBER;
19564 atv.vval.v_number = curbuf->b_changedtick;
19565 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 }
19567
19568 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 * Check for user-defined variables.
19570 */
19571 else
19572 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019573 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019575 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 }
19577
Bram Moolenaare9a41262005-01-15 22:18:47 +000019578 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019580 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 ret = FAIL;
19583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019585 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586
19587 name[len] = cc;
19588
19589 return ret;
19590}
19591
19592/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019593 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19594 * Also handle function call with Funcref variable: func(expr)
19595 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19596 */
19597 static int
19598handle_subscript(arg, rettv, evaluate, verbose)
19599 char_u **arg;
19600 typval_T *rettv;
19601 int evaluate; /* do more than finding the end */
19602 int verbose; /* give error messages */
19603{
19604 int ret = OK;
19605 dict_T *selfdict = NULL;
19606 char_u *s;
19607 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019608 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019609
19610 while (ret == OK
19611 && (**arg == '['
19612 || (**arg == '.' && rettv->v_type == VAR_DICT)
19613 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19614 && !vim_iswhite(*(*arg - 1)))
19615 {
19616 if (**arg == '(')
19617 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019618 /* need to copy the funcref so that we can clear rettv */
19619 functv = *rettv;
19620 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019621
19622 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019623 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019624 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019625 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19626 &len, evaluate, selfdict);
19627
19628 /* Clear the funcref afterwards, so that deleting it while
19629 * evaluating the arguments is possible (see test55). */
19630 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019631
19632 /* Stop the expression evaluation when immediately aborting on
19633 * error, or when an interrupt occurred or an exception was thrown
19634 * but not caught. */
19635 if (aborting())
19636 {
19637 if (ret == OK)
19638 clear_tv(rettv);
19639 ret = FAIL;
19640 }
19641 dict_unref(selfdict);
19642 selfdict = NULL;
19643 }
19644 else /* **arg == '[' || **arg == '.' */
19645 {
19646 dict_unref(selfdict);
19647 if (rettv->v_type == VAR_DICT)
19648 {
19649 selfdict = rettv->vval.v_dict;
19650 if (selfdict != NULL)
19651 ++selfdict->dv_refcount;
19652 }
19653 else
19654 selfdict = NULL;
19655 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19656 {
19657 clear_tv(rettv);
19658 ret = FAIL;
19659 }
19660 }
19661 }
19662 dict_unref(selfdict);
19663 return ret;
19664}
19665
19666/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019667 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668 * value).
19669 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019672{
Bram Moolenaar33570922005-01-25 22:26:29 +000019673 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019674}
19675
19676/*
19677 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 * The string "s" must have been allocated, it is consumed.
19679 * Return NULL for out of memory, the variable otherwise.
19680 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019681 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019682alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 char_u *s;
19684{
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019687 rettv = alloc_tv();
19688 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019690 rettv->v_type = VAR_STRING;
19691 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 }
19693 else
19694 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696}
19697
19698/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019699 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019701 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704{
19705 if (varp != NULL)
19706 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019707 switch (varp->v_type)
19708 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019710 func_unref(varp->vval.v_string);
19711 /*FALLTHROUGH*/
19712 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019713 vim_free(varp->vval.v_string);
19714 break;
19715 case VAR_LIST:
19716 list_unref(varp->vval.v_list);
19717 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019718 case VAR_DICT:
19719 dict_unref(varp->vval.v_dict);
19720 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019721 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019722#ifdef FEAT_FLOAT
19723 case VAR_FLOAT:
19724#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019725 case VAR_UNKNOWN:
19726 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019727 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019728 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019729 break;
19730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 vim_free(varp);
19732 }
19733}
19734
19735/*
19736 * Free the memory for a variable value and set the value to NULL or 0.
19737 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019738 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019740 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741{
19742 if (varp != NULL)
19743 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019747 func_unref(varp->vval.v_string);
19748 /*FALLTHROUGH*/
19749 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750 vim_free(varp->vval.v_string);
19751 varp->vval.v_string = NULL;
19752 break;
19753 case VAR_LIST:
19754 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019755 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019756 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019757 case VAR_DICT:
19758 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019759 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019760 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019762 varp->vval.v_number = 0;
19763 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019764#ifdef FEAT_FLOAT
19765 case VAR_FLOAT:
19766 varp->vval.v_float = 0.0;
19767 break;
19768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 case VAR_UNKNOWN:
19770 break;
19771 default:
19772 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019774 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 }
19776}
19777
19778/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 * Set the value of a variable to NULL without freeing items.
19780 */
19781 static void
19782init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784{
19785 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019786 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019787}
19788
19789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 * Get the number value of a variable.
19791 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019792 * For incompatible types, return 0.
19793 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19794 * caller of incompatible types: it sets *denote to TRUE if "denote"
19795 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 */
19797 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019798get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019801 int error = FALSE;
19802
19803 return get_tv_number_chk(varp, &error); /* return 0L on error */
19804}
19805
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019806 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019807get_tv_number_chk(varp, denote)
19808 typval_T *varp;
19809 int *denote;
19810{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019811 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019813 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019815 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019816 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019817#ifdef FEAT_FLOAT
19818 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019819 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019820 break;
19821#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019822 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019823 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824 break;
19825 case VAR_STRING:
19826 if (varp->vval.v_string != NULL)
19827 vim_str2nr(varp->vval.v_string, NULL, NULL,
19828 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019829 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019830 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019831 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019832 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019833 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019834 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019835 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019836 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019837 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019840 if (denote == NULL) /* useful for values that must be unsigned */
19841 n = -1;
19842 else
19843 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019844 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845}
19846
19847/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019848 * Get the lnum from the first argument.
19849 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019850 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 */
19852 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019854 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855{
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 linenr_T lnum;
19858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019859 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 if (lnum == 0) /* no valid number, try using line() */
19861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862 rettv.v_type = VAR_NUMBER;
19863 f_line(argvars, &rettv);
19864 lnum = rettv.vval.v_number;
19865 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 }
19867 return lnum;
19868}
19869
19870/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019871 * Get the lnum from the first argument.
19872 * Also accepts "$", then "buf" is used.
19873 * Returns 0 on error.
19874 */
19875 static linenr_T
19876get_tv_lnum_buf(argvars, buf)
19877 typval_T *argvars;
19878 buf_T *buf;
19879{
19880 if (argvars[0].v_type == VAR_STRING
19881 && argvars[0].vval.v_string != NULL
19882 && argvars[0].vval.v_string[0] == '$'
19883 && buf != NULL)
19884 return buf->b_ml.ml_line_count;
19885 return get_tv_number_chk(&argvars[0], NULL);
19886}
19887
19888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 * Get the string value of a variable.
19890 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019891 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19892 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 * If the String variable has never been set, return an empty string.
19894 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019895 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19896 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 */
19898 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901{
19902 static char_u mybuf[NUMBUFLEN];
19903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019904 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905}
19906
19907 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019909 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019910 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019912 char_u *res = get_tv_string_buf_chk(varp, buf);
19913
19914 return res != NULL ? res : (char_u *)"";
19915}
19916
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019917 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019918get_tv_string_chk(varp)
19919 typval_T *varp;
19920{
19921 static char_u mybuf[NUMBUFLEN];
19922
19923 return get_tv_string_buf_chk(varp, mybuf);
19924}
19925
19926 static char_u *
19927get_tv_string_buf_chk(varp, buf)
19928 typval_T *varp;
19929 char_u *buf;
19930{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019931 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019933 case VAR_NUMBER:
19934 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19935 return buf;
19936 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019937 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019938 break;
19939 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019940 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019941 break;
19942 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019943 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019944 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019945#ifdef FEAT_FLOAT
19946 case VAR_FLOAT:
19947 EMSG(_("E806: using Float as a String"));
19948 break;
19949#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019950 case VAR_STRING:
19951 if (varp->vval.v_string != NULL)
19952 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019953 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959}
19960
19961/*
19962 * Find variable "name" in the list of variables.
19963 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019964 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019965 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019968 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019969find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019974 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975
Bram Moolenaara7043832005-01-21 11:56:39 +000019976 ht = find_var_ht(name, &varname);
19977 if (htp != NULL)
19978 *htp = ht;
19979 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019981 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982}
19983
19984/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019985 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019986 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019989find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019991 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019992 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019993{
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 hashitem_T *hi;
19995
19996 if (*varname == NUL)
19997 {
19998 /* Must be something like "s:", otherwise "ht" would be NULL. */
19999 switch (varname[-2])
20000 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020001 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020002 case 'g': return &globvars_var;
20003 case 'v': return &vimvars_var;
20004 case 'b': return &curbuf->b_bufvar;
20005 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020006#ifdef FEAT_WINDOWS
20007 case 't': return &curtab->tp_winvar;
20008#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020009 case 'l': return current_funccal == NULL
20010 ? NULL : &current_funccal->l_vars_var;
20011 case 'a': return current_funccal == NULL
20012 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020013 }
20014 return NULL;
20015 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020016
20017 hi = hash_find(ht, varname);
20018 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020019 {
20020 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020021 * worked find the variable again. Don't auto-load a script if it was
20022 * loaded already, otherwise it would be loaded every time when
20023 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020024 if (ht == &globvarht && !writing)
20025 {
20026 /* Note: script_autoload() may make "hi" invalid. It must either
20027 * be obtained again or not used. */
20028 if (!script_autoload(varname, FALSE) || aborting())
20029 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020030 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020031 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020032 if (HASHITEM_EMPTY(hi))
20033 return NULL;
20034 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020035 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020036}
20037
20038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020039 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020040 * Set "varname" to the start of name without ':'.
20041 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020043find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 char_u *name;
20045 char_u **varname;
20046{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020047 hashitem_T *hi;
20048
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 if (name[1] != ':')
20050 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020051 /* The name must not start with a colon or #. */
20052 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 return NULL;
20054 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020055
20056 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020057 hi = hash_find(&compat_hashtab, name);
20058 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020059 return &compat_hashtab;
20060
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 return &globvarht; /* global variable */
20063 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 }
20065 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020066 if (*name == 'g') /* global variable */
20067 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020068 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20069 */
20070 if (vim_strchr(name + 2, ':') != NULL
20071 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020072 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020077#ifdef FEAT_WINDOWS
20078 if (*name == 't') /* tab page variable */
20079 return &curtab->tp_vars.dv_hashtab;
20080#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020081 if (*name == 'v') /* v: variable */
20082 return &vimvarht;
20083 if (*name == 'a' && current_funccal != NULL) /* function argument */
20084 return &current_funccal->l_avars.dv_hashtab;
20085 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20086 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 if (*name == 's' /* script variable */
20088 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20089 return &SCRIPT_VARS(current_SID);
20090 return NULL;
20091}
20092
20093/*
20094 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020095 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 * Returns NULL when it doesn't exist.
20097 */
20098 char_u *
20099get_var_value(name)
20100 char_u *name;
20101{
Bram Moolenaar33570922005-01-25 22:26:29 +000020102 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103
Bram Moolenaara7043832005-01-21 11:56:39 +000020104 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 if (v == NULL)
20106 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108}
20109
20110/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 * sourcing this script and when executing functions defined in the script.
20113 */
20114 void
20115new_script_vars(id)
20116 scid_T id;
20117{
Bram Moolenaara7043832005-01-21 11:56:39 +000020118 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020119 hashtab_T *ht;
20120 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020121
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20123 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020124 /* Re-allocating ga_data means that an ht_array pointing to
20125 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020126 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020127 for (i = 1; i <= ga_scripts.ga_len; ++i)
20128 {
20129 ht = &SCRIPT_VARS(i);
20130 if (ht->ht_mask == HT_INIT_SIZE - 1)
20131 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020132 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020134 }
20135
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 while (ga_scripts.ga_len < id)
20137 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020138 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020139 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020140 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 }
20143 }
20144}
20145
20146/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20148 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 */
20150 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020151init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 dict_T *dict;
20153 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020154 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155{
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020157 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020158 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020159 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020160 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020161 dict_var->di_tv.vval.v_dict = dict;
20162 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020163 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20165 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166}
20167
20168/*
20169 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020170 * Frees all allocated variables and the value they contain.
20171 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 */
20173 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020174vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 hashtab_T *ht;
20176{
20177 vars_clear_ext(ht, TRUE);
20178}
20179
20180/*
20181 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20182 */
20183 static void
20184vars_clear_ext(ht, free_val)
20185 hashtab_T *ht;
20186 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187{
Bram Moolenaara7043832005-01-21 11:56:39 +000020188 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 hashitem_T *hi;
20190 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020193 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020194 for (hi = ht->ht_array; todo > 0; ++hi)
20195 {
20196 if (!HASHITEM_EMPTY(hi))
20197 {
20198 --todo;
20199
Bram Moolenaar33570922005-01-25 22:26:29 +000020200 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020201 * ht_array might change then. hash_clear() takes care of it
20202 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020203 v = HI2DI(hi);
20204 if (free_val)
20205 clear_tv(&v->di_tv);
20206 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20207 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020208 }
20209 }
20210 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020211 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212}
20213
Bram Moolenaara7043832005-01-21 11:56:39 +000020214/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020215 * Delete a variable from hashtab "ht" at item "hi".
20216 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020219delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 hashtab_T *ht;
20221 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222{
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020224
20225 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 clear_tv(&di->di_tv);
20227 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
20231 * List the value of one internal variable.
20232 */
20233 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020234list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020237 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020239 char_u *tofree;
20240 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020241 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020242
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020243 current_copyID += COPYID_INC;
20244 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020246 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020247 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248}
20249
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020251list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 char_u *prefix;
20253 char_u *name;
20254 int type;
20255 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020256 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257{
Bram Moolenaar31859182007-08-14 20:41:13 +000020258 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20259 msg_start();
20260 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 if (name != NULL) /* "a:" vars don't have a name stored */
20262 msg_puts(name);
20263 msg_putchar(' ');
20264 msg_advance(22);
20265 if (type == VAR_NUMBER)
20266 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020267 else if (type == VAR_FUNC)
20268 msg_putchar('*');
20269 else if (type == VAR_LIST)
20270 {
20271 msg_putchar('[');
20272 if (*string == '[')
20273 ++string;
20274 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020275 else if (type == VAR_DICT)
20276 {
20277 msg_putchar('{');
20278 if (*string == '{')
20279 ++string;
20280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 else
20282 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020283
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020285
20286 if (type == VAR_FUNC)
20287 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020288 if (*first)
20289 {
20290 msg_clr_eos();
20291 *first = FALSE;
20292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293}
20294
20295/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020296 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 * If the variable already exists, the value is updated.
20298 * Otherwise the variable is created.
20299 */
20300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020301set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020304 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305{
Bram Moolenaar33570922005-01-25 22:26:29 +000020306 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020308 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020310 ht = find_var_ht(name, &varname);
20311 if (ht == NULL || *varname == NUL)
20312 {
20313 EMSG2(_(e_illvar), name);
20314 return;
20315 }
20316 v = find_var_in_ht(ht, varname, TRUE);
20317
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020318 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20319 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020320
Bram Moolenaar33570922005-01-25 22:26:29 +000020321 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020324 if (var_check_ro(v->di_flags, name)
20325 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020326 return;
20327 if (v->di_tv.v_type != tv->v_type
20328 && !((v->di_tv.v_type == VAR_STRING
20329 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020331 || tv->v_type == VAR_NUMBER))
20332#ifdef FEAT_FLOAT
20333 && !((v->di_tv.v_type == VAR_NUMBER
20334 || v->di_tv.v_type == VAR_FLOAT)
20335 && (tv->v_type == VAR_NUMBER
20336 || tv->v_type == VAR_FLOAT))
20337#endif
20338 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020339 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020340 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020341 return;
20342 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020343
20344 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020345 * Handle setting internal v: variables separately: we don't change
20346 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 */
20348 if (ht == &vimvarht)
20349 {
20350 if (v->di_tv.v_type == VAR_STRING)
20351 {
20352 vim_free(v->di_tv.vval.v_string);
20353 if (copy || tv->v_type != VAR_STRING)
20354 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20355 else
20356 {
20357 /* Take over the string to avoid an extra alloc/free. */
20358 v->di_tv.vval.v_string = tv->vval.v_string;
20359 tv->vval.v_string = NULL;
20360 }
20361 }
20362 else if (v->di_tv.v_type != VAR_NUMBER)
20363 EMSG2(_(e_intern2), "set_var()");
20364 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020365 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020367 if (STRCMP(varname, "searchforward") == 0)
20368 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20369 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020370 return;
20371 }
20372
20373 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 }
20375 else /* add a new variable */
20376 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020377 /* Can't add "v:" variable. */
20378 if (ht == &vimvarht)
20379 {
20380 EMSG2(_(e_illvar), name);
20381 return;
20382 }
20383
Bram Moolenaar92124a32005-06-17 22:03:40 +000020384 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020385 if (!valid_varname(varname))
20386 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020387
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020388 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20389 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020390 if (v == NULL)
20391 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020395 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 return;
20397 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020398 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020401 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020402 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020403 else
20404 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020406 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020407 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409}
20410
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020411/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020412 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020413 * Also give an error message.
20414 */
20415 static int
20416var_check_ro(flags, name)
20417 int flags;
20418 char_u *name;
20419{
20420 if (flags & DI_FLAGS_RO)
20421 {
20422 EMSG2(_(e_readonlyvar), name);
20423 return TRUE;
20424 }
20425 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20426 {
20427 EMSG2(_(e_readonlysbx), name);
20428 return TRUE;
20429 }
20430 return FALSE;
20431}
20432
20433/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020434 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20435 * Also give an error message.
20436 */
20437 static int
20438var_check_fixed(flags, name)
20439 int flags;
20440 char_u *name;
20441{
20442 if (flags & DI_FLAGS_FIX)
20443 {
20444 EMSG2(_("E795: Cannot delete variable %s"), name);
20445 return TRUE;
20446 }
20447 return FALSE;
20448}
20449
20450/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020451 * Check if a funcref is assigned to a valid variable name.
20452 * Return TRUE and give an error if not.
20453 */
20454 static int
20455var_check_func_name(name, new_var)
20456 char_u *name; /* points to start of variable name */
20457 int new_var; /* TRUE when creating the variable */
20458{
20459 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20460 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20461 ? name[2] : name[0]))
20462 {
20463 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20464 name);
20465 return TRUE;
20466 }
20467 /* Don't allow hiding a function. When "v" is not NULL we might be
20468 * assigning another function to the same var, the type is checked
20469 * below. */
20470 if (new_var && function_exists(name))
20471 {
20472 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20473 name);
20474 return TRUE;
20475 }
20476 return FALSE;
20477}
20478
20479/*
20480 * Check if a variable name is valid.
20481 * Return FALSE and give an error if not.
20482 */
20483 static int
20484valid_varname(varname)
20485 char_u *varname;
20486{
20487 char_u *p;
20488
20489 for (p = varname; *p != NUL; ++p)
20490 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20491 && *p != AUTOLOAD_CHAR)
20492 {
20493 EMSG2(_(e_illvar), varname);
20494 return FALSE;
20495 }
20496 return TRUE;
20497}
20498
20499/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020500 * Return TRUE if typeval "tv" is set to be locked (immutable).
20501 * Also give an error message, using "name".
20502 */
20503 static int
20504tv_check_lock(lock, name)
20505 int lock;
20506 char_u *name;
20507{
20508 if (lock & VAR_LOCKED)
20509 {
20510 EMSG2(_("E741: Value is locked: %s"),
20511 name == NULL ? (char_u *)_("Unknown") : name);
20512 return TRUE;
20513 }
20514 if (lock & VAR_FIXED)
20515 {
20516 EMSG2(_("E742: Cannot change value of %s"),
20517 name == NULL ? (char_u *)_("Unknown") : name);
20518 return TRUE;
20519 }
20520 return FALSE;
20521}
20522
20523/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020525 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020526 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020527 * It is OK for "from" and "to" to point to the same item. This is used to
20528 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020529 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020530 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020531copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 typval_T *from;
20533 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020535 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020536 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020537 switch (from->v_type)
20538 {
20539 case VAR_NUMBER:
20540 to->vval.v_number = from->vval.v_number;
20541 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020542#ifdef FEAT_FLOAT
20543 case VAR_FLOAT:
20544 to->vval.v_float = from->vval.v_float;
20545 break;
20546#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020547 case VAR_STRING:
20548 case VAR_FUNC:
20549 if (from->vval.v_string == NULL)
20550 to->vval.v_string = NULL;
20551 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020552 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020554 if (from->v_type == VAR_FUNC)
20555 func_ref(to->vval.v_string);
20556 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020557 break;
20558 case VAR_LIST:
20559 if (from->vval.v_list == NULL)
20560 to->vval.v_list = NULL;
20561 else
20562 {
20563 to->vval.v_list = from->vval.v_list;
20564 ++to->vval.v_list->lv_refcount;
20565 }
20566 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020567 case VAR_DICT:
20568 if (from->vval.v_dict == NULL)
20569 to->vval.v_dict = NULL;
20570 else
20571 {
20572 to->vval.v_dict = from->vval.v_dict;
20573 ++to->vval.v_dict->dv_refcount;
20574 }
20575 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020576 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020577 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020578 break;
20579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580}
20581
20582/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020583 * Make a copy of an item.
20584 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020585 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20586 * reference to an already copied list/dict can be used.
20587 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020588 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020589 static int
20590item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 typval_T *from;
20592 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020593 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020594 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020595{
20596 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020597 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020598
Bram Moolenaar33570922005-01-25 22:26:29 +000020599 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020600 {
20601 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020602 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020603 }
20604 ++recurse;
20605
20606 switch (from->v_type)
20607 {
20608 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020609#ifdef FEAT_FLOAT
20610 case VAR_FLOAT:
20611#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 case VAR_STRING:
20613 case VAR_FUNC:
20614 copy_tv(from, to);
20615 break;
20616 case VAR_LIST:
20617 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020618 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020619 if (from->vval.v_list == NULL)
20620 to->vval.v_list = NULL;
20621 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20622 {
20623 /* use the copy made earlier */
20624 to->vval.v_list = from->vval.v_list->lv_copylist;
20625 ++to->vval.v_list->lv_refcount;
20626 }
20627 else
20628 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20629 if (to->vval.v_list == NULL)
20630 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020631 break;
20632 case VAR_DICT:
20633 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020634 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020635 if (from->vval.v_dict == NULL)
20636 to->vval.v_dict = NULL;
20637 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20638 {
20639 /* use the copy made earlier */
20640 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20641 ++to->vval.v_dict->dv_refcount;
20642 }
20643 else
20644 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20645 if (to->vval.v_dict == NULL)
20646 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020647 break;
20648 default:
20649 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020650 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020651 }
20652 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020653 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020654}
20655
20656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 * ":echo expr1 ..." print each argument separated with a space, add a
20658 * newline at the end.
20659 * ":echon expr1 ..." print each argument plain.
20660 */
20661 void
20662ex_echo(eap)
20663 exarg_T *eap;
20664{
20665 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020666 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020667 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 char_u *p;
20669 int needclr = TRUE;
20670 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020671 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672
20673 if (eap->skip)
20674 ++emsg_skip;
20675 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20676 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020677 /* If eval1() causes an error message the text from the command may
20678 * still need to be cleared. E.g., "echo 22,44". */
20679 need_clr_eos = needclr;
20680
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020682 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 {
20684 /*
20685 * Report the invalid expression unless the expression evaluation
20686 * has been cancelled due to an aborting error, an interrupt, or an
20687 * exception.
20688 */
20689 if (!aborting())
20690 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020691 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 break;
20693 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020694 need_clr_eos = FALSE;
20695
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 if (!eap->skip)
20697 {
20698 if (atstart)
20699 {
20700 atstart = FALSE;
20701 /* Call msg_start() after eval1(), evaluating the expression
20702 * may cause a message to appear. */
20703 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020704 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020705 /* Mark the saved text as finishing the line, so that what
20706 * follows is displayed on a new line when scrolling back
20707 * at the more prompt. */
20708 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 }
20712 else if (eap->cmdidx == CMD_echo)
20713 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020714 current_copyID += COPYID_INC;
20715 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020716 if (p != NULL)
20717 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020719 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020721 if (*p != TAB && needclr)
20722 {
20723 /* remove any text still there from the command */
20724 msg_clr_eos();
20725 needclr = FALSE;
20726 }
20727 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 }
20729 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020730 {
20731#ifdef FEAT_MBYTE
20732 if (has_mbyte)
20733 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020734 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020735
20736 (void)msg_outtrans_len_attr(p, i, echo_attr);
20737 p += i - 1;
20738 }
20739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020741 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020744 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 arg = skipwhite(arg);
20748 }
20749 eap->nextcmd = check_nextcmd(arg);
20750
20751 if (eap->skip)
20752 --emsg_skip;
20753 else
20754 {
20755 /* remove text that may still be there from the command */
20756 if (needclr)
20757 msg_clr_eos();
20758 if (eap->cmdidx == CMD_echo)
20759 msg_end();
20760 }
20761}
20762
20763/*
20764 * ":echohl {name}".
20765 */
20766 void
20767ex_echohl(eap)
20768 exarg_T *eap;
20769{
20770 int id;
20771
20772 id = syn_name2id(eap->arg);
20773 if (id == 0)
20774 echo_attr = 0;
20775 else
20776 echo_attr = syn_id2attr(id);
20777}
20778
20779/*
20780 * ":execute expr1 ..." execute the result of an expression.
20781 * ":echomsg expr1 ..." Print a message
20782 * ":echoerr expr1 ..." Print an error
20783 * Each gets spaces around each argument and a newline at the end for
20784 * echo commands
20785 */
20786 void
20787ex_execute(eap)
20788 exarg_T *eap;
20789{
20790 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020791 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 int ret = OK;
20793 char_u *p;
20794 garray_T ga;
20795 int len;
20796 int save_did_emsg;
20797
20798 ga_init2(&ga, 1, 80);
20799
20800 if (eap->skip)
20801 ++emsg_skip;
20802 while (*arg != NUL && *arg != '|' && *arg != '\n')
20803 {
20804 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 {
20807 /*
20808 * Report the invalid expression unless the expression evaluation
20809 * has been cancelled due to an aborting error, an interrupt, or an
20810 * exception.
20811 */
20812 if (!aborting())
20813 EMSG2(_(e_invexpr2), p);
20814 ret = FAIL;
20815 break;
20816 }
20817
20818 if (!eap->skip)
20819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020820 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 len = (int)STRLEN(p);
20822 if (ga_grow(&ga, len + 2) == FAIL)
20823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020824 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 ret = FAIL;
20826 break;
20827 }
20828 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 ga.ga_len += len;
20832 }
20833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 arg = skipwhite(arg);
20836 }
20837
20838 if (ret != FAIL && ga.ga_data != NULL)
20839 {
20840 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020843 out_flush();
20844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 else if (eap->cmdidx == CMD_echoerr)
20846 {
20847 /* We don't want to abort following commands, restore did_emsg. */
20848 save_did_emsg = did_emsg;
20849 EMSG((char_u *)ga.ga_data);
20850 if (!force_abort)
20851 did_emsg = save_did_emsg;
20852 }
20853 else if (eap->cmdidx == CMD_execute)
20854 do_cmdline((char_u *)ga.ga_data,
20855 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20856 }
20857
20858 ga_clear(&ga);
20859
20860 if (eap->skip)
20861 --emsg_skip;
20862
20863 eap->nextcmd = check_nextcmd(arg);
20864}
20865
20866/*
20867 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20868 * "arg" points to the "&" or '+' when called, to "option" when returning.
20869 * Returns NULL when no option name found. Otherwise pointer to the char
20870 * after the option name.
20871 */
20872 static char_u *
20873find_option_end(arg, opt_flags)
20874 char_u **arg;
20875 int *opt_flags;
20876{
20877 char_u *p = *arg;
20878
20879 ++p;
20880 if (*p == 'g' && p[1] == ':')
20881 {
20882 *opt_flags = OPT_GLOBAL;
20883 p += 2;
20884 }
20885 else if (*p == 'l' && p[1] == ':')
20886 {
20887 *opt_flags = OPT_LOCAL;
20888 p += 2;
20889 }
20890 else
20891 *opt_flags = 0;
20892
20893 if (!ASCII_ISALPHA(*p))
20894 return NULL;
20895 *arg = p;
20896
20897 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20898 p += 4; /* termcap option */
20899 else
20900 while (ASCII_ISALPHA(*p))
20901 ++p;
20902 return p;
20903}
20904
20905/*
20906 * ":function"
20907 */
20908 void
20909ex_function(eap)
20910 exarg_T *eap;
20911{
20912 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020913 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 int j;
20915 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 char_u *name = NULL;
20918 char_u *p;
20919 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020920 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 garray_T newargs;
20922 garray_T newlines;
20923 int varargs = FALSE;
20924 int mustend = FALSE;
20925 int flags = 0;
20926 ufunc_T *fp;
20927 int indent;
20928 int nesting;
20929 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020930 dictitem_T *v;
20931 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932 static int func_nr = 0; /* number for nameless function */
20933 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020934 hashtab_T *ht;
20935 int todo;
20936 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020937 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938
20939 /*
20940 * ":function" without argument: list functions.
20941 */
20942 if (ends_excmd(*eap->arg))
20943 {
20944 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020945 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020946 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020947 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020948 {
20949 if (!HASHITEM_EMPTY(hi))
20950 {
20951 --todo;
20952 fp = HI2UF(hi);
20953 if (!isdigit(*fp->uf_name))
20954 list_func_head(fp, FALSE);
20955 }
20956 }
20957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 eap->nextcmd = check_nextcmd(eap->arg);
20959 return;
20960 }
20961
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020963 * ":function /pat": list functions matching pattern.
20964 */
20965 if (*eap->arg == '/')
20966 {
20967 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20968 if (!eap->skip)
20969 {
20970 regmatch_T regmatch;
20971
20972 c = *p;
20973 *p = NUL;
20974 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20975 *p = c;
20976 if (regmatch.regprog != NULL)
20977 {
20978 regmatch.rm_ic = p_ic;
20979
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020980 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020981 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20982 {
20983 if (!HASHITEM_EMPTY(hi))
20984 {
20985 --todo;
20986 fp = HI2UF(hi);
20987 if (!isdigit(*fp->uf_name)
20988 && vim_regexec(&regmatch, fp->uf_name, 0))
20989 list_func_head(fp, FALSE);
20990 }
20991 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020992 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020993 }
20994 }
20995 if (*p == '/')
20996 ++p;
20997 eap->nextcmd = check_nextcmd(p);
20998 return;
20999 }
21000
21001 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021002 * Get the function name. There are these situations:
21003 * func normal function name
21004 * "name" == func, "fudi.fd_dict" == NULL
21005 * dict.func new dictionary entry
21006 * "name" == NULL, "fudi.fd_dict" set,
21007 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21008 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021009 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021010 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21011 * dict.func existing dict entry that's not a Funcref
21012 * "name" == NULL, "fudi.fd_dict" set,
21013 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016 name = trans_function_name(&p, eap->skip, 0, &fudi);
21017 paren = (vim_strchr(p, '(') != NULL);
21018 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 {
21020 /*
21021 * Return on an invalid expression in braces, unless the expression
21022 * evaluation has been cancelled due to an aborting error, an
21023 * interrupt, or an exception.
21024 */
21025 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021027 if (!eap->skip && fudi.fd_newkey != NULL)
21028 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021029 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 else
21033 eap->skip = TRUE;
21034 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021035
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 /* An error in a function call during evaluation of an expression in magic
21037 * braces should not cause the function not to be defined. */
21038 saved_did_emsg = did_emsg;
21039 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040
21041 /*
21042 * ":function func" with only function name: list function.
21043 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021044 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 {
21046 if (!ends_excmd(*skipwhite(p)))
21047 {
21048 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021049 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 }
21051 eap->nextcmd = check_nextcmd(p);
21052 if (eap->nextcmd != NULL)
21053 *p = NUL;
21054 if (!eap->skip && !got_int)
21055 {
21056 fp = find_func(name);
21057 if (fp != NULL)
21058 {
21059 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021060 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021062 if (FUNCLINE(fp, j) == NULL)
21063 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 msg_putchar('\n');
21065 msg_outnum((long)(j + 1));
21066 if (j < 9)
21067 msg_putchar(' ');
21068 if (j < 99)
21069 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021070 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 out_flush(); /* show a line at a time */
21072 ui_breakcheck();
21073 }
21074 if (!got_int)
21075 {
21076 msg_putchar('\n');
21077 msg_puts((char_u *)" endfunction");
21078 }
21079 }
21080 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021081 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021083 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 }
21085
21086 /*
21087 * ":function name(arg1, arg2)" Define function.
21088 */
21089 p = skipwhite(p);
21090 if (*p != '(')
21091 {
21092 if (!eap->skip)
21093 {
21094 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021095 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 }
21097 /* attempt to continue by skipping some text */
21098 if (vim_strchr(p, '(') != NULL)
21099 p = vim_strchr(p, '(');
21100 }
21101 p = skipwhite(p + 1);
21102
21103 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21104 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21105
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021106 if (!eap->skip)
21107 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021108 /* Check the name of the function. Unless it's a dictionary function
21109 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021110 if (name != NULL)
21111 arg = name;
21112 else
21113 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021114 if (arg != NULL && (fudi.fd_di == NULL
21115 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021116 {
21117 if (*arg == K_SPECIAL)
21118 j = 3;
21119 else
21120 j = 0;
21121 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21122 : eval_isnamec(arg[j])))
21123 ++j;
21124 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021125 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021126 }
21127 }
21128
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 /*
21130 * Isolate the arguments: "arg1, arg2, ...)"
21131 */
21132 while (*p != ')')
21133 {
21134 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21135 {
21136 varargs = TRUE;
21137 p += 3;
21138 mustend = TRUE;
21139 }
21140 else
21141 {
21142 arg = p;
21143 while (ASCII_ISALNUM(*p) || *p == '_')
21144 ++p;
21145 if (arg == p || isdigit(*arg)
21146 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21147 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21148 {
21149 if (!eap->skip)
21150 EMSG2(_("E125: Illegal argument: %s"), arg);
21151 break;
21152 }
21153 if (ga_grow(&newargs, 1) == FAIL)
21154 goto erret;
21155 c = *p;
21156 *p = NUL;
21157 arg = vim_strsave(arg);
21158 if (arg == NULL)
21159 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021160
21161 /* Check for duplicate argument name. */
21162 for (i = 0; i < newargs.ga_len; ++i)
21163 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21164 {
21165 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21166 goto erret;
21167 }
21168
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21170 *p = c;
21171 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 if (*p == ',')
21173 ++p;
21174 else
21175 mustend = TRUE;
21176 }
21177 p = skipwhite(p);
21178 if (mustend && *p != ')')
21179 {
21180 if (!eap->skip)
21181 EMSG2(_(e_invarg2), eap->arg);
21182 break;
21183 }
21184 }
21185 ++p; /* skip the ')' */
21186
Bram Moolenaare9a41262005-01-15 22:18:47 +000021187 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 for (;;)
21189 {
21190 p = skipwhite(p);
21191 if (STRNCMP(p, "range", 5) == 0)
21192 {
21193 flags |= FC_RANGE;
21194 p += 5;
21195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021196 else if (STRNCMP(p, "dict", 4) == 0)
21197 {
21198 flags |= FC_DICT;
21199 p += 4;
21200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 else if (STRNCMP(p, "abort", 5) == 0)
21202 {
21203 flags |= FC_ABORT;
21204 p += 5;
21205 }
21206 else
21207 break;
21208 }
21209
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021210 /* When there is a line break use what follows for the function body.
21211 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21212 if (*p == '\n')
21213 line_arg = p + 1;
21214 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 EMSG(_(e_trailing));
21216
21217 /*
21218 * Read the body of the function, until ":endfunction" is found.
21219 */
21220 if (KeyTyped)
21221 {
21222 /* Check if the function already exists, don't let the user type the
21223 * whole function before telling him it doesn't work! For a script we
21224 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021225 if (!eap->skip && !eap->forceit)
21226 {
21227 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21228 EMSG(_(e_funcdict));
21229 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021230 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021233 if (!eap->skip && did_emsg)
21234 goto erret;
21235
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 msg_putchar('\n'); /* don't overwrite the function name */
21237 cmdline_row = msg_row;
21238 }
21239
21240 indent = 2;
21241 nesting = 0;
21242 for (;;)
21243 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021244 if (KeyTyped)
21245 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021247 sourcing_lnum_off = sourcing_lnum;
21248
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021249 if (line_arg != NULL)
21250 {
21251 /* Use eap->arg, split up in parts by line breaks. */
21252 theline = line_arg;
21253 p = vim_strchr(theline, '\n');
21254 if (p == NULL)
21255 line_arg += STRLEN(line_arg);
21256 else
21257 {
21258 *p = NUL;
21259 line_arg = p + 1;
21260 }
21261 }
21262 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 theline = getcmdline(':', 0L, indent);
21264 else
21265 theline = eap->getline(':', eap->cookie, indent);
21266 if (KeyTyped)
21267 lines_left = Rows - 1;
21268 if (theline == NULL)
21269 {
21270 EMSG(_("E126: Missing :endfunction"));
21271 goto erret;
21272 }
21273
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021274 /* Detect line continuation: sourcing_lnum increased more than one. */
21275 if (sourcing_lnum > sourcing_lnum_off + 1)
21276 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21277 else
21278 sourcing_lnum_off = 0;
21279
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 if (skip_until != NULL)
21281 {
21282 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21283 * don't check for ":endfunc". */
21284 if (STRCMP(theline, skip_until) == 0)
21285 {
21286 vim_free(skip_until);
21287 skip_until = NULL;
21288 }
21289 }
21290 else
21291 {
21292 /* skip ':' and blanks*/
21293 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21294 ;
21295
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021296 /* Check for "endfunction". */
21297 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021299 if (line_arg == NULL)
21300 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 break;
21302 }
21303
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021304 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 * at "end". */
21306 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21307 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021308 else if (STRNCMP(p, "if", 2) == 0
21309 || STRNCMP(p, "wh", 2) == 0
21310 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 || STRNCMP(p, "try", 3) == 0)
21312 indent += 2;
21313
21314 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021315 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021317 if (*p == '!')
21318 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 p += eval_fname_script(p);
21320 if (ASCII_ISALPHA(*p))
21321 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021322 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 if (*skipwhite(p) == '(')
21324 {
21325 ++nesting;
21326 indent += 2;
21327 }
21328 }
21329 }
21330
21331 /* Check for ":append" or ":insert". */
21332 p = skip_range(p, NULL);
21333 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21334 || (p[0] == 'i'
21335 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21336 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21337 skip_until = vim_strsave((char_u *)".");
21338
21339 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21340 arg = skipwhite(skiptowhite(p));
21341 if (arg[0] == '<' && arg[1] =='<'
21342 && ((p[0] == 'p' && p[1] == 'y'
21343 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21344 || (p[0] == 'p' && p[1] == 'e'
21345 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21346 || (p[0] == 't' && p[1] == 'c'
21347 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021348 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21349 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21351 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021352 || (p[0] == 'm' && p[1] == 'z'
21353 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 ))
21355 {
21356 /* ":python <<" continues until a dot, like ":append" */
21357 p = skipwhite(arg + 2);
21358 if (*p == NUL)
21359 skip_until = vim_strsave((char_u *)".");
21360 else
21361 skip_until = vim_strsave(p);
21362 }
21363 }
21364
21365 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021366 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021367 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021368 if (line_arg == NULL)
21369 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021371 }
21372
21373 /* Copy the line to newly allocated memory. get_one_sourceline()
21374 * allocates 250 bytes per line, this saves 80% on average. The cost
21375 * is an extra alloc/free. */
21376 p = vim_strsave(theline);
21377 if (p != NULL)
21378 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021379 if (line_arg == NULL)
21380 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021381 theline = p;
21382 }
21383
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021384 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21385
21386 /* Add NULL lines for continuation lines, so that the line count is
21387 * equal to the index in the growarray. */
21388 while (sourcing_lnum_off-- > 0)
21389 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021390
21391 /* Check for end of eap->arg. */
21392 if (line_arg != NULL && *line_arg == NUL)
21393 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 }
21395
21396 /* Don't define the function when skipping commands or when an error was
21397 * detected. */
21398 if (eap->skip || did_emsg)
21399 goto erret;
21400
21401 /*
21402 * If there are no errors, add the function
21403 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021404 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021405 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021406 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021407 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021408 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021409 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021410 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021411 goto erret;
21412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021413
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021414 fp = find_func(name);
21415 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021417 if (!eap->forceit)
21418 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021419 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021420 goto erret;
21421 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021422 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021423 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021424 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021425 name);
21426 goto erret;
21427 }
21428 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021429 ga_clear_strings(&(fp->uf_args));
21430 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021431 vim_free(name);
21432 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 }
21435 else
21436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021437 char numbuf[20];
21438
21439 fp = NULL;
21440 if (fudi.fd_newkey == NULL && !eap->forceit)
21441 {
21442 EMSG(_(e_funcdict));
21443 goto erret;
21444 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021445 if (fudi.fd_di == NULL)
21446 {
21447 /* Can't add a function to a locked dictionary */
21448 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21449 goto erret;
21450 }
21451 /* Can't change an existing function if it is locked */
21452 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21453 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454
21455 /* Give the function a sequential number. Can only be used with a
21456 * Funcref! */
21457 vim_free(name);
21458 sprintf(numbuf, "%d", ++func_nr);
21459 name = vim_strsave((char_u *)numbuf);
21460 if (name == NULL)
21461 goto erret;
21462 }
21463
21464 if (fp == NULL)
21465 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021466 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021467 {
21468 int slen, plen;
21469 char_u *scriptname;
21470
21471 /* Check that the autoload name matches the script name. */
21472 j = FAIL;
21473 if (sourcing_name != NULL)
21474 {
21475 scriptname = autoload_name(name);
21476 if (scriptname != NULL)
21477 {
21478 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021479 plen = (int)STRLEN(p);
21480 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021481 if (slen > plen && fnamecmp(p,
21482 sourcing_name + slen - plen) == 0)
21483 j = OK;
21484 vim_free(scriptname);
21485 }
21486 }
21487 if (j == FAIL)
21488 {
21489 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21490 goto erret;
21491 }
21492 }
21493
21494 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 if (fp == NULL)
21496 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021497
21498 if (fudi.fd_dict != NULL)
21499 {
21500 if (fudi.fd_di == NULL)
21501 {
21502 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021503 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504 if (fudi.fd_di == NULL)
21505 {
21506 vim_free(fp);
21507 goto erret;
21508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021509 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21510 {
21511 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021512 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021513 goto erret;
21514 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515 }
21516 else
21517 /* overwrite existing dict entry */
21518 clear_tv(&fudi.fd_di->di_tv);
21519 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021520 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021521 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021522 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021523
21524 /* behave like "dict" was used */
21525 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021526 }
21527
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021529 STRCPY(fp->uf_name, name);
21530 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021532 fp->uf_args = newargs;
21533 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021534#ifdef FEAT_PROFILE
21535 fp->uf_tml_count = NULL;
21536 fp->uf_tml_total = NULL;
21537 fp->uf_tml_self = NULL;
21538 fp->uf_profiling = FALSE;
21539 if (prof_def_func())
21540 func_do_profile(fp);
21541#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021542 fp->uf_varargs = varargs;
21543 fp->uf_flags = flags;
21544 fp->uf_calls = 0;
21545 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021546 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547
21548erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 ga_clear_strings(&newargs);
21550 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021551ret_free:
21552 vim_free(skip_until);
21553 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556}
21557
21558/*
21559 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021560 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021562 * flags:
21563 * TFN_INT: internal function name OK
21564 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 * Advances "pp" to just after the function name (if no error).
21566 */
21567 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021568trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 char_u **pp;
21570 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021571 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021572 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021574 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 char_u *start;
21576 char_u *end;
21577 int lead;
21578 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021580 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021581
21582 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021583 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021585
21586 /* Check for hard coded <SNR>: already translated function ID (from a user
21587 * command). */
21588 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21589 && (*pp)[2] == (int)KE_SNR)
21590 {
21591 *pp += 3;
21592 len = get_id_len(pp) + 3;
21593 return vim_strnsave(start, len);
21594 }
21595
21596 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21597 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021599 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021601
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021602 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21603 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021604 if (end == start)
21605 {
21606 if (!skip)
21607 EMSG(_("E129: Function name required"));
21608 goto theend;
21609 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021610 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021611 {
21612 /*
21613 * Report an invalid expression in braces, unless the expression
21614 * evaluation has been cancelled due to an aborting error, an
21615 * interrupt, or an exception.
21616 */
21617 if (!aborting())
21618 {
21619 if (end != NULL)
21620 EMSG2(_(e_invarg2), start);
21621 }
21622 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021623 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021624 goto theend;
21625 }
21626
21627 if (lv.ll_tv != NULL)
21628 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629 if (fdp != NULL)
21630 {
21631 fdp->fd_dict = lv.ll_dict;
21632 fdp->fd_newkey = lv.ll_newkey;
21633 lv.ll_newkey = NULL;
21634 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021635 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021636 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21637 {
21638 name = vim_strsave(lv.ll_tv->vval.v_string);
21639 *pp = end;
21640 }
21641 else
21642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021643 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21644 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021645 EMSG(_(e_funcref));
21646 else
21647 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021648 name = NULL;
21649 }
21650 goto theend;
21651 }
21652
21653 if (lv.ll_name == NULL)
21654 {
21655 /* Error found, but continue after the function name. */
21656 *pp = end;
21657 goto theend;
21658 }
21659
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021660 /* Check if the name is a Funcref. If so, use the value. */
21661 if (lv.ll_exp_name != NULL)
21662 {
21663 len = (int)STRLEN(lv.ll_exp_name);
21664 name = deref_func_name(lv.ll_exp_name, &len);
21665 if (name == lv.ll_exp_name)
21666 name = NULL;
21667 }
21668 else
21669 {
21670 len = (int)(end - *pp);
21671 name = deref_func_name(*pp, &len);
21672 if (name == *pp)
21673 name = NULL;
21674 }
21675 if (name != NULL)
21676 {
21677 name = vim_strsave(name);
21678 *pp = end;
21679 goto theend;
21680 }
21681
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021682 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021683 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021684 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021685 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21686 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21687 {
21688 /* When there was "s:" already or the name expanded to get a
21689 * leading "s:" then remove it. */
21690 lv.ll_name += 2;
21691 len -= 2;
21692 lead = 2;
21693 }
21694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021695 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021696 {
21697 if (lead == 2) /* skip over "s:" */
21698 lv.ll_name += 2;
21699 len = (int)(end - lv.ll_name);
21700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021701
21702 /*
21703 * Copy the function name to allocated memory.
21704 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21705 * Accept <SNR>123_name() outside a script.
21706 */
21707 if (skip)
21708 lead = 0; /* do nothing */
21709 else if (lead > 0)
21710 {
21711 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021712 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21713 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021714 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021715 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021716 if (current_SID <= 0)
21717 {
21718 EMSG(_(e_usingsid));
21719 goto theend;
21720 }
21721 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21722 lead += (int)STRLEN(sid_buf);
21723 }
21724 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021725 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021726 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021727 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021728 goto theend;
21729 }
21730 name = alloc((unsigned)(len + lead + 1));
21731 if (name != NULL)
21732 {
21733 if (lead > 0)
21734 {
21735 name[0] = K_SPECIAL;
21736 name[1] = KS_EXTRA;
21737 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021738 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021739 STRCPY(name + 3, sid_buf);
21740 }
21741 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21742 name[len + lead] = NUL;
21743 }
21744 *pp = end;
21745
21746theend:
21747 clear_lval(&lv);
21748 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749}
21750
21751/*
21752 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21753 * Return 2 if "p" starts with "s:".
21754 * Return 0 otherwise.
21755 */
21756 static int
21757eval_fname_script(p)
21758 char_u *p;
21759{
21760 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21761 || STRNICMP(p + 1, "SNR>", 4) == 0))
21762 return 5;
21763 if (p[0] == 's' && p[1] == ':')
21764 return 2;
21765 return 0;
21766}
21767
21768/*
21769 * Return TRUE if "p" starts with "<SID>" or "s:".
21770 * Only works if eval_fname_script() returned non-zero for "p"!
21771 */
21772 static int
21773eval_fname_sid(p)
21774 char_u *p;
21775{
21776 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21777}
21778
21779/*
21780 * List the head of the function: "name(arg1, arg2)".
21781 */
21782 static void
21783list_func_head(fp, indent)
21784 ufunc_T *fp;
21785 int indent;
21786{
21787 int j;
21788
21789 msg_start();
21790 if (indent)
21791 MSG_PUTS(" ");
21792 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021793 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 {
21795 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021796 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 }
21798 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021799 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021801 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 {
21803 if (j)
21804 MSG_PUTS(", ");
21805 msg_puts(FUNCARG(fp, j));
21806 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021807 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 {
21809 if (j)
21810 MSG_PUTS(", ");
21811 MSG_PUTS("...");
21812 }
21813 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021814 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021815 if (p_verbose > 0)
21816 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817}
21818
21819/*
21820 * Find a function by name, return pointer to it in ufuncs.
21821 * Return NULL for unknown function.
21822 */
21823 static ufunc_T *
21824find_func(name)
21825 char_u *name;
21826{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021827 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021829 hi = hash_find(&func_hashtab, name);
21830 if (!HASHITEM_EMPTY(hi))
21831 return HI2UF(hi);
21832 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833}
21834
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021835#if defined(EXITFREE) || defined(PROTO)
21836 void
21837free_all_functions()
21838{
21839 hashitem_T *hi;
21840
21841 /* Need to start all over every time, because func_free() may change the
21842 * hash table. */
21843 while (func_hashtab.ht_used > 0)
21844 for (hi = func_hashtab.ht_array; ; ++hi)
21845 if (!HASHITEM_EMPTY(hi))
21846 {
21847 func_free(HI2UF(hi));
21848 break;
21849 }
21850}
21851#endif
21852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021853/*
21854 * Return TRUE if a function "name" exists.
21855 */
21856 static int
21857function_exists(name)
21858 char_u *name;
21859{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021860 char_u *nm = name;
21861 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021862 int n = FALSE;
21863
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021864 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021865 nm = skipwhite(nm);
21866
21867 /* Only accept "funcname", "funcname ", "funcname (..." and
21868 * "funcname(...", not "funcname!...". */
21869 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021870 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021871 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021872 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021873 else
21874 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021875 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021876 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021877 return n;
21878}
21879
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021880/*
21881 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021882 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021883 */
21884 static int
21885builtin_function(name)
21886 char_u *name;
21887{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021888 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21889 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021890}
21891
Bram Moolenaar05159a02005-02-26 23:04:13 +000021892#if defined(FEAT_PROFILE) || defined(PROTO)
21893/*
21894 * Start profiling function "fp".
21895 */
21896 static void
21897func_do_profile(fp)
21898 ufunc_T *fp;
21899{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021900 int len = fp->uf_lines.ga_len;
21901
21902 if (len == 0)
21903 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021904 fp->uf_tm_count = 0;
21905 profile_zero(&fp->uf_tm_self);
21906 profile_zero(&fp->uf_tm_total);
21907 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021908 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021909 if (fp->uf_tml_total == NULL)
21910 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021911 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021912 if (fp->uf_tml_self == NULL)
21913 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021914 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021915 fp->uf_tml_idx = -1;
21916 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21917 || fp->uf_tml_self == NULL)
21918 return; /* out of memory */
21919
21920 fp->uf_profiling = TRUE;
21921}
21922
21923/*
21924 * Dump the profiling results for all functions in file "fd".
21925 */
21926 void
21927func_dump_profile(fd)
21928 FILE *fd;
21929{
21930 hashitem_T *hi;
21931 int todo;
21932 ufunc_T *fp;
21933 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021934 ufunc_T **sorttab;
21935 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021936
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021937 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021938 if (todo == 0)
21939 return; /* nothing to dump */
21940
Bram Moolenaar73830342005-02-28 22:48:19 +000021941 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21942
Bram Moolenaar05159a02005-02-26 23:04:13 +000021943 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21944 {
21945 if (!HASHITEM_EMPTY(hi))
21946 {
21947 --todo;
21948 fp = HI2UF(hi);
21949 if (fp->uf_profiling)
21950 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021951 if (sorttab != NULL)
21952 sorttab[st_len++] = fp;
21953
Bram Moolenaar05159a02005-02-26 23:04:13 +000021954 if (fp->uf_name[0] == K_SPECIAL)
21955 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21956 else
21957 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21958 if (fp->uf_tm_count == 1)
21959 fprintf(fd, "Called 1 time\n");
21960 else
21961 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21962 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21963 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21964 fprintf(fd, "\n");
21965 fprintf(fd, "count total (s) self (s)\n");
21966
21967 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21968 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021969 if (FUNCLINE(fp, i) == NULL)
21970 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021971 prof_func_line(fd, fp->uf_tml_count[i],
21972 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021973 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21974 }
21975 fprintf(fd, "\n");
21976 }
21977 }
21978 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021979
21980 if (sorttab != NULL && st_len > 0)
21981 {
21982 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21983 prof_total_cmp);
21984 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21985 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21986 prof_self_cmp);
21987 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21988 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021989
21990 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021991}
Bram Moolenaar73830342005-02-28 22:48:19 +000021992
21993 static void
21994prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21995 FILE *fd;
21996 ufunc_T **sorttab;
21997 int st_len;
21998 char *title;
21999 int prefer_self; /* when equal print only self time */
22000{
22001 int i;
22002 ufunc_T *fp;
22003
22004 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22005 fprintf(fd, "count total (s) self (s) function\n");
22006 for (i = 0; i < 20 && i < st_len; ++i)
22007 {
22008 fp = sorttab[i];
22009 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22010 prefer_self);
22011 if (fp->uf_name[0] == K_SPECIAL)
22012 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22013 else
22014 fprintf(fd, " %s()\n", fp->uf_name);
22015 }
22016 fprintf(fd, "\n");
22017}
22018
22019/*
22020 * Print the count and times for one function or function line.
22021 */
22022 static void
22023prof_func_line(fd, count, total, self, prefer_self)
22024 FILE *fd;
22025 int count;
22026 proftime_T *total;
22027 proftime_T *self;
22028 int prefer_self; /* when equal print only self time */
22029{
22030 if (count > 0)
22031 {
22032 fprintf(fd, "%5d ", count);
22033 if (prefer_self && profile_equal(total, self))
22034 fprintf(fd, " ");
22035 else
22036 fprintf(fd, "%s ", profile_msg(total));
22037 if (!prefer_self && profile_equal(total, self))
22038 fprintf(fd, " ");
22039 else
22040 fprintf(fd, "%s ", profile_msg(self));
22041 }
22042 else
22043 fprintf(fd, " ");
22044}
22045
22046/*
22047 * Compare function for total time sorting.
22048 */
22049 static int
22050#ifdef __BORLANDC__
22051_RTLENTRYF
22052#endif
22053prof_total_cmp(s1, s2)
22054 const void *s1;
22055 const void *s2;
22056{
22057 ufunc_T *p1, *p2;
22058
22059 p1 = *(ufunc_T **)s1;
22060 p2 = *(ufunc_T **)s2;
22061 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22062}
22063
22064/*
22065 * Compare function for self time sorting.
22066 */
22067 static int
22068#ifdef __BORLANDC__
22069_RTLENTRYF
22070#endif
22071prof_self_cmp(s1, s2)
22072 const void *s1;
22073 const void *s2;
22074{
22075 ufunc_T *p1, *p2;
22076
22077 p1 = *(ufunc_T **)s1;
22078 p2 = *(ufunc_T **)s2;
22079 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22080}
22081
Bram Moolenaar05159a02005-02-26 23:04:13 +000022082#endif
22083
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022084/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022085 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022086 * Return TRUE if a package was loaded.
22087 */
22088 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022089script_autoload(name, reload)
22090 char_u *name;
22091 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022092{
22093 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022094 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022095 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022096 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022097
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022098 /* Return quickly when autoload disabled. */
22099 if (no_autoload)
22100 return FALSE;
22101
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022102 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022103 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022104 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022105 return FALSE;
22106
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022107 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022108
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022109 /* Find the name in the list of previously loaded package names. Skip
22110 * "autoload/", it's always the same. */
22111 for (i = 0; i < ga_loaded.ga_len; ++i)
22112 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22113 break;
22114 if (!reload && i < ga_loaded.ga_len)
22115 ret = FALSE; /* was loaded already */
22116 else
22117 {
22118 /* Remember the name if it wasn't loaded already. */
22119 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22120 {
22121 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22122 tofree = NULL;
22123 }
22124
22125 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022126 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022127 ret = TRUE;
22128 }
22129
22130 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022131 return ret;
22132}
22133
22134/*
22135 * Return the autoload script name for a function or variable name.
22136 * Returns NULL when out of memory.
22137 */
22138 static char_u *
22139autoload_name(name)
22140 char_u *name;
22141{
22142 char_u *p;
22143 char_u *scriptname;
22144
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022145 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022146 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22147 if (scriptname == NULL)
22148 return FALSE;
22149 STRCPY(scriptname, "autoload/");
22150 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022151 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022152 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022153 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022154 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022155 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022156}
22157
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22159
22160/*
22161 * Function given to ExpandGeneric() to obtain the list of user defined
22162 * function names.
22163 */
22164 char_u *
22165get_user_func_name(xp, idx)
22166 expand_T *xp;
22167 int idx;
22168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022169 static long_u done;
22170 static hashitem_T *hi;
22171 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172
22173 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022175 done = 0;
22176 hi = func_hashtab.ht_array;
22177 }
22178 if (done < func_hashtab.ht_used)
22179 {
22180 if (done++ > 0)
22181 ++hi;
22182 while (HASHITEM_EMPTY(hi))
22183 ++hi;
22184 fp = HI2UF(hi);
22185
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022186 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022187 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022189 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22190 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191
22192 cat_func_name(IObuff, fp);
22193 if (xp->xp_context != EXPAND_USER_FUNC)
22194 {
22195 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022196 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 STRCAT(IObuff, ")");
22198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 return IObuff;
22200 }
22201 return NULL;
22202}
22203
22204#endif /* FEAT_CMDL_COMPL */
22205
22206/*
22207 * Copy the function name of "fp" to buffer "buf".
22208 * "buf" must be able to hold the function name plus three bytes.
22209 * Takes care of script-local function names.
22210 */
22211 static void
22212cat_func_name(buf, fp)
22213 char_u *buf;
22214 ufunc_T *fp;
22215{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022216 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 {
22218 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022219 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 }
22221 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022222 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223}
22224
22225/*
22226 * ":delfunction {name}"
22227 */
22228 void
22229ex_delfunction(eap)
22230 exarg_T *eap;
22231{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022232 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 char_u *p;
22234 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022235 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236
22237 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022238 name = trans_function_name(&p, eap->skip, 0, &fudi);
22239 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022241 {
22242 if (fudi.fd_dict != NULL && !eap->skip)
22243 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 if (!ends_excmd(*skipwhite(p)))
22247 {
22248 vim_free(name);
22249 EMSG(_(e_trailing));
22250 return;
22251 }
22252 eap->nextcmd = check_nextcmd(p);
22253 if (eap->nextcmd != NULL)
22254 *p = NUL;
22255
22256 if (!eap->skip)
22257 fp = find_func(name);
22258 vim_free(name);
22259
22260 if (!eap->skip)
22261 {
22262 if (fp == NULL)
22263 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022264 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 return;
22266 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022267 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 {
22269 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22270 return;
22271 }
22272
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022273 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022275 /* Delete the dict item that refers to the function, it will
22276 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022277 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022279 else
22280 func_free(fp);
22281 }
22282}
22283
22284/*
22285 * Free a function and remove it from the list of functions.
22286 */
22287 static void
22288func_free(fp)
22289 ufunc_T *fp;
22290{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022291 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022292
22293 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022294 ga_clear_strings(&(fp->uf_args));
22295 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022296#ifdef FEAT_PROFILE
22297 vim_free(fp->uf_tml_count);
22298 vim_free(fp->uf_tml_total);
22299 vim_free(fp->uf_tml_self);
22300#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022301
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022302 /* remove the function from the function hashtable */
22303 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22304 if (HASHITEM_EMPTY(hi))
22305 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 hash_remove(&func_hashtab, hi);
22308
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022309 vim_free(fp);
22310}
22311
22312/*
22313 * Unreference a Function: decrement the reference count and free it when it
22314 * becomes zero. Only for numbered functions.
22315 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022316 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022317func_unref(name)
22318 char_u *name;
22319{
22320 ufunc_T *fp;
22321
22322 if (name != NULL && isdigit(*name))
22323 {
22324 fp = find_func(name);
22325 if (fp == NULL)
22326 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022327 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022328 {
22329 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022330 * when "uf_calls" becomes zero. */
22331 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 func_free(fp);
22333 }
22334 }
22335}
22336
22337/*
22338 * Count a reference to a Function.
22339 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022340 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022341func_ref(name)
22342 char_u *name;
22343{
22344 ufunc_T *fp;
22345
22346 if (name != NULL && isdigit(*name))
22347 {
22348 fp = find_func(name);
22349 if (fp == NULL)
22350 EMSG2(_(e_intern2), "func_ref()");
22351 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022352 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 }
22354}
22355
22356/*
22357 * Call a user function.
22358 */
22359 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022360call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 ufunc_T *fp; /* pointer to function */
22362 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022363 typval_T *argvars; /* arguments */
22364 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 linenr_T firstline; /* first line of range */
22366 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022367 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368{
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 char_u *save_sourcing_name;
22370 linenr_T save_sourcing_lnum;
22371 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022372 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022373 int save_did_emsg;
22374 static int depth = 0;
22375 dictitem_T *v;
22376 int fixvar_idx = 0; /* index in fixvar[] */
22377 int i;
22378 int ai;
22379 char_u numbuf[NUMBUFLEN];
22380 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022381#ifdef FEAT_PROFILE
22382 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022383 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385
22386 /* If depth of calling is getting too high, don't execute the function */
22387 if (depth >= p_mfd)
22388 {
22389 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022390 rettv->v_type = VAR_NUMBER;
22391 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 return;
22393 }
22394 ++depth;
22395
22396 line_breakcheck(); /* check for CTRL-C hit */
22397
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022398 fc = (funccall_T *)alloc(sizeof(funccall_T));
22399 fc->caller = current_funccal;
22400 current_funccal = fc;
22401 fc->func = fp;
22402 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022403 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022404 fc->linenr = 0;
22405 fc->returned = FALSE;
22406 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022408 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22409 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022412 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22414 * each argument variable and saves a lot of time.
22415 */
22416 /*
22417 * Init l: variables.
22418 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022419 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022420 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022421 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022422 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22423 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022424 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022425 name = v->di_key;
22426 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022428 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022430 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022431 v->di_tv.vval.v_dict = selfdict;
22432 ++selfdict->dv_refcount;
22433 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022434
Bram Moolenaar33570922005-01-25 22:26:29 +000022435 /*
22436 * Init a: variables.
22437 * Set a:0 to "argcount".
22438 * Set a:000 to a list with room for the "..." arguments.
22439 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022440 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022441 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022442 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022443 /* Use "name" to avoid a warning from some compiler that checks the
22444 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022445 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022446 name = v->di_key;
22447 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022449 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022451 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022452 v->di_tv.vval.v_list = &fc->l_varlist;
22453 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22454 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22455 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022456
22457 /*
22458 * Set a:firstline to "firstline" and a:lastline to "lastline".
22459 * Set a:name to named arguments.
22460 * Set a:N to the "..." arguments.
22461 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022462 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022463 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022464 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 (varnumber_T)lastline);
22466 for (i = 0; i < argcount; ++i)
22467 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022468 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022469 if (ai < 0)
22470 /* named argument a:name */
22471 name = FUNCARG(fp, i);
22472 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022473 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022474 /* "..." argument a:1, a:2, etc. */
22475 sprintf((char *)numbuf, "%d", ai + 1);
22476 name = numbuf;
22477 }
22478 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22479 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022480 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022481 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22482 }
22483 else
22484 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022485 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22486 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 if (v == NULL)
22488 break;
22489 v->di_flags = DI_FLAGS_RO;
22490 }
22491 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022492 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022493
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022494 /* Note: the values are copied directly to avoid alloc/free.
22495 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022497 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022498
22499 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22500 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022501 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22502 fc->l_listitems[ai].li_tv = argvars[i];
22503 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022504 }
22505 }
22506
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 /* Don't redraw while executing the function. */
22508 ++RedrawingDisabled;
22509 save_sourcing_name = sourcing_name;
22510 save_sourcing_lnum = sourcing_lnum;
22511 sourcing_lnum = 1;
22512 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022513 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 if (sourcing_name != NULL)
22515 {
22516 if (save_sourcing_name != NULL
22517 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22518 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22519 else
22520 STRCPY(sourcing_name, "function ");
22521 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22522
22523 if (p_verbose >= 12)
22524 {
22525 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022526 verbose_enter_scroll();
22527
Bram Moolenaar555b2802005-05-19 21:08:39 +000022528 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 if (p_verbose >= 14)
22530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022532 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022533 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022534 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535
22536 msg_puts((char_u *)"(");
22537 for (i = 0; i < argcount; ++i)
22538 {
22539 if (i > 0)
22540 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022541 if (argvars[i].v_type == VAR_NUMBER)
22542 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 else
22544 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022545 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22546 if (s != NULL)
22547 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022548 if (vim_strsize(s) > MSG_BUF_CLEN)
22549 {
22550 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22551 s = buf;
22552 }
22553 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022554 vim_free(tofree);
22555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 }
22557 }
22558 msg_puts((char_u *)")");
22559 }
22560 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022561
22562 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 --no_wait_return;
22564 }
22565 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022566#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022567 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022568 {
22569 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22570 func_do_profile(fp);
22571 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022572 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022573 {
22574 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022575 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022576 profile_zero(&fp->uf_tm_children);
22577 }
22578 script_prof_save(&wait_start);
22579 }
22580#endif
22581
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022583 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 save_did_emsg = did_emsg;
22585 did_emsg = FALSE;
22586
22587 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022588 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22590
22591 --RedrawingDisabled;
22592
22593 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022594 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022596 clear_tv(rettv);
22597 rettv->v_type = VAR_NUMBER;
22598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 }
22600
Bram Moolenaar05159a02005-02-26 23:04:13 +000022601#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022602 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022603 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022604 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022605 profile_end(&call_start);
22606 profile_sub_wait(&wait_start, &call_start);
22607 profile_add(&fp->uf_tm_total, &call_start);
22608 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022609 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022610 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022611 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22612 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022613 }
22614 }
22615#endif
22616
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 /* when being verbose, mention the return value */
22618 if (p_verbose >= 12)
22619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022621 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022624 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022625 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022626 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022627 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022630 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022631 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022632 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022633 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022634
Bram Moolenaar555b2802005-05-19 21:08:39 +000022635 /* The value may be very long. Skip the middle part, so that we
22636 * have some idea how it starts and ends. smsg() would always
22637 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022638 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022639 if (s != NULL)
22640 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022641 if (vim_strsize(s) > MSG_BUF_CLEN)
22642 {
22643 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22644 s = buf;
22645 }
22646 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022647 vim_free(tofree);
22648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 }
22650 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022651
22652 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 --no_wait_return;
22654 }
22655
22656 vim_free(sourcing_name);
22657 sourcing_name = save_sourcing_name;
22658 sourcing_lnum = save_sourcing_lnum;
22659 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022660#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022661 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022662 script_prof_restore(&wait_start);
22663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664
22665 if (p_verbose >= 12 && sourcing_name != NULL)
22666 {
22667 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022668 verbose_enter_scroll();
22669
Bram Moolenaar555b2802005-05-19 21:08:39 +000022670 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022672
22673 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 --no_wait_return;
22675 }
22676
22677 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022678 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022680
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022681 /* If the a:000 list and the l: and a: dicts are not referenced we can
22682 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022683 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22684 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22685 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22686 {
22687 free_funccal(fc, FALSE);
22688 }
22689 else
22690 {
22691 hashitem_T *hi;
22692 listitem_T *li;
22693 int todo;
22694
22695 /* "fc" is still in use. This can happen when returning "a:000" or
22696 * assigning "l:" to a global variable.
22697 * Link "fc" in the list for garbage collection later. */
22698 fc->caller = previous_funccal;
22699 previous_funccal = fc;
22700
22701 /* Make a copy of the a: variables, since we didn't do that above. */
22702 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22703 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22704 {
22705 if (!HASHITEM_EMPTY(hi))
22706 {
22707 --todo;
22708 v = HI2DI(hi);
22709 copy_tv(&v->di_tv, &v->di_tv);
22710 }
22711 }
22712
22713 /* Make a copy of the a:000 items, since we didn't do that above. */
22714 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22715 copy_tv(&li->li_tv, &li->li_tv);
22716 }
22717}
22718
22719/*
22720 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022721 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022722 */
22723 static int
22724can_free_funccal(fc, copyID)
22725 funccall_T *fc;
22726 int copyID;
22727{
22728 return (fc->l_varlist.lv_copyID != copyID
22729 && fc->l_vars.dv_copyID != copyID
22730 && fc->l_avars.dv_copyID != copyID);
22731}
22732
22733/*
22734 * Free "fc" and what it contains.
22735 */
22736 static void
22737free_funccal(fc, free_val)
22738 funccall_T *fc;
22739 int free_val; /* a: vars were allocated */
22740{
22741 listitem_T *li;
22742
22743 /* The a: variables typevals may not have been allocated, only free the
22744 * allocated variables. */
22745 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22746
22747 /* free all l: variables */
22748 vars_clear(&fc->l_vars.dv_hashtab);
22749
22750 /* Free the a:000 variables if they were allocated. */
22751 if (free_val)
22752 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22753 clear_tv(&li->li_tv);
22754
22755 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756}
22757
22758/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 * Add a number variable "name" to dict "dp" with value "nr".
22760 */
22761 static void
22762add_nr_var(dp, v, name, nr)
22763 dict_T *dp;
22764 dictitem_T *v;
22765 char *name;
22766 varnumber_T nr;
22767{
22768 STRCPY(v->di_key, name);
22769 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22770 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22771 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022772 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022773 v->di_tv.vval.v_number = nr;
22774}
22775
22776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 * ":return [expr]"
22778 */
22779 void
22780ex_return(eap)
22781 exarg_T *eap;
22782{
22783 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 int returning = FALSE;
22786
22787 if (current_funccal == NULL)
22788 {
22789 EMSG(_("E133: :return not inside a function"));
22790 return;
22791 }
22792
22793 if (eap->skip)
22794 ++emsg_skip;
22795
22796 eap->nextcmd = NULL;
22797 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022798 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 {
22800 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022801 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022803 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 }
22805 /* It's safer to return also on error. */
22806 else if (!eap->skip)
22807 {
22808 /*
22809 * Return unless the expression evaluation has been cancelled due to an
22810 * aborting error, an interrupt, or an exception.
22811 */
22812 if (!aborting())
22813 returning = do_return(eap, FALSE, TRUE, NULL);
22814 }
22815
22816 /* When skipping or the return gets pending, advance to the next command
22817 * in this line (!returning). Otherwise, ignore the rest of the line.
22818 * Following lines will be ignored by get_func_line(). */
22819 if (returning)
22820 eap->nextcmd = NULL;
22821 else if (eap->nextcmd == NULL) /* no argument */
22822 eap->nextcmd = check_nextcmd(arg);
22823
22824 if (eap->skip)
22825 --emsg_skip;
22826}
22827
22828/*
22829 * Return from a function. Possibly makes the return pending. Also called
22830 * for a pending return at the ":endtry" or after returning from an extra
22831 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022833 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 * FALSE when the return gets pending.
22835 */
22836 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022837do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 exarg_T *eap;
22839 int reanimate;
22840 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022841 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842{
22843 int idx;
22844 struct condstack *cstack = eap->cstack;
22845
22846 if (reanimate)
22847 /* Undo the return. */
22848 current_funccal->returned = FALSE;
22849
22850 /*
22851 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22852 * not in its finally clause (which then is to be executed next) is found.
22853 * In this case, make the ":return" pending for execution at the ":endtry".
22854 * Otherwise, return normally.
22855 */
22856 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22857 if (idx >= 0)
22858 {
22859 cstack->cs_pending[idx] = CSTP_RETURN;
22860
22861 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022862 /* A pending return again gets pending. "rettv" points to an
22863 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022865 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 else
22867 {
22868 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022869 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022871 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022873 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 {
22875 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022876 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 else
22879 EMSG(_(e_outofmem));
22880 }
22881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022882 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883
22884 if (reanimate)
22885 {
22886 /* The pending return value could be overwritten by a ":return"
22887 * without argument in a finally clause; reset the default
22888 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022889 current_funccal->rettv->v_type = VAR_NUMBER;
22890 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 }
22892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022893 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 }
22895 else
22896 {
22897 current_funccal->returned = TRUE;
22898
22899 /* If the return is carried out now, store the return value. For
22900 * a return immediately after reanimation, the value is already
22901 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022902 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022904 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022905 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022907 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 }
22909 }
22910
22911 return idx < 0;
22912}
22913
22914/*
22915 * Free the variable with a pending return value.
22916 */
22917 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022918discard_pending_return(rettv)
22919 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920{
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922}
22923
22924/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022925 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 * is an allocated string. Used by report_pending() for verbose messages.
22927 */
22928 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022929get_return_cmd(rettv)
22930 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022932 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022933 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022934 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022936 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022937 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022938 if (s == NULL)
22939 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022940
22941 STRCPY(IObuff, ":return ");
22942 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22943 if (STRLEN(s) + 8 >= IOSIZE)
22944 STRCPY(IObuff + IOSIZE - 4, "...");
22945 vim_free(tofree);
22946 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947}
22948
22949/*
22950 * Get next function line.
22951 * Called by do_cmdline() to get the next line.
22952 * Returns allocated string, or NULL for end of function.
22953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 char_u *
22955get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022956 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022958 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959{
Bram Moolenaar33570922005-01-25 22:26:29 +000022960 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022961 ufunc_T *fp = fcp->func;
22962 char_u *retval;
22963 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964
22965 /* If breakpoints have been added/deleted need to check for it. */
22966 if (fcp->dbg_tick != debug_tick)
22967 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022968 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 sourcing_lnum);
22970 fcp->dbg_tick = debug_tick;
22971 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022972#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022973 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022974 func_line_end(cookie);
22975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976
Bram Moolenaar05159a02005-02-26 23:04:13 +000022977 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022978 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22979 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 retval = NULL;
22981 else
22982 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022983 /* Skip NULL lines (continuation lines). */
22984 while (fcp->linenr < gap->ga_len
22985 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22986 ++fcp->linenr;
22987 if (fcp->linenr >= gap->ga_len)
22988 retval = NULL;
22989 else
22990 {
22991 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22992 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022993#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022994 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022995 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022996#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 }
22999
23000 /* Did we encounter a breakpoint? */
23001 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23002 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023003 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023005 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 sourcing_lnum);
23007 fcp->dbg_tick = debug_tick;
23008 }
23009
23010 return retval;
23011}
23012
Bram Moolenaar05159a02005-02-26 23:04:13 +000023013#if defined(FEAT_PROFILE) || defined(PROTO)
23014/*
23015 * Called when starting to read a function line.
23016 * "sourcing_lnum" must be correct!
23017 * When skipping lines it may not actually be executed, but we won't find out
23018 * until later and we need to store the time now.
23019 */
23020 void
23021func_line_start(cookie)
23022 void *cookie;
23023{
23024 funccall_T *fcp = (funccall_T *)cookie;
23025 ufunc_T *fp = fcp->func;
23026
23027 if (fp->uf_profiling && sourcing_lnum >= 1
23028 && sourcing_lnum <= fp->uf_lines.ga_len)
23029 {
23030 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023031 /* Skip continuation lines. */
23032 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23033 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023034 fp->uf_tml_execed = FALSE;
23035 profile_start(&fp->uf_tml_start);
23036 profile_zero(&fp->uf_tml_children);
23037 profile_get_wait(&fp->uf_tml_wait);
23038 }
23039}
23040
23041/*
23042 * Called when actually executing a function line.
23043 */
23044 void
23045func_line_exec(cookie)
23046 void *cookie;
23047{
23048 funccall_T *fcp = (funccall_T *)cookie;
23049 ufunc_T *fp = fcp->func;
23050
23051 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23052 fp->uf_tml_execed = TRUE;
23053}
23054
23055/*
23056 * Called when done with a function line.
23057 */
23058 void
23059func_line_end(cookie)
23060 void *cookie;
23061{
23062 funccall_T *fcp = (funccall_T *)cookie;
23063 ufunc_T *fp = fcp->func;
23064
23065 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23066 {
23067 if (fp->uf_tml_execed)
23068 {
23069 ++fp->uf_tml_count[fp->uf_tml_idx];
23070 profile_end(&fp->uf_tml_start);
23071 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023072 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023073 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23074 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023075 }
23076 fp->uf_tml_idx = -1;
23077 }
23078}
23079#endif
23080
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081/*
23082 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023083 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 */
23085 int
23086func_has_ended(cookie)
23087 void *cookie;
23088{
Bram Moolenaar33570922005-01-25 22:26:29 +000023089 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090
23091 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23092 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023093 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 || fcp->returned);
23095}
23096
23097/*
23098 * return TRUE if cookie indicates a function which "abort"s on errors.
23099 */
23100 int
23101func_has_abort(cookie)
23102 void *cookie;
23103{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023104 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105}
23106
23107#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23108typedef enum
23109{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023110 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23111 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23112 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113} var_flavour_T;
23114
23115static var_flavour_T var_flavour __ARGS((char_u *varname));
23116
23117 static var_flavour_T
23118var_flavour(varname)
23119 char_u *varname;
23120{
23121 char_u *p = varname;
23122
23123 if (ASCII_ISUPPER(*p))
23124 {
23125 while (*(++p))
23126 if (ASCII_ISLOWER(*p))
23127 return VAR_FLAVOUR_SESSION;
23128 return VAR_FLAVOUR_VIMINFO;
23129 }
23130 else
23131 return VAR_FLAVOUR_DEFAULT;
23132}
23133#endif
23134
23135#if defined(FEAT_VIMINFO) || defined(PROTO)
23136/*
23137 * Restore global vars that start with a capital from the viminfo file
23138 */
23139 int
23140read_viminfo_varlist(virp, writing)
23141 vir_T *virp;
23142 int writing;
23143{
23144 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023145 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023146 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147
23148 if (!writing && (find_viminfo_parameter('!') != NULL))
23149 {
23150 tab = vim_strchr(virp->vir_line + 1, '\t');
23151 if (tab != NULL)
23152 {
23153 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023154 switch (*tab)
23155 {
23156 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023157#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023158 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023159#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023160 case 'D': type = VAR_DICT; break;
23161 case 'L': type = VAR_LIST; break;
23162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163
23164 tab = vim_strchr(tab, '\t');
23165 if (tab != NULL)
23166 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023167 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023168 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023169 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023171#ifdef FEAT_FLOAT
23172 else if (type == VAR_FLOAT)
23173 (void)string2float(tab + 1, &tv.vval.v_float);
23174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023176 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023177 if (type == VAR_DICT || type == VAR_LIST)
23178 {
23179 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23180
23181 if (etv == NULL)
23182 /* Failed to parse back the dict or list, use it as a
23183 * string. */
23184 tv.v_type = VAR_STRING;
23185 else
23186 {
23187 vim_free(tv.vval.v_string);
23188 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023189 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023190 }
23191 }
23192
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023193 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023194
23195 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023196 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023197 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23198 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 }
23200 }
23201 }
23202
23203 return viminfo_readline(virp);
23204}
23205
23206/*
23207 * Write global vars that start with a capital to the viminfo file
23208 */
23209 void
23210write_viminfo_varlist(fp)
23211 FILE *fp;
23212{
Bram Moolenaar33570922005-01-25 22:26:29 +000023213 hashitem_T *hi;
23214 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023215 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023216 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023217 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023218 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023219 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220
23221 if (find_viminfo_parameter('!') == NULL)
23222 return;
23223
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023224 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023225
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023226 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023229 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023231 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023232 this_var = HI2DI(hi);
23233 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023236 {
23237 case VAR_STRING: s = "STR"; break;
23238 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023239#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023240 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023241#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023242 case VAR_DICT: s = "DIC"; break;
23243 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023244 default: continue;
23245 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023247 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023248 if (p != NULL)
23249 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023250 vim_free(tofree);
23251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 }
23253 }
23254}
23255#endif
23256
23257#if defined(FEAT_SESSION) || defined(PROTO)
23258 int
23259store_session_globals(fd)
23260 FILE *fd;
23261{
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 hashitem_T *hi;
23263 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023264 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 char_u *p, *t;
23266
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023267 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023268 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023270 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023272 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023273 this_var = HI2DI(hi);
23274 if ((this_var->di_tv.v_type == VAR_NUMBER
23275 || this_var->di_tv.v_type == VAR_STRING)
23276 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023277 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023278 /* Escape special characters with a backslash. Turn a LF and
23279 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023280 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023281 (char_u *)"\\\"\n\r");
23282 if (p == NULL) /* out of memory */
23283 break;
23284 for (t = p; *t != NUL; ++t)
23285 if (*t == '\n')
23286 *t = 'n';
23287 else if (*t == '\r')
23288 *t = 'r';
23289 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023290 this_var->di_key,
23291 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23292 : ' ',
23293 p,
23294 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23295 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023296 || put_eol(fd) == FAIL)
23297 {
23298 vim_free(p);
23299 return FAIL;
23300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 vim_free(p);
23302 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023303#ifdef FEAT_FLOAT
23304 else if (this_var->di_tv.v_type == VAR_FLOAT
23305 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23306 {
23307 float_T f = this_var->di_tv.vval.v_float;
23308 int sign = ' ';
23309
23310 if (f < 0)
23311 {
23312 f = -f;
23313 sign = '-';
23314 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023315 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023316 this_var->di_key, sign, f) < 0)
23317 || put_eol(fd) == FAIL)
23318 return FAIL;
23319 }
23320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 }
23322 }
23323 return OK;
23324}
23325#endif
23326
Bram Moolenaar661b1822005-07-28 22:36:45 +000023327/*
23328 * Display script name where an item was last set.
23329 * Should only be invoked when 'verbose' is non-zero.
23330 */
23331 void
23332last_set_msg(scriptID)
23333 scid_T scriptID;
23334{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023335 char_u *p;
23336
Bram Moolenaar661b1822005-07-28 22:36:45 +000023337 if (scriptID != 0)
23338 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023339 p = home_replace_save(NULL, get_scriptname(scriptID));
23340 if (p != NULL)
23341 {
23342 verbose_enter();
23343 MSG_PUTS(_("\n\tLast set from "));
23344 MSG_PUTS(p);
23345 vim_free(p);
23346 verbose_leave();
23347 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023348 }
23349}
23350
Bram Moolenaard812df62008-11-09 12:46:09 +000023351/*
23352 * List v:oldfiles in a nice way.
23353 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023354 void
23355ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023356 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023357{
23358 list_T *l = vimvars[VV_OLDFILES].vv_list;
23359 listitem_T *li;
23360 int nr = 0;
23361
23362 if (l == NULL)
23363 msg((char_u *)_("No old files"));
23364 else
23365 {
23366 msg_start();
23367 msg_scroll = TRUE;
23368 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23369 {
23370 msg_outnum((long)++nr);
23371 MSG_PUTS(": ");
23372 msg_outtrans(get_tv_string(&li->li_tv));
23373 msg_putchar('\n');
23374 out_flush(); /* output one line at a time */
23375 ui_breakcheck();
23376 }
23377 /* Assume "got_int" was set to truncate the listing. */
23378 got_int = FALSE;
23379
23380#ifdef FEAT_BROWSE_CMD
23381 if (cmdmod.browse)
23382 {
23383 quit_more = FALSE;
23384 nr = prompt_for_number(FALSE);
23385 msg_starthere();
23386 if (nr > 0)
23387 {
23388 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23389 (long)nr);
23390
23391 if (p != NULL)
23392 {
23393 p = expand_env_save(p);
23394 eap->arg = p;
23395 eap->cmdidx = CMD_edit;
23396 cmdmod.browse = FALSE;
23397 do_exedit(eap, NULL);
23398 vim_free(p);
23399 }
23400 }
23401 }
23402#endif
23403 }
23404}
23405
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406#endif /* FEAT_EVAL */
23407
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023409#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410
23411#ifdef WIN3264
23412/*
23413 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23414 */
23415static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23416static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23417static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23418
23419/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023420 * Get the short path (8.3) for the filename in "fnamep".
23421 * Only works for a valid file name.
23422 * When the path gets longer "fnamep" is changed and the allocated buffer
23423 * is put in "bufp".
23424 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23425 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 */
23427 static int
23428get_short_pathname(fnamep, bufp, fnamelen)
23429 char_u **fnamep;
23430 char_u **bufp;
23431 int *fnamelen;
23432{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023433 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 char_u *newbuf;
23435
23436 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 l = GetShortPathName(*fnamep, *fnamep, len);
23438 if (l > len - 1)
23439 {
23440 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023441 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 newbuf = vim_strnsave(*fnamep, l);
23443 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445
23446 vim_free(*bufp);
23447 *fnamep = *bufp = newbuf;
23448
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023449 /* Really should always succeed, as the buffer is big enough. */
23450 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 }
23452
23453 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023454 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455}
23456
23457/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023458 * Get the short path (8.3) for the filename in "fname". The converted
23459 * path is returned in "bufp".
23460 *
23461 * Some of the directories specified in "fname" may not exist. This function
23462 * will shorten the existing directories at the beginning of the path and then
23463 * append the remaining non-existing path.
23464 *
23465 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023466 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023467 * bufp - Pointer to an allocated buffer for the filename.
23468 * fnamelen - Length of the filename pointed to by fname
23469 *
23470 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 */
23472 static int
23473shortpath_for_invalid_fname(fname, bufp, fnamelen)
23474 char_u **fname;
23475 char_u **bufp;
23476 int *fnamelen;
23477{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023478 char_u *short_fname, *save_fname, *pbuf_unused;
23479 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023481 int old_len, len;
23482 int new_len, sfx_len;
23483 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484
23485 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023486 old_len = *fnamelen;
23487 save_fname = vim_strnsave(*fname, old_len);
23488 pbuf_unused = NULL;
23489 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023491 endp = save_fname + old_len - 1; /* Find the end of the copy */
23492 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023494 /*
23495 * Try shortening the supplied path till it succeeds by removing one
23496 * directory at a time from the tail of the path.
23497 */
23498 len = 0;
23499 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023501 /* go back one path-separator */
23502 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23503 --endp;
23504 if (endp <= save_fname)
23505 break; /* processed the complete path */
23506
23507 /*
23508 * Replace the path separator with a NUL and try to shorten the
23509 * resulting path.
23510 */
23511 ch = *endp;
23512 *endp = 0;
23513 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023514 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023515 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23516 {
23517 retval = FAIL;
23518 goto theend;
23519 }
23520 *endp = ch; /* preserve the string */
23521
23522 if (len > 0)
23523 break; /* successfully shortened the path */
23524
23525 /* failed to shorten the path. Skip the path separator */
23526 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 }
23528
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023529 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023531 /*
23532 * Succeeded in shortening the path. Now concatenate the shortened
23533 * path with the remaining path at the tail.
23534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023536 /* Compute the length of the new path. */
23537 sfx_len = (int)(save_endp - endp) + 1;
23538 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023540 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023542 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023544 /* There is not enough space in the currently allocated string,
23545 * copy it to a buffer big enough. */
23546 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023548 {
23549 retval = FAIL;
23550 goto theend;
23551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 }
23553 else
23554 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023555 /* Transfer short_fname to the main buffer (it's big enough),
23556 * unless get_short_pathname() did its work in-place. */
23557 *fname = *bufp = save_fname;
23558 if (short_fname != save_fname)
23559 vim_strncpy(save_fname, short_fname, len);
23560 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023562
23563 /* concat the not-shortened part of the path */
23564 vim_strncpy(*fname + len, endp, sfx_len);
23565 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023567
23568theend:
23569 vim_free(pbuf_unused);
23570 vim_free(save_fname);
23571
23572 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573}
23574
23575/*
23576 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023577 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 */
23579 static int
23580shortpath_for_partial(fnamep, bufp, fnamelen)
23581 char_u **fnamep;
23582 char_u **bufp;
23583 int *fnamelen;
23584{
23585 int sepcount, len, tflen;
23586 char_u *p;
23587 char_u *pbuf, *tfname;
23588 int hasTilde;
23589
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023590 /* Count up the path separators from the RHS.. so we know which part
23591 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023593 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 if (vim_ispathsep(*p))
23595 ++sepcount;
23596
23597 /* Need full path first (use expand_env() to remove a "~/") */
23598 hasTilde = (**fnamep == '~');
23599 if (hasTilde)
23600 pbuf = tfname = expand_env_save(*fnamep);
23601 else
23602 pbuf = tfname = FullName_save(*fnamep, FALSE);
23603
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023604 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023606 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608
23609 if (len == 0)
23610 {
23611 /* Don't have a valid filename, so shorten the rest of the
23612 * path if we can. This CAN give us invalid 8.3 filenames, but
23613 * there's not a lot of point in guessing what it might be.
23614 */
23615 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023616 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23617 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 }
23619
23620 /* Count the paths backward to find the beginning of the desired string. */
23621 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023622 {
23623#ifdef FEAT_MBYTE
23624 if (has_mbyte)
23625 p -= mb_head_off(tfname, p);
23626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 if (vim_ispathsep(*p))
23628 {
23629 if (sepcount == 0 || (hasTilde && sepcount == 1))
23630 break;
23631 else
23632 sepcount --;
23633 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 if (hasTilde)
23636 {
23637 --p;
23638 if (p >= tfname)
23639 *p = '~';
23640 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 }
23643 else
23644 ++p;
23645
23646 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23647 vim_free(*bufp);
23648 *fnamelen = (int)STRLEN(p);
23649 *bufp = pbuf;
23650 *fnamep = p;
23651
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023652 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653}
23654#endif /* WIN3264 */
23655
23656/*
23657 * Adjust a filename, according to a string of modifiers.
23658 * *fnamep must be NUL terminated when called. When returning, the length is
23659 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023660 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 * When there is an error, *fnamep is set to NULL.
23662 */
23663 int
23664modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23665 char_u *src; /* string with modifiers */
23666 int *usedlen; /* characters after src that are used */
23667 char_u **fnamep; /* file name so far */
23668 char_u **bufp; /* buffer for allocated file name or NULL */
23669 int *fnamelen; /* length of fnamep */
23670{
23671 int valid = 0;
23672 char_u *tail;
23673 char_u *s, *p, *pbuf;
23674 char_u dirname[MAXPATHL];
23675 int c;
23676 int has_fullname = 0;
23677#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023678 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 int has_shortname = 0;
23680#endif
23681
23682repeat:
23683 /* ":p" - full path/file_name */
23684 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23685 {
23686 has_fullname = 1;
23687
23688 valid |= VALID_PATH;
23689 *usedlen += 2;
23690
23691 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23692 if ((*fnamep)[0] == '~'
23693#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23694 && ((*fnamep)[1] == '/'
23695# ifdef BACKSLASH_IN_FILENAME
23696 || (*fnamep)[1] == '\\'
23697# endif
23698 || (*fnamep)[1] == NUL)
23699
23700#endif
23701 )
23702 {
23703 *fnamep = expand_env_save(*fnamep);
23704 vim_free(*bufp); /* free any allocated file name */
23705 *bufp = *fnamep;
23706 if (*fnamep == NULL)
23707 return -1;
23708 }
23709
23710 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023711 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 {
23713 if (vim_ispathsep(*p)
23714 && p[1] == '.'
23715 && (p[2] == NUL
23716 || vim_ispathsep(p[2])
23717 || (p[2] == '.'
23718 && (p[3] == NUL || vim_ispathsep(p[3])))))
23719 break;
23720 }
23721
23722 /* FullName_save() is slow, don't use it when not needed. */
23723 if (*p != NUL || !vim_isAbsName(*fnamep))
23724 {
23725 *fnamep = FullName_save(*fnamep, *p != NUL);
23726 vim_free(*bufp); /* free any allocated file name */
23727 *bufp = *fnamep;
23728 if (*fnamep == NULL)
23729 return -1;
23730 }
23731
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023732#ifdef WIN3264
23733# if _WIN32_WINNT >= 0x0500
23734 if (vim_strchr(*fnamep, '~') != NULL)
23735 {
23736 /* Expand 8.3 filename to full path. Needed to make sure the same
23737 * file does not have two different names.
23738 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23739 p = alloc(_MAX_PATH + 1);
23740 if (p != NULL)
23741 {
23742 if (GetLongPathName(*fnamep, p, MAXPATHL))
23743 {
23744 vim_free(*bufp);
23745 *bufp = *fnamep = p;
23746 }
23747 else
23748 vim_free(p);
23749 }
23750 }
23751# endif
23752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 /* Append a path separator to a directory. */
23754 if (mch_isdir(*fnamep))
23755 {
23756 /* Make room for one or two extra characters. */
23757 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23758 vim_free(*bufp); /* free any allocated file name */
23759 *bufp = *fnamep;
23760 if (*fnamep == NULL)
23761 return -1;
23762 add_pathsep(*fnamep);
23763 }
23764 }
23765
23766 /* ":." - path relative to the current directory */
23767 /* ":~" - path relative to the home directory */
23768 /* ":8" - shortname path - postponed till after */
23769 while (src[*usedlen] == ':'
23770 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23771 {
23772 *usedlen += 2;
23773 if (c == '8')
23774 {
23775#ifdef WIN3264
23776 has_shortname = 1; /* Postpone this. */
23777#endif
23778 continue;
23779 }
23780 pbuf = NULL;
23781 /* Need full path first (use expand_env() to remove a "~/") */
23782 if (!has_fullname)
23783 {
23784 if (c == '.' && **fnamep == '~')
23785 p = pbuf = expand_env_save(*fnamep);
23786 else
23787 p = pbuf = FullName_save(*fnamep, FALSE);
23788 }
23789 else
23790 p = *fnamep;
23791
23792 has_fullname = 0;
23793
23794 if (p != NULL)
23795 {
23796 if (c == '.')
23797 {
23798 mch_dirname(dirname, MAXPATHL);
23799 s = shorten_fname(p, dirname);
23800 if (s != NULL)
23801 {
23802 *fnamep = s;
23803 if (pbuf != NULL)
23804 {
23805 vim_free(*bufp); /* free any allocated file name */
23806 *bufp = pbuf;
23807 pbuf = NULL;
23808 }
23809 }
23810 }
23811 else
23812 {
23813 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23814 /* Only replace it when it starts with '~' */
23815 if (*dirname == '~')
23816 {
23817 s = vim_strsave(dirname);
23818 if (s != NULL)
23819 {
23820 *fnamep = s;
23821 vim_free(*bufp);
23822 *bufp = s;
23823 }
23824 }
23825 }
23826 vim_free(pbuf);
23827 }
23828 }
23829
23830 tail = gettail(*fnamep);
23831 *fnamelen = (int)STRLEN(*fnamep);
23832
23833 /* ":h" - head, remove "/file_name", can be repeated */
23834 /* Don't remove the first "/" or "c:\" */
23835 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23836 {
23837 valid |= VALID_HEAD;
23838 *usedlen += 2;
23839 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023840 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023841 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 *fnamelen = (int)(tail - *fnamep);
23843#ifdef VMS
23844 if (*fnamelen > 0)
23845 *fnamelen += 1; /* the path separator is part of the path */
23846#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023847 if (*fnamelen == 0)
23848 {
23849 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23850 p = vim_strsave((char_u *)".");
23851 if (p == NULL)
23852 return -1;
23853 vim_free(*bufp);
23854 *bufp = *fnamep = tail = p;
23855 *fnamelen = 1;
23856 }
23857 else
23858 {
23859 while (tail > s && !after_pathsep(s, tail))
23860 mb_ptr_back(*fnamep, tail);
23861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 }
23863
23864 /* ":8" - shortname */
23865 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23866 {
23867 *usedlen += 2;
23868#ifdef WIN3264
23869 has_shortname = 1;
23870#endif
23871 }
23872
23873#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023874 /*
23875 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 */
23877 if (has_shortname)
23878 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023879 /* Copy the string if it is shortened by :h and when it wasn't copied
23880 * yet, because we are going to change it in place. Avoids changing
23881 * the buffer name for "%:8". */
23882 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 {
23884 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023885 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 return -1;
23887 vim_free(*bufp);
23888 *bufp = *fnamep = p;
23889 }
23890
23891 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023892 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 if (!has_fullname && !vim_isAbsName(*fnamep))
23894 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023895 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 return -1;
23897 }
23898 else
23899 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023900 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901
Bram Moolenaardc935552011-08-17 15:23:23 +020023902 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023904 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 return -1;
23906
23907 if (l == 0)
23908 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023909 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023911 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 return -1;
23913 }
23914 *fnamelen = l;
23915 }
23916 }
23917#endif /* WIN3264 */
23918
23919 /* ":t" - tail, just the basename */
23920 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23921 {
23922 *usedlen += 2;
23923 *fnamelen -= (int)(tail - *fnamep);
23924 *fnamep = tail;
23925 }
23926
23927 /* ":e" - extension, can be repeated */
23928 /* ":r" - root, without extension, can be repeated */
23929 while (src[*usedlen] == ':'
23930 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23931 {
23932 /* find a '.' in the tail:
23933 * - for second :e: before the current fname
23934 * - otherwise: The last '.'
23935 */
23936 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23937 s = *fnamep - 2;
23938 else
23939 s = *fnamep + *fnamelen - 1;
23940 for ( ; s > tail; --s)
23941 if (s[0] == '.')
23942 break;
23943 if (src[*usedlen + 1] == 'e') /* :e */
23944 {
23945 if (s > tail)
23946 {
23947 *fnamelen += (int)(*fnamep - (s + 1));
23948 *fnamep = s + 1;
23949#ifdef VMS
23950 /* cut version from the extension */
23951 s = *fnamep + *fnamelen - 1;
23952 for ( ; s > *fnamep; --s)
23953 if (s[0] == ';')
23954 break;
23955 if (s > *fnamep)
23956 *fnamelen = s - *fnamep;
23957#endif
23958 }
23959 else if (*fnamep <= tail)
23960 *fnamelen = 0;
23961 }
23962 else /* :r */
23963 {
23964 if (s > tail) /* remove one extension */
23965 *fnamelen = (int)(s - *fnamep);
23966 }
23967 *usedlen += 2;
23968 }
23969
23970 /* ":s?pat?foo?" - substitute */
23971 /* ":gs?pat?foo?" - global substitute */
23972 if (src[*usedlen] == ':'
23973 && (src[*usedlen + 1] == 's'
23974 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23975 {
23976 char_u *str;
23977 char_u *pat;
23978 char_u *sub;
23979 int sep;
23980 char_u *flags;
23981 int didit = FALSE;
23982
23983 flags = (char_u *)"";
23984 s = src + *usedlen + 2;
23985 if (src[*usedlen + 1] == 'g')
23986 {
23987 flags = (char_u *)"g";
23988 ++s;
23989 }
23990
23991 sep = *s++;
23992 if (sep)
23993 {
23994 /* find end of pattern */
23995 p = vim_strchr(s, sep);
23996 if (p != NULL)
23997 {
23998 pat = vim_strnsave(s, (int)(p - s));
23999 if (pat != NULL)
24000 {
24001 s = p + 1;
24002 /* find end of substitution */
24003 p = vim_strchr(s, sep);
24004 if (p != NULL)
24005 {
24006 sub = vim_strnsave(s, (int)(p - s));
24007 str = vim_strnsave(*fnamep, *fnamelen);
24008 if (sub != NULL && str != NULL)
24009 {
24010 *usedlen = (int)(p + 1 - src);
24011 s = do_string_sub(str, pat, sub, flags);
24012 if (s != NULL)
24013 {
24014 *fnamep = s;
24015 *fnamelen = (int)STRLEN(s);
24016 vim_free(*bufp);
24017 *bufp = s;
24018 didit = TRUE;
24019 }
24020 }
24021 vim_free(sub);
24022 vim_free(str);
24023 }
24024 vim_free(pat);
24025 }
24026 }
24027 /* after using ":s", repeat all the modifiers */
24028 if (didit)
24029 goto repeat;
24030 }
24031 }
24032
24033 return valid;
24034}
24035
24036/*
24037 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24038 * "flags" can be "g" to do a global substitute.
24039 * Returns an allocated string, NULL for error.
24040 */
24041 char_u *
24042do_string_sub(str, pat, sub, flags)
24043 char_u *str;
24044 char_u *pat;
24045 char_u *sub;
24046 char_u *flags;
24047{
24048 int sublen;
24049 regmatch_T regmatch;
24050 int i;
24051 int do_all;
24052 char_u *tail;
24053 garray_T ga;
24054 char_u *ret;
24055 char_u *save_cpo;
24056
24057 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24058 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024059 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060
24061 ga_init2(&ga, 1, 200);
24062
24063 do_all = (flags[0] == 'g');
24064
24065 regmatch.rm_ic = p_ic;
24066 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24067 if (regmatch.regprog != NULL)
24068 {
24069 tail = str;
24070 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24071 {
24072 /*
24073 * Get some space for a temporary buffer to do the substitution
24074 * into. It will contain:
24075 * - The text up to where the match is.
24076 * - The substituted text.
24077 * - The text after the match.
24078 */
24079 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24080 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24081 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24082 {
24083 ga_clear(&ga);
24084 break;
24085 }
24086
24087 /* copy the text up to where the match is */
24088 i = (int)(regmatch.startp[0] - tail);
24089 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24090 /* add the substituted text */
24091 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24092 + ga.ga_len + i, TRUE, TRUE, FALSE);
24093 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 /* avoid getting stuck on a match with an empty string */
24095 if (tail == regmatch.endp[0])
24096 {
24097 if (*tail == NUL)
24098 break;
24099 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24100 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 }
24102 else
24103 {
24104 tail = regmatch.endp[0];
24105 if (*tail == NUL)
24106 break;
24107 }
24108 if (!do_all)
24109 break;
24110 }
24111
24112 if (ga.ga_data != NULL)
24113 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24114
24115 vim_free(regmatch.regprog);
24116 }
24117
24118 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24119 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024120 if (p_cpo == empty_option)
24121 p_cpo = save_cpo;
24122 else
24123 /* Darn, evaluating {sub} expression changed the value. */
24124 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125
24126 return ret;
24127}
24128
24129#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */