blob: 7e51a8a71f7ffd8beec46822271159f80238418b [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 Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200117static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200126/* When using exists() don't auto-load a script. */
127static int no_autoload = FALSE;
128
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Structure to hold info for a user function.
160 */
161typedef struct ufunc ufunc_T;
162
163struct ufunc
164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000165 int uf_varargs; /* variable nr of arguments */
166 int uf_flags;
167 int uf_calls; /* nr of active calls */
168 garray_T uf_args; /* arguments */
169 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170#ifdef FEAT_PROFILE
171 int uf_profiling; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000174 proftime_T uf_tm_total; /* time spent in function + children */
175 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176 proftime_T uf_tm_children; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T *uf_tml_total; /* time spent in a line + children */
180 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tml_start; /* start time for current line */
182 proftime_T uf_tml_children; /* time spent in children for this line */
183 proftime_T uf_tml_wait; /* start wait time for current line */
184 int uf_tml_idx; /* index of line being timed; -1 if none */
185 int uf_tml_execed; /* line being timed was executed */
186#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000187 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 int uf_refcount; /* for numbered function: reference count */
190 char_u uf_name[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
192 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193};
194
195/* function flags */
196#define FC_ABORT 1 /* abort function on error */
197#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000198#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000203static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000205/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000206static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
207
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208/* list heads for garbage collection */
209static dict_T *first_dict = NULL; /* list of all dicts */
210static list_T *first_list = NULL; /* list of all lists */
211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000212/* From user function to hashitem and back. */
213static ufunc_T dumuf;
214#define UF2HIKEY(fp) ((fp)->uf_name)
215#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
217
218#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaar33570922005-01-25 22:26:29 +0000221#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222#define VAR_SHORT_LEN 20 /* short variable name length */
223#define FIXVAR_CNT 12 /* number of fixed variables */
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000226typedef struct funccall_S funccall_T;
227
228struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 ufunc_T *func; /* function being called */
231 int linenr; /* next line to be executed */
232 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000233 struct /* fixed variables for arguments */
234 {
235 dictitem_T var; /* variable (without room for name) */
236 char_u room[VAR_SHORT_LEN]; /* room for the name */
237 } fixvar[FIXVAR_CNT];
238 dict_T l_vars; /* l: local function variables */
239 dictitem_T l_vars_var; /* variable for l: scope */
240 dict_T l_avars; /* a: argument variables */
241 dictitem_T l_avars_var; /* variable for a: scope */
242 list_T l_varlist; /* list for a:000 */
243 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
244 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 linenr_T breakpoint; /* next line with breakpoint or zero */
246 int dbg_tick; /* debug_tick when breakpoint was set */
247 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000248#ifdef FEAT_PROFILE
249 proftime_T prof_child; /* time spent in a child */
250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000251 funccall_T *caller; /* calling function or NULL */
252};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255 * Info used by a ":for" loop.
256 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258{
259 int fi_semicolon; /* TRUE if ending in '; var]' */
260 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 listwatch_T fi_lw; /* keep an eye on the item used. */
262 list_T *fi_list; /* list being used */
263} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000266 * Struct used by trans_function_name()
267 */
268typedef struct
269{
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000271 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T *fd_di; /* Dictionary item used */
273} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274
Bram Moolenaara7043832005-01-21 11:56:39 +0000275
276/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
281 */
282#include "version.h"
283
284/* values for vv_flags: */
285#define VV_COMPAT 1 /* compatible, also used without "v:" */
286#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000287#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000289#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
291static struct vimvar
292{
293 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294 dictitem_T vv_di; /* value and name for key */
295 char vv_filler[16]; /* space for LONGEST name below!!! */
296 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297} vimvars[VV_LEN] =
298{
299 /*
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
302 */
303 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("count1", VAR_NUMBER), VV_RO},
305 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
306 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
307 {VV_NAME("warningmsg", VAR_STRING), 0},
308 {VV_NAME("statusmsg", VAR_STRING), 0},
309 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
311 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
313 {VV_NAME("termresponse", VAR_STRING), VV_RO},
314 {VV_NAME("fname", VAR_STRING), VV_RO},
315 {VV_NAME("lang", VAR_STRING), VV_RO},
316 {VV_NAME("lc_time", VAR_STRING), VV_RO},
317 {VV_NAME("ctype", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
319 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
320 {VV_NAME("fname_in", VAR_STRING), VV_RO},
321 {VV_NAME("fname_out", VAR_STRING), VV_RO},
322 {VV_NAME("fname_new", VAR_STRING), VV_RO},
323 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
324 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
325 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
327 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
328 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("progname", VAR_STRING), VV_RO},
330 {VV_NAME("servername", VAR_STRING), VV_RO},
331 {VV_NAME("dying", VAR_NUMBER), VV_RO},
332 {VV_NAME("exception", VAR_STRING), VV_RO},
333 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
334 {VV_NAME("register", VAR_STRING), VV_RO},
335 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
336 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000337 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
338 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000339 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000340 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
341 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000342 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000347 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000348 {VV_NAME("swapname", VAR_STRING), VV_RO},
349 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000350 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200351 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000352 {VV_NAME("mouse_win", VAR_NUMBER), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000355 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000356 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000357 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200358 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000359};
360
361/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362#define vv_type vv_di.di_tv.v_type
363#define vv_nr vv_di.di_tv.vval.v_number
364#define vv_float vv_di.di_tv.vval.v_float
365#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000366#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000368
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200369static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000370#define vimvarht vimvardict.dv_hashtab
371
Bram Moolenaara40058a2005-07-11 22:42:07 +0000372static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
373static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000374static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
375static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
376static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000377static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
378static void list_glob_vars __ARGS((int *first));
379static void list_buf_vars __ARGS((int *first));
380static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000381#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000382static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_vim_vars __ARGS((int *first));
385static void list_script_vars __ARGS((int *first));
386static void list_func_vars __ARGS((int *first));
387static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
389static int check_changedtick __ARGS((char_u *arg));
390static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
391static void clear_lval __ARGS((lval_T *lp));
392static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
393static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
395static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
396static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
397static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
398static void item_lock __ARGS((typval_T *tv, int deep, int lock));
399static int tv_islocked __ARGS((typval_T *tv));
400
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
402static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000407static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
408static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000409
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000410static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000415static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000420static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000422static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
424static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000425static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000426static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100427static 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 +0000428static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000429static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200430static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000435static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
438static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000440#ifdef FEAT_FLOAT
441static int string2float __ARGS((char_u *text, float_T *value));
442#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static 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 +0200447static 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 +0000448static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000449static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000451#ifdef FEAT_FLOAT
452static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200453static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100456static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200462static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200464static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#ifdef FEAT_FLOAT
478static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
479#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000480static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000483static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000485#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000486static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000487static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
493static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200494static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
499static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200510#ifdef FEAT_FLOAT
511static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
512#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000515static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000521#ifdef FEAT_FLOAT
522static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200524static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000525#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000526static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000527static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000535static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000537static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000543static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000551static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000552static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000553static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000554static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200557static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000558static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000566static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000580static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100585static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000587static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000599#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200600static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000601static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
602#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200603#ifdef FEAT_LUA
604static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
605#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000610static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000611static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000614static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618#ifdef vim_mkdir
619static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100622#ifdef FEAT_MZSCHEME
623static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
624#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100627static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000628static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629#ifdef FEAT_FLOAT
630static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000633static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000634static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200635#ifdef FEAT_PYTHON3
636static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
638#ifdef FEAT_PYTHON
639static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000643static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
656static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
657#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200658static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100660static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000663static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000665static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000672static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000673static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000674static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000675static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200677static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000678static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100680#ifdef FEAT_CRYPT
681static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
682#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000683static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200684static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
687static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200688static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000691static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000692static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
698#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000699static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200700static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701#ifdef HAVE_STRFTIME
702static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
704static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200710static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200711static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000717static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200718static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000720static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000721static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000723static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000724static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000725static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000726static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200727#ifdef FEAT_FLOAT
728static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000731static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#ifdef FEAT_FLOAT
735static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
736#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000737static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200738static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200739static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100743static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000750static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000753static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100754static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000756static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000757static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static int get_env_len __ARGS((char_u **arg));
759static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000761static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
762#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
763#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
764 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000765static 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 +0000766static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000767static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000768static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
769static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static typval_T *alloc_tv __ARGS((void));
771static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static void init_tv __ARGS((typval_T *varp));
773static long get_tv_number __ARGS((typval_T *varp));
774static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000775static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static char_u *get_tv_string __ARGS((typval_T *varp));
777static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000778static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200780static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
782static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
783static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000784static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
785static 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 +0000786static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
787static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000788static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200789static int var_check_func_name __ARGS((char_u *name, int new_var));
790static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000791static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000792static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
794static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
795static int eval_fname_script __ARGS((char_u *p));
796static int eval_fname_sid __ARGS((char_u *p));
797static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000798static ufunc_T *find_func __ARGS((char_u *name));
799static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000800static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801#ifdef FEAT_PROFILE
802static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000803static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
804static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
805static int
806# ifdef __BORLANDC__
807 _RTLENTRYF
808# endif
809 prof_total_cmp __ARGS((const void *s1, const void *s2));
810static int
811# ifdef __BORLANDC__
812 _RTLENTRYF
813# endif
814 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200816static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000817static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000818static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000819static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static 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 +0000821static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
822static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000824static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
825static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000826static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000827static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000828static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200830
831#ifdef EBCDIC
832static int compare_func_name __ARGS((const void *s1, const void *s2));
833static void sortFunctions __ARGS(());
834#endif
835
Bram Moolenaar33570922005-01-25 22:26:29 +0000836/*
837 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000838 */
839 void
840eval_init()
841{
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 int i;
843 struct vimvar *p;
844
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200845 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
846 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200847 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000848 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000850
851 for (i = 0; i < VV_LEN; ++i)
852 {
853 p = &vimvars[i];
854 STRCPY(p->vv_di.di_key, p->vv_name);
855 if (p->vv_flags & VV_RO)
856 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
857 else if (p->vv_flags & VV_RO_SBX)
858 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
859 else
860 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000861
862 /* add to v: scope dict, unless the value is not always available */
863 if (p->vv_type != VAR_UNKNOWN)
864 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000866 /* add to compat scope dict */
867 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000869 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200870 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200871
872#ifdef EBCDIC
873 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100874 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200875 */
876 sortFunctions();
877#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000878}
879
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000880#if defined(EXITFREE) || defined(PROTO)
881 void
882eval_clear()
883{
884 int i;
885 struct vimvar *p;
886
887 for (i = 0; i < VV_LEN; ++i)
888 {
889 p = &vimvars[i];
890 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000891 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000892 vim_free(p->vv_str);
893 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000894 }
895 else if (p->vv_di.di_tv.v_type == VAR_LIST)
896 {
897 list_unref(p->vv_list);
898 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900 }
901 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000902 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000903 hash_clear(&compat_hashtab);
904
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100906# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200907 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909
910 /* global variables */
911 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000913 /* autoloaded script names */
914 ga_clear_strings(&ga_loaded);
915
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200916 /* script-local variables */
917 for (i = 1; i <= ga_scripts.ga_len; ++i)
918 {
919 vars_clear(&SCRIPT_VARS(i));
920 vim_free(SCRIPT_SV(i));
921 }
922 ga_clear(&ga_scripts);
923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000924 /* unreferenced lists and dicts */
925 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000926
927 /* functions */
928 free_all_functions();
929 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930}
931#endif
932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Return the name of the executed function.
935 */
936 char_u *
937func_name(cookie)
938 void *cookie;
939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942
943/*
944 * Return the address holding the next breakpoint line for a funccall cookie.
945 */
946 linenr_T *
947func_breakpoint(cookie)
948 void *cookie;
949{
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951}
952
953/*
954 * Return the address holding the debug tick for a funccall cookie.
955 */
956 int *
957func_dbg_tick(cookie)
958 void *cookie;
959{
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the nesting level for a funccall cookie.
965 */
966 int
967func_level(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000974funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000976/* pointer to list of previously used funccal, still around because some
977 * item in it is still being used. */
978funccall_T *previous_funccal = NULL;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
981 * Return TRUE when a function was ended by a ":return" command.
982 */
983 int
984current_func_returned()
985{
986 return current_funccal->returned;
987}
988
989
990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * Set an internal variable to a string value. Creates the variable if it does
992 * not already exist.
993 */
994 void
995set_internal_string_var(name, value)
996 char_u *name;
997 char_u *value;
998{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 val = vim_strsave(value);
1003 if (val != NULL)
1004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012}
1013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001014static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static char_u *redir_endp = NULL;
1017static char_u *redir_varname = NULL;
1018
1019/*
1020 * Start recording command output to a variable
1021 * Returns OK if successfully completed the setup. FAIL otherwise.
1022 */
1023 int
1024var_redir_start(name, append)
1025 char_u *name;
1026 int append; /* append to an existing variable */
1027{
1028 int save_emsg;
1029 int err;
1030 typval_T tv;
1031
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001032 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001033 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 {
1035 EMSG(_(e_invarg));
1036 return FAIL;
1037 }
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 redir_varname = vim_strsave(name);
1041 if (redir_varname == NULL)
1042 return FAIL;
1043
1044 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1045 if (redir_lval == NULL)
1046 {
1047 var_redir_stop();
1048 return FAIL;
1049 }
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 /* The output is stored in growarray "redir_ga" until redirection ends. */
1052 ga_init2(&redir_ga, (int)sizeof(char), 500);
1053
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1056 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1058 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001059 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp != NULL && *redir_endp != NUL)
1061 /* Trailing characters are present after the variable name */
1062 EMSG(_(e_trailing));
1063 else
1064 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001065 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 var_redir_stop();
1067 return FAIL;
1068 }
1069
1070 /* check if we can write to the variable: set it to or append an empty
1071 * string */
1072 save_emsg = did_emsg;
1073 did_emsg = FALSE;
1074 tv.v_type = VAR_STRING;
1075 tv.vval.v_string = (char_u *)"";
1076 if (append)
1077 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1078 else
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001080 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001082 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 if (err)
1084 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 var_redir_stop();
1087 return FAIL;
1088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089
1090 return OK;
1091}
1092
1093/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 * Append "value[value_len]" to the variable set by var_redir_start().
1095 * The actual appending is postponed until redirection ends, because the value
1096 * appended may in fact be the string we write to, changing it may cause freed
1097 * memory to be used:
1098 * :redir => foo
1099 * :let foo
1100 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 */
1102 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001107 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108
1109 if (redir_lval == NULL)
1110 return;
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 {
1119 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 }
1122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124}
1125
1126/*
1127 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
1131var_redir_stop()
1132{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 typval_T tv;
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_lval != NULL)
1136 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 /* If there was no error: assign the text to the variable. */
1138 if (redir_endp != NULL)
1139 {
1140 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 /* Call get_lval() again, if it's inside a Dict or List it may
1144 * have changed. */
1145 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1146 FALSE, FALSE, FALSE, FNE_CHECK_START);
1147 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1148 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1149 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* free the collected output */
1153 vim_free(redir_ga.ga_data);
1154 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 vim_free(redir_lval);
1157 redir_lval = NULL;
1158 }
1159 vim_free(redir_varname);
1160 redir_varname = NULL;
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163# if defined(FEAT_MBYTE) || defined(PROTO)
1164 int
1165eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1166 char_u *enc_from;
1167 char_u *enc_to;
1168 char_u *fname_from;
1169 char_u *fname_to;
1170{
1171 int err = FALSE;
1172
1173 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1174 set_vim_var_string(VV_CC_TO, enc_to, -1);
1175 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1176 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1177 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1178 err = TRUE;
1179 set_vim_var_string(VV_CC_FROM, NULL, -1);
1180 set_vim_var_string(VV_CC_TO, NULL, -1);
1181 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1182 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1183
1184 if (err)
1185 return FAIL;
1186 return OK;
1187}
1188# endif
1189
1190# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1191 int
1192eval_printexpr(fname, args)
1193 char_u *fname;
1194 char_u *args;
1195{
1196 int err = FALSE;
1197
1198 set_vim_var_string(VV_FNAME_IN, fname, -1);
1199 set_vim_var_string(VV_CMDARG, args, -1);
1200 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1201 err = TRUE;
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_CMDARG, NULL, -1);
1204
1205 if (err)
1206 {
1207 mch_remove(fname);
1208 return FAIL;
1209 }
1210 return OK;
1211}
1212# endif
1213
1214# if defined(FEAT_DIFF) || defined(PROTO)
1215 void
1216eval_diff(origfile, newfile, outfile)
1217 char_u *origfile;
1218 char_u *newfile;
1219 char_u *outfile;
1220{
1221 int err = FALSE;
1222
1223 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1224 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1225 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1226 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1229 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1230}
1231
1232 void
1233eval_patch(origfile, difffile, outfile)
1234 char_u *origfile;
1235 char_u *difffile;
1236 char_u *outfile;
1237{
1238 int err;
1239
1240 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1241 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1242 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1243 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1244 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1246 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1247}
1248# endif
1249
1250/*
1251 * Top level evaluation function, returning a boolean.
1252 * Sets "error" to TRUE if there was an error.
1253 * Return TRUE or FALSE.
1254 */
1255 int
1256eval_to_bool(arg, error, nextcmd, skip)
1257 char_u *arg;
1258 int *error;
1259 char_u **nextcmd;
1260 int skip; /* only parse, don't execute */
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval = FALSE;
1264
1265 if (skip)
1266 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 else
1270 {
1271 *error = FALSE;
1272 if (!skip)
1273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001275 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 }
1278 if (skip)
1279 --emsg_skip;
1280
1281 return retval;
1282}
1283
1284/*
1285 * Top level evaluation function, returning a string. If "skip" is TRUE,
1286 * only parsing to "nextcmd" is done, without reporting errors. Return
1287 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1288 */
1289 char_u *
1290eval_to_string_skip(arg, nextcmd, skip)
1291 char_u *arg;
1292 char_u **nextcmd;
1293 int skip; /* only parse, don't execute */
1294{
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 char_u *retval;
1297
1298 if (skip)
1299 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 retval = NULL;
1302 else
1303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 retval = vim_strsave(get_tv_string(&tv));
1305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 if (skip)
1308 --emsg_skip;
1309
1310 return retval;
1311}
1312
1313/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001314 * Skip over an expression at "*pp".
1315 * Return FAIL for an error, OK otherwise.
1316 */
1317 int
1318skip_expr(pp)
1319 char_u **pp;
1320{
Bram Moolenaar33570922005-01-25 22:26:29 +00001321 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322
1323 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325}
1326
1327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329 * When "convert" is TRUE convert a List into a sequence of lines and convert
1330 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Return pointer to allocated memory, or NULL for failure.
1332 */
1333 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *arg;
1336 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001342#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 retval = NULL;
1348 else
1349 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001351 {
1352 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001353 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001354 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 if (tv.vval.v_list->lv_len > 0)
1357 ga_append(&ga, NL);
1358 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001359 ga_append(&ga, NUL);
1360 retval = (char_u *)ga.ga_data;
1361 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362#ifdef FEAT_FLOAT
1363 else if (convert && tv.v_type == VAR_FLOAT)
1364 {
1365 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1366 retval = vim_strsave(numbuf);
1367 }
1368#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 else
1370 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373
1374 return retval;
1375}
1376
1377/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 * Call eval_to_string() without using current local variables and using
1379 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 char_u *arg;
1384 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 char_u *retval;
1388 void *save_funccalp;
1389
1390 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 ++sandbox;
1393 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395 if (use_sandbox)
1396 --sandbox;
1397 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 restore_funccal(save_funccalp);
1399 return retval;
1400}
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * Top level evaluation function, returning a number.
1404 * Evaluates "expr" silently.
1405 * Returns -1 for an error.
1406 */
1407 int
1408eval_to_number(expr)
1409 char_u *expr;
1410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001413 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415 ++emsg_off;
1416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 retval = -1;
1419 else
1420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 --emsg_off;
1425
1426 return retval;
1427}
1428
Bram Moolenaara40058a2005-07-11 22:42:07 +00001429/*
1430 * Prepare v: variable "idx" to be used.
1431 * Save the current typeval in "save_tv".
1432 * When not used yet add the variable to the v: hashtable.
1433 */
1434 static void
1435prepare_vimvar(idx, save_tv)
1436 int idx;
1437 typval_T *save_tv;
1438{
1439 *save_tv = vimvars[idx].vv_tv;
1440 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1441 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1442}
1443
1444/*
1445 * Restore v: variable "idx" to typeval "save_tv".
1446 * When no longer defined, remove the variable from the v: hashtable.
1447 */
1448 static void
1449restore_vimvar(idx, save_tv)
1450 int idx;
1451 typval_T *save_tv;
1452{
1453 hashitem_T *hi;
1454
Bram Moolenaara40058a2005-07-11 22:42:07 +00001455 vimvars[idx].vv_tv = *save_tv;
1456 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1457 {
1458 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1459 if (HASHITEM_EMPTY(hi))
1460 EMSG2(_(e_intern2), "restore_vimvar()");
1461 else
1462 hash_remove(&vimvarht, hi);
1463 }
1464}
1465
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001466#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467/*
1468 * Evaluate an expression to a list with suggestions.
1469 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001470 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 */
1472 list_T *
1473eval_spell_expr(badword, expr)
1474 char_u *badword;
1475 char_u *expr;
1476{
1477 typval_T save_val;
1478 typval_T rettv;
1479 list_T *list = NULL;
1480 char_u *p = skipwhite(expr);
1481
1482 /* Set "v:val" to the bad word. */
1483 prepare_vimvar(VV_VAL, &save_val);
1484 vimvars[VV_VAL].vv_type = VAR_STRING;
1485 vimvars[VV_VAL].vv_str = badword;
1486 if (p_verbose == 0)
1487 ++emsg_off;
1488
1489 if (eval1(&p, &rettv, TRUE) == OK)
1490 {
1491 if (rettv.v_type != VAR_LIST)
1492 clear_tv(&rettv);
1493 else
1494 list = rettv.vval.v_list;
1495 }
1496
1497 if (p_verbose == 0)
1498 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 restore_vimvar(VV_VAL, &save_val);
1500
1501 return list;
1502}
1503
1504/*
1505 * "list" is supposed to contain two items: a word and a number. Return the
1506 * word in "pp" and the number as the return value.
1507 * Return -1 if anything isn't right.
1508 * Used to get the good word and score from the eval_spell_expr() result.
1509 */
1510 int
1511get_spellword(list, pp)
1512 list_T *list;
1513 char_u **pp;
1514{
1515 listitem_T *li;
1516
1517 li = list->lv_first;
1518 if (li == NULL)
1519 return -1;
1520 *pp = get_tv_string(&li->li_tv);
1521
1522 li = li->li_next;
1523 if (li == NULL)
1524 return -1;
1525 return get_tv_number(&li->li_tv);
1526}
1527#endif
1528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001530 * Top level evaluation function.
1531 * Returns an allocated typval_T with the result.
1532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 */
1534 typval_T *
1535eval_expr(arg, nextcmd)
1536 char_u *arg;
1537 char_u **nextcmd;
1538{
1539 typval_T *tv;
1540
1541 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 {
1544 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 }
1547
1548 return tv;
1549}
1550
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 * Uses argv[argc] for the function arguments. Only Number and String
1555 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001558 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001559call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 char_u *func;
1561 int argc;
1562 char_u **argv;
1563 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001564 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar33570922005-01-25 22:26:29 +00001567 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 long n;
1569 int len;
1570 int i;
1571 int doesrange;
1572 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001575 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 for (i = 0; i < argc; i++)
1580 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001581 /* Pass a NULL or empty argument as an empty string */
1582 if (argv[i] == NULL || *argv[i] == NUL)
1583 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001584 argvars[i].v_type = VAR_STRING;
1585 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001586 continue;
1587 }
1588
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589 if (str_arg_only)
1590 len = 0;
1591 else
1592 /* Recognize a number argument, the others must be strings. */
1593 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (len != 0 && len == (int)STRLEN(argv[i]))
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_NUMBER;
1597 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_STRING;
1602 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 }
1605
1606 if (safe)
1607 {
1608 save_funccalp = save_funccal();
1609 ++sandbox;
1610 }
1611
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1613 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (safe)
1617 {
1618 --sandbox;
1619 restore_funccal(save_funccalp);
1620 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 vim_free(argvars);
1622
1623 if (ret == FAIL)
1624 clear_tv(rettv);
1625
1626 return ret;
1627}
1628
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001629/*
1630 * Call vimL function "func" and return the result as a number.
1631 * Returns -1 when calling the function fails.
1632 * Uses argv[argc] for the function arguments.
1633 */
1634 long
1635call_func_retnr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
1642 long retval;
1643
1644 /* All arguments are passed as strings, no conversion to number. */
1645 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1646 return -1;
1647
1648 retval = get_tv_number_chk(&rettv, NULL);
1649 clear_tv(&rettv);
1650 return retval;
1651}
1652
1653#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1654 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1655
Bram Moolenaar4f688582007-07-24 12:34:30 +00001656# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001658 * Call vimL function "func" and return the result as a string.
1659 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 * Uses argv[argc] for the function arguments.
1661 */
1662 void *
1663call_func_retstr(func, argc, argv, safe)
1664 char_u *func;
1665 int argc;
1666 char_u **argv;
1667 int safe; /* use the sandbox */
1668{
1669 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001670 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001672 /* All arguments are passed as strings, no conversion to number. */
1673 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 return NULL;
1675
1676 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 return retval;
1679}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001680# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001683 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 */
1687 void *
1688call_func_retlist(func, argc, argv, safe)
1689 char_u *func;
1690 int argc;
1691 char_u **argv;
1692 int safe; /* use the sandbox */
1693{
1694 typval_T rettv;
1695
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001696 /* All arguments are passed as strings, no conversion to number. */
1697 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 return NULL;
1699
1700 if (rettv.v_type != VAR_LIST)
1701 {
1702 clear_tv(&rettv);
1703 return NULL;
1704 }
1705
1706 return rettv.vval.v_list;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
1710/*
1711 * Save the current function call pointer, and set it to NULL.
1712 * Used when executing autocommands and for ":source".
1713 */
1714 void *
1715save_funccal()
1716{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 current_funccal = NULL;
1720 return (void *)fc;
1721}
1722
1723 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724restore_funccal(vfc)
1725 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727 funccall_T *fc = (funccall_T *)vfc;
1728
1729 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730}
1731
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732#if defined(FEAT_PROFILE) || defined(PROTO)
1733/*
1734 * Prepare profiling for entering a child or something else that is not
1735 * counted for the script/function itself.
1736 * Should always be called in pair with prof_child_exit().
1737 */
1738 void
1739prof_child_enter(tm)
1740 proftime_T *tm; /* place to store waittime */
1741{
1742 funccall_T *fc = current_funccal;
1743
1744 if (fc != NULL && fc->func->uf_profiling)
1745 profile_start(&fc->prof_child);
1746 script_prof_save(tm);
1747}
1748
1749/*
1750 * Take care of time spent in a child.
1751 * Should always be called after prof_child_enter().
1752 */
1753 void
1754prof_child_exit(tm)
1755 proftime_T *tm; /* where waittime was stored */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 {
1761 profile_end(&fc->prof_child);
1762 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1763 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1764 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1765 }
1766 script_prof_restore(tm);
1767}
1768#endif
1769
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_FOLDING
1772/*
1773 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1774 * it in "*cp". Doesn't give error messages.
1775 */
1776 int
1777eval_foldexpr(arg, cp)
1778 char_u *arg;
1779 int *cp;
1780{
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 int retval;
1783 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001784 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1785 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001788 if (use_sandbox)
1789 ++sandbox;
1790 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 retval = 0;
1794 else
1795 {
1796 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 if (tv.v_type == VAR_NUMBER)
1798 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001799 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 retval = 0;
1801 else
1802 {
1803 /* If the result is a string, check if there is a non-digit before
1804 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (!VIM_ISDIGIT(*s) && *s != '-')
1807 *cp = *s++;
1808 retval = atol((char *)s);
1809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001813 if (use_sandbox)
1814 --sandbox;
1815 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 return retval;
1818}
1819#endif
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 * ":let" list all variable values
1823 * ":let var1 var2" list variable values
1824 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 * ":let var += expr" assignment command.
1826 * ":let var -= expr" assignment command.
1827 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 */
1830 void
1831ex_let(eap)
1832 exarg_T *eap;
1833{
1834 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 int var_count = 0;
1839 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001842 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 argend = skip_var_list(arg, &var_count, &semicolon);
1845 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001847 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1848 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001849 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001852 /*
1853 * ":let" without "=": list variables
1854 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 if (*arg == '[')
1856 EMSG(_(e_invarg));
1857 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_glob_vars(&first);
1864 list_buf_vars(&first);
1865 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001866#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001867 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_script_vars(&first);
1870 list_func_vars(&first);
1871 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 eap->nextcmd = check_nextcmd(arg);
1874 }
1875 else
1876 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 op[0] = '=';
1878 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 {
1881 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1882 op[0] = expr[-1]; /* +=, -= or .= */
1883 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (eap->skip)
1890 {
1891 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 --emsg_skip;
1894 }
1895 else if (i != FAIL)
1896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 }
1902}
1903
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904/*
1905 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1906 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 * When "nextchars" is not NULL it points to a string with characters that
1908 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1909 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 * Returns OK or FAIL;
1911 */
1912 static int
1913ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1914 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 int copy; /* copy values from "tv", don't move */
1917 int semicolon; /* from skip_var_list() */
1918 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920{
1921 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 listitem_T *item;
1925 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926
1927 if (*arg != '[')
1928 {
1929 /*
1930 * ":let var = expr" or ":for var in list"
1931 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 return FAIL;
1934 return OK;
1935 }
1936
1937 /*
1938 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1939 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001940 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 {
1942 EMSG(_(e_listreq));
1943 return FAIL;
1944 }
1945
1946 i = list_len(l);
1947 if (semicolon == 0 && var_count < i)
1948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001949 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 return FAIL;
1951 }
1952 if (var_count - semicolon > i)
1953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001954 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 return FAIL;
1956 }
1957
1958 item = l->lv_first;
1959 while (*arg != ']')
1960 {
1961 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 item = item->li_next;
1964 if (arg == NULL)
1965 return FAIL;
1966
1967 arg = skipwhite(arg);
1968 if (*arg == ';')
1969 {
1970 /* Put the rest of the list (may be empty) in the var after ';'.
1971 * Create a new list for this. */
1972 l = list_alloc();
1973 if (l == NULL)
1974 return FAIL;
1975 while (item != NULL)
1976 {
1977 list_append_tv(l, &item->li_tv);
1978 item = item->li_next;
1979 }
1980
1981 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001982 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 ltv.vval.v_list = l;
1984 l->lv_refcount = 1;
1985
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1987 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 clear_tv(&ltv);
1989 if (arg == NULL)
1990 return FAIL;
1991 break;
1992 }
1993 else if (*arg != ',' && *arg != ']')
1994 {
1995 EMSG2(_(e_intern2), "ex_let_vars()");
1996 return FAIL;
1997 }
1998 }
1999
2000 return OK;
2001}
2002
2003/*
2004 * Skip over assignable variable "var" or list of variables "[var, var]".
2005 * Used for ":let varvar = expr" and ":for varvar in expr".
2006 * For "[var, var]" increment "*var_count" for each variable.
2007 * for "[var, var; var]" set "semicolon".
2008 * Return NULL for an error.
2009 */
2010 static char_u *
2011skip_var_list(arg, var_count, semicolon)
2012 char_u *arg;
2013 int *var_count;
2014 int *semicolon;
2015{
2016 char_u *p, *s;
2017
2018 if (*arg == '[')
2019 {
2020 /* "[var, var]": find the matching ']'. */
2021 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002022 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 {
2024 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2025 s = skip_var_one(p);
2026 if (s == p)
2027 {
2028 EMSG2(_(e_invarg2), p);
2029 return NULL;
2030 }
2031 ++*var_count;
2032
2033 p = skipwhite(s);
2034 if (*p == ']')
2035 break;
2036 else if (*p == ';')
2037 {
2038 if (*semicolon == 1)
2039 {
2040 EMSG(_("Double ; in list of variables"));
2041 return NULL;
2042 }
2043 *semicolon = 1;
2044 }
2045 else if (*p != ',')
2046 {
2047 EMSG2(_(e_invarg2), p);
2048 return NULL;
2049 }
2050 }
2051 return p + 1;
2052 }
2053 else
2054 return skip_var_one(arg);
2055}
2056
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002058 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002059 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002060 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 static char_u *
2062skip_var_one(arg)
2063 char_u *arg;
2064{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002065 if (*arg == '@' && arg[1] != NUL)
2066 return arg + 2;
2067 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2068 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069}
2070
Bram Moolenaara7043832005-01-21 11:56:39 +00002071/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 * List variables for hashtab "ht" with prefix "prefix".
2073 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002080 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081{
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 hashitem_T *hi;
2083 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 int todo;
2085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002086 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2088 {
2089 if (!HASHITEM_EMPTY(hi))
2090 {
2091 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 di = HI2DI(hi);
2093 if (empty || di->di_tv.v_type != VAR_STRING
2094 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 }
2097 }
2098}
2099
2100/*
2101 * List global variables.
2102 */
2103 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104list_glob_vars(first)
2105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002108}
2109
2110/*
2111 * List buffer variables.
2112 */
2113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_buf_vars(first)
2115 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117 char_u numbuf[NUMBUFLEN];
2118
Bram Moolenaar429fa852013-04-15 12:27:36 +02002119 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121
2122 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2124 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002125}
2126
2127/*
2128 * List window variables.
2129 */
2130 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131list_win_vars(first)
2132 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002133{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136}
2137
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138#ifdef FEAT_WINDOWS
2139/*
2140 * List tab page variables.
2141 */
2142 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143list_tab_vars(first)
2144 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002146 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148}
2149#endif
2150
Bram Moolenaara7043832005-01-21 11:56:39 +00002151/*
2152 * List Vim variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_vim_vars(first)
2156 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159}
2160
2161/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162 * List script-local variables, if there is a script.
2163 */
2164 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165list_script_vars(first)
2166 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167{
2168 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2170 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171}
2172
2173/*
2174 * List function variables, if there is a function.
2175 */
2176 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177list_func_vars(first)
2178 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179{
2180 if (current_funccal != NULL)
2181 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002183}
2184
2185/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 * List variables in "arg".
2187 */
2188 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 exarg_T *eap;
2191 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193{
2194 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 char_u *name_start;
2198 char_u *arg_subsc;
2199 char_u *tofree;
2200 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201
2202 while (!ends_excmd(*arg) && !got_int)
2203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002206 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2208 {
2209 emsg_severe = TRUE;
2210 EMSG(_(e_trailing));
2211 break;
2212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 /* get_name_len() takes care of expanding curly braces */
2217 name_start = name = arg;
2218 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2219 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* This is mainly to keep test 49 working: when expanding
2222 * curly braces fails overrule the exception error message. */
2223 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 emsg_severe = TRUE;
2226 EMSG2(_(e_invarg2), arg);
2227 break;
2228 }
2229 error = TRUE;
2230 }
2231 else
2232 {
2233 if (tofree != NULL)
2234 name = tofree;
2235 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 else
2238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* handle d.key, l[idx], f(expr) */
2240 arg_subsc = arg;
2241 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 'g': list_glob_vars(first); break;
2250 case 'b': list_buf_vars(first); break;
2251 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002252#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 'v': list_vim_vars(first); break;
2256 case 's': list_script_vars(first); break;
2257 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 default:
2259 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
2262 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 {
2264 char_u numbuf[NUMBUFLEN];
2265 char_u *tf;
2266 int c;
2267 char_u *s;
2268
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002269 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 c = *arg;
2271 *arg = NUL;
2272 list_one_var_a((char_u *)"",
2273 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 tv.v_type,
2275 s == NULL ? (char_u *)"" : s,
2276 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 *arg = c;
2278 vim_free(tf);
2279 }
2280 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287
2288 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
2290
2291 return arg;
2292}
2293
2294/*
2295 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2296 * Returns a pointer to the char just after the var name.
2297 * Returns NULL if there is an error.
2298 */
2299 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002302 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002304 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306{
2307 int c1;
2308 char_u *name;
2309 char_u *p;
2310 char_u *arg_end = NULL;
2311 int len;
2312 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002313 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314
2315 /*
2316 * ":let $VAR = expr": Set environment variable.
2317 */
2318 if (*arg == '$')
2319 {
2320 /* Find the end of the name. */
2321 ++arg;
2322 name = arg;
2323 len = get_env_len(&arg);
2324 if (len == 0)
2325 EMSG2(_(e_invarg2), name - 1);
2326 else
2327 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 if (op != NULL && (*op == '+' || *op == '-'))
2329 EMSG2(_(e_letwrong), op);
2330 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002331 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002333 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 {
2335 c1 = name[len];
2336 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 p = get_tv_string_chk(tv);
2338 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 {
2340 int mustfree = FALSE;
2341 char_u *s = vim_getenv(name, &mustfree);
2342
2343 if (s != NULL)
2344 {
2345 p = tofree = concat_str(s, p);
2346 if (mustfree)
2347 vim_free(s);
2348 }
2349 }
2350 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 if (STRICMP(name, "HOME") == 0)
2354 init_homedir();
2355 else if (didset_vim && STRICMP(name, "VIM") == 0)
2356 didset_vim = FALSE;
2357 else if (didset_vimruntime
2358 && STRICMP(name, "VIMRUNTIME") == 0)
2359 didset_vimruntime = FALSE;
2360 arg_end = arg;
2361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
2365 }
2366 }
2367
2368 /*
2369 * ":let &option = expr": Set option value.
2370 * ":let &l:option = expr": Set local option value.
2371 * ":let &g:option = expr": Set global option value.
2372 */
2373 else if (*arg == '&')
2374 {
2375 /* Find the end of the name. */
2376 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002377 if (p == NULL || (endchars != NULL
2378 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 EMSG(_(e_letunexp));
2380 else
2381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 long n;
2383 int opt_type;
2384 long numval;
2385 char_u *stringval = NULL;
2386 char_u *s;
2387
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 c1 = *p;
2389 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390
2391 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 s = get_tv_string_chk(tv); /* != NULL if number or string */
2393 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
2395 opt_type = get_option_value(arg, &numval,
2396 &stringval, opt_flags);
2397 if ((opt_type == 1 && *op == '.')
2398 || (opt_type == 0 && *op != '.'))
2399 EMSG2(_(e_letwrong), op);
2400 else
2401 {
2402 if (opt_type == 1) /* number */
2403 {
2404 if (*op == '+')
2405 n = numval + n;
2406 else
2407 n = numval - n;
2408 }
2409 else if (opt_type == 0 && stringval != NULL) /* string */
2410 {
2411 s = concat_str(stringval, s);
2412 vim_free(stringval);
2413 stringval = s;
2414 }
2415 }
2416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 if (s != NULL)
2418 {
2419 set_option_value(arg, n, s, opt_flags);
2420 arg_end = p;
2421 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 }
2425 }
2426
2427 /*
2428 * ":let @r = expr": Set register contents.
2429 */
2430 else if (*arg == '@')
2431 {
2432 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 if (op != NULL && (*op == '+' || *op == '-'))
2434 EMSG2(_(e_letwrong), op);
2435 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002436 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 EMSG(_(e_letunexp));
2438 else
2439 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002440 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 char_u *s;
2442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 p = get_tv_string_chk(tv);
2444 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002446 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 if (s != NULL)
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(s);
2451 }
2452 }
2453 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 arg_end = arg + 1;
2457 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 }
2460 }
2461
2462 /*
2463 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002468 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002470 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2474 EMSG(_(e_letunexp));
2475 else
2476 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 arg_end = p;
2479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483
2484 else
2485 EMSG2(_(e_invarg2), arg);
2486
2487 return arg_end;
2488}
2489
2490/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2492 */
2493 static int
2494check_changedtick(arg)
2495 char_u *arg;
2496{
2497 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2498 {
2499 EMSG2(_(e_readonlyvar), arg);
2500 return TRUE;
2501 }
2502 return FALSE;
2503}
2504
2505/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * Get an lval: variable, Dict item or List item that can be assigned a value
2507 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2508 * "name.key", "name.key[expr]" etc.
2509 * Indexing only works if "name" is an existing List or Dictionary.
2510 * "name" points to the start of the name.
2511 * If "rettv" is not NULL it points to the value to be assigned.
2512 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2513 * wrong; must end in space or cmd separator.
2514 *
2515 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002516 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 */
2519 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002520get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 typval_T *rettv;
2523 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 int unlet;
2525 int skip;
2526 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002527 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 char_u *expr_start, *expr_end;
2531 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 dictitem_T *v;
2533 typval_T var1;
2534 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543
2544 if (skip)
2545 {
2546 /* When skipping just find the end of the name. */
2547 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 }
2550
2551 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002552 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 if (expr_start != NULL)
2554 {
2555 /* Don't expand the name when we already know there is an error. */
2556 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2557 && *p != '[' && *p != '.')
2558 {
2559 EMSG(_(e_trailing));
2560 return NULL;
2561 }
2562
2563 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2564 if (lp->ll_exp_name == NULL)
2565 {
2566 /* Report an invalid expression in braces, unless the
2567 * expression evaluation has been cancelled due to an
2568 * aborting error, an interrupt, or an exception. */
2569 if (!aborting() && !quiet)
2570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002571 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 EMSG2(_(e_invarg2), name);
2573 return NULL;
2574 }
2575 }
2576 lp->ll_name = lp->ll_exp_name;
2577 }
2578 else
2579 lp->ll_name = name;
2580
2581 /* Without [idx] or .key we are done. */
2582 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2583 return p;
2584
2585 cc = *p;
2586 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002587 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (v == NULL && !quiet)
2589 EMSG2(_(e_undefvar), lp->ll_name);
2590 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 if (v == NULL)
2592 return NULL;
2593
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 /*
2595 * Loop until no more [idx] or .key is following.
2596 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2601 && !(lp->ll_tv->v_type == VAR_DICT
2602 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (!quiet)
2605 EMSG(_("E689: Can only index a List or Dictionary"));
2606 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (!quiet)
2611 EMSG(_("E708: [:] must come last"));
2612 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 len = -1;
2616 if (*p == '.')
2617 {
2618 key = p + 1;
2619 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2620 ;
2621 if (len == 0)
2622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!quiet)
2624 EMSG(_(e_emptykey));
2625 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 }
2627 p = key + len;
2628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 else
2630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 if (*p == ':')
2634 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 else
2636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 empty1 = FALSE;
2638 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002640 if (get_tv_string_chk(&var1) == NULL)
2641 {
2642 /* not a number or string */
2643 clear_tv(&var1);
2644 return NULL;
2645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
2647
2648 /* Optionally get the second index [ :expr]. */
2649 if (*p == ':')
2650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 if (!empty1)
2656 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (rettv != NULL && (rettv->v_type != VAR_LIST
2660 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!quiet)
2663 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 if (!empty1)
2665 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 }
2668 p = skipwhite(p + 1);
2669 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 else
2672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (!empty1)
2677 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002680 if (get_tv_string_chk(&var2) == NULL)
2681 {
2682 /* not a number or string */
2683 if (!empty1)
2684 clear_tv(&var1);
2685 clear_tv(&var2);
2686 return NULL;
2687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
2697 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 if (!empty1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704
2705 /* Skip to past ']'. */
2706 ++p;
2707 }
2708
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
2711 if (len == -1)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002714 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (*key == NUL)
2716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 lp->ll_list = NULL;
2724 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002725 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002726
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002727 /* When assigning to a scope dictionary check that a function and
2728 * variable name is valid (only variable name unless it is l: or
2729 * g: dictionary). Disallow overwriting a builtin function. */
2730 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002731 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002732 int prevval;
2733 int wrong;
2734
2735 if (len != -1)
2736 {
2737 prevval = key[len];
2738 key[len] = NUL;
2739 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002740 else
2741 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2743 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002744 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002745 || !valid_varname(key);
2746 if (len != -1)
2747 key[len] = prevval;
2748 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749 return NULL;
2750 }
2751
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 /* Can't add "v:" variable. */
2755 if (lp->ll_dict == &vimvardict)
2756 {
2757 EMSG2(_(e_illvar), name);
2758 return NULL;
2759 }
2760
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002761 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (len == -1)
2767 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 }
2770 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 if (len == -1)
2775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 p = NULL;
2778 break;
2779 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 /* existing variable, need to check if it can be changed */
2781 else if (var_check_ro(lp->ll_di->di_flags, name))
2782 return NULL;
2783
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (len == -1)
2785 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 }
2788 else
2789 {
2790 /*
2791 * Get the number and item for the only or first index of the List.
2792 */
2793 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
2796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002797 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 clear_tv(&var1);
2799 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002800 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_list = lp->ll_tv->vval.v_list;
2802 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2803 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002805 if (lp->ll_n1 < 0)
2806 {
2807 lp->ll_n1 = 0;
2808 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2809 }
2810 }
2811 if (lp->ll_li == NULL)
2812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002815 if (!quiet)
2816 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 }
2819
2820 /*
2821 * May need to find the item or absolute index for the second
2822 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 * When no index given: "lp->ll_empty2" is TRUE.
2824 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002828 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002834 {
2835 if (!quiet)
2836 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 }
2841
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2843 if (lp->ll_n1 < 0)
2844 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2845 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002846 {
2847 if (!quiet)
2848 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002850 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002851 }
2852
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002855 }
2856
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return p;
2858}
2859
2860/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 */
2863 static void
2864clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866{
2867 vim_free(lp->ll_exp_name);
2868 vim_free(lp->ll_newkey);
2869}
2870
2871/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002872 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002874 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 */
2876 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883{
2884 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 listitem_T *ri;
2886 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887
2888 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 cc = *endp;
2893 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 if (op != NULL && *op != '=')
2895 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002898 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002899 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002900 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901 {
2902 if (tv_op(&tv, rettv, op) == OK)
2903 set_var(lp->ll_name, &tv, FALSE);
2904 clear_tv(&tv);
2905 }
2906 }
2907 else
2908 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 else if (tv_check_lock(lp->ll_newkey == NULL
2913 ? lp->ll_tv->v_lock
2914 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2915 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 else if (lp->ll_range)
2917 {
2918 /*
2919 * Assign the List values to the list items.
2920 */
2921 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002922 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923 if (op != NULL && *op != '=')
2924 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2925 else
2926 {
2927 clear_tv(&lp->ll_li->li_tv);
2928 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 ri = ri->li_next;
2931 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2932 break;
2933 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002936 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 ri = NULL;
2939 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 lp->ll_li = lp->ll_li->li_next;
2943 ++lp->ll_n1;
2944 }
2945 if (ri != NULL)
2946 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 else if (lp->ll_empty2
2948 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 : lp->ll_n1 != lp->ll_n2)
2950 EMSG(_("E711: List value has not enough items"));
2951 }
2952 else
2953 {
2954 /*
2955 * Assign to a List or Dictionary item.
2956 */
2957 if (lp->ll_newkey != NULL)
2958 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 if (op != NULL && *op != '=')
2960 {
2961 EMSG2(_(e_letwrong), op);
2962 return;
2963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002966 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 if (di == NULL)
2968 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002969 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2970 {
2971 vim_free(di);
2972 return;
2973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002975 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 else if (op != NULL && *op != '=')
2977 {
2978 tv_op(lp->ll_tv, rettv, op);
2979 return;
2980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 /*
2985 * Assign the value to the variable or list item.
2986 */
2987 if (copy)
2988 copy_tv(rettv, lp->ll_tv);
2989 else
2990 {
2991 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002992 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 }
2995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996}
2997
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002998/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3000 * Returns OK or FAIL.
3001 */
3002 static int
3003tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003004 typval_T *tv1;
3005 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 char_u *op;
3007{
3008 long n;
3009 char_u numbuf[NUMBUFLEN];
3010 char_u *s;
3011
3012 /* Can't do anything with a Funcref or a Dict on the right. */
3013 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3014 {
3015 switch (tv1->v_type)
3016 {
3017 case VAR_DICT:
3018 case VAR_FUNC:
3019 break;
3020
3021 case VAR_LIST:
3022 if (*op != '+' || tv2->v_type != VAR_LIST)
3023 break;
3024 /* List += List */
3025 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3026 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3027 return OK;
3028
3029 case VAR_NUMBER:
3030 case VAR_STRING:
3031 if (tv2->v_type == VAR_LIST)
3032 break;
3033 if (*op == '+' || *op == '-')
3034 {
3035 /* nr += nr or nr -= nr*/
3036 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003037#ifdef FEAT_FLOAT
3038 if (tv2->v_type == VAR_FLOAT)
3039 {
3040 float_T f = n;
3041
3042 if (*op == '+')
3043 f += tv2->vval.v_float;
3044 else
3045 f -= tv2->vval.v_float;
3046 clear_tv(tv1);
3047 tv1->v_type = VAR_FLOAT;
3048 tv1->vval.v_float = f;
3049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051#endif
3052 {
3053 if (*op == '+')
3054 n += get_tv_number(tv2);
3055 else
3056 n -= get_tv_number(tv2);
3057 clear_tv(tv1);
3058 tv1->v_type = VAR_NUMBER;
3059 tv1->vval.v_number = n;
3060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003061 }
3062 else
3063 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064 if (tv2->v_type == VAR_FLOAT)
3065 break;
3066
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 /* str .= str */
3068 s = get_tv_string(tv1);
3069 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3070 clear_tv(tv1);
3071 tv1->v_type = VAR_STRING;
3072 tv1->vval.v_string = s;
3073 }
3074 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003075
3076#ifdef FEAT_FLOAT
3077 case VAR_FLOAT:
3078 {
3079 float_T f;
3080
3081 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3082 && tv2->v_type != VAR_NUMBER
3083 && tv2->v_type != VAR_STRING))
3084 break;
3085 if (tv2->v_type == VAR_FLOAT)
3086 f = tv2->vval.v_float;
3087 else
3088 f = get_tv_number(tv2);
3089 if (*op == '+')
3090 tv1->vval.v_float += f;
3091 else
3092 tv1->vval.v_float -= f;
3093 }
3094 return OK;
3095#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 }
3097 }
3098
3099 EMSG2(_(e_letwrong), op);
3100 return FAIL;
3101}
3102
3103/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 * Add a watcher to a list.
3105 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003106 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 list_T *l;
3109 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110{
3111 lw->lw_next = l->lv_watch;
3112 l->lv_watch = lw;
3113}
3114
3115/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003116 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 * No warning when it isn't found...
3118 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003119 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 list_T *l;
3122 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123{
Bram Moolenaar33570922005-01-25 22:26:29 +00003124 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125
3126 lwp = &l->lv_watch;
3127 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3128 {
3129 if (lw == lwrem)
3130 {
3131 *lwp = lw->lw_next;
3132 break;
3133 }
3134 lwp = &lw->lw_next;
3135 }
3136}
3137
3138/*
3139 * Just before removing an item from a list: advance watchers to the next
3140 * item.
3141 */
3142 static void
3143list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 list_T *l;
3145 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148
3149 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3150 if (lw->lw_item == item)
3151 lw->lw_item = item->li_next;
3152}
3153
3154/*
3155 * Evaluate the expression used in a ":for var in expr" command.
3156 * "arg" points to "var".
3157 * Set "*errp" to TRUE for an error, FALSE otherwise;
3158 * Return a pointer that holds the info. Null when there is an error.
3159 */
3160 void *
3161eval_for_line(arg, errp, nextcmdp, skip)
3162 char_u *arg;
3163 int *errp;
3164 char_u **nextcmdp;
3165 int skip;
3166{
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 typval_T tv;
3170 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 *errp = TRUE; /* default: there is an error */
3173
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 if (fi == NULL)
3176 return NULL;
3177
3178 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3179 if (expr == NULL)
3180 return fi;
3181
3182 expr = skipwhite(expr);
3183 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3184 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003185 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186 return fi;
3187 }
3188
3189 if (skip)
3190 ++emsg_skip;
3191 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3192 {
3193 *errp = FALSE;
3194 if (!skip)
3195 {
3196 l = tv.vval.v_list;
3197 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 clear_tv(&tv);
3201 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 else
3203 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003204 /* No need to increment the refcount, it's already set for the
3205 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 fi->fi_list = l;
3207 list_add_watch(l, &fi->fi_lw);
3208 fi->fi_lw.lw_item = l->lv_first;
3209 }
3210 }
3211 }
3212 if (skip)
3213 --emsg_skip;
3214
3215 return fi;
3216}
3217
3218/*
3219 * Use the first item in a ":for" list. Advance to the next.
3220 * Assign the values to the variable (list). "arg" points to the first one.
3221 * Return TRUE when a valid item was found, FALSE when at end of list or
3222 * something wrong.
3223 */
3224 int
3225next_for_item(fi_void, arg)
3226 void *fi_void;
3227 char_u *arg;
3228{
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232
3233 item = fi->fi_lw.lw_item;
3234 if (item == NULL)
3235 result = FALSE;
3236 else
3237 {
3238 fi->fi_lw.lw_item = item->li_next;
3239 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3240 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3241 }
3242 return result;
3243}
3244
3245/*
3246 * Free the structure used to store info used by ":for".
3247 */
3248 void
3249free_for_info(fi_void)
3250 void *fi_void;
3251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003254 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 list_unref(fi->fi_list);
3258 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 vim_free(fi);
3260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3263
3264 void
3265set_context_for_expression(xp, arg, cmdidx)
3266 expand_T *xp;
3267 char_u *arg;
3268 cmdidx_T cmdidx;
3269{
3270 int got_eq = FALSE;
3271 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 if (cmdidx == CMD_let)
3275 {
3276 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003277 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 {
3279 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003280 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 {
3282 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 if (vim_iswhite(*p))
3285 break;
3286 }
3287 return;
3288 }
3289 }
3290 else
3291 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3292 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 while ((xp->xp_pattern = vim_strpbrk(arg,
3294 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3295 {
3296 c = *xp->xp_pattern;
3297 if (c == '&')
3298 {
3299 c = xp->xp_pattern[1];
3300 if (c == '&')
3301 {
3302 ++xp->xp_pattern;
3303 xp->xp_context = cmdidx != CMD_let || got_eq
3304 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3305 }
3306 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003309 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3310 xp->xp_pattern += 2;
3311
3312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 else if (c == '$')
3315 {
3316 /* environment variable */
3317 xp->xp_context = EXPAND_ENV_VARS;
3318 }
3319 else if (c == '=')
3320 {
3321 got_eq = TRUE;
3322 xp->xp_context = EXPAND_EXPRESSION;
3323 }
3324 else if (c == '<'
3325 && xp->xp_context == EXPAND_FUNCTIONS
3326 && vim_strchr(xp->xp_pattern, '(') == NULL)
3327 {
3328 /* Function name can start with "<SNR>" */
3329 break;
3330 }
3331 else if (cmdidx != CMD_let || got_eq)
3332 {
3333 if (c == '"') /* string */
3334 {
3335 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3336 if (c == '\\' && xp->xp_pattern[1] != NUL)
3337 ++xp->xp_pattern;
3338 xp->xp_context = EXPAND_NOTHING;
3339 }
3340 else if (c == '\'') /* literal string */
3341 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003342 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3344 /* skip */ ;
3345 xp->xp_context = EXPAND_NOTHING;
3346 }
3347 else if (c == '|')
3348 {
3349 if (xp->xp_pattern[1] == '|')
3350 {
3351 ++xp->xp_pattern;
3352 xp->xp_context = EXPAND_EXPRESSION;
3353 }
3354 else
3355 xp->xp_context = EXPAND_COMMANDS;
3356 }
3357 else
3358 xp->xp_context = EXPAND_EXPRESSION;
3359 }
3360 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361 /* Doesn't look like something valid, expand as an expression
3362 * anyway. */
3363 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 arg = xp->xp_pattern;
3365 if (*arg != NUL)
3366 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3367 /* skip */ ;
3368 }
3369 xp->xp_pattern = arg;
3370}
3371
3372#endif /* FEAT_CMDL_COMPL */
3373
3374/*
3375 * ":1,25call func(arg1, arg2)" function call.
3376 */
3377 void
3378ex_call(eap)
3379 exarg_T *eap;
3380{
3381 char_u *arg = eap->arg;
3382 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003384 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003386 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 linenr_T lnum;
3388 int doesrange;
3389 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003390 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003392 if (eap->skip)
3393 {
3394 /* trans_function_name() doesn't work well when skipping, use eval0()
3395 * instead to skip to any following command, e.g. for:
3396 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003397 ++emsg_skip;
3398 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3399 clear_tv(&rettv);
3400 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003401 return;
3402 }
3403
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003404 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003405 if (fudi.fd_newkey != NULL)
3406 {
3407 /* Still need to give an error message for missing key. */
3408 EMSG2(_(e_dictkey), fudi.fd_newkey);
3409 vim_free(fudi.fd_newkey);
3410 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003411 if (tofree == NULL)
3412 return;
3413
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003414 /* Increase refcount on dictionary, it could get deleted when evaluating
3415 * the arguments. */
3416 if (fudi.fd_dict != NULL)
3417 ++fudi.fd_dict->dv_refcount;
3418
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003419 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003420 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaar532c7802005-01-27 14:44:31 +00003423 /* Skip white space to allow ":call func ()". Not good, but required for
3424 * backward compatibility. */
3425 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 if (*startarg != '(')
3429 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003430 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 goto end;
3432 }
3433
3434 /*
3435 * When skipping, evaluate the function once, to find the end of the
3436 * arguments.
3437 * When the function takes a range, this is discovered after the first
3438 * call, and the loop is broken.
3439 */
3440 if (eap->skip)
3441 {
3442 ++emsg_skip;
3443 lnum = eap->line2; /* do it once, also with an invalid range */
3444 }
3445 else
3446 lnum = eap->line1;
3447 for ( ; lnum <= eap->line2; ++lnum)
3448 {
3449 if (!eap->skip && eap->addr_count > 0)
3450 {
3451 curwin->w_cursor.lnum = lnum;
3452 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003453#ifdef FEAT_VIRTUALEDIT
3454 curwin->w_cursor.coladd = 0;
3455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003458 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003459 eap->line1, eap->line2, &doesrange,
3460 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 failed = TRUE;
3463 break;
3464 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003465
3466 /* Handle a function returning a Funcref, Dictionary or List. */
3467 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3468 {
3469 failed = TRUE;
3470 break;
3471 }
3472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (doesrange || eap->skip)
3475 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003478 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003479 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003480 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (aborting())
3482 break;
3483 }
3484 if (eap->skip)
3485 --emsg_skip;
3486
3487 if (!failed)
3488 {
3489 /* Check for trailing illegal characters and a following command. */
3490 if (!ends_excmd(*arg))
3491 {
3492 emsg_severe = TRUE;
3493 EMSG(_(e_trailing));
3494 }
3495 else
3496 eap->nextcmd = check_nextcmd(arg);
3497 }
3498
3499end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003500 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003501 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503
3504/*
3505 * ":unlet[!] var1 ... " command.
3506 */
3507 void
3508ex_unlet(eap)
3509 exarg_T *eap;
3510{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003511 ex_unletlock(eap, eap->arg, 0);
3512}
3513
3514/*
3515 * ":lockvar" and ":unlockvar" commands
3516 */
3517 void
3518ex_lockvar(eap)
3519 exarg_T *eap;
3520{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003522 int deep = 2;
3523
3524 if (eap->forceit)
3525 deep = -1;
3526 else if (vim_isdigit(*arg))
3527 {
3528 deep = getdigits(&arg);
3529 arg = skipwhite(arg);
3530 }
3531
3532 ex_unletlock(eap, arg, deep);
3533}
3534
3535/*
3536 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3537 */
3538 static void
3539ex_unletlock(eap, argstart, deep)
3540 exarg_T *eap;
3541 char_u *argstart;
3542 int deep;
3543{
3544 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003547 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 do
3550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003552 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3553 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 if (lv.ll_name == NULL)
3555 error = TRUE; /* error but continue parsing */
3556 if (name_end == NULL || (!vim_iswhite(*name_end)
3557 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 if (name_end != NULL)
3560 {
3561 emsg_severe = TRUE;
3562 EMSG(_(e_trailing));
3563 }
3564 if (!(eap->skip || error))
3565 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 break;
3567 }
3568
3569 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 {
3571 if (eap->cmdidx == CMD_unlet)
3572 {
3573 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3574 error = TRUE;
3575 }
3576 else
3577 {
3578 if (do_lock_var(&lv, name_end, deep,
3579 eap->cmdidx == CMD_lockvar) == FAIL)
3580 error = TRUE;
3581 }
3582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584 if (!eap->skip)
3585 clear_lval(&lv);
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 arg = skipwhite(name_end);
3588 } while (!ends_excmd(*arg));
3589
3590 eap->nextcmd = check_nextcmd(arg);
3591}
3592
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003594do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 int forceit;
3598{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 int ret = OK;
3600 int cc;
3601
3602 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 cc = *name_end;
3605 *name_end = NUL;
3606
3607 /* Normal name or expanded name. */
3608 if (check_changedtick(lp->ll_name))
3609 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3615 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 else if (lp->ll_range)
3617 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619
3620 /* Delete a range of List items. */
3621 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3622 {
3623 li = lp->ll_li->li_next;
3624 listitem_remove(lp->ll_list, lp->ll_li);
3625 lp->ll_li = li;
3626 ++lp->ll_n1;
3627 }
3628 }
3629 else
3630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 /* unlet a List item. */
3633 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003636 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 }
3638
3639 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640}
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642/*
3643 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
3646 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
Bram Moolenaar33570922005-01-25 22:26:29 +00003651 hashtab_T *ht;
3652 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003653 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003654 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 ht = find_var_ht(name, &varname);
3657 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 hi = hash_find(ht, varname);
3660 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003661 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003662 di = HI2DI(hi);
3663 if (var_check_fixed(di->di_flags, name)
3664 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 return FAIL;
3666 delete_var(ht, hi);
3667 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 if (forceit)
3671 return OK;
3672 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return FAIL;
3674}
3675
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676/*
3677 * Lock or unlock variable indicated by "lp".
3678 * "deep" is the levels to go (-1 for unlimited);
3679 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3680 */
3681 static int
3682do_lock_var(lp, name_end, deep, lock)
3683 lval_T *lp;
3684 char_u *name_end;
3685 int deep;
3686 int lock;
3687{
3688 int ret = OK;
3689 int cc;
3690 dictitem_T *di;
3691
3692 if (deep == 0) /* nothing to do */
3693 return OK;
3694
3695 if (lp->ll_tv == NULL)
3696 {
3697 cc = *name_end;
3698 *name_end = NUL;
3699
3700 /* Normal name or expanded name. */
3701 if (check_changedtick(lp->ll_name))
3702 ret = FAIL;
3703 else
3704 {
3705 di = find_var(lp->ll_name, NULL);
3706 if (di == NULL)
3707 ret = FAIL;
3708 else
3709 {
3710 if (lock)
3711 di->di_flags |= DI_FLAGS_LOCK;
3712 else
3713 di->di_flags &= ~DI_FLAGS_LOCK;
3714 item_lock(&di->di_tv, deep, lock);
3715 }
3716 }
3717 *name_end = cc;
3718 }
3719 else if (lp->ll_range)
3720 {
3721 listitem_T *li = lp->ll_li;
3722
3723 /* (un)lock a range of List items. */
3724 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3725 {
3726 item_lock(&li->li_tv, deep, lock);
3727 li = li->li_next;
3728 ++lp->ll_n1;
3729 }
3730 }
3731 else if (lp->ll_list != NULL)
3732 /* (un)lock a List item. */
3733 item_lock(&lp->ll_li->li_tv, deep, lock);
3734 else
3735 /* un(lock) a Dictionary item. */
3736 item_lock(&lp->ll_di->di_tv, deep, lock);
3737
3738 return ret;
3739}
3740
3741/*
3742 * Lock or unlock an item. "deep" is nr of levels to go.
3743 */
3744 static void
3745item_lock(tv, deep, lock)
3746 typval_T *tv;
3747 int deep;
3748 int lock;
3749{
3750 static int recurse = 0;
3751 list_T *l;
3752 listitem_T *li;
3753 dict_T *d;
3754 hashitem_T *hi;
3755 int todo;
3756
3757 if (recurse >= DICT_MAXNEST)
3758 {
3759 EMSG(_("E743: variable nested too deep for (un)lock"));
3760 return;
3761 }
3762 if (deep == 0)
3763 return;
3764 ++recurse;
3765
3766 /* lock/unlock the item itself */
3767 if (lock)
3768 tv->v_lock |= VAR_LOCKED;
3769 else
3770 tv->v_lock &= ~VAR_LOCKED;
3771
3772 switch (tv->v_type)
3773 {
3774 case VAR_LIST:
3775 if ((l = tv->vval.v_list) != NULL)
3776 {
3777 if (lock)
3778 l->lv_lock |= VAR_LOCKED;
3779 else
3780 l->lv_lock &= ~VAR_LOCKED;
3781 if (deep < 0 || deep > 1)
3782 /* recursive: lock/unlock the items the List contains */
3783 for (li = l->lv_first; li != NULL; li = li->li_next)
3784 item_lock(&li->li_tv, deep - 1, lock);
3785 }
3786 break;
3787 case VAR_DICT:
3788 if ((d = tv->vval.v_dict) != NULL)
3789 {
3790 if (lock)
3791 d->dv_lock |= VAR_LOCKED;
3792 else
3793 d->dv_lock &= ~VAR_LOCKED;
3794 if (deep < 0 || deep > 1)
3795 {
3796 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003797 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3799 {
3800 if (!HASHITEM_EMPTY(hi))
3801 {
3802 --todo;
3803 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3804 }
3805 }
3806 }
3807 }
3808 }
3809 --recurse;
3810}
3811
Bram Moolenaara40058a2005-07-11 22:42:07 +00003812/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003813 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3814 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003815 */
3816 static int
3817tv_islocked(tv)
3818 typval_T *tv;
3819{
3820 return (tv->v_lock & VAR_LOCKED)
3821 || (tv->v_type == VAR_LIST
3822 && tv->vval.v_list != NULL
3823 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3824 || (tv->v_type == VAR_DICT
3825 && tv->vval.v_dict != NULL
3826 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3827}
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3830/*
3831 * Delete all "menutrans_" variables.
3832 */
3833 void
3834del_menutrans_vars()
3835{
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003837 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003840 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003842 {
3843 if (!HASHITEM_EMPTY(hi))
3844 {
3845 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3847 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003848 }
3849 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851}
3852#endif
3853
3854#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3855
3856/*
3857 * Local string buffer for the next two functions to store a variable name
3858 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3859 * get_user_var_name().
3860 */
3861
3862static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3863
3864static char_u *varnamebuf = NULL;
3865static int varnamebuflen = 0;
3866
3867/*
3868 * Function to concatenate a prefix and a variable name.
3869 */
3870 static char_u *
3871cat_prefix_varname(prefix, name)
3872 int prefix;
3873 char_u *name;
3874{
3875 int len;
3876
3877 len = (int)STRLEN(name) + 3;
3878 if (len > varnamebuflen)
3879 {
3880 vim_free(varnamebuf);
3881 len += 10; /* some additional space */
3882 varnamebuf = alloc(len);
3883 if (varnamebuf == NULL)
3884 {
3885 varnamebuflen = 0;
3886 return NULL;
3887 }
3888 varnamebuflen = len;
3889 }
3890 *varnamebuf = prefix;
3891 varnamebuf[1] = ':';
3892 STRCPY(varnamebuf + 2, name);
3893 return varnamebuf;
3894}
3895
3896/*
3897 * Function given to ExpandGeneric() to obtain the list of user defined
3898 * (global/buffer/window/built-in) variable names.
3899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 char_u *
3901get_user_var_name(xp, idx)
3902 expand_T *xp;
3903 int idx;
3904{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003905 static long_u gdone;
3906 static long_u bdone;
3907 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908#ifdef FEAT_WINDOWS
3909 static long_u tdone;
3910#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003911 static int vidx;
3912 static hashitem_T *hi;
3913 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003916 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003918#ifdef FEAT_WINDOWS
3919 tdone = 0;
3920#endif
3921 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003922
3923 /* Global variables */
3924 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003928 else
3929 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003930 while (HASHITEM_EMPTY(hi))
3931 ++hi;
3932 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3933 return cat_prefix_varname('g', hi->hi_key);
3934 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936
3937 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003938 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003941 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003943 else
3944 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 while (HASHITEM_EMPTY(hi))
3946 ++hi;
3947 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 return (char_u *)"b:changedtick";
3953 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003954
3955 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003956 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003959 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003961 else
3962 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003963 while (HASHITEM_EMPTY(hi))
3964 ++hi;
3965 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003968#ifdef FEAT_WINDOWS
3969 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003970 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003971 if (tdone < ht->ht_used)
3972 {
3973 if (tdone++ == 0)
3974 hi = ht->ht_array;
3975 else
3976 ++hi;
3977 while (HASHITEM_EMPTY(hi))
3978 ++hi;
3979 return cat_prefix_varname('t', hi->hi_key);
3980 }
3981#endif
3982
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 /* v: variables */
3984 if (vidx < VV_LEN)
3985 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 vim_free(varnamebuf);
3988 varnamebuf = NULL;
3989 varnamebuflen = 0;
3990 return NULL;
3991}
3992
3993#endif /* FEAT_CMDL_COMPL */
3994
3995/*
3996 * types for expressions.
3997 */
3998typedef enum
3999{
4000 TYPE_UNKNOWN = 0
4001 , TYPE_EQUAL /* == */
4002 , TYPE_NEQUAL /* != */
4003 , TYPE_GREATER /* > */
4004 , TYPE_GEQUAL /* >= */
4005 , TYPE_SMALLER /* < */
4006 , TYPE_SEQUAL /* <= */
4007 , TYPE_MATCH /* =~ */
4008 , TYPE_NOMATCH /* !~ */
4009} exptype_T;
4010
4011/*
4012 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4015 */
4016
4017/*
4018 * Handle zero level expression.
4019 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004021 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 * Return OK or FAIL.
4023 */
4024 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u **nextcmd;
4029 int evaluate;
4030{
4031 int ret;
4032 char_u *p;
4033
4034 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004035 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (ret == FAIL || !ends_excmd(*p))
4037 {
4038 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /*
4041 * Report the invalid expression unless the expression evaluation has
4042 * been cancelled due to an aborting error, an interrupt, or an
4043 * exception.
4044 */
4045 if (!aborting())
4046 EMSG2(_(e_invexpr2), arg);
4047 ret = FAIL;
4048 }
4049 if (nextcmd != NULL)
4050 *nextcmd = check_nextcmd(p);
4051
4052 return ret;
4053}
4054
4055/*
4056 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004057 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 *
4059 * "arg" must point to the first non-white of the expression.
4060 * "arg" is advanced to the next non-white after the recognized expression.
4061 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004062 * Note: "rettv.v_lock" is not set.
4063 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 * Return OK or FAIL.
4065 */
4066 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 int evaluate;
4071{
4072 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 /*
4076 * Get the first variable.
4077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 return FAIL;
4080
4081 if ((*arg)[0] == '?')
4082 {
4083 result = FALSE;
4084 if (evaluate)
4085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004086 int error = FALSE;
4087
4088 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004091 if (error)
4092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094
4095 /*
4096 * Get the second variable.
4097 */
4098 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 return FAIL;
4101
4102 /*
4103 * Check for the ":".
4104 */
4105 if ((*arg)[0] != ':')
4106 {
4107 EMSG(_("E109: Missing ':' after '?'"));
4108 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 return FAIL;
4111 }
4112
4113 /*
4114 * Get the third variable.
4115 */
4116 *arg = skipwhite(*arg + 1);
4117 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4118 {
4119 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 return FAIL;
4122 }
4123 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126
4127 return OK;
4128}
4129
4130/*
4131 * Handle first level expression:
4132 * expr2 || expr2 || expr2 logical OR
4133 *
4134 * "arg" must point to the first non-white of the expression.
4135 * "arg" is advanced to the next non-white after the recognized expression.
4136 *
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 long result;
4147 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 /*
4151 * Get the first variable.
4152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 return FAIL;
4155
4156 /*
4157 * Repeat until there is no following "||".
4158 */
4159 first = TRUE;
4160 result = FALSE;
4161 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4162 {
4163 if (evaluate && first)
4164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (error)
4169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 first = FALSE;
4171 }
4172
4173 /*
4174 * Get the second variable.
4175 */
4176 *arg = skipwhite(*arg + 2);
4177 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4178 return FAIL;
4179
4180 /*
4181 * Compute the result.
4182 */
4183 if (evaluate && !result)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 if (evaluate)
4192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 rettv->v_type = VAR_NUMBER;
4194 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
4197
4198 return OK;
4199}
4200
4201/*
4202 * Handle second level expression:
4203 * expr3 && expr3 && expr3 logical AND
4204 *
4205 * "arg" must point to the first non-white of the expression.
4206 * "arg" is advanced to the next non-white after the recognized expression.
4207 *
4208 * Return OK or FAIL.
4209 */
4210 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int evaluate;
4215{
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 long result;
4218 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004219 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 /*
4222 * Get the first variable.
4223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return FAIL;
4226
4227 /*
4228 * Repeat until there is no following "&&".
4229 */
4230 first = TRUE;
4231 result = TRUE;
4232 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4233 {
4234 if (evaluate && first)
4235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (error)
4240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 first = FALSE;
4242 }
4243
4244 /*
4245 * Get the second variable.
4246 */
4247 *arg = skipwhite(*arg + 2);
4248 if (eval4(arg, &var2, evaluate && result) == FAIL)
4249 return FAIL;
4250
4251 /*
4252 * Compute the result.
4253 */
4254 if (evaluate && result)
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 if (evaluate)
4263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 rettv->v_type = VAR_NUMBER;
4265 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 }
4268
4269 return OK;
4270}
4271
4272/*
4273 * Handle third level expression:
4274 * var1 == var2
4275 * var1 =~ var2
4276 * var1 != var2
4277 * var1 !~ var2
4278 * var1 > var2
4279 * var1 >= var2
4280 * var1 < var2
4281 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004282 * var1 is var2
4283 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 *
4285 * "arg" must point to the first non-white of the expression.
4286 * "arg" is advanced to the next non-white after the recognized expression.
4287 *
4288 * Return OK or FAIL.
4289 */
4290 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 int evaluate;
4295{
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u *p;
4298 int i;
4299 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004300 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 int len = 2;
4302 long n1, n2;
4303 char_u *s1, *s2;
4304 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4305 regmatch_T regmatch;
4306 int ic;
4307 char_u *save_cpo;
4308
4309 /*
4310 * Get the first variable.
4311 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314
4315 p = *arg;
4316 switch (p[0])
4317 {
4318 case '=': if (p[1] == '=')
4319 type = TYPE_EQUAL;
4320 else if (p[1] == '~')
4321 type = TYPE_MATCH;
4322 break;
4323 case '!': if (p[1] == '=')
4324 type = TYPE_NEQUAL;
4325 else if (p[1] == '~')
4326 type = TYPE_NOMATCH;
4327 break;
4328 case '>': if (p[1] != '=')
4329 {
4330 type = TYPE_GREATER;
4331 len = 1;
4332 }
4333 else
4334 type = TYPE_GEQUAL;
4335 break;
4336 case '<': if (p[1] != '=')
4337 {
4338 type = TYPE_SMALLER;
4339 len = 1;
4340 }
4341 else
4342 type = TYPE_SEQUAL;
4343 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344 case 'i': if (p[1] == 's')
4345 {
4346 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4347 len = 5;
4348 if (!vim_isIDc(p[len]))
4349 {
4350 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4351 type_is = TRUE;
4352 }
4353 }
4354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356
4357 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004358 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 */
4360 if (type != TYPE_UNKNOWN)
4361 {
4362 /* extra question mark appended: ignore case */
4363 if (p[len] == '?')
4364 {
4365 ic = TRUE;
4366 ++len;
4367 }
4368 /* extra '#' appended: match case */
4369 else if (p[len] == '#')
4370 {
4371 ic = FALSE;
4372 ++len;
4373 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004374 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 else
4376 ic = p_ic;
4377
4378 /*
4379 * Get the second variable.
4380 */
4381 *arg = skipwhite(p + len);
4382 if (eval5(arg, &var2, evaluate) == FAIL)
4383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 return FAIL;
4386 }
4387
4388 if (evaluate)
4389 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 if (type_is && rettv->v_type != var2.v_type)
4391 {
4392 /* For "is" a different type always means FALSE, for "notis"
4393 * it means TRUE. */
4394 n1 = (type == TYPE_NEQUAL);
4395 }
4396 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4397 {
4398 if (type_is)
4399 {
4400 n1 = (rettv->v_type == var2.v_type
4401 && rettv->vval.v_list == var2.vval.v_list);
4402 if (type == TYPE_NEQUAL)
4403 n1 = !n1;
4404 }
4405 else if (rettv->v_type != var2.v_type
4406 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4407 {
4408 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004409 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004410 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004411 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 clear_tv(rettv);
4413 clear_tv(&var2);
4414 return FAIL;
4415 }
4416 else
4417 {
4418 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004419 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4420 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004421 if (type == TYPE_NEQUAL)
4422 n1 = !n1;
4423 }
4424 }
4425
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004426 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4427 {
4428 if (type_is)
4429 {
4430 n1 = (rettv->v_type == var2.v_type
4431 && rettv->vval.v_dict == var2.vval.v_dict);
4432 if (type == TYPE_NEQUAL)
4433 n1 = !n1;
4434 }
4435 else if (rettv->v_type != var2.v_type
4436 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4437 {
4438 if (rettv->v_type != var2.v_type)
4439 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4440 else
4441 EMSG(_("E736: Invalid operation for Dictionary"));
4442 clear_tv(rettv);
4443 clear_tv(&var2);
4444 return FAIL;
4445 }
4446 else
4447 {
4448 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004449 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4450 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004451 if (type == TYPE_NEQUAL)
4452 n1 = !n1;
4453 }
4454 }
4455
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004456 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4457 {
4458 if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004462 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004464 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Funcrefs for being equal or unequal. */
4472 if (rettv->vval.v_string == NULL
4473 || var2.vval.v_string == NULL)
4474 n1 = FALSE;
4475 else
4476 n1 = STRCMP(rettv->vval.v_string,
4477 var2.vval.v_string) == 0;
4478 if (type == TYPE_NEQUAL)
4479 n1 = !n1;
4480 }
4481 }
4482
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004483#ifdef FEAT_FLOAT
4484 /*
4485 * If one of the two variables is a float, compare as a float.
4486 * When using "=~" or "!~", always compare as string.
4487 */
4488 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4489 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4490 {
4491 float_T f1, f2;
4492
4493 if (rettv->v_type == VAR_FLOAT)
4494 f1 = rettv->vval.v_float;
4495 else
4496 f1 = get_tv_number(rettv);
4497 if (var2.v_type == VAR_FLOAT)
4498 f2 = var2.vval.v_float;
4499 else
4500 f2 = get_tv_number(&var2);
4501 n1 = FALSE;
4502 switch (type)
4503 {
4504 case TYPE_EQUAL: n1 = (f1 == f2); break;
4505 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4506 case TYPE_GREATER: n1 = (f1 > f2); break;
4507 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4508 case TYPE_SMALLER: n1 = (f1 < f2); break;
4509 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4510 case TYPE_UNKNOWN:
4511 case TYPE_MATCH:
4512 case TYPE_NOMATCH: break; /* avoid gcc warning */
4513 }
4514 }
4515#endif
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 /*
4518 * If one of the two variables is a number, compare as a number.
4519 * When using "=~" or "!~", always compare as string.
4520 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 n1 = get_tv_number(rettv);
4525 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 switch (type)
4527 {
4528 case TYPE_EQUAL: n1 = (n1 == n2); break;
4529 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4530 case TYPE_GREATER: n1 = (n1 > n2); break;
4531 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4532 case TYPE_SMALLER: n1 = (n1 < n2); break;
4533 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4534 case TYPE_UNKNOWN:
4535 case TYPE_MATCH:
4536 case TYPE_NOMATCH: break; /* avoid gcc warning */
4537 }
4538 }
4539 else
4540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 s1 = get_tv_string_buf(rettv, buf1);
4542 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4544 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4545 else
4546 i = 0;
4547 n1 = FALSE;
4548 switch (type)
4549 {
4550 case TYPE_EQUAL: n1 = (i == 0); break;
4551 case TYPE_NEQUAL: n1 = (i != 0); break;
4552 case TYPE_GREATER: n1 = (i > 0); break;
4553 case TYPE_GEQUAL: n1 = (i >= 0); break;
4554 case TYPE_SMALLER: n1 = (i < 0); break;
4555 case TYPE_SEQUAL: n1 = (i <= 0); break;
4556
4557 case TYPE_MATCH:
4558 case TYPE_NOMATCH:
4559 /* avoid 'l' flag in 'cpoptions' */
4560 save_cpo = p_cpo;
4561 p_cpo = (char_u *)"";
4562 regmatch.regprog = vim_regcomp(s2,
4563 RE_MAGIC + RE_STRING);
4564 regmatch.rm_ic = ic;
4565 if (regmatch.regprog != NULL)
4566 {
4567 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004568 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (type == TYPE_NOMATCH)
4570 n1 = !n1;
4571 }
4572 p_cpo = save_cpo;
4573 break;
4574
4575 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4576 }
4577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 clear_tv(rettv);
4579 clear_tv(&var2);
4580 rettv->v_type = VAR_NUMBER;
4581 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 }
4584
4585 return OK;
4586}
4587
4588/*
4589 * Handle fourth level expression:
4590 * + number addition
4591 * - number subtraction
4592 * . string concatenation
4593 *
4594 * "arg" must point to the first non-white of the expression.
4595 * "arg" is advanced to the next non-white after the recognized expression.
4596 *
4597 * Return OK or FAIL.
4598 */
4599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 int evaluate;
4604{
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T var2;
4606 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 int op;
4608 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004609#ifdef FEAT_FLOAT
4610 float_T f1 = 0, f2 = 0;
4611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 char_u *s1, *s2;
4613 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4614 char_u *p;
4615
4616 /*
4617 * Get the first variable.
4618 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004619 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 return FAIL;
4621
4622 /*
4623 * Repeat computing, until no '+', '-' or '.' is following.
4624 */
4625 for (;;)
4626 {
4627 op = **arg;
4628 if (op != '+' && op != '-' && op != '.')
4629 break;
4630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 if ((op != '+' || rettv->v_type != VAR_LIST)
4632#ifdef FEAT_FLOAT
4633 && (op == '.' || rettv->v_type != VAR_FLOAT)
4634#endif
4635 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 {
4637 /* For "list + ...", an illegal use of the first operand as
4638 * a number cannot be determined before evaluating the 2nd
4639 * operand: if this is also a list, all is ok.
4640 * For "something . ...", "something - ..." or "non-list + ...",
4641 * we know that the first operand needs to be a string or number
4642 * without evaluating the 2nd operand. So check before to avoid
4643 * side effects after an error. */
4644 if (evaluate && get_tv_string_chk(rettv) == NULL)
4645 {
4646 clear_tv(rettv);
4647 return FAIL;
4648 }
4649 }
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 /*
4652 * Get the second variable.
4653 */
4654 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004655 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 return FAIL;
4659 }
4660
4661 if (evaluate)
4662 {
4663 /*
4664 * Compute the result.
4665 */
4666 if (op == '.')
4667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4669 s2 = get_tv_string_buf_chk(&var2, buf2);
4670 if (s2 == NULL) /* type error ? */
4671 {
4672 clear_tv(rettv);
4673 clear_tv(&var2);
4674 return FAIL;
4675 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004676 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
4678 rettv->v_type = VAR_STRING;
4679 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004681 else if (op == '+' && rettv->v_type == VAR_LIST
4682 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004683 {
4684 /* concatenate Lists */
4685 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4686 &var3) == FAIL)
4687 {
4688 clear_tv(rettv);
4689 clear_tv(&var2);
4690 return FAIL;
4691 }
4692 clear_tv(rettv);
4693 *rettv = var3;
4694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 else
4696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004697 int error = FALSE;
4698
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004699#ifdef FEAT_FLOAT
4700 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702 f1 = rettv->vval.v_float;
4703 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 else
4706#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708 n1 = get_tv_number_chk(rettv, &error);
4709 if (error)
4710 {
4711 /* This can only happen for "list + non-list". For
4712 * "non-list + ..." or "something - ...", we returned
4713 * before evaluating the 2nd operand. */
4714 clear_tv(rettv);
4715 return FAIL;
4716 }
4717#ifdef FEAT_FLOAT
4718 if (var2.v_type == VAR_FLOAT)
4719 f1 = n1;
4720#endif
4721 }
4722#ifdef FEAT_FLOAT
4723 if (var2.v_type == VAR_FLOAT)
4724 {
4725 f2 = var2.vval.v_float;
4726 n2 = 0;
4727 }
4728 else
4729#endif
4730 {
4731 n2 = get_tv_number_chk(&var2, &error);
4732 if (error)
4733 {
4734 clear_tv(rettv);
4735 clear_tv(&var2);
4736 return FAIL;
4737 }
4738#ifdef FEAT_FLOAT
4739 if (rettv->v_type == VAR_FLOAT)
4740 f2 = n2;
4741#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744
4745#ifdef FEAT_FLOAT
4746 /* If there is a float on either side the result is a float. */
4747 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4748 {
4749 if (op == '+')
4750 f1 = f1 + f2;
4751 else
4752 f1 = f1 - f2;
4753 rettv->v_type = VAR_FLOAT;
4754 rettv->vval.v_float = f1;
4755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757#endif
4758 {
4759 if (op == '+')
4760 n1 = n1 + n2;
4761 else
4762 n1 = n1 - n2;
4763 rettv->v_type = VAR_NUMBER;
4764 rettv->vval.v_number = n1;
4765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 }
4770 return OK;
4771}
4772
4773/*
4774 * Handle fifth level expression:
4775 * * number multiplication
4776 * / number division
4777 * % number modulo
4778 *
4779 * "arg" must point to the first non-white of the expression.
4780 * "arg" is advanced to the next non-white after the recognized expression.
4781 *
4782 * Return OK or FAIL.
4783 */
4784 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004785eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004789 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
Bram Moolenaar33570922005-01-25 22:26:29 +00004791 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 int op;
4793 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794#ifdef FEAT_FLOAT
4795 int use_float = FALSE;
4796 float_T f1 = 0, f2;
4797#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800 /*
4801 * Get the first variable.
4802 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return FAIL;
4805
4806 /*
4807 * Repeat computing, until no '*', '/' or '%' is following.
4808 */
4809 for (;;)
4810 {
4811 op = **arg;
4812 if (op != '*' && op != '/' && op != '%')
4813 break;
4814
4815 if (evaluate)
4816 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#ifdef FEAT_FLOAT
4818 if (rettv->v_type == VAR_FLOAT)
4819 {
4820 f1 = rettv->vval.v_float;
4821 use_float = TRUE;
4822 n1 = 0;
4823 }
4824 else
4825#endif
4826 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 if (error)
4829 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831 else
4832 n1 = 0;
4833
4834 /*
4835 * Get the second variable.
4836 */
4837 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004838 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 return FAIL;
4840
4841 if (evaluate)
4842 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843#ifdef FEAT_FLOAT
4844 if (var2.v_type == VAR_FLOAT)
4845 {
4846 if (!use_float)
4847 {
4848 f1 = n1;
4849 use_float = TRUE;
4850 }
4851 f2 = var2.vval.v_float;
4852 n2 = 0;
4853 }
4854 else
4855#endif
4856 {
4857 n2 = get_tv_number_chk(&var2, &error);
4858 clear_tv(&var2);
4859 if (error)
4860 return FAIL;
4861#ifdef FEAT_FLOAT
4862 if (use_float)
4863 f2 = n2;
4864#endif
4865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 /*
4868 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871#ifdef FEAT_FLOAT
4872 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874 if (op == '*')
4875 f1 = f1 * f2;
4876 else if (op == '/')
4877 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004878# ifdef VMS
4879 /* VMS crashes on divide by zero, work around it */
4880 if (f2 == 0.0)
4881 {
4882 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004883 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004884 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004885 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004887 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004888 }
4889 else
4890 f1 = f1 / f2;
4891# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 /* We rely on the floating point library to handle divide
4893 * by zero to result in "inf" and not a crash. */
4894 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004895# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004899 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900 return FAIL;
4901 }
4902 rettv->v_type = VAR_FLOAT;
4903 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908 if (op == '*')
4909 n1 = n1 * n2;
4910 else if (op == '/')
4911 {
4912 if (n2 == 0) /* give an error message? */
4913 {
4914 if (n1 == 0)
4915 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4916 else if (n1 < 0)
4917 n1 = -0x7fffffffL;
4918 else
4919 n1 = 0x7fffffffL;
4920 }
4921 else
4922 n1 = n1 / n2;
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925 {
4926 if (n2 == 0) /* give an error message? */
4927 n1 = 0;
4928 else
4929 n1 = n1 % n2;
4930 }
4931 rettv->v_type = VAR_NUMBER;
4932 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 }
4936
4937 return OK;
4938}
4939
4940/*
4941 * Handle sixth level expression:
4942 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004943 * "string" string constant
4944 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * &option-name option value
4946 * @r register contents
4947 * identifier variable value
4948 * function() function call
4949 * $VAR environment variable
4950 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004951 * [expr, expr] List
4952 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 *
4954 * Also handle:
4955 * ! in front logical NOT
4956 * - in front unary minus
4957 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 * trailing [] subscript in String or List
4959 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 *
4961 * "arg" must point to the first non-white of the expression.
4962 * "arg" is advanced to the next non-white after the recognized expression.
4963 *
4964 * Return OK or FAIL.
4965 */
4966 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004967eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004971 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 long n;
4974 int len;
4975 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 char_u *start_leader, *end_leader;
4977 int ret = OK;
4978 char_u *alias;
4979
4980 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
4986 /*
4987 * Skip '!' and '-' characters. They are handled later.
4988 */
4989 start_leader = *arg;
4990 while (**arg == '!' || **arg == '-' || **arg == '+')
4991 *arg = skipwhite(*arg + 1);
4992 end_leader = *arg;
4993
4994 switch (**arg)
4995 {
4996 /*
4997 * Number constant.
4998 */
4999 case '0':
5000 case '1':
5001 case '2':
5002 case '3':
5003 case '4':
5004 case '5':
5005 case '6':
5006 case '7':
5007 case '8':
5008 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010#ifdef FEAT_FLOAT
5011 char_u *p = skipdigits(*arg + 1);
5012 int get_float = FALSE;
5013
5014 /* We accept a float when the format matches
5015 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005016 * strict to avoid backwards compatibility problems.
5017 * Don't look for a float after the "." operator, so that
5018 * ":let vers = 1.2.3" doesn't fail. */
5019 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 get_float = TRUE;
5022 p = skipdigits(p + 2);
5023 if (*p == 'e' || *p == 'E')
5024 {
5025 ++p;
5026 if (*p == '-' || *p == '+')
5027 ++p;
5028 if (!vim_isdigit(*p))
5029 get_float = FALSE;
5030 else
5031 p = skipdigits(p + 1);
5032 }
5033 if (ASCII_ISALPHA(*p) || *p == '.')
5034 get_float = FALSE;
5035 }
5036 if (get_float)
5037 {
5038 float_T f;
5039
5040 *arg += string2float(*arg, &f);
5041 if (evaluate)
5042 {
5043 rettv->v_type = VAR_FLOAT;
5044 rettv->vval.v_float = f;
5045 }
5046 }
5047 else
5048#endif
5049 {
5050 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5051 *arg += len;
5052 if (evaluate)
5053 {
5054 rettv->v_type = VAR_NUMBER;
5055 rettv->vval.v_number = n;
5056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 /*
5062 * String constant: "string".
5063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
5066
5067 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005071 break;
5072
5073 /*
5074 * List: [expr, expr]
5075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078
5079 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 * Dictionary: {key: val, key: val}
5081 */
5082 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5083 break;
5084
5085 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005086 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090
5091 /*
5092 * Environment variable: $VAR.
5093 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096
5097 /*
5098 * Register contents: @r.
5099 */
5100 case '@': ++*arg;
5101 if (evaluate)
5102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005104 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106 if (**arg != NUL)
5107 ++*arg;
5108 break;
5109
5110 /*
5111 * nested expression: (expression).
5112 */
5113 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (**arg == ')')
5116 ++*arg;
5117 else if (ret == OK)
5118 {
5119 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 ret = FAIL;
5122 }
5123 break;
5124
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 break;
5127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128
5129 if (ret == NOTDONE)
5130 {
5131 /*
5132 * Must be a variable or function name.
5133 * Can also be a curly-braces kind of name: {expr}.
5134 */
5135 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005136 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 if (alias != NULL)
5138 s = alias;
5139
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005140 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 ret = FAIL;
5142 else
5143 {
5144 if (**arg == '(') /* recursive! */
5145 {
5146 /* If "s" is the name of a variable of type VAR_FUNC
5147 * use its contents. */
5148 s = deref_func_name(s, &len);
5149
5150 /* Invoke the function. */
5151 ret = get_func_tv(s, len, rettv, arg,
5152 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005153 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005154
5155 /* If evaluate is FALSE rettv->v_type was not set in
5156 * get_func_tv, but it's needed in handle_subscript() to parse
5157 * what follows. So set it here. */
5158 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5159 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005160 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005161 rettv->v_type = VAR_FUNC;
5162 }
5163
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 /* Stop the expression evaluation when immediately
5165 * aborting on error, or when an interrupt occurred or
5166 * an exception was thrown but not caught. */
5167 if (aborting())
5168 {
5169 if (ret == OK)
5170 clear_tv(rettv);
5171 ret = FAIL;
5172 }
5173 }
5174 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005175 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005176 else
5177 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005179 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 }
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 *arg = skipwhite(*arg);
5183
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5185 * expr(expr). */
5186 if (ret == OK)
5187 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 /*
5190 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5191 */
5192 if (ret == OK && evaluate && end_leader > start_leader)
5193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005194 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005195 int val = 0;
5196#ifdef FEAT_FLOAT
5197 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005198
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199 if (rettv->v_type == VAR_FLOAT)
5200 f = rettv->vval.v_float;
5201 else
5202#endif
5203 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005204 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 clear_tv(rettv);
5207 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 else
5210 {
5211 while (end_leader > start_leader)
5212 {
5213 --end_leader;
5214 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 {
5216#ifdef FEAT_FLOAT
5217 if (rettv->v_type == VAR_FLOAT)
5218 f = !f;
5219 else
5220#endif
5221 val = !val;
5222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005224 {
5225#ifdef FEAT_FLOAT
5226 if (rettv->v_type == VAR_FLOAT)
5227 f = -f;
5228 else
5229#endif
5230 val = -val;
5231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005233#ifdef FEAT_FLOAT
5234 if (rettv->v_type == VAR_FLOAT)
5235 {
5236 clear_tv(rettv);
5237 rettv->vval.v_float = f;
5238 }
5239 else
5240#endif
5241 {
5242 clear_tv(rettv);
5243 rettv->v_type = VAR_NUMBER;
5244 rettv->vval.v_number = val;
5245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248
5249 return ret;
5250}
5251
5252/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005253 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5254 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5256 */
5257 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005262 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263{
5264 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 long len = -1;
5268 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005272 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 if (verbose)
5275 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 return FAIL;
5277 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005278#ifdef FEAT_FLOAT
5279 else if (rettv->v_type == VAR_FLOAT)
5280 {
5281 if (verbose)
5282 EMSG(_(e_float_as_string));
5283 return FAIL;
5284 }
5285#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 /*
5290 * dict.name
5291 */
5292 key = *arg + 1;
5293 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5294 ;
5295 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 }
5299 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 /*
5302 * something[idx]
5303 *
5304 * Get the (first) variable from inside the [].
5305 */
5306 *arg = skipwhite(*arg + 1);
5307 if (**arg == ':')
5308 empty1 = TRUE;
5309 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5310 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5312 {
5313 /* not a number or string */
5314 clear_tv(&var1);
5315 return FAIL;
5316 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317
5318 /*
5319 * Get the second variable from inside the [:].
5320 */
5321 if (**arg == ':')
5322 {
5323 range = TRUE;
5324 *arg = skipwhite(*arg + 1);
5325 if (**arg == ']')
5326 empty2 = TRUE;
5327 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 if (!empty1)
5330 clear_tv(&var1);
5331 return FAIL;
5332 }
5333 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5334 {
5335 /* not a number or string */
5336 if (!empty1)
5337 clear_tv(&var1);
5338 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 return FAIL;
5340 }
5341 }
5342
5343 /* Check for the ']'. */
5344 if (**arg != ']')
5345 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346 if (verbose)
5347 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 clear_tv(&var1);
5349 if (range)
5350 clear_tv(&var2);
5351 return FAIL;
5352 }
5353 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 }
5355
5356 if (evaluate)
5357 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 n1 = 0;
5359 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 n1 = get_tv_number(&var1);
5362 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 }
5364 if (range)
5365 {
5366 if (empty2)
5367 n2 = -1;
5368 else
5369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 n2 = get_tv_number(&var2);
5371 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 }
5373 }
5374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 {
5377 case VAR_NUMBER:
5378 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 len = (long)STRLEN(s);
5381 if (range)
5382 {
5383 /* The resulting variable is a substring. If the indexes
5384 * are out of range the result is empty. */
5385 if (n1 < 0)
5386 {
5387 n1 = len + n1;
5388 if (n1 < 0)
5389 n1 = 0;
5390 }
5391 if (n2 < 0)
5392 n2 = len + n2;
5393 else if (n2 >= len)
5394 n2 = len;
5395 if (n1 >= len || n2 < 0 || n1 > n2)
5396 s = NULL;
5397 else
5398 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5399 }
5400 else
5401 {
5402 /* The resulting variable is a string of a single
5403 * character. If the index is too big or negative the
5404 * result is empty. */
5405 if (n1 >= len || n1 < 0)
5406 s = NULL;
5407 else
5408 s = vim_strnsave(s + n1, 1);
5409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 clear_tv(rettv);
5411 rettv->v_type = VAR_STRING;
5412 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 break;
5414
5415 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005416 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 if (n1 < 0)
5418 n1 = len + n1;
5419 if (!empty1 && (n1 < 0 || n1 >= len))
5420 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005421 /* For a range we allow invalid values and return an empty
5422 * list. A list index out of range is an error. */
5423 if (!range)
5424 {
5425 if (verbose)
5426 EMSGN(_(e_listidx), n1);
5427 return FAIL;
5428 }
5429 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 }
5431 if (range)
5432 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005433 list_T *l;
5434 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435
5436 if (n2 < 0)
5437 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005438 else if (n2 >= len)
5439 n2 = len - 1;
5440 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005441 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 l = list_alloc();
5443 if (l == NULL)
5444 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005445 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 n1 <= n2; ++n1)
5447 {
5448 if (list_append_tv(l, &item->li_tv) == FAIL)
5449 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005450 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 return FAIL;
5452 }
5453 item = item->li_next;
5454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 clear_tv(rettv);
5456 rettv->v_type = VAR_LIST;
5457 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005458 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 else
5461 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005462 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 clear_tv(rettv);
5464 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467
5468 case VAR_DICT:
5469 if (range)
5470 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005471 if (verbose)
5472 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005473 if (len == -1)
5474 clear_tv(&var1);
5475 return FAIL;
5476 }
5477 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005479
5480 if (len == -1)
5481 {
5482 key = get_tv_string(&var1);
5483 if (*key == NUL)
5484 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (verbose)
5486 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 clear_tv(&var1);
5488 return FAIL;
5489 }
5490 }
5491
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005492 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005494 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005495 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 if (len == -1)
5497 clear_tv(&var1);
5498 if (item == NULL)
5499 return FAIL;
5500
5501 copy_tv(&item->di_tv, &var1);
5502 clear_tv(rettv);
5503 *rettv = var1;
5504 }
5505 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 }
5507 }
5508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 return OK;
5510}
5511
5512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * Get an option value.
5514 * "arg" points to the '&' or '+' before the option name.
5515 * "arg" is advanced to character after the option name.
5516 * Return OK or FAIL.
5517 */
5518 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005521 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 int evaluate;
5523{
5524 char_u *option_end;
5525 long numval;
5526 char_u *stringval;
5527 int opt_type;
5528 int c;
5529 int working = (**arg == '+'); /* has("+option") */
5530 int ret = OK;
5531 int opt_flags;
5532
5533 /*
5534 * Isolate the option name and find its value.
5535 */
5536 option_end = find_option_end(arg, &opt_flags);
5537 if (option_end == NULL)
5538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 EMSG2(_("E112: Option name missing: %s"), *arg);
5541 return FAIL;
5542 }
5543
5544 if (!evaluate)
5545 {
5546 *arg = option_end;
5547 return OK;
5548 }
5549
5550 c = *option_end;
5551 *option_end = NUL;
5552 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
5555 if (opt_type == -3) /* invalid name */
5556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 EMSG2(_("E113: Unknown option: %s"), *arg);
5559 ret = FAIL;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
5563 if (opt_type == -2) /* hidden string option */
5564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 rettv->v_type = VAR_STRING;
5566 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 else if (opt_type == -1) /* hidden number option */
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 rettv->v_type = VAR_NUMBER;
5571 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 else if (opt_type == 1) /* number option */
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv->v_type = VAR_NUMBER;
5576 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578 else /* string option */
5579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 rettv->v_type = VAR_STRING;
5581 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 }
5584 else if (working && (opt_type == -2 || opt_type == -1))
5585 ret = FAIL;
5586
5587 *option_end = c; /* put back for error messages */
5588 *arg = option_end;
5589
5590 return ret;
5591}
5592
5593/*
5594 * Allocate a variable for a string constant.
5595 * Return OK or FAIL.
5596 */
5597 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 int evaluate;
5602{
5603 char_u *p;
5604 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 int extra = 0;
5606
5607 /*
5608 * Find the end of the string, skipping backslashed characters.
5609 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005610 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
5612 if (*p == '\\' && p[1] != NUL)
5613 {
5614 ++p;
5615 /* A "\<x>" form occupies at least 4 characters, and produces up
5616 * to 6 characters: reserve space for 2 extra */
5617 if (*p == '<')
5618 extra += 2;
5619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621
5622 if (*p != '"')
5623 {
5624 EMSG2(_("E114: Missing quote: %s"), *arg);
5625 return FAIL;
5626 }
5627
5628 /* If only parsing, set *arg and return here */
5629 if (!evaluate)
5630 {
5631 *arg = p + 1;
5632 return OK;
5633 }
5634
5635 /*
5636 * Copy the string into allocated memory, handling backslashed
5637 * characters.
5638 */
5639 name = alloc((unsigned)(p - *arg + extra));
5640 if (name == NULL)
5641 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 rettv->v_type = VAR_STRING;
5643 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
5647 if (*p == '\\')
5648 {
5649 switch (*++p)
5650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 case 'b': *name++ = BS; ++p; break;
5652 case 'e': *name++ = ESC; ++p; break;
5653 case 'f': *name++ = FF; ++p; break;
5654 case 'n': *name++ = NL; ++p; break;
5655 case 'r': *name++ = CAR; ++p; break;
5656 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 case 'X': /* hex: "\x1", "\x12" */
5659 case 'x':
5660 case 'u': /* Unicode: "\u0023" */
5661 case 'U':
5662 if (vim_isxdigit(p[1]))
5663 {
5664 int n, nr;
5665 int c = toupper(*p);
5666
5667 if (c == 'X')
5668 n = 2;
5669 else
5670 n = 4;
5671 nr = 0;
5672 while (--n >= 0 && vim_isxdigit(p[1]))
5673 {
5674 ++p;
5675 nr = (nr << 4) + hex2nr(*p);
5676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678#ifdef FEAT_MBYTE
5679 /* For "\u" store the number according to
5680 * 'encoding'. */
5681 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 else
5684#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 break;
5688
5689 /* octal: "\1", "\12", "\123" */
5690 case '0':
5691 case '1':
5692 case '2':
5693 case '3':
5694 case '4':
5695 case '5':
5696 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 case '7': *name = *p++ - '0';
5698 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 *name = (*name << 3) + *p++ - '0';
5701 if (*p >= '0' && *p <= '7')
5702 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 break;
5706
5707 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (extra != 0)
5710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713 }
5714 /* FALLTHROUGH */
5715
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 break;
5718 }
5719 }
5720 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 *arg = p + 1;
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 return OK;
5728}
5729
5730/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 * Return OK or FAIL.
5733 */
5734 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 int evaluate;
5739{
5740 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 int reduce = 0;
5743
5744 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 */
5747 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5748 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 break;
5753 ++reduce;
5754 ++p;
5755 }
5756 }
5757
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 return FAIL;
5762 }
5763
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 if (!evaluate)
5766 {
5767 *arg = p + 1;
5768 return OK;
5769 }
5770
5771 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 */
5774 str = alloc((unsigned)((p - *arg) - reduce));
5775 if (str == NULL)
5776 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 rettv->v_type = VAR_STRING;
5778 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 break;
5786 ++p;
5787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 *arg = p + 1;
5792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 return OK;
5794}
5795
5796/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 * Allocate a variable for a List and fill it from "*arg".
5798 * Return OK or FAIL.
5799 */
5800 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005801get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005803 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 int evaluate;
5805{
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 list_T *l = NULL;
5807 typval_T tv;
5808 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809
5810 if (evaluate)
5811 {
5812 l = list_alloc();
5813 if (l == NULL)
5814 return FAIL;
5815 }
5816
5817 *arg = skipwhite(*arg + 1);
5818 while (**arg != ']' && **arg != NUL)
5819 {
5820 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5821 goto failret;
5822 if (evaluate)
5823 {
5824 item = listitem_alloc();
5825 if (item != NULL)
5826 {
5827 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005828 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 list_append(l, item);
5830 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005831 else
5832 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 }
5834
5835 if (**arg == ']')
5836 break;
5837 if (**arg != ',')
5838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 goto failret;
5841 }
5842 *arg = skipwhite(*arg + 1);
5843 }
5844
5845 if (**arg != ']')
5846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848failret:
5849 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005850 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 return FAIL;
5852 }
5853
5854 *arg = skipwhite(*arg + 1);
5855 if (evaluate)
5856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005857 rettv->v_type = VAR_LIST;
5858 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 ++l->lv_refcount;
5860 }
5861
5862 return OK;
5863}
5864
5865/*
5866 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005867 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005869 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870list_alloc()
5871{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005872 list_T *l;
5873
5874 l = (list_T *)alloc_clear(sizeof(list_T));
5875 if (l != NULL)
5876 {
5877 /* Prepend the list to the list of lists for garbage collection. */
5878 if (first_list != NULL)
5879 first_list->lv_used_prev = l;
5880 l->lv_used_prev = NULL;
5881 l->lv_used_next = first_list;
5882 first_list = l;
5883 }
5884 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885}
5886
5887/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005888 * Allocate an empty list for a return value.
5889 * Returns OK or FAIL.
5890 */
5891 static int
5892rettv_list_alloc(rettv)
5893 typval_T *rettv;
5894{
5895 list_T *l = list_alloc();
5896
5897 if (l == NULL)
5898 return FAIL;
5899
5900 rettv->vval.v_list = l;
5901 rettv->v_type = VAR_LIST;
5902 ++l->lv_refcount;
5903 return OK;
5904}
5905
5906/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 * Unreference a list: decrement the reference count and free it when it
5908 * becomes zero.
5909 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005910 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005914 if (l != NULL && --l->lv_refcount <= 0)
5915 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916}
5917
5918/*
5919 * Free a list, including all items it points to.
5920 * Ignores the reference count.
5921 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005922 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005923list_free(l, recurse)
5924 list_T *l;
5925 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926{
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 /* Remove the list from the list of lists for garbage collection. */
5930 if (l->lv_used_prev == NULL)
5931 first_list = l->lv_used_next;
5932 else
5933 l->lv_used_prev->lv_used_next = l->lv_used_next;
5934 if (l->lv_used_next != NULL)
5935 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5936
Bram Moolenaard9fba312005-06-26 22:34:35 +00005937 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 /* Remove the item before deleting it. */
5940 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005941 if (recurse || (item->li_tv.v_type != VAR_LIST
5942 && item->li_tv.v_type != VAR_DICT))
5943 clear_tv(&item->li_tv);
5944 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 }
5946 vim_free(l);
5947}
5948
5949/*
5950 * Allocate a list item.
5951 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005952 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953listitem_alloc()
5954{
Bram Moolenaar33570922005-01-25 22:26:29 +00005955 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956}
5957
5958/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005959 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005961 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005963 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 vim_free(item);
5967}
5968
5969/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005970 * Remove a list item from a List and free it. Also clears the value.
5971 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005972 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005973listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005974 list_T *l;
5975 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005976{
5977 list_remove(l, item, item);
5978 listitem_free(item);
5979}
5980
5981/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 * Get the number of items in a list.
5983 */
5984 static long
5985list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 if (l == NULL)
5989 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005990 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991}
5992
5993/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 * Return TRUE when two lists have exactly the same values.
5995 */
5996 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005997list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 list_T *l1;
5999 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006000 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006001 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006005 if (l1 == NULL || l2 == NULL)
6006 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006007 if (l1 == l2)
6008 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006009 if (list_len(l1) != list_len(l2))
6010 return FALSE;
6011
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006012 for (item1 = l1->lv_first, item2 = l2->lv_first;
6013 item1 != NULL && item2 != NULL;
6014 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016 return FALSE;
6017 return item1 == NULL && item2 == NULL;
6018}
6019
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006020#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6021 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006022/*
6023 * Return the dictitem that an entry in a hashtable points to.
6024 */
6025 dictitem_T *
6026dict_lookup(hi)
6027 hashitem_T *hi;
6028{
6029 return HI2DI(hi);
6030}
6031#endif
6032
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034 * Return TRUE when two dictionaries have exactly the same key/values.
6035 */
6036 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006037dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 dict_T *d1;
6039 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006041 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042{
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 hashitem_T *hi;
6044 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006045 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006047 if (d1 == NULL || d2 == NULL)
6048 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006049 if (d1 == d2)
6050 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 if (dict_len(d1) != dict_len(d2))
6052 return FALSE;
6053
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006054 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006057 if (!HASHITEM_EMPTY(hi))
6058 {
6059 item2 = dict_find(d2, hi->hi_key, -1);
6060 if (item2 == NULL)
6061 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006062 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006063 return FALSE;
6064 --todo;
6065 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066 }
6067 return TRUE;
6068}
6069
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006070static int tv_equal_recurse_limit;
6071
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006072/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006073 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006074 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006075 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 */
6077 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 typval_T *tv1;
6080 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 int ic; /* ignore case */
6082 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083{
6084 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006085 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006087 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006089 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006090 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006092 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093 * recursiveness to a limit. We guess they are equal then.
6094 * A fixed limit has the problem of still taking an awful long time.
6095 * Reduce the limit every time running into it. That should work fine for
6096 * deeply linked structures that are not recursively linked and catch
6097 * recursiveness quickly. */
6098 if (!recursive)
6099 tv_equal_recurse_limit = 1000;
6100 if (recursive_cnt >= tv_equal_recurse_limit)
6101 {
6102 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006105
6106 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 ++recursive_cnt;
6110 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6111 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006112 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006113
6114 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115 ++recursive_cnt;
6116 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6117 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006118 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119
6120 case VAR_FUNC:
6121 return (tv1->vval.v_string != NULL
6122 && tv2->vval.v_string != NULL
6123 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6124
6125 case VAR_NUMBER:
6126 return tv1->vval.v_number == tv2->vval.v_number;
6127
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006128#ifdef FEAT_FLOAT
6129 case VAR_FLOAT:
6130 return tv1->vval.v_float == tv2->vval.v_float;
6131#endif
6132
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133 case VAR_STRING:
6134 s1 = get_tv_string_buf(tv1, buf1);
6135 s2 = get_tv_string_buf(tv2, buf2);
6136 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006137 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006138
6139 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006140 return TRUE;
6141}
6142
6143/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144 * Locate item with index "n" in list "l" and return it.
6145 * A negative index is counted from the end; -1 is the last item.
6146 * Returns NULL when "n" is out of range.
6147 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006148 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 long n;
6152{
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 long idx;
6155
6156 if (l == NULL)
6157 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006158
6159 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006161 n = l->lv_len + n;
6162
6163 /* Check for index out of range. */
6164 if (n < 0 || n >= l->lv_len)
6165 return NULL;
6166
6167 /* When there is a cached index may start search from there. */
6168 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006170 if (n < l->lv_idx / 2)
6171 {
6172 /* closest to the start of the list */
6173 item = l->lv_first;
6174 idx = 0;
6175 }
6176 else if (n > (l->lv_idx + l->lv_len) / 2)
6177 {
6178 /* closest to the end of the list */
6179 item = l->lv_last;
6180 idx = l->lv_len - 1;
6181 }
6182 else
6183 {
6184 /* closest to the cached index */
6185 item = l->lv_idx_item;
6186 idx = l->lv_idx;
6187 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 }
6189 else
6190 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006191 if (n < l->lv_len / 2)
6192 {
6193 /* closest to the start of the list */
6194 item = l->lv_first;
6195 idx = 0;
6196 }
6197 else
6198 {
6199 /* closest to the end of the list */
6200 item = l->lv_last;
6201 idx = l->lv_len - 1;
6202 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006204
6205 while (n > idx)
6206 {
6207 /* search forward */
6208 item = item->li_next;
6209 ++idx;
6210 }
6211 while (n < idx)
6212 {
6213 /* search backward */
6214 item = item->li_prev;
6215 --idx;
6216 }
6217
6218 /* cache the used index */
6219 l->lv_idx = idx;
6220 l->lv_idx_item = item;
6221
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222 return item;
6223}
6224
6225/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006226 * Get list item "l[idx]" as a number.
6227 */
6228 static long
6229list_find_nr(l, idx, errorp)
6230 list_T *l;
6231 long idx;
6232 int *errorp; /* set to TRUE when something wrong */
6233{
6234 listitem_T *li;
6235
6236 li = list_find(l, idx);
6237 if (li == NULL)
6238 {
6239 if (errorp != NULL)
6240 *errorp = TRUE;
6241 return -1L;
6242 }
6243 return get_tv_number_chk(&li->li_tv, errorp);
6244}
6245
6246/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006247 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6248 */
6249 char_u *
6250list_find_str(l, idx)
6251 list_T *l;
6252 long idx;
6253{
6254 listitem_T *li;
6255
6256 li = list_find(l, idx - 1);
6257 if (li == NULL)
6258 {
6259 EMSGN(_(e_listidx), idx);
6260 return NULL;
6261 }
6262 return get_tv_string(&li->li_tv);
6263}
6264
6265/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006266 * Locate "item" list "l" and return its index.
6267 * Returns -1 when "item" is not in the list.
6268 */
6269 static long
6270list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006271 list_T *l;
6272 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006273{
6274 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006276
6277 if (l == NULL)
6278 return -1;
6279 idx = 0;
6280 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6281 ++idx;
6282 if (li == NULL)
6283 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006284 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006285}
6286
6287/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 * Append item "item" to the end of list "l".
6289 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006290 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 list_T *l;
6293 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294{
6295 if (l->lv_last == NULL)
6296 {
6297 /* empty list */
6298 l->lv_first = item;
6299 l->lv_last = item;
6300 item->li_prev = NULL;
6301 }
6302 else
6303 {
6304 l->lv_last->li_next = item;
6305 item->li_prev = l->lv_last;
6306 l->lv_last = item;
6307 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 item->li_next = NULL;
6310}
6311
6312/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006316 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 list_T *l;
6319 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006321 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 copy_tv(tv, &li->li_tv);
6326 list_append(l, li);
6327 return OK;
6328}
6329
6330/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006331 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006332 * Return FAIL when out of memory.
6333 */
6334 int
6335list_append_dict(list, dict)
6336 list_T *list;
6337 dict_T *dict;
6338{
6339 listitem_T *li = listitem_alloc();
6340
6341 if (li == NULL)
6342 return FAIL;
6343 li->li_tv.v_type = VAR_DICT;
6344 li->li_tv.v_lock = 0;
6345 li->li_tv.vval.v_dict = dict;
6346 list_append(list, li);
6347 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return OK;
6349}
6350
6351/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006352 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006353 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354 * Returns FAIL when out of memory.
6355 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006357list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006358 list_T *l;
6359 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006360 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361{
6362 listitem_T *li = listitem_alloc();
6363
6364 if (li == NULL)
6365 return FAIL;
6366 list_append(l, li);
6367 li->li_tv.v_type = VAR_STRING;
6368 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006369 if (str == NULL)
6370 li->li_tv.vval.v_string = NULL;
6371 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006372 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006373 return FAIL;
6374 return OK;
6375}
6376
6377/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006378 * Append "n" to list "l".
6379 * Returns FAIL when out of memory.
6380 */
6381 static int
6382list_append_number(l, n)
6383 list_T *l;
6384 varnumber_T n;
6385{
6386 listitem_T *li;
6387
6388 li = listitem_alloc();
6389 if (li == NULL)
6390 return FAIL;
6391 li->li_tv.v_type = VAR_NUMBER;
6392 li->li_tv.v_lock = 0;
6393 li->li_tv.vval.v_number = n;
6394 list_append(l, li);
6395 return OK;
6396}
6397
6398/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006400 * If "item" is NULL append at the end.
6401 * Return FAIL when out of memory.
6402 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006403 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 list_T *l;
6406 typval_T *tv;
6407 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006408{
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410
6411 if (ni == NULL)
6412 return FAIL;
6413 copy_tv(tv, &ni->li_tv);
6414 if (item == NULL)
6415 /* Append new item at end of list. */
6416 list_append(l, ni);
6417 else
6418 {
6419 /* Insert new item before existing item. */
6420 ni->li_prev = item->li_prev;
6421 ni->li_next = item;
6422 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 ++l->lv_idx;
6426 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 l->lv_idx_item = NULL;
6431 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006433 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 }
6435 return OK;
6436}
6437
6438/*
6439 * Extend "l1" with "l2".
6440 * If "bef" is NULL append at the end, otherwise insert before this item.
6441 * Returns FAIL when out of memory.
6442 */
6443 static int
6444list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 list_T *l1;
6446 list_T *l2;
6447 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448{
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006450 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006452 /* We also quit the loop when we have inserted the original item count of
6453 * the list, avoid a hang when we extend a list with itself. */
6454 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6456 return FAIL;
6457 return OK;
6458}
6459
6460/*
6461 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6462 * Return FAIL when out of memory.
6463 */
6464 static int
6465list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 list_T *l1;
6467 list_T *l2;
6468 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469{
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006472 if (l1 == NULL || l2 == NULL)
6473 return FAIL;
6474
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006476 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477 if (l == NULL)
6478 return FAIL;
6479 tv->v_type = VAR_LIST;
6480 tv->vval.v_list = l;
6481
6482 /* append all items from the second list */
6483 return list_extend(l, l2, NULL);
6484}
6485
6486/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 * Returns NULL when out of memory.
6491 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006493list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497{
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 list_T *copy;
6499 listitem_T *item;
6500 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501
6502 if (orig == NULL)
6503 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504
6505 copy = list_alloc();
6506 if (copy != NULL)
6507 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 if (copyID != 0)
6509 {
6510 /* Do this before adding the items, because one of the items may
6511 * refer back to this list. */
6512 orig->lv_copyID = copyID;
6513 orig->lv_copylist = copy;
6514 }
6515 for (item = orig->lv_first; item != NULL && !got_int;
6516 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 {
6518 ni = listitem_alloc();
6519 if (ni == NULL)
6520 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 {
6523 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6524 {
6525 vim_free(ni);
6526 break;
6527 }
6528 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006530 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 list_append(copy, ni);
6532 }
6533 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 if (item != NULL)
6535 {
6536 list_unref(copy);
6537 copy = NULL;
6538 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 }
6540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 return copy;
6542}
6543
6544/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006546 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006548 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006549list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006550 list_T *l;
6551 listitem_T *item;
6552 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556 /* notify watchers */
6557 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006559 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 list_fix_watch(l, ip);
6561 if (ip == item2)
6562 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564
6565 if (item2->li_next == NULL)
6566 l->lv_last = item->li_prev;
6567 else
6568 item2->li_next->li_prev = item->li_prev;
6569 if (item->li_prev == NULL)
6570 l->lv_first = item2->li_next;
6571 else
6572 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006573 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574}
6575
6576/*
6577 * Return an allocated string with the string representation of a list.
6578 * May return NULL.
6579 */
6580 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006581list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584{
6585 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586
6587 if (tv->vval.v_list == NULL)
6588 return NULL;
6589 ga_init2(&ga, (int)sizeof(char), 80);
6590 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006591 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 {
6593 vim_free(ga.ga_data);
6594 return NULL;
6595 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 ga_append(&ga, ']');
6597 ga_append(&ga, NUL);
6598 return (char_u *)ga.ga_data;
6599}
6600
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006601typedef struct join_S {
6602 char_u *s;
6603 char_u *tofree;
6604} join_T;
6605
6606 static int
6607list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6608 garray_T *gap; /* to store the result in */
6609 list_T *l;
6610 char_u *sep;
6611 int echo_style;
6612 int copyID;
6613 garray_T *join_gap; /* to keep each list item string */
6614{
6615 int i;
6616 join_T *p;
6617 int len;
6618 int sumlen = 0;
6619 int first = TRUE;
6620 char_u *tofree;
6621 char_u numbuf[NUMBUFLEN];
6622 listitem_T *item;
6623 char_u *s;
6624
6625 /* Stringify each item in the list. */
6626 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6627 {
6628 if (echo_style)
6629 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6630 else
6631 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6632 if (s == NULL)
6633 return FAIL;
6634
6635 len = (int)STRLEN(s);
6636 sumlen += len;
6637
6638 ga_grow(join_gap, 1);
6639 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6640 if (tofree != NULL || s != numbuf)
6641 {
6642 p->s = s;
6643 p->tofree = tofree;
6644 }
6645 else
6646 {
6647 p->s = vim_strnsave(s, len);
6648 p->tofree = p->s;
6649 }
6650
6651 line_breakcheck();
6652 }
6653
6654 /* Allocate result buffer with its total size, avoid re-allocation and
6655 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6656 if (join_gap->ga_len >= 2)
6657 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6658 if (ga_grow(gap, sumlen + 2) == FAIL)
6659 return FAIL;
6660
6661 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6662 {
6663 if (first)
6664 first = FALSE;
6665 else
6666 ga_concat(gap, sep);
6667 p = ((join_T *)join_gap->ga_data) + i;
6668
6669 if (p->s != NULL)
6670 ga_concat(gap, p->s);
6671 line_breakcheck();
6672 }
6673
6674 return OK;
6675}
6676
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006679 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006680 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006683list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006687 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006688 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006690 garray_T join_ga;
6691 int retval;
6692 join_T *p;
6693 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006694
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006695 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6696 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6697
6698 /* Dispose each item in join_ga. */
6699 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006700 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006701 p = (join_T *)join_ga.ga_data;
6702 for (i = 0; i < join_ga.ga_len; ++i)
6703 {
6704 vim_free(p->tofree);
6705 ++p;
6706 }
6707 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006708 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006709
6710 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711}
6712
6713/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006714 * Garbage collection for lists and dictionaries.
6715 *
6716 * We use reference counts to be able to free most items right away when they
6717 * are no longer used. But for composite items it's possible that it becomes
6718 * unused while the reference count is > 0: When there is a recursive
6719 * reference. Example:
6720 * :let l = [1, 2, 3]
6721 * :let d = {9: l}
6722 * :let l[1] = d
6723 *
6724 * Since this is quite unusual we handle this with garbage collection: every
6725 * once in a while find out which lists and dicts are not referenced from any
6726 * variable.
6727 *
6728 * Here is a good reference text about garbage collection (refers to Python
6729 * but it applies to all reference-counting mechanisms):
6730 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006732
6733/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 * Do garbage collection for lists and dicts.
6735 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006736 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 int
6738garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006739{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006741 buf_T *buf;
6742 win_T *wp;
6743 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006744 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006745 int did_free;
6746 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006747#ifdef FEAT_WINDOWS
6748 tabpage_T *tp;
6749#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006751 /* Only do this once. */
6752 want_garbage_collect = FALSE;
6753 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006754 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006755
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006756 /* We advance by two because we add one for items referenced through
6757 * previous_funccal. */
6758 current_copyID += COPYID_INC;
6759 copyID = current_copyID;
6760
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006761 /*
6762 * 1. Go through all accessible variables and mark all lists and dicts
6763 * with copyID.
6764 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006765
6766 /* Don't free variables in the previous_funccal list unless they are only
6767 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006768 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006769 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6770 {
6771 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6772 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6773 }
6774
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 /* script-local variables */
6776 for (i = 1; i <= ga_scripts.ga_len; ++i)
6777 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6778
6779 /* buffer-local variables */
6780 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006781 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006782
6783 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006784 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006785 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006786#ifdef FEAT_AUTOCMD
6787 if (aucmd_win != NULL)
6788 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6789#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006791#ifdef FEAT_WINDOWS
6792 /* tabpage-local variables */
6793 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006794 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006795#endif
6796
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006797 /* global variables */
6798 set_ref_in_ht(&globvarht, copyID);
6799
6800 /* function-local variables */
6801 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6802 {
6803 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6804 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6805 }
6806
Bram Moolenaard812df62008-11-09 12:46:09 +00006807 /* v: vars */
6808 set_ref_in_ht(&vimvarht, copyID);
6809
Bram Moolenaar1dced572012-04-05 16:54:08 +02006810#ifdef FEAT_LUA
6811 set_ref_in_lua(copyID);
6812#endif
6813
Bram Moolenaardb913952012-06-29 12:54:53 +02006814#ifdef FEAT_PYTHON
6815 set_ref_in_python(copyID);
6816#endif
6817
6818#ifdef FEAT_PYTHON3
6819 set_ref_in_python3(copyID);
6820#endif
6821
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006822 /*
6823 * 2. Free lists and dictionaries that are not referenced.
6824 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006825 did_free = free_unref_items(copyID);
6826
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006827 /*
6828 * 3. Check if any funccal can be freed now.
6829 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 for (pfc = &previous_funccal; *pfc != NULL; )
6831 {
6832 if (can_free_funccal(*pfc, copyID))
6833 {
6834 fc = *pfc;
6835 *pfc = fc->caller;
6836 free_funccal(fc, TRUE);
6837 did_free = TRUE;
6838 did_free_funccal = TRUE;
6839 }
6840 else
6841 pfc = &(*pfc)->caller;
6842 }
6843 if (did_free_funccal)
6844 /* When a funccal was freed some more items might be garbage
6845 * collected, so run again. */
6846 (void)garbage_collect();
6847
6848 return did_free;
6849}
6850
6851/*
6852 * Free lists and dictionaries that are no longer referenced.
6853 */
6854 static int
6855free_unref_items(copyID)
6856 int copyID;
6857{
6858 dict_T *dd;
6859 list_T *ll;
6860 int did_free = FALSE;
6861
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 */
6865 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006868 /* Free the Dictionary and ordinary items it contains, but don't
6869 * recurse into Lists and Dictionaries, they will be in the list
6870 * of dicts or list of lists. */
6871 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 did_free = TRUE;
6873
6874 /* restart, next dict may also have been freed */
6875 dd = first_dict;
6876 }
6877 else
6878 dd = dd->dv_used_next;
6879
6880 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 * Go through the list of lists and free items without the copyID.
6882 * But don't free a list that has a watcher (used in a for loop), these
6883 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 */
6885 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6887 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006889 /* Free the List and ordinary items it contains, but don't recurse
6890 * into Lists and Dictionaries, they will be in the list of dicts
6891 * or list of lists. */
6892 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 did_free = TRUE;
6894
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006895 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 ll = first_list;
6897 }
6898 else
6899 ll = ll->lv_used_next;
6900
6901 return did_free;
6902}
6903
6904/*
6905 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6906 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006907 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908set_ref_in_ht(ht, copyID)
6909 hashtab_T *ht;
6910 int copyID;
6911{
6912 int todo;
6913 hashitem_T *hi;
6914
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006915 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 for (hi = ht->ht_array; todo > 0; ++hi)
6917 if (!HASHITEM_EMPTY(hi))
6918 {
6919 --todo;
6920 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6921 }
6922}
6923
6924/*
6925 * Mark all lists and dicts referenced through list "l" with "copyID".
6926 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006927 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928set_ref_in_list(l, copyID)
6929 list_T *l;
6930 int copyID;
6931{
6932 listitem_T *li;
6933
6934 for (li = l->lv_first; li != NULL; li = li->li_next)
6935 set_ref_in_item(&li->li_tv, copyID);
6936}
6937
6938/*
6939 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6940 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006941 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006942set_ref_in_item(tv, copyID)
6943 typval_T *tv;
6944 int copyID;
6945{
6946 dict_T *dd;
6947 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006948
6949 switch (tv->v_type)
6950 {
6951 case VAR_DICT:
6952 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006953 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006954 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955 /* Didn't see this dict yet. */
6956 dd->dv_copyID = copyID;
6957 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006958 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006960
6961 case VAR_LIST:
6962 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006963 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006964 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965 /* Didn't see this list yet. */
6966 ll->lv_copyID = copyID;
6967 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006968 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006972}
6973
6974/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975 * Allocate an empty header for a dictionary.
6976 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006977 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978dict_alloc()
6979{
Bram Moolenaar33570922005-01-25 22:26:29 +00006980 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006981
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006985 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 if (first_dict != NULL)
6987 first_dict->dv_used_prev = d;
6988 d->dv_used_next = first_dict;
6989 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006990 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
Bram Moolenaar33570922005-01-25 22:26:29 +00006992 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006993 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006994 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006995 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006996 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006998 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006999}
7000
7001/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007002 * Allocate an empty dict for a return value.
7003 * Returns OK or FAIL.
7004 */
7005 static int
7006rettv_dict_alloc(rettv)
7007 typval_T *rettv;
7008{
7009 dict_T *d = dict_alloc();
7010
7011 if (d == NULL)
7012 return FAIL;
7013
7014 rettv->vval.v_dict = d;
7015 rettv->v_type = VAR_DICT;
7016 ++d->dv_refcount;
7017 return OK;
7018}
7019
7020
7021/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022 * Unreference a Dictionary: decrement the reference count and free it when it
7023 * becomes zero.
7024 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007025 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007029 if (d != NULL && --d->dv_refcount <= 0)
7030 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031}
7032
7033/*
7034 * Free a Dictionary, including all items it contains.
7035 * Ignores the reference count.
7036 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007037 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007038dict_free(d, recurse)
7039 dict_T *d;
7040 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007042 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007044 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 /* Remove the dict from the list of dicts for garbage collection. */
7047 if (d->dv_used_prev == NULL)
7048 first_dict = d->dv_used_next;
7049 else
7050 d->dv_used_prev->dv_used_next = d->dv_used_next;
7051 if (d->dv_used_next != NULL)
7052 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7053
7054 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007056 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 if (!HASHITEM_EMPTY(hi))
7060 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007061 /* Remove the item before deleting it, just in case there is
7062 * something recursive causing trouble. */
7063 di = HI2DI(hi);
7064 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007065 if (recurse || (di->di_tv.v_type != VAR_LIST
7066 && di->di_tv.v_type != VAR_DICT))
7067 clear_tv(&di->di_tv);
7068 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 --todo;
7070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073 vim_free(d);
7074}
7075
7076/*
7077 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078 * The "key" is copied to the new item.
7079 * Note that the value of the item "di_tv" still needs to be initialized!
7080 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007082 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083dictitem_alloc(key)
7084 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085{
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007088 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007092 di->di_flags = 0;
7093 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095}
7096
7097/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098 * Make a copy of a Dictionary item.
7099 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007101dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103{
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007106 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7107 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108 if (di != NULL)
7109 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007111 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 copy_tv(&org->di_tv, &di->di_tv);
7113 }
7114 return di;
7115}
7116
7117/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118 * Remove item "item" from Dictionary "dict" and free it.
7119 */
7120 static void
7121dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 dict_T *dict;
7123 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124{
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 if (HASHITEM_EMPTY(hi))
7129 EMSG2(_(e_intern2), "dictitem_remove()");
7130 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 dictitem_free(item);
7133}
7134
7135/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136 * Free a dict item. Also clears the value.
7137 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007138 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007142 clear_tv(&item->di_tv);
7143 vim_free(item);
7144}
7145
7146/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7148 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007149 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 * Returns NULL when out of memory.
7151 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007153dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007156 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157{
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 dict_T *copy;
7159 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162
7163 if (orig == NULL)
7164 return NULL;
7165
7166 copy = dict_alloc();
7167 if (copy != NULL)
7168 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 if (copyID != 0)
7170 {
7171 orig->dv_copyID = copyID;
7172 orig->dv_copydict = copy;
7173 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007174 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007175 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 --todo;
7180
7181 di = dictitem_alloc(hi->hi_key);
7182 if (di == NULL)
7183 break;
7184 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007185 {
7186 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7187 copyID) == FAIL)
7188 {
7189 vim_free(di);
7190 break;
7191 }
7192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 else
7194 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7195 if (dict_add(copy, di) == FAIL)
7196 {
7197 dictitem_free(di);
7198 break;
7199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 if (todo > 0)
7205 {
7206 dict_unref(copy);
7207 copy = NULL;
7208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 }
7210
7211 return copy;
7212}
7213
7214/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007216 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007218 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007220 dict_T *d;
7221 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222{
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224}
7225
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007227 * Add a number or string entry to dictionary "d".
7228 * When "str" is NULL use number "nr", otherwise use "str".
7229 * Returns FAIL when out of memory and when key already exists.
7230 */
7231 int
7232dict_add_nr_str(d, key, nr, str)
7233 dict_T *d;
7234 char *key;
7235 long nr;
7236 char_u *str;
7237{
7238 dictitem_T *item;
7239
7240 item = dictitem_alloc((char_u *)key);
7241 if (item == NULL)
7242 return FAIL;
7243 item->di_tv.v_lock = 0;
7244 if (str == NULL)
7245 {
7246 item->di_tv.v_type = VAR_NUMBER;
7247 item->di_tv.vval.v_number = nr;
7248 }
7249 else
7250 {
7251 item->di_tv.v_type = VAR_STRING;
7252 item->di_tv.vval.v_string = vim_strsave(str);
7253 }
7254 if (dict_add(d, item) == FAIL)
7255 {
7256 dictitem_free(item);
7257 return FAIL;
7258 }
7259 return OK;
7260}
7261
7262/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007263 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007264 * Returns FAIL when out of memory and when key already exists.
7265 */
7266 int
7267dict_add_list(d, key, list)
7268 dict_T *d;
7269 char *key;
7270 list_T *list;
7271{
7272 dictitem_T *item;
7273
7274 item = dictitem_alloc((char_u *)key);
7275 if (item == NULL)
7276 return FAIL;
7277 item->di_tv.v_lock = 0;
7278 item->di_tv.v_type = VAR_LIST;
7279 item->di_tv.vval.v_list = list;
7280 if (dict_add(d, item) == FAIL)
7281 {
7282 dictitem_free(item);
7283 return FAIL;
7284 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007285 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007286 return OK;
7287}
7288
7289/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 * Get the number of items in a Dictionary.
7291 */
7292 static long
7293dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 if (d == NULL)
7297 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299}
7300
7301/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 * Find item "key[len]" in Dictionary "d".
7303 * If "len" is negative use strlen(key).
7304 * Returns NULL when not found.
7305 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007306 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 char_u *key;
7310 int len;
7311{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312#define AKEYLEN 200
7313 char_u buf[AKEYLEN];
7314 char_u *akey;
7315 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 if (len < 0)
7319 akey = key;
7320 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 tofree = akey = vim_strnsave(key, len);
7323 if (akey == NULL)
7324 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326 else
7327 {
7328 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007329 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 akey = buf;
7331 }
7332
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 vim_free(tofree);
7335 if (HASHITEM_EMPTY(hi))
7336 return NULL;
7337 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338}
7339
7340/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007341 * Get a string item from a dictionary.
7342 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007343 * Returns NULL if the entry doesn't exist or out of memory.
7344 */
7345 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007347 dict_T *d;
7348 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007349 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007350{
7351 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007352 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007353
7354 di = dict_find(d, key, -1);
7355 if (di == NULL)
7356 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007357 s = get_tv_string(&di->di_tv);
7358 if (save && s != NULL)
7359 s = vim_strsave(s);
7360 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007361}
7362
7363/*
7364 * Get a number item from a dictionary.
7365 * Returns 0 if the entry doesn't exist or out of memory.
7366 */
7367 long
7368get_dict_number(d, key)
7369 dict_T *d;
7370 char_u *key;
7371{
7372 dictitem_T *di;
7373
7374 di = dict_find(d, key, -1);
7375 if (di == NULL)
7376 return 0;
7377 return get_tv_number(&di->di_tv);
7378}
7379
7380/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381 * Return an allocated string with the string representation of a Dictionary.
7382 * May return NULL.
7383 */
7384 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007385dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388{
7389 garray_T ga;
7390 int first = TRUE;
7391 char_u *tofree;
7392 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 return NULL;
7400 ga_init2(&ga, (int)sizeof(char), 80);
7401 ga_append(&ga, '{');
7402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007403 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007404 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 --todo;
7409
7410 if (first)
7411 first = FALSE;
7412 else
7413 ga_concat(&ga, (char_u *)", ");
7414
7415 tofree = string_quote(hi->hi_key, FALSE);
7416 if (tofree != NULL)
7417 {
7418 ga_concat(&ga, tofree);
7419 vim_free(tofree);
7420 }
7421 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007422 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007423 if (s != NULL)
7424 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007426 if (s == NULL)
7427 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007430 if (todo > 0)
7431 {
7432 vim_free(ga.ga_data);
7433 return NULL;
7434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435
7436 ga_append(&ga, '}');
7437 ga_append(&ga, NUL);
7438 return (char_u *)ga.ga_data;
7439}
7440
7441/*
7442 * Allocate a variable for a Dictionary and fill it from "*arg".
7443 * Return OK or FAIL. Returns NOTDONE for {expr}.
7444 */
7445 static int
7446get_dict_tv(arg, rettv, evaluate)
7447 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 int evaluate;
7450{
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 dict_T *d = NULL;
7452 typval_T tvkey;
7453 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007454 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007457 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458
7459 /*
7460 * First check if it's not a curly-braces thing: {expr}.
7461 * Must do this without evaluating, otherwise a function may be called
7462 * twice. Unfortunately this means we need to call eval1() twice for the
7463 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 if (*start != '}')
7467 {
7468 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7469 return FAIL;
7470 if (*start == '}')
7471 return NOTDONE;
7472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473
7474 if (evaluate)
7475 {
7476 d = dict_alloc();
7477 if (d == NULL)
7478 return FAIL;
7479 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 tvkey.v_type = VAR_UNKNOWN;
7481 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482
7483 *arg = skipwhite(*arg + 1);
7484 while (**arg != '}' && **arg != NUL)
7485 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 goto failret;
7488 if (**arg != ':')
7489 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007490 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 goto failret;
7493 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007494 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007496 key = get_tv_string_buf_chk(&tvkey, buf);
7497 if (key == NULL || *key == NUL)
7498 {
7499 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7500 if (key != NULL)
7501 EMSG(_(e_emptykey));
7502 clear_tv(&tvkey);
7503 goto failret;
7504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
7507 *arg = skipwhite(*arg + 1);
7508 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7509 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007510 if (evaluate)
7511 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 goto failret;
7513 }
7514 if (evaluate)
7515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 if (item != NULL)
7518 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007519 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 clear_tv(&tv);
7522 goto failret;
7523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 item = dictitem_alloc(key);
7525 clear_tv(&tvkey);
7526 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007529 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 if (dict_add(d, item) == FAIL)
7531 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 }
7533 }
7534
7535 if (**arg == '}')
7536 break;
7537 if (**arg != ',')
7538 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007539 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 goto failret;
7541 }
7542 *arg = skipwhite(*arg + 1);
7543 }
7544
7545 if (**arg != '}')
7546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007547 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548failret:
7549 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007550 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 return FAIL;
7552 }
7553
7554 *arg = skipwhite(*arg + 1);
7555 if (evaluate)
7556 {
7557 rettv->v_type = VAR_DICT;
7558 rettv->vval.v_dict = d;
7559 ++d->dv_refcount;
7560 }
7561
7562 return OK;
7563}
7564
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 * Return a string with the string representation of a variable.
7567 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007568 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007569 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007570 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007571 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572 */
7573 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007574echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007576 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007577 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007578 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 static int recurse = 0;
7581 char_u *r = NULL;
7582
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007585 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 *tofree = NULL;
7587 return NULL;
7588 }
7589 ++recurse;
7590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007591 switch (tv->v_type)
7592 {
7593 case VAR_FUNC:
7594 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007595 r = tv->vval.v_string;
7596 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007598 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 if (tv->vval.v_list == NULL)
7600 {
7601 *tofree = NULL;
7602 r = NULL;
7603 }
7604 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7605 {
7606 *tofree = NULL;
7607 r = (char_u *)"[...]";
7608 }
7609 else
7610 {
7611 tv->vval.v_list->lv_copyID = copyID;
7612 *tofree = list2string(tv, copyID);
7613 r = *tofree;
7614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007616
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618 if (tv->vval.v_dict == NULL)
7619 {
7620 *tofree = NULL;
7621 r = NULL;
7622 }
7623 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7624 {
7625 *tofree = NULL;
7626 r = (char_u *)"{...}";
7627 }
7628 else
7629 {
7630 tv->vval.v_dict->dv_copyID = copyID;
7631 *tofree = dict2string(tv, copyID);
7632 r = *tofree;
7633 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 case VAR_STRING:
7637 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 *tofree = NULL;
7639 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007640 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007641
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007642#ifdef FEAT_FLOAT
7643 case VAR_FLOAT:
7644 *tofree = NULL;
7645 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7646 r = numbuf;
7647 break;
7648#endif
7649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007651 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007652 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654
7655 --recurse;
7656 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007657}
7658
7659/*
7660 * Return a string with the string representation of a variable.
7661 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7662 * "numbuf" is used for a number.
7663 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007664 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665 */
7666 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007667tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007669 char_u **tofree;
7670 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007671 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672{
7673 switch (tv->v_type)
7674 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675 case VAR_FUNC:
7676 *tofree = string_quote(tv->vval.v_string, TRUE);
7677 return *tofree;
7678 case VAR_STRING:
7679 *tofree = string_quote(tv->vval.v_string, FALSE);
7680 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007681#ifdef FEAT_FLOAT
7682 case VAR_FLOAT:
7683 *tofree = NULL;
7684 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7685 return numbuf;
7686#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007691 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695}
7696
7697/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 * Return string "str" in ' quotes, doubling ' characters.
7699 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 */
7702 static char_u *
7703string_quote(str, function)
7704 char_u *str;
7705 int function;
7706{
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007708 char_u *p, *r, *s;
7709
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 len = (function ? 13 : 3);
7711 if (str != NULL)
7712 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007713 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 for (p = str; *p != NUL; mb_ptr_adv(p))
7715 if (*p == '\'')
7716 ++len;
7717 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007718 s = r = alloc(len);
7719 if (r != NULL)
7720 {
7721 if (function)
7722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 r += 10;
7725 }
7726 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 if (str != NULL)
7729 for (p = str; *p != NUL; )
7730 {
7731 if (*p == '\'')
7732 *r++ = '\'';
7733 MB_COPY_CHAR(p, r);
7734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007736 if (function)
7737 *r++ = ')';
7738 *r++ = NUL;
7739 }
7740 return s;
7741}
7742
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007743#ifdef FEAT_FLOAT
7744/*
7745 * Convert the string "text" to a floating point number.
7746 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7747 * this always uses a decimal point.
7748 * Returns the length of the text that was consumed.
7749 */
7750 static int
7751string2float(text, value)
7752 char_u *text;
7753 float_T *value; /* result stored here */
7754{
7755 char *s = (char *)text;
7756 float_T f;
7757
7758 f = strtod(s, &s);
7759 *value = f;
7760 return (int)((char_u *)s - text);
7761}
7762#endif
7763
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 * Get the value of an environment variable.
7766 * "arg" is pointing to the '$'. It is advanced to after the name.
7767 * If the environment variable was not set, silently assume it is empty.
7768 * Always return OK.
7769 */
7770 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 int evaluate;
7775{
7776 char_u *string = NULL;
7777 int len;
7778 int cc;
7779 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007780 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
7782 ++*arg;
7783 name = *arg;
7784 len = get_env_len(arg);
7785 if (evaluate)
7786 {
7787 if (len != 0)
7788 {
7789 cc = name[len];
7790 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007791 /* first try vim_getenv(), fast for normal environment vars */
7792 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007794 {
7795 if (!mustfree)
7796 string = vim_strsave(string);
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 else
7799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007800 if (mustfree)
7801 vim_free(string);
7802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 /* next try expanding things like $VIM and ${HOME} */
7804 string = expand_env_save(name - 1);
7805 if (string != NULL && *string == '$')
7806 {
7807 vim_free(string);
7808 string = NULL;
7809 }
7810 }
7811 name[len] = cc;
7812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813 rettv->v_type = VAR_STRING;
7814 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 }
7816
7817 return OK;
7818}
7819
7820/*
7821 * Array with names and number of arguments of all internal functions
7822 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7823 */
7824static struct fst
7825{
7826 char *f_name; /* function name */
7827 char f_min_argc; /* minimal number of arguments */
7828 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007830 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831} functions[] =
7832{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007833#ifdef FEAT_FLOAT
7834 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007835 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007837 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007838 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"append", 2, 2, f_append},
7840 {"argc", 0, 0, f_argc},
7841 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007842 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007843#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007844 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007846 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007849 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"bufexists", 1, 1, f_bufexists},
7851 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7852 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7853 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7854 {"buflisted", 1, 1, f_buflisted},
7855 {"bufloaded", 1, 1, f_bufloaded},
7856 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007857 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"bufwinnr", 1, 1, f_bufwinnr},
7859 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007860 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862#ifdef FEAT_FLOAT
7863 {"ceil", 1, 1, f_ceil},
7864#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007865 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007866 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007868 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007870#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007871 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007872 {"complete_add", 1, 1, f_complete_add},
7873 {"complete_check", 0, 0, f_complete_check},
7874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877#ifdef FEAT_FLOAT
7878 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007879 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007883 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007884 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"delete", 1, 1, f_delete},
7886 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007887 {"diff_filler", 1, 1, f_diff_filler},
7888 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007889 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"eventhandler", 0, 0, f_eventhandler},
7893 {"executable", 1, 1, f_executable},
7894 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007895#ifdef FEAT_FLOAT
7896 {"exp", 1, 1, f_exp},
7897#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007898 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007899 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007900 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7902 {"filereadable", 1, 1, f_filereadable},
7903 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007905 {"finddir", 1, 3, f_finddir},
7906 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907#ifdef FEAT_FLOAT
7908 {"float2nr", 1, 1, f_float2nr},
7909 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007910 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007912 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"fnamemodify", 2, 2, f_fnamemodify},
7914 {"foldclosed", 1, 1, f_foldclosed},
7915 {"foldclosedend", 1, 1, f_foldclosedend},
7916 {"foldlevel", 1, 1, f_foldlevel},
7917 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007918 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007921 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007922 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007923 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007924 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"getchar", 0, 1, f_getchar},
7926 {"getcharmod", 0, 0, f_getcharmod},
7927 {"getcmdline", 0, 0, f_getcmdline},
7928 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007929 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007931 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007932 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"getfsize", 1, 1, f_getfsize},
7934 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007935 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007936 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007937 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007938 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007939 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007940 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007941 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007942 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007944 {"gettabvar", 2, 3, f_gettabvar},
7945 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"getwinposx", 0, 0, f_getwinposx},
7947 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007948 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007949 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007950 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007952 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007953 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007954 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7956 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7957 {"histadd", 2, 2, f_histadd},
7958 {"histdel", 1, 2, f_histdel},
7959 {"histget", 1, 2, f_histget},
7960 {"histnr", 1, 1, f_histnr},
7961 {"hlID", 1, 1, f_hlID},
7962 {"hlexists", 1, 1, f_hlexists},
7963 {"hostname", 0, 0, f_hostname},
7964 {"iconv", 3, 3, f_iconv},
7965 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007967 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007969 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"inputrestore", 0, 0, f_inputrestore},
7971 {"inputsave", 0, 0, f_inputsave},
7972 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007973 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007974 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007976 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007981 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"libcall", 3, 3, f_libcall},
7983 {"libcallnr", 3, 3, f_libcallnr},
7984 {"line", 1, 1, f_line},
7985 {"line2byte", 1, 1, f_line2byte},
7986 {"lispindent", 1, 1, f_lispindent},
7987 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007989 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990 {"log10", 1, 1, f_log10},
7991#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007992#ifdef FEAT_LUA
7993 {"luaeval", 1, 2, f_luaeval},
7994#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007996 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007997 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007998 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007999 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008000 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008001 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008002 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008003 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008005 {"max", 1, 1, f_max},
8006 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008007#ifdef vim_mkdir
8008 {"mkdir", 1, 3, f_mkdir},
8009#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008010 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008011#ifdef FEAT_MZSCHEME
8012 {"mzeval", 1, 1, f_mzeval},
8013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008015 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008016 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008017 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018#ifdef FEAT_FLOAT
8019 {"pow", 2, 2, f_pow},
8020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008022 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008023 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008024#ifdef FEAT_PYTHON3
8025 {"py3eval", 1, 1, f_py3eval},
8026#endif
8027#ifdef FEAT_PYTHON
8028 {"pyeval", 1, 1, f_pyeval},
8029#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008030 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008031 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008032 {"reltime", 0, 2, f_reltime},
8033 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {"remote_expr", 2, 3, f_remote_expr},
8035 {"remote_foreground", 1, 1, f_remote_foreground},
8036 {"remote_peek", 1, 2, f_remote_peek},
8037 {"remote_read", 1, 1, f_remote_read},
8038 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008039 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008041 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008043 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008044#ifdef FEAT_FLOAT
8045 {"round", 1, 1, f_round},
8046#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008047 {"screenattr", 2, 2, f_screenattr},
8048 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008049 {"screencol", 0, 0, f_screencol},
8050 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008051 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008052 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008053 {"searchpair", 3, 7, f_searchpair},
8054 {"searchpairpos", 3, 7, f_searchpairpos},
8055 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"server2client", 2, 2, f_server2client},
8057 {"serverlist", 0, 0, f_serverlist},
8058 {"setbufvar", 3, 3, f_setbufvar},
8059 {"setcmdpos", 1, 1, f_setcmdpos},
8060 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008061 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008062 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008063 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008064 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008066 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008067 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008069#ifdef FEAT_CRYPT
8070 {"sha256", 1, 1, f_sha256},
8071#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008072 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008073 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008077 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008079 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008080 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008081 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008082 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008083 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008084#ifdef FEAT_FLOAT
8085 {"sqrt", 1, 1, f_sqrt},
8086 {"str2float", 1, 1, f_str2float},
8087#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008088 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008089 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008090 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#ifdef HAVE_STRFTIME
8092 {"strftime", 1, 2, f_strftime},
8093#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"strlen", 1, 1, f_strlen},
8097 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008098 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008100 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"submatch", 1, 1, f_submatch},
8102 {"substitute", 4, 4, f_substitute},
8103 {"synID", 3, 3, f_synID},
8104 {"synIDattr", 2, 3, f_synIDattr},
8105 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008106 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008107 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008108 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008109 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008110 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008111 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008112 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008113 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008114#ifdef FEAT_FLOAT
8115 {"tan", 1, 1, f_tan},
8116 {"tanh", 1, 1, f_tanh},
8117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008119 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"tolower", 1, 1, f_tolower},
8121 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008122 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#ifdef FEAT_FLOAT
8124 {"trunc", 1, 1, f_trunc},
8125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008127 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008128 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008129 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"virtcol", 1, 1, f_virtcol},
8131 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008132 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"winbufnr", 1, 1, f_winbufnr},
8134 {"wincol", 0, 0, f_wincol},
8135 {"winheight", 1, 1, f_winheight},
8136 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008137 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008139 {"winrestview", 1, 1, f_winrestview},
8140 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008142 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008143 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144};
8145
8146#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8147
8148/*
8149 * Function given to ExpandGeneric() to obtain the list of internal
8150 * or user defined function names.
8151 */
8152 char_u *
8153get_function_name(xp, idx)
8154 expand_T *xp;
8155 int idx;
8156{
8157 static int intidx = -1;
8158 char_u *name;
8159
8160 if (idx == 0)
8161 intidx = -1;
8162 if (intidx < 0)
8163 {
8164 name = get_user_func_name(xp, idx);
8165 if (name != NULL)
8166 return name;
8167 }
8168 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8169 {
8170 STRCPY(IObuff, functions[intidx].f_name);
8171 STRCAT(IObuff, "(");
8172 if (functions[intidx].f_max_argc == 0)
8173 STRCAT(IObuff, ")");
8174 return IObuff;
8175 }
8176
8177 return NULL;
8178}
8179
8180/*
8181 * Function given to ExpandGeneric() to obtain the list of internal or
8182 * user defined variable or function names.
8183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 char_u *
8185get_expr_name(xp, idx)
8186 expand_T *xp;
8187 int idx;
8188{
8189 static int intidx = -1;
8190 char_u *name;
8191
8192 if (idx == 0)
8193 intidx = -1;
8194 if (intidx < 0)
8195 {
8196 name = get_function_name(xp, idx);
8197 if (name != NULL)
8198 return name;
8199 }
8200 return get_user_var_name(xp, ++intidx);
8201}
8202
8203#endif /* FEAT_CMDL_COMPL */
8204
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008205#if defined(EBCDIC) || defined(PROTO)
8206/*
8207 * Compare struct fst by function name.
8208 */
8209 static int
8210compare_func_name(s1, s2)
8211 const void *s1;
8212 const void *s2;
8213{
8214 struct fst *p1 = (struct fst *)s1;
8215 struct fst *p2 = (struct fst *)s2;
8216
8217 return STRCMP(p1->f_name, p2->f_name);
8218}
8219
8220/*
8221 * Sort the function table by function name.
8222 * The sorting of the table above is ASCII dependant.
8223 * On machines using EBCDIC we have to sort it.
8224 */
8225 static void
8226sortFunctions()
8227{
8228 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8229
8230 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8231}
8232#endif
8233
8234
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235/*
8236 * Find internal function in table above.
8237 * Return index, or -1 if not found
8238 */
8239 static int
8240find_internal_func(name)
8241 char_u *name; /* name of the function */
8242{
8243 int first = 0;
8244 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8245 int cmp;
8246 int x;
8247
8248 /*
8249 * Find the function name in the table. Binary search.
8250 */
8251 while (first <= last)
8252 {
8253 x = first + ((unsigned)(last - first) >> 1);
8254 cmp = STRCMP(name, functions[x].f_name);
8255 if (cmp < 0)
8256 last = x - 1;
8257 else if (cmp > 0)
8258 first = x + 1;
8259 else
8260 return x;
8261 }
8262 return -1;
8263}
8264
8265/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8267 * name it contains, otherwise return "name".
8268 */
8269 static char_u *
8270deref_func_name(name, lenp)
8271 char_u *name;
8272 int *lenp;
8273{
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 int cc;
8276
8277 cc = name[*lenp];
8278 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008279 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008280 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 {
8285 *lenp = 0;
8286 return (char_u *)""; /* just in case */
8287 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008288 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 }
8291
8292 return name;
8293}
8294
8295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 * Allocate a variable for the result of a function.
8297 * Return OK or FAIL.
8298 */
8299 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008300get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8301 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 char_u *name; /* name of the function */
8303 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 char_u **arg; /* argument, pointing to the '(' */
8306 linenr_T firstline; /* first line of range */
8307 linenr_T lastline; /* last line of range */
8308 int *doesrange; /* return: function handled range */
8309 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312 char_u *argp;
8313 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008314 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 int argcount = 0; /* number of arguments found */
8316
8317 /*
8318 * Get the arguments.
8319 */
8320 argp = *arg;
8321 while (argcount < MAX_FUNC_ARGS)
8322 {
8323 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8324 if (*argp == ')' || *argp == ',' || *argp == NUL)
8325 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8327 {
8328 ret = FAIL;
8329 break;
8330 }
8331 ++argcount;
8332 if (*argp != ',')
8333 break;
8334 }
8335 if (*argp == ')')
8336 ++argp;
8337 else
8338 ret = FAIL;
8339
8340 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008342 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 {
8345 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008346 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008348 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
8351 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 *arg = skipwhite(argp);
8355 return ret;
8356}
8357
8358
8359/*
8360 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008361 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008362 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 */
8364 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008365call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008366 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008367 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008371 typval_T *argvars; /* vars for arguments, must have "argcount"
8372 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 linenr_T firstline; /* first line of range */
8374 linenr_T lastline; /* last line of range */
8375 int *doesrange; /* return: function handled range */
8376 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380#define ERROR_UNKNOWN 0
8381#define ERROR_TOOMANY 1
8382#define ERROR_TOOFEW 2
8383#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008384#define ERROR_DICT 4
8385#define ERROR_NONE 5
8386#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 int error = ERROR_NONE;
8388 int i;
8389 int llen;
8390 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391#define FLEN_FIXED 40
8392 char_u fname_buf[FLEN_FIXED + 1];
8393 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008394 char_u *name;
8395
8396 /* Make a copy of the name, if it comes from a funcref variable it could
8397 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008398 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008399 if (name == NULL)
8400 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401
8402 /*
8403 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8404 * Change <SNR>123_name() to K_SNR 123_name().
8405 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 llen = eval_fname_script(name);
8408 if (llen > 0)
8409 {
8410 fname_buf[0] = K_SPECIAL;
8411 fname_buf[1] = KS_EXTRA;
8412 fname_buf[2] = (int)KE_SNR;
8413 i = 3;
8414 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8415 {
8416 if (current_SID <= 0)
8417 error = ERROR_SCRIPT;
8418 else
8419 {
8420 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8421 i = (int)STRLEN(fname_buf);
8422 }
8423 }
8424 if (i + STRLEN(name + llen) < FLEN_FIXED)
8425 {
8426 STRCPY(fname_buf + i, name + llen);
8427 fname = fname_buf;
8428 }
8429 else
8430 {
8431 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8432 if (fname == NULL)
8433 error = ERROR_OTHER;
8434 else
8435 {
8436 mch_memmove(fname, fname_buf, (size_t)i);
8437 STRCPY(fname + i, name + llen);
8438 }
8439 }
8440 }
8441 else
8442 fname = name;
8443
8444 *doesrange = FALSE;
8445
8446
8447 /* execute the function if no errors detected and executing */
8448 if (evaluate && error == ERROR_NONE)
8449 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008450 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8451 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 error = ERROR_UNKNOWN;
8453
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008454 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {
8456 /*
8457 * User defined function.
8458 */
8459 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008462 /* Trigger FuncUndefined event, may load the function. */
8463 if (fp == NULL
8464 && apply_autocmds(EVENT_FUNCUNDEFINED,
8465 fname, fname, TRUE, NULL)
8466 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008468 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 fp = find_func(fname);
8470 }
8471#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008472 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008473 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 {
8475 /* loaded a package, search for the function again */
8476 fp = find_func(fname);
8477 }
8478
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (fp != NULL)
8480 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008481 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008485 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008488 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 else
8490 {
8491 /*
8492 * Call the user function.
8493 * Save and restore search patterns, script variables and
8494 * redo buffer.
8495 */
8496 save_search_patterns();
8497 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008498 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008500 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008501 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8502 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8503 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008504 /* Function was unreferenced while being used, free it
8505 * now. */
8506 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 restoreRedobuff();
8508 restore_search_patterns();
8509 error = ERROR_NONE;
8510 }
8511 }
8512 }
8513 else
8514 {
8515 /*
8516 * Find the function name in the table, call its implementation.
8517 */
8518 i = find_internal_func(fname);
8519 if (i >= 0)
8520 {
8521 if (argcount < functions[i].f_min_argc)
8522 error = ERROR_TOOFEW;
8523 else if (argcount > functions[i].f_max_argc)
8524 error = ERROR_TOOMANY;
8525 else
8526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 error = ERROR_NONE;
8530 }
8531 }
8532 }
8533 /*
8534 * The function call (or "FuncUndefined" autocommand sequence) might
8535 * have been aborted by an error, an interrupt, or an explicitly thrown
8536 * exception that has not been caught so far. This situation can be
8537 * tested for by calling aborting(). For an error in an internal
8538 * function or for the "E132" error in call_user_func(), however, the
8539 * throw point at which the "force_abort" flag (temporarily reset by
8540 * emsg()) is normally updated has not been reached yet. We need to
8541 * update that flag first to make aborting() reliable.
8542 */
8543 update_force_abort();
8544 }
8545 if (error == ERROR_NONE)
8546 ret = OK;
8547
8548 /*
8549 * Report an error unless the argument evaluation or function call has been
8550 * cancelled due to an aborting error, an interrupt, or an exception.
8551 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008552 if (!aborting())
8553 {
8554 switch (error)
8555 {
8556 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008557 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008558 break;
8559 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008560 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 break;
8562 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008563 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008564 name);
8565 break;
8566 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008567 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008568 name);
8569 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008571 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 name);
8573 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008574 }
8575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (fname != name && fname != fname_buf)
8578 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008579 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580
8581 return ret;
8582}
8583
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584/*
8585 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008586 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008587 */
8588 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008589emsg_funcname(ermsg, name)
8590 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008591 char_u *name;
8592{
8593 char_u *p;
8594
8595 if (*name == K_SPECIAL)
8596 p = concat_str((char_u *)"<SNR>", name + 3);
8597 else
8598 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008599 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008600 if (p != name)
8601 vim_free(p);
8602}
8603
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008604/*
8605 * Return TRUE for a non-zero Number and a non-empty String.
8606 */
8607 static int
8608non_zero_arg(argvars)
8609 typval_T *argvars;
8610{
8611 return ((argvars[0].v_type == VAR_NUMBER
8612 && argvars[0].vval.v_number != 0)
8613 || (argvars[0].v_type == VAR_STRING
8614 && argvars[0].vval.v_string != NULL
8615 && *argvars[0].vval.v_string != NUL));
8616}
8617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618/*********************************************
8619 * Implementation of the built-in functions
8620 */
8621
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008623static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8624
8625/*
8626 * Get the float value of "argvars[0]" into "f".
8627 * Returns FAIL when the argument is not a Number or Float.
8628 */
8629 static int
8630get_float_arg(argvars, f)
8631 typval_T *argvars;
8632 float_T *f;
8633{
8634 if (argvars[0].v_type == VAR_FLOAT)
8635 {
8636 *f = argvars[0].vval.v_float;
8637 return OK;
8638 }
8639 if (argvars[0].v_type == VAR_NUMBER)
8640 {
8641 *f = (float_T)argvars[0].vval.v_number;
8642 return OK;
8643 }
8644 EMSG(_("E808: Number or Float required"));
8645 return FAIL;
8646}
8647
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008648/*
8649 * "abs(expr)" function
8650 */
8651 static void
8652f_abs(argvars, rettv)
8653 typval_T *argvars;
8654 typval_T *rettv;
8655{
8656 if (argvars[0].v_type == VAR_FLOAT)
8657 {
8658 rettv->v_type = VAR_FLOAT;
8659 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8660 }
8661 else
8662 {
8663 varnumber_T n;
8664 int error = FALSE;
8665
8666 n = get_tv_number_chk(&argvars[0], &error);
8667 if (error)
8668 rettv->vval.v_number = -1;
8669 else if (n > 0)
8670 rettv->vval.v_number = n;
8671 else
8672 rettv->vval.v_number = -n;
8673 }
8674}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008675
8676/*
8677 * "acos()" function
8678 */
8679 static void
8680f_acos(argvars, rettv)
8681 typval_T *argvars;
8682 typval_T *rettv;
8683{
8684 float_T f;
8685
8686 rettv->v_type = VAR_FLOAT;
8687 if (get_float_arg(argvars, &f) == OK)
8688 rettv->vval.v_float = acos(f);
8689 else
8690 rettv->vval.v_float = 0.0;
8691}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692#endif
8693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008695 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 */
8697 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 typval_T *argvars;
8700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008705 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008707 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008708 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008709 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008711 }
8712 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008713 EMSG(_(e_listreq));
8714}
8715
8716/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008717 * "and(expr, expr)" function
8718 */
8719 static void
8720f_and(argvars, rettv)
8721 typval_T *argvars;
8722 typval_T *rettv;
8723{
8724 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8725 & get_tv_number_chk(&argvars[1], NULL);
8726}
8727
8728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729 * "append(lnum, string/list)" function
8730 */
8731 static void
8732f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 typval_T *argvars;
8734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008735{
8736 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 list_T *l = NULL;
8739 listitem_T *li = NULL;
8740 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741 long added = 0;
8742
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 lnum = get_tv_lnum(argvars);
8744 if (lnum >= 0
8745 && lnum <= curbuf->b_ml.ml_line_count
8746 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008747 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008748 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750 l = argvars[1].vval.v_list;
8751 if (l == NULL)
8752 return;
8753 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008754 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008755 for (;;)
8756 {
8757 if (l == NULL)
8758 tv = &argvars[1]; /* append a string */
8759 else if (li == NULL)
8760 break; /* end of list */
8761 else
8762 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008763 line = get_tv_string_chk(tv);
8764 if (line == NULL) /* type error */
8765 {
8766 rettv->vval.v_number = 1; /* Failed */
8767 break;
8768 }
8769 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008770 ++added;
8771 if (l == NULL)
8772 break;
8773 li = li->li_next;
8774 }
8775
8776 appended_lines_mark(lnum, added);
8777 if (curwin->w_cursor.lnum > lnum)
8778 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 else
8781 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782}
8783
8784/*
8785 * "argc()" function
8786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008789 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795/*
8796 * "argidx()" function
8797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
8806/*
8807 * "argv(nr)" function
8808 */
8809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *argvars;
8812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
8814 int idx;
8815
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008816 if (argvars[0].v_type != VAR_UNKNOWN)
8817 {
8818 idx = get_tv_number_chk(&argvars[0], NULL);
8819 if (idx >= 0 && idx < ARGCOUNT)
8820 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8821 else
8822 rettv->vval.v_string = NULL;
8823 rettv->v_type = VAR_STRING;
8824 }
8825 else if (rettv_list_alloc(rettv) == OK)
8826 for (idx = 0; idx < ARGCOUNT; ++idx)
8827 list_append_string(rettv->vval.v_list,
8828 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008831#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008833 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835 static void
8836f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008837 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008838 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008839{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008840 float_T f;
8841
8842 rettv->v_type = VAR_FLOAT;
8843 if (get_float_arg(argvars, &f) == OK)
8844 rettv->vval.v_float = asin(f);
8845 else
8846 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008847}
8848
8849/*
8850 * "atan()" function
8851 */
8852 static void
8853f_atan(argvars, rettv)
8854 typval_T *argvars;
8855 typval_T *rettv;
8856{
8857 float_T f;
8858
8859 rettv->v_type = VAR_FLOAT;
8860 if (get_float_arg(argvars, &f) == OK)
8861 rettv->vval.v_float = atan(f);
8862 else
8863 rettv->vval.v_float = 0.0;
8864}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008865
8866/*
8867 * "atan2()" function
8868 */
8869 static void
8870f_atan2(argvars, rettv)
8871 typval_T *argvars;
8872 typval_T *rettv;
8873{
8874 float_T fx, fy;
8875
8876 rettv->v_type = VAR_FLOAT;
8877 if (get_float_arg(argvars, &fx) == OK
8878 && get_float_arg(&argvars[1], &fy) == OK)
8879 rettv->vval.v_float = atan2(fx, fy);
8880 else
8881 rettv->vval.v_float = 0.0;
8882}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008883#endif
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885/*
8886 * "browse(save, title, initdir, default)" function
8887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008890 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893#ifdef FEAT_BROWSE
8894 int save;
8895 char_u *title;
8896 char_u *initdir;
8897 char_u *defname;
8898 char_u buf[NUMBUFLEN];
8899 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 save = get_tv_number_chk(&argvars[0], &error);
8903 title = get_tv_string_chk(&argvars[1]);
8904 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8905 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 if (error || title == NULL || initdir == NULL || defname == NULL)
8908 rettv->vval.v_string = NULL;
8909 else
8910 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008911 do_browse(save ? BROWSE_SAVE : 0,
8912 title, defname, NULL, initdir, NULL, curbuf);
8913#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008915#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917}
8918
8919/*
8920 * "browsedir(title, initdir)" function
8921 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008924 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008926{
8927#ifdef FEAT_BROWSE
8928 char_u *title;
8929 char_u *initdir;
8930 char_u buf[NUMBUFLEN];
8931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 title = get_tv_string_chk(&argvars[0]);
8933 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 if (title == NULL || initdir == NULL)
8936 rettv->vval.v_string = NULL;
8937 else
8938 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008939 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944}
8945
Bram Moolenaar33570922005-01-25 22:26:29 +00008946static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008947
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948/*
8949 * Find a buffer by number or exact name.
8950 */
8951 static buf_T *
8952find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 if (avar->v_type == VAR_NUMBER)
8958 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008959 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008961 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008962 if (buf == NULL)
8963 {
8964 /* No full path name match, try a match with a URL or a "nofile"
8965 * buffer, these don't use the full path. */
8966 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8967 if (buf->b_fname != NULL
8968 && (path_with_url(buf->b_fname)
8969#ifdef FEAT_QUICKFIX
8970 || bt_nofile(buf)
8971#endif
8972 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008973 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008974 break;
8975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 }
8977 return buf;
8978}
8979
8980/*
8981 * "bufexists(expr)" function
8982 */
8983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 typval_T *argvars;
8986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989}
8990
8991/*
8992 * "buflisted(expr)" function
8993 */
8994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 typval_T *argvars;
8997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998{
8999 buf_T *buf;
9000
9001 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003}
9004
9005/*
9006 * "bufloaded(expr)" function
9007 */
9008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 typval_T *argvars;
9011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 buf_T *buf;
9014
9015 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017}
9018
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009019static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009020
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021/*
9022 * Get buffer by number or pattern.
9023 */
9024 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009025get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009027 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 int save_magic;
9031 char_u *save_cpo;
9032 buf_T *buf;
9033
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 if (tv->v_type == VAR_NUMBER)
9035 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009036 if (tv->v_type != VAR_STRING)
9037 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 if (name == NULL || *name == NUL)
9039 return curbuf;
9040 if (name[0] == '$' && name[1] == NUL)
9041 return lastbuf;
9042
9043 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9044 save_magic = p_magic;
9045 p_magic = TRUE;
9046 save_cpo = p_cpo;
9047 p_cpo = (char_u *)"";
9048
9049 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009050 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 p_magic = save_magic;
9053 p_cpo = save_cpo;
9054
9055 /* If not found, try expanding the name, like done for bufexists(). */
9056 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 return buf;
9060}
9061
9062/*
9063 * "bufname(expr)" function
9064 */
9065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *argvars;
9068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070 buf_T *buf;
9071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009074 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 --emsg_off;
9081}
9082
9083/*
9084 * "bufnr(expr)" function
9085 */
9086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 typval_T *argvars;
9089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090{
9091 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009092 int error = FALSE;
9093 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009095 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009097 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009098 --emsg_off;
9099
9100 /* If the buffer isn't found and the second argument is not zero create a
9101 * new buffer. */
9102 if (buf == NULL
9103 && argvars[1].v_type != VAR_UNKNOWN
9104 && get_tv_number_chk(&argvars[1], &error) != 0
9105 && !error
9106 && (name = get_tv_string_chk(&argvars[0])) != NULL
9107 && !error)
9108 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9109
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114}
9115
9116/*
9117 * "bufwinnr(nr)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124#ifdef FEAT_WINDOWS
9125 win_T *wp;
9126 int winnr = 0;
9127#endif
9128 buf_T *buf;
9129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009132 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#ifdef FEAT_WINDOWS
9134 for (wp = firstwin; wp; wp = wp->w_next)
9135 {
9136 ++winnr;
9137 if (wp->w_buffer == buf)
9138 break;
9139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#endif
9144 --emsg_off;
9145}
9146
9147/*
9148 * "byte2line(byte)" function
9149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157#else
9158 long boff = 0;
9159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 (linenr_T)0, &boff);
9166#endif
9167}
9168
9169/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009170 * "byteidx()" function
9171 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009174 typval_T *argvars;
9175 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009176{
9177#ifdef FEAT_MBYTE
9178 char_u *t;
9179#endif
9180 char_u *str;
9181 long idx;
9182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 str = get_tv_string_chk(&argvars[0]);
9184 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009187 return;
9188
9189#ifdef FEAT_MBYTE
9190 t = str;
9191 for ( ; idx > 0; idx--)
9192 {
9193 if (*t == NUL) /* EOL reached */
9194 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009195 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009196 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009197 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009199 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009201#endif
9202}
9203
Bram Moolenaardb913952012-06-29 12:54:53 +02009204 int
9205func_call(name, args, selfdict, rettv)
9206 char_u *name;
9207 typval_T *args;
9208 dict_T *selfdict;
9209 typval_T *rettv;
9210{
9211 listitem_T *item;
9212 typval_T argv[MAX_FUNC_ARGS + 1];
9213 int argc = 0;
9214 int dummy;
9215 int r = 0;
9216
9217 for (item = args->vval.v_list->lv_first; item != NULL;
9218 item = item->li_next)
9219 {
9220 if (argc == MAX_FUNC_ARGS)
9221 {
9222 EMSG(_("E699: Too many arguments"));
9223 break;
9224 }
9225 /* Make a copy of each argument. This is needed to be able to set
9226 * v_lock to VAR_FIXED in the copy without changing the original list.
9227 */
9228 copy_tv(&item->li_tv, &argv[argc++]);
9229 }
9230
9231 if (item == NULL)
9232 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9233 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9234 &dummy, TRUE, selfdict);
9235
9236 /* Free the arguments. */
9237 while (argc > 0)
9238 clear_tv(&argv[--argc]);
9239
9240 return r;
9241}
9242
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009243/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009244 * "call(func, arglist)" function
9245 */
9246 static void
9247f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009248 typval_T *argvars;
9249 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009250{
9251 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009252 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009253
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009254 if (argvars[1].v_type != VAR_LIST)
9255 {
9256 EMSG(_(e_listreq));
9257 return;
9258 }
9259 if (argvars[1].vval.v_list == NULL)
9260 return;
9261
9262 if (argvars[0].v_type == VAR_FUNC)
9263 func = argvars[0].vval.v_string;
9264 else
9265 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 if (*func == NUL)
9267 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009268
Bram Moolenaare9a41262005-01-15 22:18:47 +00009269 if (argvars[2].v_type != VAR_UNKNOWN)
9270 {
9271 if (argvars[2].v_type != VAR_DICT)
9272 {
9273 EMSG(_(e_dictreq));
9274 return;
9275 }
9276 selfdict = argvars[2].vval.v_dict;
9277 }
9278
Bram Moolenaardb913952012-06-29 12:54:53 +02009279 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009280}
9281
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009282#ifdef FEAT_FLOAT
9283/*
9284 * "ceil({float})" function
9285 */
9286 static void
9287f_ceil(argvars, rettv)
9288 typval_T *argvars;
9289 typval_T *rettv;
9290{
9291 float_T f;
9292
9293 rettv->v_type = VAR_FLOAT;
9294 if (get_float_arg(argvars, &f) == OK)
9295 rettv->vval.v_float = ceil(f);
9296 else
9297 rettv->vval.v_float = 0.0;
9298}
9299#endif
9300
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009301/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009302 * "changenr()" function
9303 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009304 static void
9305f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009306 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009307 typval_T *rettv;
9308{
9309 rettv->vval.v_number = curbuf->b_u_seq_cur;
9310}
9311
9312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 * "char2nr(string)" function
9314 */
9315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320#ifdef FEAT_MBYTE
9321 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009322 {
9323 int utf8 = 0;
9324
9325 if (argvars[1].v_type != VAR_UNKNOWN)
9326 utf8 = get_tv_number_chk(&argvars[1], NULL);
9327
9328 if (utf8)
9329 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9330 else
9331 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 else
9334#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336}
9337
9338/*
9339 * "cindent(lnum)" function
9340 */
9341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346#ifdef FEAT_CINDENT
9347 pos_T pos;
9348 linenr_T lnum;
9349
9350 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9353 {
9354 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 curwin->w_cursor = pos;
9357 }
9358 else
9359#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361}
9362
9363/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009364 * "clearmatches()" function
9365 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009366 static void
9367f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009368 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009369 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009370{
9371#ifdef FEAT_SEARCH_EXTRA
9372 clear_matches(curwin);
9373#endif
9374}
9375
9376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 * "col(string)" function
9378 */
9379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *argvars;
9382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 colnr_T col = 0;
9385 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009386 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009388 fp = var2fpos(&argvars[0], FALSE, &fnum);
9389 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 {
9391 if (fp->col == MAXCOL)
9392 {
9393 /* '> can be MAXCOL, get the length of the line then */
9394 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009395 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 else
9397 col = MAXCOL;
9398 }
9399 else
9400 {
9401 col = fp->col + 1;
9402#ifdef FEAT_VIRTUALEDIT
9403 /* col(".") when the cursor is on the NUL at the end of the line
9404 * because of "coladd" can be seen as an extra column. */
9405 if (virtual_active() && fp == &curwin->w_cursor)
9406 {
9407 char_u *p = ml_get_cursor();
9408
9409 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9410 curwin->w_virtcol - curwin->w_cursor.coladd))
9411 {
9412# ifdef FEAT_MBYTE
9413 int l;
9414
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009415 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 col += l;
9417# else
9418 if (*p != NUL && p[1] == NUL)
9419 ++col;
9420# endif
9421 }
9422 }
9423#endif
9424 }
9425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
Bram Moolenaar572cb562005-08-05 21:35:02 +00009429#if defined(FEAT_INS_EXPAND)
9430/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009431 * "complete()" function
9432 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009433 static void
9434f_complete(argvars, rettv)
9435 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009436 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009437{
9438 int startcol;
9439
9440 if ((State & INSERT) == 0)
9441 {
9442 EMSG(_("E785: complete() can only be used in Insert mode"));
9443 return;
9444 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009445
9446 /* Check for undo allowed here, because if something was already inserted
9447 * the line was already saved for undo and this check isn't done. */
9448 if (!undo_allowed())
9449 return;
9450
Bram Moolenaarade00832006-03-10 21:46:58 +00009451 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9452 {
9453 EMSG(_(e_invarg));
9454 return;
9455 }
9456
9457 startcol = get_tv_number_chk(&argvars[0], NULL);
9458 if (startcol <= 0)
9459 return;
9460
9461 set_completion(startcol - 1, argvars[1].vval.v_list);
9462}
9463
9464/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009465 * "complete_add()" function
9466 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009467 static void
9468f_complete_add(argvars, rettv)
9469 typval_T *argvars;
9470 typval_T *rettv;
9471{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009472 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009473}
9474
9475/*
9476 * "complete_check()" function
9477 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009478 static void
9479f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009480 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009481 typval_T *rettv;
9482{
9483 int saved = RedrawingDisabled;
9484
9485 RedrawingDisabled = 0;
9486 ins_compl_check_keys(0);
9487 rettv->vval.v_number = compl_interrupted;
9488 RedrawingDisabled = saved;
9489}
9490#endif
9491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492/*
9493 * "confirm(message, buttons[, default [, type]])" function
9494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009497 typval_T *argvars UNUSED;
9498 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9501 char_u *message;
9502 char_u *buttons = NULL;
9503 char_u buf[NUMBUFLEN];
9504 char_u buf2[NUMBUFLEN];
9505 int def = 1;
9506 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 char_u *typestr;
9508 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 message = get_tv_string_chk(&argvars[0]);
9511 if (message == NULL)
9512 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9516 if (buttons == NULL)
9517 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9524 if (typestr == NULL)
9525 error = TRUE;
9526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009528 switch (TOUPPER_ASC(*typestr))
9529 {
9530 case 'E': type = VIM_ERROR; break;
9531 case 'Q': type = VIM_QUESTION; break;
9532 case 'I': type = VIM_INFO; break;
9533 case 'W': type = VIM_WARNING; break;
9534 case 'G': type = VIM_GENERIC; break;
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537 }
9538 }
9539 }
9540
9541 if (buttons == NULL || *buttons == NUL)
9542 buttons = (char_u *)_("&Ok");
9543
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009544 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009545 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009546 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547#endif
9548}
9549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009550/*
9551 * "copy()" function
9552 */
9553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009555 typval_T *argvars;
9556 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009557{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009558 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009561#ifdef FEAT_FLOAT
9562/*
9563 * "cos()" function
9564 */
9565 static void
9566f_cos(argvars, rettv)
9567 typval_T *argvars;
9568 typval_T *rettv;
9569{
9570 float_T f;
9571
9572 rettv->v_type = VAR_FLOAT;
9573 if (get_float_arg(argvars, &f) == OK)
9574 rettv->vval.v_float = cos(f);
9575 else
9576 rettv->vval.v_float = 0.0;
9577}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009578
9579/*
9580 * "cosh()" function
9581 */
9582 static void
9583f_cosh(argvars, rettv)
9584 typval_T *argvars;
9585 typval_T *rettv;
9586{
9587 float_T f;
9588
9589 rettv->v_type = VAR_FLOAT;
9590 if (get_float_arg(argvars, &f) == OK)
9591 rettv->vval.v_float = cosh(f);
9592 else
9593 rettv->vval.v_float = 0.0;
9594}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009595#endif
9596
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009598 * "count()" function
9599 */
9600 static void
9601f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009602 typval_T *argvars;
9603 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009604{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009605 long n = 0;
9606 int ic = FALSE;
9607
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009609 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 listitem_T *li;
9611 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009613
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 if ((l = argvars[0].vval.v_list) != NULL)
9615 {
9616 li = l->lv_first;
9617 if (argvars[2].v_type != VAR_UNKNOWN)
9618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619 int error = FALSE;
9620
9621 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 if (argvars[3].v_type != VAR_UNKNOWN)
9623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009624 idx = get_tv_number_chk(&argvars[3], &error);
9625 if (!error)
9626 {
9627 li = list_find(l, idx);
9628 if (li == NULL)
9629 EMSGN(_(e_listidx), idx);
9630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009632 if (error)
9633 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009634 }
9635
9636 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009637 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 ++n;
9639 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 else if (argvars[0].v_type == VAR_DICT)
9642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 int todo;
9644 dict_T *d;
9645 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009647 if ((d = argvars[0].vval.v_dict) != NULL)
9648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009649 int error = FALSE;
9650
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 if (argvars[2].v_type != VAR_UNKNOWN)
9652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 if (argvars[3].v_type != VAR_UNKNOWN)
9655 EMSG(_(e_invarg));
9656 }
9657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009658 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009660 {
9661 if (!HASHITEM_EMPTY(hi))
9662 {
9663 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009664 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009665 ++n;
9666 }
9667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 }
9669 }
9670 else
9671 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009672 rettv->vval.v_number = n;
9673}
9674
9675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9677 *
9678 * Checks the existence of a cscope connection.
9679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009682 typval_T *argvars UNUSED;
9683 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685#ifdef FEAT_CSCOPE
9686 int num = 0;
9687 char_u *dbpath = NULL;
9688 char_u *prepend = NULL;
9689 char_u buf[NUMBUFLEN];
9690
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691 if (argvars[0].v_type != VAR_UNKNOWN
9692 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 num = (int)get_tv_number(&argvars[0]);
9695 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 }
9699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#endif
9702}
9703
9704/*
9705 * "cursor(lnum, col)" function
9706 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009707 * Moves the cursor to the specified line and column.
9708 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 typval_T *argvars;
9713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
9715 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009716#ifdef FEAT_VIRTUALEDIT
9717 long coladd = 0;
9718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009720 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009721 if (argvars[1].v_type == VAR_UNKNOWN)
9722 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009723 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009724
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009726 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 line = pos.lnum;
9728 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009729#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009730 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731#endif
9732 }
9733 else
9734 {
9735 line = get_tv_lnum(argvars);
9736 col = get_tv_number_chk(&argvars[1], NULL);
9737#ifdef FEAT_VIRTUALEDIT
9738 if (argvars[2].v_type != VAR_UNKNOWN)
9739 coladd = get_tv_number_chk(&argvars[2], NULL);
9740#endif
9741 }
9742 if (line < 0 || col < 0
9743#ifdef FEAT_VIRTUALEDIT
9744 || coladd < 0
9745#endif
9746 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (line > 0)
9749 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (col > 0)
9751 curwin->w_cursor.col = col - 1;
9752#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009753 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754#endif
9755
9756 /* Make sure the cursor is in a valid position. */
9757 check_cursor();
9758#ifdef FEAT_MBYTE
9759 /* Correct cursor for multi-byte character. */
9760 if (has_mbyte)
9761 mb_adjust_cursor();
9762#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009763
9764 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009765 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767
9768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009769 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 */
9771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009773 typval_T *argvars;
9774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009776 int noref = 0;
9777
9778 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009780 if (noref < 0 || noref > 1)
9781 EMSG(_(e_invarg));
9782 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009783 {
9784 current_copyID += COPYID_INC;
9785 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789/*
9790 * "delete()" function
9791 */
9792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009794 typval_T *argvars;
9795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
9797 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801}
9802
9803/*
9804 * "did_filetype()" function
9805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009808 typval_T *argvars UNUSED;
9809 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813#endif
9814}
9815
9816/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009817 * "diff_filler()" function
9818 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009821 typval_T *argvars UNUSED;
9822 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009823{
9824#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009826#endif
9827}
9828
9829/*
9830 * "diff_hlID()" function
9831 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009834 typval_T *argvars UNUSED;
9835 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009836{
9837#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009839 static linenr_T prev_lnum = 0;
9840 static int changedtick = 0;
9841 static int fnum = 0;
9842 static int change_start = 0;
9843 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009844 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009845 int filler_lines;
9846 int col;
9847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 if (lnum < 0) /* ignore type error in {lnum} arg */
9849 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009850 if (lnum != prev_lnum
9851 || changedtick != curbuf->b_changedtick
9852 || fnum != curbuf->b_fnum)
9853 {
9854 /* New line, buffer, change: need to get the values. */
9855 filler_lines = diff_check(curwin, lnum);
9856 if (filler_lines < 0)
9857 {
9858 if (filler_lines == -1)
9859 {
9860 change_start = MAXCOL;
9861 change_end = -1;
9862 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9863 hlID = HLF_ADD; /* added line */
9864 else
9865 hlID = HLF_CHD; /* changed line */
9866 }
9867 else
9868 hlID = HLF_ADD; /* added line */
9869 }
9870 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009871 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009872 prev_lnum = lnum;
9873 changedtick = curbuf->b_changedtick;
9874 fnum = curbuf->b_fnum;
9875 }
9876
9877 if (hlID == HLF_CHD || hlID == HLF_TXD)
9878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009880 if (col >= change_start && col <= change_end)
9881 hlID = HLF_TXD; /* changed text */
9882 else
9883 hlID = HLF_CHD; /* changed line */
9884 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009885 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009886#endif
9887}
9888
9889/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009890 * "empty({expr})" function
9891 */
9892 static void
9893f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009894 typval_T *argvars;
9895 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009896{
9897 int n;
9898
9899 switch (argvars[0].v_type)
9900 {
9901 case VAR_STRING:
9902 case VAR_FUNC:
9903 n = argvars[0].vval.v_string == NULL
9904 || *argvars[0].vval.v_string == NUL;
9905 break;
9906 case VAR_NUMBER:
9907 n = argvars[0].vval.v_number == 0;
9908 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009909#ifdef FEAT_FLOAT
9910 case VAR_FLOAT:
9911 n = argvars[0].vval.v_float == 0.0;
9912 break;
9913#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009914 case VAR_LIST:
9915 n = argvars[0].vval.v_list == NULL
9916 || argvars[0].vval.v_list->lv_first == NULL;
9917 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 case VAR_DICT:
9919 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009922 default:
9923 EMSG2(_(e_intern2), "f_empty()");
9924 n = 0;
9925 }
9926
9927 rettv->vval.v_number = n;
9928}
9929
9930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 * "escape({string}, {chars})" function
9932 */
9933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009935 typval_T *argvars;
9936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 char_u buf[NUMBUFLEN];
9939
Bram Moolenaar758711c2005-02-02 23:11:38 +00009940 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9941 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943}
9944
9945/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009946 * "eval()" function
9947 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948 static void
9949f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009950 typval_T *argvars;
9951 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009952{
9953 char_u *s;
9954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 s = get_tv_string_chk(&argvars[0]);
9956 if (s != NULL)
9957 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009959 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9960 {
9961 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009962 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009963 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009964 else if (*s != NUL)
9965 EMSG(_(e_trailing));
9966}
9967
9968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 * "eventhandler()" function
9970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009973 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979/*
9980 * "executable()" function
9981 */
9982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *argvars;
9985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988}
9989
9990/*
9991 * "exists()" function
9992 */
9993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 typval_T *argvars;
9996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 char_u *p;
9999 char_u *name;
10000 int n = FALSE;
10001 int len = 0;
10002
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010003 no_autoload = TRUE;
10004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010005 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (*p == '$') /* environment variable */
10007 {
10008 /* first try "normal" environment variables (fast) */
10009 if (mch_getenv(p + 1) != NULL)
10010 n = TRUE;
10011 else
10012 {
10013 /* try expanding things like $VIM and ${HOME} */
10014 p = expand_env_save(p);
10015 if (p != NULL && *p != '$')
10016 n = TRUE;
10017 vim_free(p);
10018 }
10019 }
10020 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010023 if (*skipwhite(p) != NUL)
10024 n = FALSE; /* trailing garbage */
10025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else if (*p == '*') /* internal or user defined function */
10027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010028 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 }
10030 else if (*p == ':')
10031 {
10032 n = cmd_exists(p + 1);
10033 }
10034 else if (*p == '#')
10035 {
10036#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010037 if (p[1] == '#')
10038 n = autocmd_supported(p + 2);
10039 else
10040 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041#endif
10042 }
10043 else /* internal variable */
10044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010045 char_u *tofree;
10046 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010048 /* get_name_len() takes care of expanding curly braces */
10049 name = p;
10050 len = get_name_len(&p, &tofree, TRUE, FALSE);
10051 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010053 if (tofree != NULL)
10054 name = tofree;
10055 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10056 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010058 /* handle d.key, l[idx], f(expr) */
10059 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10060 if (n)
10061 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 }
10063 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010064 if (*p != NUL)
10065 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010067 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 }
10069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010071
10072 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073}
10074
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010075#ifdef FEAT_FLOAT
10076/*
10077 * "exp()" function
10078 */
10079 static void
10080f_exp(argvars, rettv)
10081 typval_T *argvars;
10082 typval_T *rettv;
10083{
10084 float_T f;
10085
10086 rettv->v_type = VAR_FLOAT;
10087 if (get_float_arg(argvars, &f) == OK)
10088 rettv->vval.v_float = exp(f);
10089 else
10090 rettv->vval.v_float = 0.0;
10091}
10092#endif
10093
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094/*
10095 * "expand()" function
10096 */
10097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 char_u *s;
10103 int len;
10104 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010105 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010108 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010111 if (argvars[1].v_type != VAR_UNKNOWN
10112 && argvars[2].v_type != VAR_UNKNOWN
10113 && get_tv_number_chk(&argvars[2], &error)
10114 && !error)
10115 {
10116 rettv->v_type = VAR_LIST;
10117 rettv->vval.v_list = NULL;
10118 }
10119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (*s == '%' || *s == '#' || *s == '<')
10122 {
10123 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010124 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010126 if (rettv->v_type == VAR_LIST)
10127 {
10128 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10129 list_append_string(rettv->vval.v_list, result, -1);
10130 else
10131 vim_free(result);
10132 }
10133 else
10134 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 }
10136 else
10137 {
10138 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010139 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 if (argvars[1].v_type != VAR_UNKNOWN
10141 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010142 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 if (!error)
10144 {
10145 ExpandInit(&xpc);
10146 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010147 if (p_wic)
10148 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010149 if (rettv->v_type == VAR_STRING)
10150 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10151 options, WILD_ALL);
10152 else if (rettv_list_alloc(rettv) != FAIL)
10153 {
10154 int i;
10155
10156 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10157 for (i = 0; i < xpc.xp_numfiles; i++)
10158 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10159 ExpandCleanup(&xpc);
10160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010161 }
10162 else
10163 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 }
10165}
10166
10167/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010168 * Go over all entries in "d2" and add them to "d1".
10169 * When "action" is "error" then a duplicate key is an error.
10170 * When "action" is "force" then a duplicate key is overwritten.
10171 * Otherwise duplicate keys are ignored ("action" is "keep").
10172 */
10173 void
10174dict_extend(d1, d2, action)
10175 dict_T *d1;
10176 dict_T *d2;
10177 char_u *action;
10178{
10179 dictitem_T *di1;
10180 hashitem_T *hi2;
10181 int todo;
10182
10183 todo = (int)d2->dv_hashtab.ht_used;
10184 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10185 {
10186 if (!HASHITEM_EMPTY(hi2))
10187 {
10188 --todo;
10189 di1 = dict_find(d1, hi2->hi_key, -1);
10190 if (d1->dv_scope != 0)
10191 {
10192 /* Disallow replacing a builtin function in l: and g:.
10193 * Check the key to be valid when adding to any
10194 * scope. */
10195 if (d1->dv_scope == VAR_DEF_SCOPE
10196 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10197 && var_check_func_name(hi2->hi_key,
10198 di1 == NULL))
10199 break;
10200 if (!valid_varname(hi2->hi_key))
10201 break;
10202 }
10203 if (di1 == NULL)
10204 {
10205 di1 = dictitem_copy(HI2DI(hi2));
10206 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10207 dictitem_free(di1);
10208 }
10209 else if (*action == 'e')
10210 {
10211 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10212 break;
10213 }
10214 else if (*action == 'f' && HI2DI(hi2) != di1)
10215 {
10216 clear_tv(&di1->di_tv);
10217 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10218 }
10219 }
10220 }
10221}
10222
10223/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010224 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 */
10227 static void
10228f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 typval_T *argvars;
10230 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010231{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010232 char *arg_errmsg = N_("extend() argument");
10233
Bram Moolenaare9a41262005-01-15 22:18:47 +000010234 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010235 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 list_T *l1, *l2;
10237 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010238 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010240
Bram Moolenaare9a41262005-01-15 22:18:47 +000010241 l1 = argvars[0].vval.v_list;
10242 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010243 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010244 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 {
10246 if (argvars[2].v_type != VAR_UNKNOWN)
10247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 before = get_tv_number_chk(&argvars[2], &error);
10249 if (error)
10250 return; /* type error; errmsg already given */
10251
Bram Moolenaar758711c2005-02-02 23:11:38 +000010252 if (before == l1->lv_len)
10253 item = NULL;
10254 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010256 item = list_find(l1, before);
10257 if (item == NULL)
10258 {
10259 EMSGN(_(e_listidx), before);
10260 return;
10261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 }
10263 }
10264 else
10265 item = NULL;
10266 list_extend(l1, l2, item);
10267
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 copy_tv(&argvars[0], rettv);
10269 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10272 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010273 dict_T *d1, *d2;
10274 char_u *action;
10275 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276
10277 d1 = argvars[0].vval.v_dict;
10278 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010279 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010280 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 {
10282 /* Check the third argument. */
10283 if (argvars[2].v_type != VAR_UNKNOWN)
10284 {
10285 static char *(av[]) = {"keep", "force", "error"};
10286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 action = get_tv_string_chk(&argvars[2]);
10288 if (action == NULL)
10289 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 for (i = 0; i < 3; ++i)
10291 if (STRCMP(action, av[i]) == 0)
10292 break;
10293 if (i == 3)
10294 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010295 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 return;
10297 }
10298 }
10299 else
10300 action = (char_u *)"force";
10301
Bram Moolenaara9922d62013-05-30 13:01:18 +020010302 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303
Bram Moolenaare9a41262005-01-15 22:18:47 +000010304 copy_tv(&argvars[0], rettv);
10305 }
10306 }
10307 else
10308 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010309}
10310
10311/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010312 * "feedkeys()" function
10313 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010314 static void
10315f_feedkeys(argvars, rettv)
10316 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010317 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010318{
10319 int remap = TRUE;
10320 char_u *keys, *flags;
10321 char_u nbuf[NUMBUFLEN];
10322 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010323 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010324
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010325 /* This is not allowed in the sandbox. If the commands would still be
10326 * executed in the sandbox it would be OK, but it probably happens later,
10327 * when "sandbox" is no longer set. */
10328 if (check_secure())
10329 return;
10330
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010331 keys = get_tv_string(&argvars[0]);
10332 if (*keys != NUL)
10333 {
10334 if (argvars[1].v_type != VAR_UNKNOWN)
10335 {
10336 flags = get_tv_string_buf(&argvars[1], nbuf);
10337 for ( ; *flags != NUL; ++flags)
10338 {
10339 switch (*flags)
10340 {
10341 case 'n': remap = FALSE; break;
10342 case 'm': remap = TRUE; break;
10343 case 't': typed = TRUE; break;
10344 }
10345 }
10346 }
10347
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010348 /* Need to escape K_SPECIAL and CSI before putting the string in the
10349 * typeahead buffer. */
10350 keys_esc = vim_strsave_escape_csi(keys);
10351 if (keys_esc != NULL)
10352 {
10353 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010354 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010355 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010356 if (vgetc_busy)
10357 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010358 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010359 }
10360}
10361
10362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 * "filereadable()" function
10364 */
10365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010367 typval_T *argvars;
10368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010370 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 char_u *p;
10372 int n;
10373
Bram Moolenaarc236c162008-07-13 17:41:49 +000010374#ifndef O_NONBLOCK
10375# define O_NONBLOCK 0
10376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010377 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010378 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10379 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
10381 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010382 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 }
10384 else
10385 n = FALSE;
10386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388}
10389
10390/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010391 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 * rights to write into.
10393 */
10394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *argvars;
10397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010399 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400}
10401
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010402static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010403
10404 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010405findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010406 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010408 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010409{
10410#ifdef FEAT_SEARCHPATH
10411 char_u *fname;
10412 char_u *fresult = NULL;
10413 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10414 char_u *p;
10415 char_u pathbuf[NUMBUFLEN];
10416 int count = 1;
10417 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010418 int error = FALSE;
10419#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010420
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010421 rettv->vval.v_string = NULL;
10422 rettv->v_type = VAR_STRING;
10423
10424#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010427 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10430 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010431 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 else
10433 {
10434 if (*p != NUL)
10435 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010438 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010440 }
10441
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010442 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10443 error = TRUE;
10444
10445 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 do
10448 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010449 if (rettv->v_type == VAR_STRING)
10450 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 fresult = find_file_in_path_option(first ? fname : NULL,
10452 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010453 0, first, path,
10454 find_what,
10455 curbuf->b_ffname,
10456 find_what == FINDFILE_DIR
10457 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010459
10460 if (fresult != NULL && rettv->v_type == VAR_LIST)
10461 list_append_string(rettv->vval.v_list, fresult, -1);
10462
10463 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010465
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010466 if (rettv->v_type == VAR_STRING)
10467 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010468#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010469}
10470
Bram Moolenaar33570922005-01-25 22:26:29 +000010471static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10472static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010473
10474/*
10475 * Implementation of map() and filter().
10476 */
10477 static void
10478filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010479 typval_T *argvars;
10480 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010481 int map;
10482{
10483 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010485 listitem_T *li, *nli;
10486 list_T *l = NULL;
10487 dictitem_T *di;
10488 hashtab_T *ht;
10489 hashitem_T *hi;
10490 dict_T *d = NULL;
10491 typval_T save_val;
10492 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010493 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010494 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010495 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10496 char *arg_errmsg = (map ? N_("map() argument")
10497 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010498 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010499 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010500
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010502 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010503 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010504 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 return;
10506 }
10507 else if (argvars[0].v_type == VAR_DICT)
10508 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010509 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010510 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 return;
10512 }
10513 else
10514 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010515 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516 return;
10517 }
10518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 expr = get_tv_string_buf_chk(&argvars[1], buf);
10520 /* On type errors, the preceding call has already displayed an error
10521 * message. Avoid a misleading error message for an empty string that
10522 * was not passed as argument. */
10523 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 prepare_vimvar(VV_VAL, &save_val);
10526 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010527
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010528 /* We reset "did_emsg" to be able to detect whether an error
10529 * occurred during evaluation of the expression. */
10530 save_did_emsg = did_emsg;
10531 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010532
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010533 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 vimvars[VV_KEY].vv_type = VAR_STRING;
10537
10538 ht = &d->dv_hashtab;
10539 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010540 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 if (!HASHITEM_EMPTY(hi))
10544 {
10545 --todo;
10546 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010547 if (tv_check_lock(di->di_tv.v_lock,
10548 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 break;
10550 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010551 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010552 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 break;
10554 if (!map && rem)
10555 dictitem_remove(d, di);
10556 clear_tv(&vimvars[VV_KEY].vv_tv);
10557 }
10558 }
10559 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 }
10561 else
10562 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010563 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010565 for (li = l->lv_first; li != NULL; li = nli)
10566 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010567 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010568 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010570 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010571 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010572 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010573 break;
10574 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010576 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010578 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010579
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010580 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010582
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010583 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010584 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585
10586 copy_tv(&argvars[0], rettv);
10587}
10588
10589 static int
10590filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 char_u *expr;
10593 int map;
10594 int *remp;
10595{
Bram Moolenaar33570922005-01-25 22:26:29 +000010596 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010598 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 s = expr;
10602 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010603 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 if (*s != NUL) /* check for trailing chars after expr */
10605 {
10606 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010607 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 }
10609 if (map)
10610 {
10611 /* map(): replace the list item value */
10612 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010613 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614 *tv = rettv;
10615 }
10616 else
10617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 int error = FALSE;
10619
Bram Moolenaare9a41262005-01-15 22:18:47 +000010620 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 /* On type error, nothing has been removed; return FAIL to stop the
10624 * loop. The error message was given by get_tv_number_chk(). */
10625 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010626 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010627 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010628 retval = OK;
10629theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010631 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010632}
10633
10634/*
10635 * "filter()" function
10636 */
10637 static void
10638f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010639 typval_T *argvars;
10640 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010641{
10642 filter_map(argvars, rettv, FALSE);
10643}
10644
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010645/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010646 * "finddir({fname}[, {path}[, {count}]])" function
10647 */
10648 static void
10649f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010650 typval_T *argvars;
10651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010652{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010653 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654}
10655
10656/*
10657 * "findfile({fname}[, {path}[, {count}]])" function
10658 */
10659 static void
10660f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *argvars;
10662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010663{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010664 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010665}
10666
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010667#ifdef FEAT_FLOAT
10668/*
10669 * "float2nr({float})" function
10670 */
10671 static void
10672f_float2nr(argvars, rettv)
10673 typval_T *argvars;
10674 typval_T *rettv;
10675{
10676 float_T f;
10677
10678 if (get_float_arg(argvars, &f) == OK)
10679 {
10680 if (f < -0x7fffffff)
10681 rettv->vval.v_number = -0x7fffffff;
10682 else if (f > 0x7fffffff)
10683 rettv->vval.v_number = 0x7fffffff;
10684 else
10685 rettv->vval.v_number = (varnumber_T)f;
10686 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010687}
10688
10689/*
10690 * "floor({float})" function
10691 */
10692 static void
10693f_floor(argvars, rettv)
10694 typval_T *argvars;
10695 typval_T *rettv;
10696{
10697 float_T f;
10698
10699 rettv->v_type = VAR_FLOAT;
10700 if (get_float_arg(argvars, &f) == OK)
10701 rettv->vval.v_float = floor(f);
10702 else
10703 rettv->vval.v_float = 0.0;
10704}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010705
10706/*
10707 * "fmod()" function
10708 */
10709 static void
10710f_fmod(argvars, rettv)
10711 typval_T *argvars;
10712 typval_T *rettv;
10713{
10714 float_T fx, fy;
10715
10716 rettv->v_type = VAR_FLOAT;
10717 if (get_float_arg(argvars, &fx) == OK
10718 && get_float_arg(&argvars[1], &fy) == OK)
10719 rettv->vval.v_float = fmod(fx, fy);
10720 else
10721 rettv->vval.v_float = 0.0;
10722}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010723#endif
10724
Bram Moolenaar0d660222005-01-07 21:51:51 +000010725/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010726 * "fnameescape({string})" function
10727 */
10728 static void
10729f_fnameescape(argvars, rettv)
10730 typval_T *argvars;
10731 typval_T *rettv;
10732{
10733 rettv->vval.v_string = vim_strsave_fnameescape(
10734 get_tv_string(&argvars[0]), FALSE);
10735 rettv->v_type = VAR_STRING;
10736}
10737
10738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 * "fnamemodify({fname}, {mods})" function
10740 */
10741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010743 typval_T *argvars;
10744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
10746 char_u *fname;
10747 char_u *mods;
10748 int usedlen = 0;
10749 int len;
10750 char_u *fbuf = NULL;
10751 char_u buf[NUMBUFLEN];
10752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 fname = get_tv_string_chk(&argvars[0]);
10754 mods = get_tv_string_buf_chk(&argvars[1], buf);
10755 if (fname == NULL || mods == NULL)
10756 fname = NULL;
10757 else
10758 {
10759 len = (int)STRLEN(fname);
10760 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 vim_free(fbuf);
10769}
10770
Bram Moolenaar33570922005-01-25 22:26:29 +000010771static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
10773/*
10774 * "foldclosed()" function
10775 */
10776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010778 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010780 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781{
10782#ifdef FEAT_FOLDING
10783 linenr_T lnum;
10784 linenr_T first, last;
10785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10788 {
10789 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10790 {
10791 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 return;
10796 }
10797 }
10798#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800}
10801
10802/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010803 * "foldclosed()" function
10804 */
10805 static void
10806f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 typval_T *argvars;
10808 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010809{
10810 foldclosed_both(argvars, rettv, FALSE);
10811}
10812
10813/*
10814 * "foldclosedend()" function
10815 */
10816 static void
10817f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *argvars;
10819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820{
10821 foldclosed_both(argvars, rettv, TRUE);
10822}
10823
10824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 * "foldlevel()" function
10826 */
10827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010829 typval_T *argvars UNUSED;
10830 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832#ifdef FEAT_FOLDING
10833 linenr_T lnum;
10834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010837 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839}
10840
10841/*
10842 * "foldtext()" function
10843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848{
10849#ifdef FEAT_FOLDING
10850 linenr_T lnum;
10851 char_u *s;
10852 char_u *r;
10853 int len;
10854 char *txt;
10855#endif
10856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 rettv->v_type = VAR_STRING;
10858 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10861 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10862 <= curbuf->b_ml.ml_line_count
10863 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 {
10865 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010866 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10867 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
10869 if (!linewhite(lnum))
10870 break;
10871 ++lnum;
10872 }
10873
10874 /* Find interesting text in this line. */
10875 s = skipwhite(ml_get(lnum));
10876 /* skip C comment-start */
10877 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010880 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010882 {
10883 s = skipwhite(ml_get(lnum + 1));
10884 if (*s == '*')
10885 s = skipwhite(s + 1);
10886 }
10887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 txt = _("+-%s%3ld lines: ");
10889 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010890 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 + 20 /* for %3ld */
10892 + STRLEN(s))); /* concatenated */
10893 if (r != NULL)
10894 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010895 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10896 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10897 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 len = (int)STRLEN(r);
10899 STRCAT(r, s);
10900 /* remove 'foldmarker' and 'commentstring' */
10901 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 }
10904 }
10905#endif
10906}
10907
10908/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010909 * "foldtextresult(lnum)" function
10910 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010913 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010914 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010915{
10916#ifdef FEAT_FOLDING
10917 linenr_T lnum;
10918 char_u *text;
10919 char_u buf[51];
10920 foldinfo_T foldinfo;
10921 int fold_count;
10922#endif
10923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->v_type = VAR_STRING;
10925 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010926#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 /* treat illegal types and illegal string values for {lnum} the same */
10929 if (lnum < 0)
10930 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010931 fold_count = foldedCount(curwin, lnum, &foldinfo);
10932 if (fold_count > 0)
10933 {
10934 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10935 &foldinfo, buf);
10936 if (text == buf)
10937 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010939 }
10940#endif
10941}
10942
10943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 * "foreground()" function
10945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010948 typval_T *argvars UNUSED;
10949 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951#ifdef FEAT_GUI
10952 if (gui.in_use)
10953 gui_mch_set_foreground();
10954#else
10955# ifdef WIN32
10956 win32_set_foreground();
10957# endif
10958#endif
10959}
10960
10961/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962 * "function()" function
10963 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *argvars;
10967 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010968{
10969 char_u *s;
10970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010972 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010973 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010974 /* Don't check an autoload name for existence here. */
10975 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010976 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010977 else
10978 {
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010979 if (STRNCMP(s, "s:", 2) == 0)
10980 {
10981 char sid_buf[25];
10982
10983 /* Expand s: into <SNR>nr_, so that the function can also be
10984 * called from another script. Using trans_function_name() would
10985 * also work, but some plugins depend on the name being printable
10986 * text. */
10987 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020010988 rettv->vval.v_string =
10989 alloc((int)(STRLEN(sid_buf) + STRLEN(s + 2) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010990 if (rettv->vval.v_string != NULL)
10991 {
10992 STRCPY(rettv->vval.v_string, sid_buf);
10993 STRCAT(rettv->vval.v_string, s + 2);
10994 }
10995 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020010996 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010997 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010999 }
11000}
11001
11002/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011003 * "garbagecollect()" function
11004 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011005 static void
11006f_garbagecollect(argvars, rettv)
11007 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011008 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011009{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011010 /* This is postponed until we are back at the toplevel, because we may be
11011 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11012 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011013
11014 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11015 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011016}
11017
11018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019 * "get()" function
11020 */
11021 static void
11022f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011023 typval_T *argvars;
11024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011025{
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 listitem_T *li;
11027 list_T *l;
11028 dictitem_T *di;
11029 dict_T *d;
11030 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011031
Bram Moolenaare9a41262005-01-15 22:18:47 +000011032 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011033 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 int error = FALSE;
11037
11038 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11039 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011041 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 else if (argvars[0].v_type == VAR_DICT)
11044 {
11045 if ((d = argvars[0].vval.v_dict) != NULL)
11046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011047 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011048 if (di != NULL)
11049 tv = &di->di_tv;
11050 }
11051 }
11052 else
11053 EMSG2(_(e_listdictarg), "get()");
11054
11055 if (tv == NULL)
11056 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011057 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011058 copy_tv(&argvars[2], rettv);
11059 }
11060 else
11061 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011062}
11063
Bram Moolenaar342337a2005-07-21 21:11:17 +000011064static 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 +000011065
11066/*
11067 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011068 * Return a range (from start to end) of lines in rettv from the specified
11069 * buffer.
11070 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011071 */
11072 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011073get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011074 buf_T *buf;
11075 linenr_T start;
11076 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011077 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011078 typval_T *rettv;
11079{
11080 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011081
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011082 if (retlist && rettv_list_alloc(rettv) == FAIL)
11083 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011084
11085 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11086 return;
11087
11088 if (!retlist)
11089 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011090 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11091 p = ml_get_buf(buf, start, FALSE);
11092 else
11093 p = (char_u *)"";
11094
11095 rettv->v_type = VAR_STRING;
11096 rettv->vval.v_string = vim_strsave(p);
11097 }
11098 else
11099 {
11100 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011101 return;
11102
11103 if (start < 1)
11104 start = 1;
11105 if (end > buf->b_ml.ml_line_count)
11106 end = buf->b_ml.ml_line_count;
11107 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011108 if (list_append_string(rettv->vval.v_list,
11109 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011110 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011111 }
11112}
11113
11114/*
11115 * "getbufline()" function
11116 */
11117 static void
11118f_getbufline(argvars, rettv)
11119 typval_T *argvars;
11120 typval_T *rettv;
11121{
11122 linenr_T lnum;
11123 linenr_T end;
11124 buf_T *buf;
11125
11126 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11127 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011128 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011129 --emsg_off;
11130
Bram Moolenaar661b1822005-07-28 22:36:45 +000011131 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011132 if (argvars[2].v_type == VAR_UNKNOWN)
11133 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011134 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011135 end = get_tv_lnum_buf(&argvars[2], buf);
11136
Bram Moolenaar342337a2005-07-21 21:11:17 +000011137 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011138}
11139
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140/*
11141 * "getbufvar()" function
11142 */
11143 static void
11144f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 typval_T *argvars;
11146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147{
11148 buf_T *buf;
11149 buf_T *save_curbuf;
11150 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011151 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011152 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11155 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011157 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011159 rettv->v_type = VAR_STRING;
11160 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161
11162 if (buf != NULL && varname != NULL)
11163 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011164 /* set curbuf to be our buf, temporarily */
11165 save_curbuf = curbuf;
11166 curbuf = buf;
11167
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011169 {
11170 if (get_option_tv(&varname, rettv, TRUE) == OK)
11171 done = TRUE;
11172 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011173 else if (STRCMP(varname, "changedtick") == 0)
11174 {
11175 rettv->v_type = VAR_NUMBER;
11176 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011177 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011178 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011179 else
11180 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011181 /* Look up the variable. */
11182 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11183 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11184 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011185 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011186 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011188 done = TRUE;
11189 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011191
11192 /* restore previous notion of curbuf */
11193 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011194 }
11195
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011196 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11197 /* use the default value */
11198 copy_tv(&argvars[2], rettv);
11199
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200 --emsg_off;
11201}
11202
11203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 * "getchar()" function
11205 */
11206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011208 typval_T *argvars;
11209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
11211 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011214 /* Position the cursor. Needed after a message that ends in a space. */
11215 windgoto(msg_row, msg_col);
11216
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 ++no_mapping;
11218 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011219 for (;;)
11220 {
11221 if (argvars[0].v_type == VAR_UNKNOWN)
11222 /* getchar(): blocking wait. */
11223 n = safe_vgetc();
11224 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11225 /* getchar(1): only check if char avail */
11226 n = vpeekc();
11227 else if (error || vpeekc() == NUL)
11228 /* illegal argument or getchar(0) and no char avail: return zero */
11229 n = 0;
11230 else
11231 /* getchar(0) and char avail: return char */
11232 n = safe_vgetc();
11233 if (n == K_IGNORE)
11234 continue;
11235 break;
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 --no_mapping;
11238 --allow_keys;
11239
Bram Moolenaar219b8702006-11-01 14:32:36 +000011240 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11241 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11242 vimvars[VV_MOUSE_COL].vv_nr = 0;
11243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 if (IS_SPECIAL(n) || mod_mask != 0)
11246 {
11247 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11248 int i = 0;
11249
11250 /* Turn a special key into three bytes, plus modifier. */
11251 if (mod_mask != 0)
11252 {
11253 temp[i++] = K_SPECIAL;
11254 temp[i++] = KS_MODIFIER;
11255 temp[i++] = mod_mask;
11256 }
11257 if (IS_SPECIAL(n))
11258 {
11259 temp[i++] = K_SPECIAL;
11260 temp[i++] = K_SECOND(n);
11261 temp[i++] = K_THIRD(n);
11262 }
11263#ifdef FEAT_MBYTE
11264 else if (has_mbyte)
11265 i += (*mb_char2bytes)(n, temp + i);
11266#endif
11267 else
11268 temp[i++] = n;
11269 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->v_type = VAR_STRING;
11271 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011272
11273#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011274 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011275 {
11276 int row = mouse_row;
11277 int col = mouse_col;
11278 win_T *win;
11279 linenr_T lnum;
11280# ifdef FEAT_WINDOWS
11281 win_T *wp;
11282# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011283 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011284
11285 if (row >= 0 && col >= 0)
11286 {
11287 /* Find the window at the mouse coordinates and compute the
11288 * text position. */
11289 win = mouse_find_win(&row, &col);
11290 (void)mouse_comp_pos(win, &row, &col, &lnum);
11291# ifdef FEAT_WINDOWS
11292 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011293 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011294# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011295 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011296 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11297 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11298 }
11299 }
11300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 }
11302}
11303
11304/*
11305 * "getcharmod()" function
11306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
11315/*
11316 * "getcmdline()" function
11317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->v_type = VAR_STRING;
11324 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325}
11326
11327/*
11328 * "getcmdpos()" function
11329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336}
11337
11338/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011339 * "getcmdtype()" function
11340 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011341 static void
11342f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011343 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011344 typval_T *rettv;
11345{
11346 rettv->v_type = VAR_STRING;
11347 rettv->vval.v_string = alloc(2);
11348 if (rettv->vval.v_string != NULL)
11349 {
11350 rettv->vval.v_string[0] = get_cmdline_type();
11351 rettv->vval.v_string[1] = NUL;
11352 }
11353}
11354
11355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 * "getcwd()" function
11357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011363 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011366 rettv->vval.v_string = NULL;
11367 cwd = alloc(MAXPATHL);
11368 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011370 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11371 {
11372 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011374 if (rettv->vval.v_string != NULL)
11375 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011377 }
11378 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 }
11380}
11381
11382/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011383 * "getfontname()" function
11384 */
11385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011387 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011388 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->v_type = VAR_STRING;
11391 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011392#ifdef FEAT_GUI
11393 if (gui.in_use)
11394 {
11395 GuiFont font;
11396 char_u *name = NULL;
11397
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011398 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011399 {
11400 /* Get the "Normal" font. Either the name saved by
11401 * hl_set_font_name() or from the font ID. */
11402 font = gui.norm_font;
11403 name = hl_get_font_name();
11404 }
11405 else
11406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011408 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11409 return;
11410 font = gui_mch_get_font(name, FALSE);
11411 if (font == NOFONT)
11412 return; /* Invalid font name, return empty string. */
11413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011415 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011416 gui_mch_free_font(font);
11417 }
11418#endif
11419}
11420
11421/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011422 * "getfperm({fname})" function
11423 */
11424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *argvars;
11427 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011428{
11429 char_u *fname;
11430 struct stat st;
11431 char_u *perm = NULL;
11432 char_u flags[] = "rwx";
11433 int i;
11434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011438 if (mch_stat((char *)fname, &st) >= 0)
11439 {
11440 perm = vim_strsave((char_u *)"---------");
11441 if (perm != NULL)
11442 {
11443 for (i = 0; i < 9; i++)
11444 {
11445 if (st.st_mode & (1 << (8 - i)))
11446 perm[i] = flags[i % 3];
11447 }
11448 }
11449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011451}
11452
11453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 * "getfsize({fname})" function
11455 */
11456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 typval_T *argvars;
11459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460{
11461 char_u *fname;
11462 struct stat st;
11463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467
11468 if (mch_stat((char *)fname, &st) >= 0)
11469 {
11470 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011475
11476 /* non-perfect check for overflow */
11477 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11478 rettv->vval.v_number = -2;
11479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 }
11481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483}
11484
11485/*
11486 * "getftime({fname})" function
11487 */
11488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011490 typval_T *argvars;
11491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492{
11493 char_u *fname;
11494 struct stat st;
11495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497
11498 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502}
11503
11504/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011505 * "getftype({fname})" function
11506 */
11507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 typval_T *argvars;
11510 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011511{
11512 char_u *fname;
11513 struct stat st;
11514 char_u *type = NULL;
11515 char *t;
11516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011520 if (mch_lstat((char *)fname, &st) >= 0)
11521 {
11522#ifdef S_ISREG
11523 if (S_ISREG(st.st_mode))
11524 t = "file";
11525 else if (S_ISDIR(st.st_mode))
11526 t = "dir";
11527# ifdef S_ISLNK
11528 else if (S_ISLNK(st.st_mode))
11529 t = "link";
11530# endif
11531# ifdef S_ISBLK
11532 else if (S_ISBLK(st.st_mode))
11533 t = "bdev";
11534# endif
11535# ifdef S_ISCHR
11536 else if (S_ISCHR(st.st_mode))
11537 t = "cdev";
11538# endif
11539# ifdef S_ISFIFO
11540 else if (S_ISFIFO(st.st_mode))
11541 t = "fifo";
11542# endif
11543# ifdef S_ISSOCK
11544 else if (S_ISSOCK(st.st_mode))
11545 t = "fifo";
11546# endif
11547 else
11548 t = "other";
11549#else
11550# ifdef S_IFMT
11551 switch (st.st_mode & S_IFMT)
11552 {
11553 case S_IFREG: t = "file"; break;
11554 case S_IFDIR: t = "dir"; break;
11555# ifdef S_IFLNK
11556 case S_IFLNK: t = "link"; break;
11557# endif
11558# ifdef S_IFBLK
11559 case S_IFBLK: t = "bdev"; break;
11560# endif
11561# ifdef S_IFCHR
11562 case S_IFCHR: t = "cdev"; break;
11563# endif
11564# ifdef S_IFIFO
11565 case S_IFIFO: t = "fifo"; break;
11566# endif
11567# ifdef S_IFSOCK
11568 case S_IFSOCK: t = "socket"; break;
11569# endif
11570 default: t = "other";
11571 }
11572# else
11573 if (mch_isdir(fname))
11574 t = "dir";
11575 else
11576 t = "file";
11577# endif
11578#endif
11579 type = vim_strsave((char_u *)t);
11580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011582}
11583
11584/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011585 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586 */
11587 static void
11588f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011589 typval_T *argvars;
11590 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591{
11592 linenr_T lnum;
11593 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011594 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595
11596 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011598 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011599 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011600 retlist = FALSE;
11601 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011602 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011603 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011604 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011605 retlist = TRUE;
11606 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011607
Bram Moolenaar342337a2005-07-21 21:11:17 +000011608 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011609}
11610
11611/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011612 * "getmatches()" function
11613 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011614 static void
11615f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011616 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011617 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011618{
11619#ifdef FEAT_SEARCH_EXTRA
11620 dict_T *dict;
11621 matchitem_T *cur = curwin->w_match_head;
11622
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011623 if (rettv_list_alloc(rettv) == OK)
11624 {
11625 while (cur != NULL)
11626 {
11627 dict = dict_alloc();
11628 if (dict == NULL)
11629 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011630 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11631 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11632 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11633 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11634 list_append_dict(rettv->vval.v_list, dict);
11635 cur = cur->next;
11636 }
11637 }
11638#endif
11639}
11640
11641/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011642 * "getpid()" function
11643 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011644 static void
11645f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011646 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011647 typval_T *rettv;
11648{
11649 rettv->vval.v_number = mch_get_pid();
11650}
11651
11652/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011653 * "getpos(string)" function
11654 */
11655 static void
11656f_getpos(argvars, rettv)
11657 typval_T *argvars;
11658 typval_T *rettv;
11659{
11660 pos_T *fp;
11661 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011662 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011663
11664 if (rettv_list_alloc(rettv) == OK)
11665 {
11666 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011667 fp = var2fpos(&argvars[0], TRUE, &fnum);
11668 if (fnum != -1)
11669 list_append_number(l, (varnumber_T)fnum);
11670 else
11671 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011672 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11673 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011674 list_append_number(l, (fp != NULL)
11675 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011676 : (varnumber_T)0);
11677 list_append_number(l,
11678#ifdef FEAT_VIRTUALEDIT
11679 (fp != NULL) ? (varnumber_T)fp->coladd :
11680#endif
11681 (varnumber_T)0);
11682 }
11683 else
11684 rettv->vval.v_number = FALSE;
11685}
11686
11687/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011688 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011689 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011690 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011691f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011692 typval_T *argvars UNUSED;
11693 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011694{
11695#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011696 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011697#endif
11698
Bram Moolenaar2641f772005-03-25 21:58:17 +000011699#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011700 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011701 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011702 wp = NULL;
11703 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11704 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011705 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011706 if (wp == NULL)
11707 return;
11708 }
11709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011710 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011711 }
11712#endif
11713}
11714
11715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 * "getreg()" function
11717 */
11718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 typval_T *argvars;
11721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722{
11723 char_u *strregname;
11724 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011725 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011726 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011728 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 strregname = get_tv_string_chk(&argvars[0]);
11731 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011732 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011736 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 regname = (strregname == NULL ? '"' : *strregname);
11738 if (regname == 0)
11739 regname = '"';
11740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 rettv->vval.v_string = error ? NULL :
11743 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744}
11745
11746/*
11747 * "getregtype()" function
11748 */
11749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011751 typval_T *argvars;
11752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753{
11754 char_u *strregname;
11755 int regname;
11756 char_u buf[NUMBUFLEN + 2];
11757 long reglen = 0;
11758
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011759 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 {
11761 strregname = get_tv_string_chk(&argvars[0]);
11762 if (strregname == NULL) /* type error; errmsg already given */
11763 {
11764 rettv->v_type = VAR_STRING;
11765 rettv->vval.v_string = NULL;
11766 return;
11767 }
11768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 else
11770 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011771 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772
11773 regname = (strregname == NULL ? '"' : *strregname);
11774 if (regname == 0)
11775 regname = '"';
11776
11777 buf[0] = NUL;
11778 buf[1] = NUL;
11779 switch (get_reg_type(regname, &reglen))
11780 {
11781 case MLINE: buf[0] = 'V'; break;
11782 case MCHAR: buf[0] = 'v'; break;
11783#ifdef FEAT_VISUAL
11784 case MBLOCK:
11785 buf[0] = Ctrl_V;
11786 sprintf((char *)buf + 1, "%ld", reglen + 1);
11787 break;
11788#endif
11789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->v_type = VAR_STRING;
11791 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792}
11793
11794/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011795 * "gettabvar()" function
11796 */
11797 static void
11798f_gettabvar(argvars, rettv)
11799 typval_T *argvars;
11800 typval_T *rettv;
11801{
11802 tabpage_T *tp;
11803 dictitem_T *v;
11804 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011805 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011806
11807 rettv->v_type = VAR_STRING;
11808 rettv->vval.v_string = NULL;
11809
11810 varname = get_tv_string_chk(&argvars[1]);
11811 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11812 if (tp != NULL && varname != NULL)
11813 {
11814 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011815 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011816 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011817 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011818 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011819 done = TRUE;
11820 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011821 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011822
11823 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011824 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011825}
11826
11827/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011828 * "gettabwinvar()" function
11829 */
11830 static void
11831f_gettabwinvar(argvars, rettv)
11832 typval_T *argvars;
11833 typval_T *rettv;
11834{
11835 getwinvar(argvars, rettv, 1);
11836}
11837
11838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 * "getwinposx()" function
11840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011843 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847#ifdef FEAT_GUI
11848 if (gui.in_use)
11849 {
11850 int x, y;
11851
11852 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 }
11855#endif
11856}
11857
11858/*
11859 * "getwinposy()" function
11860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867#ifdef FEAT_GUI
11868 if (gui.in_use)
11869 {
11870 int x, y;
11871
11872 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 }
11875#endif
11876}
11877
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011878/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011879 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011880 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011881 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011882find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011883 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011884 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011885{
11886#ifdef FEAT_WINDOWS
11887 win_T *wp;
11888#endif
11889 int nr;
11890
11891 nr = get_tv_number_chk(vp, NULL);
11892
11893#ifdef FEAT_WINDOWS
11894 if (nr < 0)
11895 return NULL;
11896 if (nr == 0)
11897 return curwin;
11898
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011899 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11900 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011901 if (--nr <= 0)
11902 break;
11903 return wp;
11904#else
11905 if (nr == 0 || nr == 1)
11906 return curwin;
11907 return NULL;
11908#endif
11909}
11910
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911/*
11912 * "getwinvar()" function
11913 */
11914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011916 typval_T *argvars;
11917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011919 getwinvar(argvars, rettv, 0);
11920}
11921
11922/*
11923 * getwinvar() and gettabwinvar()
11924 */
11925 static void
11926getwinvar(argvars, rettv, off)
11927 typval_T *argvars;
11928 typval_T *rettv;
11929 int off; /* 1 for gettabwinvar() */
11930{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 win_T *win, *oldcurwin;
11932 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011933 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011934 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011935 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011937#ifdef FEAT_WINDOWS
11938 if (off == 1)
11939 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11940 else
11941 tp = curtab;
11942#endif
11943 win = find_win_by_nr(&argvars[off], tp);
11944 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011945 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011947 rettv->v_type = VAR_STRING;
11948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949
11950 if (win != NULL && varname != NULL)
11951 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011952 /* Set curwin to be our win, temporarily. Also set the tabpage,
11953 * otherwise the window is not valid. */
11954 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011955
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011957 {
11958 if (get_option_tv(&varname, rettv, 1) == OK)
11959 done = TRUE;
11960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 else
11962 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011963 /* Look up the variable. */
11964 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11965 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011967 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011969 done = TRUE;
11970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011972
11973 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011974 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
11976
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011977 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11978 /* use the default return value */
11979 copy_tv(&argvars[off + 2], rettv);
11980
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 --emsg_off;
11982}
11983
11984/*
11985 * "glob()" function
11986 */
11987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011989 typval_T *argvars;
11990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011992 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011994 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011996 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011997 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011999 if (argvars[1].v_type != VAR_UNKNOWN)
12000 {
12001 if (get_tv_number_chk(&argvars[1], &error))
12002 options |= WILD_KEEP_ALL;
12003 if (argvars[2].v_type != VAR_UNKNOWN
12004 && get_tv_number_chk(&argvars[2], &error))
12005 {
12006 rettv->v_type = VAR_LIST;
12007 rettv->vval.v_list = NULL;
12008 }
12009 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012010 if (!error)
12011 {
12012 ExpandInit(&xpc);
12013 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012014 if (p_wic)
12015 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012016 if (rettv->v_type == VAR_STRING)
12017 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012018 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012019 else if (rettv_list_alloc(rettv) != FAIL)
12020 {
12021 int i;
12022
12023 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12024 NULL, options, WILD_ALL_KEEP);
12025 for (i = 0; i < xpc.xp_numfiles; i++)
12026 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12027
12028 ExpandCleanup(&xpc);
12029 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012030 }
12031 else
12032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033}
12034
12035/*
12036 * "globpath()" function
12037 */
12038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012040 typval_T *argvars;
12041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012043 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012046 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012048 /* When the optional second argument is non-zero, don't remove matches
12049 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12050 if (argvars[2].v_type != VAR_UNKNOWN
12051 && get_tv_number_chk(&argvars[2], &error))
12052 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012054 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012055 rettv->vval.v_string = NULL;
12056 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012057 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12058 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059}
12060
12061/*
12062 * "has()" function
12063 */
12064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 typval_T *argvars;
12067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068{
12069 int i;
12070 char_u *name;
12071 int n = FALSE;
12072 static char *(has_list[]) =
12073 {
12074#ifdef AMIGA
12075 "amiga",
12076# ifdef FEAT_ARP
12077 "arp",
12078# endif
12079#endif
12080#ifdef __BEOS__
12081 "beos",
12082#endif
12083#ifdef MSDOS
12084# ifdef DJGPP
12085 "dos32",
12086# else
12087 "dos16",
12088# endif
12089#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012090#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 "mac",
12092#endif
12093#if defined(MACOS_X_UNIX)
12094 "macunix",
12095#endif
12096#ifdef OS2
12097 "os2",
12098#endif
12099#ifdef __QNX__
12100 "qnx",
12101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102#ifdef UNIX
12103 "unix",
12104#endif
12105#ifdef VMS
12106 "vms",
12107#endif
12108#ifdef WIN16
12109 "win16",
12110#endif
12111#ifdef WIN32
12112 "win32",
12113#endif
12114#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12115 "win32unix",
12116#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012117#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 "win64",
12119#endif
12120#ifdef EBCDIC
12121 "ebcdic",
12122#endif
12123#ifndef CASE_INSENSITIVE_FILENAME
12124 "fname_case",
12125#endif
12126#ifdef FEAT_ARABIC
12127 "arabic",
12128#endif
12129#ifdef FEAT_AUTOCMD
12130 "autocmd",
12131#endif
12132#ifdef FEAT_BEVAL
12133 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012134# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12135 "balloon_multiline",
12136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137#endif
12138#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12139 "builtin_terms",
12140# ifdef ALL_BUILTIN_TCAPS
12141 "all_builtin_terms",
12142# endif
12143#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012144#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12145 || defined(FEAT_GUI_W32) \
12146 || defined(FEAT_GUI_MOTIF))
12147 "browsefilter",
12148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149#ifdef FEAT_BYTEOFF
12150 "byte_offset",
12151#endif
12152#ifdef FEAT_CINDENT
12153 "cindent",
12154#endif
12155#ifdef FEAT_CLIENTSERVER
12156 "clientserver",
12157#endif
12158#ifdef FEAT_CLIPBOARD
12159 "clipboard",
12160#endif
12161#ifdef FEAT_CMDL_COMPL
12162 "cmdline_compl",
12163#endif
12164#ifdef FEAT_CMDHIST
12165 "cmdline_hist",
12166#endif
12167#ifdef FEAT_COMMENTS
12168 "comments",
12169#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012170#ifdef FEAT_CONCEAL
12171 "conceal",
12172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173#ifdef FEAT_CRYPT
12174 "cryptv",
12175#endif
12176#ifdef FEAT_CSCOPE
12177 "cscope",
12178#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012179#ifdef FEAT_CURSORBIND
12180 "cursorbind",
12181#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012182#ifdef CURSOR_SHAPE
12183 "cursorshape",
12184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185#ifdef DEBUG
12186 "debug",
12187#endif
12188#ifdef FEAT_CON_DIALOG
12189 "dialog_con",
12190#endif
12191#ifdef FEAT_GUI_DIALOG
12192 "dialog_gui",
12193#endif
12194#ifdef FEAT_DIFF
12195 "diff",
12196#endif
12197#ifdef FEAT_DIGRAPHS
12198 "digraphs",
12199#endif
12200#ifdef FEAT_DND
12201 "dnd",
12202#endif
12203#ifdef FEAT_EMACS_TAGS
12204 "emacs_tags",
12205#endif
12206 "eval", /* always present, of course! */
12207#ifdef FEAT_EX_EXTRA
12208 "ex_extra",
12209#endif
12210#ifdef FEAT_SEARCH_EXTRA
12211 "extra_search",
12212#endif
12213#ifdef FEAT_FKMAP
12214 "farsi",
12215#endif
12216#ifdef FEAT_SEARCHPATH
12217 "file_in_path",
12218#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012219#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012220 "filterpipe",
12221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222#ifdef FEAT_FIND_ID
12223 "find_in_path",
12224#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012225#ifdef FEAT_FLOAT
12226 "float",
12227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#ifdef FEAT_FOLDING
12229 "folding",
12230#endif
12231#ifdef FEAT_FOOTER
12232 "footer",
12233#endif
12234#if !defined(USE_SYSTEM) && defined(UNIX)
12235 "fork",
12236#endif
12237#ifdef FEAT_GETTEXT
12238 "gettext",
12239#endif
12240#ifdef FEAT_GUI
12241 "gui",
12242#endif
12243#ifdef FEAT_GUI_ATHENA
12244# ifdef FEAT_GUI_NEXTAW
12245 "gui_neXtaw",
12246# else
12247 "gui_athena",
12248# endif
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250#ifdef FEAT_GUI_GTK
12251 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012254#ifdef FEAT_GUI_GNOME
12255 "gui_gnome",
12256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#ifdef FEAT_GUI_MAC
12258 "gui_mac",
12259#endif
12260#ifdef FEAT_GUI_MOTIF
12261 "gui_motif",
12262#endif
12263#ifdef FEAT_GUI_PHOTON
12264 "gui_photon",
12265#endif
12266#ifdef FEAT_GUI_W16
12267 "gui_win16",
12268#endif
12269#ifdef FEAT_GUI_W32
12270 "gui_win32",
12271#endif
12272#ifdef FEAT_HANGULIN
12273 "hangul_input",
12274#endif
12275#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12276 "iconv",
12277#endif
12278#ifdef FEAT_INS_EXPAND
12279 "insert_expand",
12280#endif
12281#ifdef FEAT_JUMPLIST
12282 "jumplist",
12283#endif
12284#ifdef FEAT_KEYMAP
12285 "keymap",
12286#endif
12287#ifdef FEAT_LANGMAP
12288 "langmap",
12289#endif
12290#ifdef FEAT_LIBCALL
12291 "libcall",
12292#endif
12293#ifdef FEAT_LINEBREAK
12294 "linebreak",
12295#endif
12296#ifdef FEAT_LISP
12297 "lispindent",
12298#endif
12299#ifdef FEAT_LISTCMDS
12300 "listcmds",
12301#endif
12302#ifdef FEAT_LOCALMAP
12303 "localmap",
12304#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012305#ifdef FEAT_LUA
12306# ifndef DYNAMIC_LUA
12307 "lua",
12308# endif
12309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310#ifdef FEAT_MENU
12311 "menu",
12312#endif
12313#ifdef FEAT_SESSION
12314 "mksession",
12315#endif
12316#ifdef FEAT_MODIFY_FNAME
12317 "modify_fname",
12318#endif
12319#ifdef FEAT_MOUSE
12320 "mouse",
12321#endif
12322#ifdef FEAT_MOUSESHAPE
12323 "mouseshape",
12324#endif
12325#if defined(UNIX) || defined(VMS)
12326# ifdef FEAT_MOUSE_DEC
12327 "mouse_dec",
12328# endif
12329# ifdef FEAT_MOUSE_GPM
12330 "mouse_gpm",
12331# endif
12332# ifdef FEAT_MOUSE_JSB
12333 "mouse_jsbterm",
12334# endif
12335# ifdef FEAT_MOUSE_NET
12336 "mouse_netterm",
12337# endif
12338# ifdef FEAT_MOUSE_PTERM
12339 "mouse_pterm",
12340# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012341# ifdef FEAT_MOUSE_SGR
12342 "mouse_sgr",
12343# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012344# ifdef FEAT_SYSMOUSE
12345 "mouse_sysmouse",
12346# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012347# ifdef FEAT_MOUSE_URXVT
12348 "mouse_urxvt",
12349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350# ifdef FEAT_MOUSE_XTERM
12351 "mouse_xterm",
12352# endif
12353#endif
12354#ifdef FEAT_MBYTE
12355 "multi_byte",
12356#endif
12357#ifdef FEAT_MBYTE_IME
12358 "multi_byte_ime",
12359#endif
12360#ifdef FEAT_MULTI_LANG
12361 "multi_lang",
12362#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012363#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012364#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012365 "mzscheme",
12366#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368#ifdef FEAT_OLE
12369 "ole",
12370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371#ifdef FEAT_PATH_EXTRA
12372 "path_extra",
12373#endif
12374#ifdef FEAT_PERL
12375#ifndef DYNAMIC_PERL
12376 "perl",
12377#endif
12378#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012379#ifdef FEAT_PERSISTENT_UNDO
12380 "persistent_undo",
12381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382#ifdef FEAT_PYTHON
12383#ifndef DYNAMIC_PYTHON
12384 "python",
12385#endif
12386#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012387#ifdef FEAT_PYTHON3
12388#ifndef DYNAMIC_PYTHON3
12389 "python3",
12390#endif
12391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392#ifdef FEAT_POSTSCRIPT
12393 "postscript",
12394#endif
12395#ifdef FEAT_PRINTER
12396 "printer",
12397#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012398#ifdef FEAT_PROFILE
12399 "profile",
12400#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012401#ifdef FEAT_RELTIME
12402 "reltime",
12403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404#ifdef FEAT_QUICKFIX
12405 "quickfix",
12406#endif
12407#ifdef FEAT_RIGHTLEFT
12408 "rightleft",
12409#endif
12410#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12411 "ruby",
12412#endif
12413#ifdef FEAT_SCROLLBIND
12414 "scrollbind",
12415#endif
12416#ifdef FEAT_CMDL_INFO
12417 "showcmd",
12418 "cmdline_info",
12419#endif
12420#ifdef FEAT_SIGNS
12421 "signs",
12422#endif
12423#ifdef FEAT_SMARTINDENT
12424 "smartindent",
12425#endif
12426#ifdef FEAT_SNIFF
12427 "sniff",
12428#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012429#ifdef STARTUPTIME
12430 "startuptime",
12431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432#ifdef FEAT_STL_OPT
12433 "statusline",
12434#endif
12435#ifdef FEAT_SUN_WORKSHOP
12436 "sun_workshop",
12437#endif
12438#ifdef FEAT_NETBEANS_INTG
12439 "netbeans_intg",
12440#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012441#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012442 "spell",
12443#endif
12444#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 "syntax",
12446#endif
12447#if defined(USE_SYSTEM) || !defined(UNIX)
12448 "system",
12449#endif
12450#ifdef FEAT_TAG_BINS
12451 "tag_binary",
12452#endif
12453#ifdef FEAT_TAG_OLDSTATIC
12454 "tag_old_static",
12455#endif
12456#ifdef FEAT_TAG_ANYWHITE
12457 "tag_any_white",
12458#endif
12459#ifdef FEAT_TCL
12460# ifndef DYNAMIC_TCL
12461 "tcl",
12462# endif
12463#endif
12464#ifdef TERMINFO
12465 "terminfo",
12466#endif
12467#ifdef FEAT_TERMRESPONSE
12468 "termresponse",
12469#endif
12470#ifdef FEAT_TEXTOBJ
12471 "textobjects",
12472#endif
12473#ifdef HAVE_TGETENT
12474 "tgetent",
12475#endif
12476#ifdef FEAT_TITLE
12477 "title",
12478#endif
12479#ifdef FEAT_TOOLBAR
12480 "toolbar",
12481#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012482#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12483 "unnamedplus",
12484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485#ifdef FEAT_USR_CMDS
12486 "user-commands", /* was accidentally included in 5.4 */
12487 "user_commands",
12488#endif
12489#ifdef FEAT_VIMINFO
12490 "viminfo",
12491#endif
12492#ifdef FEAT_VERTSPLIT
12493 "vertsplit",
12494#endif
12495#ifdef FEAT_VIRTUALEDIT
12496 "virtualedit",
12497#endif
12498#ifdef FEAT_VISUAL
12499 "visual",
12500#endif
12501#ifdef FEAT_VISUALEXTRA
12502 "visualextra",
12503#endif
12504#ifdef FEAT_VREPLACE
12505 "vreplace",
12506#endif
12507#ifdef FEAT_WILDIGN
12508 "wildignore",
12509#endif
12510#ifdef FEAT_WILDMENU
12511 "wildmenu",
12512#endif
12513#ifdef FEAT_WINDOWS
12514 "windows",
12515#endif
12516#ifdef FEAT_WAK
12517 "winaltkeys",
12518#endif
12519#ifdef FEAT_WRITEBACKUP
12520 "writebackup",
12521#endif
12522#ifdef FEAT_XIM
12523 "xim",
12524#endif
12525#ifdef FEAT_XFONTSET
12526 "xfontset",
12527#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012528#ifdef FEAT_XPM_W32
12529 "xpm_w32",
12530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531#ifdef USE_XSMP
12532 "xsmp",
12533#endif
12534#ifdef USE_XSMP_INTERACT
12535 "xsmp_interact",
12536#endif
12537#ifdef FEAT_XCLIPBOARD
12538 "xterm_clipboard",
12539#endif
12540#ifdef FEAT_XTERM_SAVE
12541 "xterm_save",
12542#endif
12543#if defined(UNIX) && defined(FEAT_X11)
12544 "X11",
12545#endif
12546 NULL
12547 };
12548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 for (i = 0; has_list[i] != NULL; ++i)
12551 if (STRICMP(name, has_list[i]) == 0)
12552 {
12553 n = TRUE;
12554 break;
12555 }
12556
12557 if (n == FALSE)
12558 {
12559 if (STRNICMP(name, "patch", 5) == 0)
12560 n = has_patch(atoi((char *)name + 5));
12561 else if (STRICMP(name, "vim_starting") == 0)
12562 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012563#ifdef FEAT_MBYTE
12564 else if (STRICMP(name, "multi_byte_encoding") == 0)
12565 n = has_mbyte;
12566#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012567#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12568 else if (STRICMP(name, "balloon_multiline") == 0)
12569 n = multiline_balloon_available();
12570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571#ifdef DYNAMIC_TCL
12572 else if (STRICMP(name, "tcl") == 0)
12573 n = tcl_enabled(FALSE);
12574#endif
12575#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12576 else if (STRICMP(name, "iconv") == 0)
12577 n = iconv_enabled(FALSE);
12578#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012579#ifdef DYNAMIC_LUA
12580 else if (STRICMP(name, "lua") == 0)
12581 n = lua_enabled(FALSE);
12582#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012583#ifdef DYNAMIC_MZSCHEME
12584 else if (STRICMP(name, "mzscheme") == 0)
12585 n = mzscheme_enabled(FALSE);
12586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587#ifdef DYNAMIC_RUBY
12588 else if (STRICMP(name, "ruby") == 0)
12589 n = ruby_enabled(FALSE);
12590#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012591#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592#ifdef DYNAMIC_PYTHON
12593 else if (STRICMP(name, "python") == 0)
12594 n = python_enabled(FALSE);
12595#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012596#endif
12597#ifdef FEAT_PYTHON3
12598#ifdef DYNAMIC_PYTHON3
12599 else if (STRICMP(name, "python3") == 0)
12600 n = python3_enabled(FALSE);
12601#endif
12602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603#ifdef DYNAMIC_PERL
12604 else if (STRICMP(name, "perl") == 0)
12605 n = perl_enabled(FALSE);
12606#endif
12607#ifdef FEAT_GUI
12608 else if (STRICMP(name, "gui_running") == 0)
12609 n = (gui.in_use || gui.starting);
12610# ifdef FEAT_GUI_W32
12611 else if (STRICMP(name, "gui_win32s") == 0)
12612 n = gui_is_win32s();
12613# endif
12614# ifdef FEAT_BROWSE
12615 else if (STRICMP(name, "browse") == 0)
12616 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12617# endif
12618#endif
12619#ifdef FEAT_SYN_HL
12620 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012621 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622#endif
12623#if defined(WIN3264)
12624 else if (STRICMP(name, "win95") == 0)
12625 n = mch_windows95();
12626#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012627#ifdef FEAT_NETBEANS_INTG
12628 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012629 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 }
12632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634}
12635
12636/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012637 * "has_key()" function
12638 */
12639 static void
12640f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012641 typval_T *argvars;
12642 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012643{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012644 if (argvars[0].v_type != VAR_DICT)
12645 {
12646 EMSG(_(e_dictreq));
12647 return;
12648 }
12649 if (argvars[0].vval.v_dict == NULL)
12650 return;
12651
12652 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012653 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012654}
12655
12656/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012657 * "haslocaldir()" function
12658 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012659 static void
12660f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012661 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012662 typval_T *rettv;
12663{
12664 rettv->vval.v_number = (curwin->w_localdir != NULL);
12665}
12666
12667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 * "hasmapto()" function
12669 */
12670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 typval_T *argvars;
12673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674{
12675 char_u *name;
12676 char_u *mode;
12677 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012678 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012681 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 mode = (char_u *)"nvo";
12683 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012685 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012686 if (argvars[2].v_type != VAR_UNKNOWN)
12687 abbr = get_tv_number(&argvars[2]);
12688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689
Bram Moolenaar2c932302006-03-18 21:42:09 +000012690 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694}
12695
12696/*
12697 * "histadd()" function
12698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012701 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703{
12704#ifdef FEAT_CMDHIST
12705 int histype;
12706 char_u *str;
12707 char_u buf[NUMBUFLEN];
12708#endif
12709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 if (check_restricted() || check_secure())
12712 return;
12713#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12715 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 if (histype >= 0)
12717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 if (*str != NUL)
12720 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012721 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 return;
12725 }
12726 }
12727#endif
12728}
12729
12730/*
12731 * "histdel()" function
12732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012735 typval_T *argvars UNUSED;
12736 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737{
12738#ifdef FEAT_CMDHIST
12739 int n;
12740 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12744 if (str == NULL)
12745 n = 0;
12746 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012749 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012751 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 else
12754 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 get_tv_string_buf(&argvars[1], buf));
12757 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758#endif
12759}
12760
12761/*
12762 * "histget()" function
12763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768{
12769#ifdef FEAT_CMDHIST
12770 int type;
12771 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12775 if (str == NULL)
12776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 {
12779 type = get_histtype(str);
12780 if (argvars[1].v_type == VAR_UNKNOWN)
12781 idx = get_history_idx(type);
12782 else
12783 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12784 /* -1 on type error */
12785 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791}
12792
12793/*
12794 * "histnr()" function
12795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012798 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800{
12801 int i;
12802
12803#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 char_u *history = get_tv_string_chk(&argvars[0]);
12805
12806 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 if (i >= HIST_CMD && i < HIST_COUNT)
12808 i = get_history_idx(i);
12809 else
12810#endif
12811 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813}
12814
12815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 * "highlightID(name)" function
12817 */
12818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 typval_T *argvars;
12821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827 * "highlight_exists()" function
12828 */
12829 static void
12830f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012831 typval_T *argvars;
12832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012833{
12834 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12835}
12836
12837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 * "hostname()" function
12839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012842 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844{
12845 char_u hostname[256];
12846
12847 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->v_type = VAR_STRING;
12849 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850}
12851
12852/*
12853 * iconv() function
12854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859{
12860#ifdef FEAT_MBYTE
12861 char_u buf1[NUMBUFLEN];
12862 char_u buf2[NUMBUFLEN];
12863 char_u *from, *to, *str;
12864 vimconv_T vimconv;
12865#endif
12866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->v_type = VAR_STRING;
12868 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869
12870#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 str = get_tv_string(&argvars[0]);
12872 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12873 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 vimconv.vc_type = CONV_NONE;
12875 convert_setup(&vimconv, from, to);
12876
12877 /* If the encodings are equal, no conversion needed. */
12878 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882
12883 convert_setup(&vimconv, NULL, NULL);
12884 vim_free(from);
12885 vim_free(to);
12886#endif
12887}
12888
12889/*
12890 * "indent()" function
12891 */
12892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 typval_T *argvars;
12895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896{
12897 linenr_T lnum;
12898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904}
12905
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012906/*
12907 * "index()" function
12908 */
12909 static void
12910f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012911 typval_T *argvars;
12912 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012913{
Bram Moolenaar33570922005-01-25 22:26:29 +000012914 list_T *l;
12915 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012916 long idx = 0;
12917 int ic = FALSE;
12918
12919 rettv->vval.v_number = -1;
12920 if (argvars[0].v_type != VAR_LIST)
12921 {
12922 EMSG(_(e_listreq));
12923 return;
12924 }
12925 l = argvars[0].vval.v_list;
12926 if (l != NULL)
12927 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012928 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012929 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 int error = FALSE;
12932
Bram Moolenaar758711c2005-02-02 23:11:38 +000012933 /* Start at specified item. Use the cached index that list_find()
12934 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012935 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012936 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012937 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012938 ic = get_tv_number_chk(&argvars[3], &error);
12939 if (error)
12940 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012941 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012942
Bram Moolenaar758711c2005-02-02 23:11:38 +000012943 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012944 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012945 {
12946 rettv->vval.v_number = idx;
12947 break;
12948 }
12949 }
12950}
12951
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952static int inputsecret_flag = 0;
12953
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012954static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12955
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012957 * This function is used by f_input() and f_inputdialog() functions. The third
12958 * argument to f_input() specifies the type of completion to use at the
12959 * prompt. The third argument to f_inputdialog() specifies the value to return
12960 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 */
12962 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012963get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012964 typval_T *argvars;
12965 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012966 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012968 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969 char_u *p = NULL;
12970 int c;
12971 char_u buf[NUMBUFLEN];
12972 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012973 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012974 int xp_type = EXPAND_NOTHING;
12975 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012978 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979
12980#ifdef NO_CONSOLE_INPUT
12981 /* While starting up, there is no place to enter text. */
12982 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984#endif
12985
12986 cmd_silent = FALSE; /* Want to see the prompt. */
12987 if (prompt != NULL)
12988 {
12989 /* Only the part of the message after the last NL is considered as
12990 * prompt for the command line */
12991 p = vim_strrchr(prompt, '\n');
12992 if (p == NULL)
12993 p = prompt;
12994 else
12995 {
12996 ++p;
12997 c = *p;
12998 *p = NUL;
12999 msg_start();
13000 msg_clr_eos();
13001 msg_puts_attr(prompt, echo_attr);
13002 msg_didout = FALSE;
13003 msg_starthere();
13004 *p = c;
13005 }
13006 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 if (argvars[1].v_type != VAR_UNKNOWN)
13009 {
13010 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13011 if (defstr != NULL)
13012 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013014 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013015 {
13016 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013017 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013018 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013019
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013020 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013021 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013022
Bram Moolenaar4463f292005-09-25 22:20:24 +000013023 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13024 if (xp_name == NULL)
13025 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013026
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013027 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013028
Bram Moolenaar4463f292005-09-25 22:20:24 +000013029 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13030 &xp_arg) == FAIL)
13031 return;
13032 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013033 }
13034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 if (defstr != NULL)
13036 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013037 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13038 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013039 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013040 && argvars[1].v_type != VAR_UNKNOWN
13041 && argvars[2].v_type != VAR_UNKNOWN)
13042 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13043 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013044
13045 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 /* since the user typed this, no need to wait for return */
13048 need_wait_return = FALSE;
13049 msg_didout = FALSE;
13050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 cmd_silent = cmd_silent_save;
13052}
13053
13054/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013055 * "input()" function
13056 * Also handles inputsecret() when inputsecret is set.
13057 */
13058 static void
13059f_input(argvars, rettv)
13060 typval_T *argvars;
13061 typval_T *rettv;
13062{
13063 get_user_input(argvars, rettv, FALSE);
13064}
13065
13066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 * "inputdialog()" function
13068 */
13069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 typval_T *argvars;
13072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073{
13074#if defined(FEAT_GUI_TEXTDIALOG)
13075 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13076 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13077 {
13078 char_u *message;
13079 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013080 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013082 message = get_tv_string_chk(&argvars[0]);
13083 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013084 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013085 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 else
13087 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 if (message != NULL && defstr != NULL
13089 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013090 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 else
13093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 if (message != NULL && defstr != NULL
13095 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013096 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097 rettv->vval.v_string = vim_strsave(
13098 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 }
13104 else
13105#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013106 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107}
13108
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013109/*
13110 * "inputlist()" function
13111 */
13112 static void
13113f_inputlist(argvars, rettv)
13114 typval_T *argvars;
13115 typval_T *rettv;
13116{
13117 listitem_T *li;
13118 int selected;
13119 int mouse_used;
13120
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013121#ifdef NO_CONSOLE_INPUT
13122 /* While starting up, there is no place to enter text. */
13123 if (no_console_input())
13124 return;
13125#endif
13126 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13127 {
13128 EMSG2(_(e_listarg), "inputlist()");
13129 return;
13130 }
13131
13132 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013133 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013134 lines_left = Rows; /* avoid more prompt */
13135 msg_scroll = TRUE;
13136 msg_clr_eos();
13137
13138 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13139 {
13140 msg_puts(get_tv_string(&li->li_tv));
13141 msg_putchar('\n');
13142 }
13143
13144 /* Ask for choice. */
13145 selected = prompt_for_number(&mouse_used);
13146 if (mouse_used)
13147 selected -= lines_left;
13148
13149 rettv->vval.v_number = selected;
13150}
13151
13152
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13154
13155/*
13156 * "inputrestore()" function
13157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162{
13163 if (ga_userinput.ga_len > 0)
13164 {
13165 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13167 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013168 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 }
13170 else if (p_verbose > 1)
13171 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013172 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
13175}
13176
13177/*
13178 * "inputsave()" function
13179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013185 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 if (ga_grow(&ga_userinput, 1) == OK)
13187 {
13188 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13189 + ga_userinput.ga_len);
13190 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013191 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 }
13193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195}
13196
13197/*
13198 * "inputsecret()" function
13199 */
13200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013202 typval_T *argvars;
13203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204{
13205 ++cmdline_star;
13206 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 --cmdline_star;
13209 --inputsecret_flag;
13210}
13211
13212/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 * "insert()" function
13214 */
13215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013217 typval_T *argvars;
13218 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219{
13220 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 listitem_T *item;
13222 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224
13225 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013226 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013227 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013228 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013229 {
13230 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013231 before = get_tv_number_chk(&argvars[2], &error);
13232 if (error)
13233 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013234
Bram Moolenaar758711c2005-02-02 23:11:38 +000013235 if (before == l->lv_len)
13236 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013237 else
13238 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013239 item = list_find(l, before);
13240 if (item == NULL)
13241 {
13242 EMSGN(_(e_listidx), before);
13243 l = NULL;
13244 }
13245 }
13246 if (l != NULL)
13247 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013248 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013249 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013250 }
13251 }
13252}
13253
13254/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013255 * "invert(expr)" function
13256 */
13257 static void
13258f_invert(argvars, rettv)
13259 typval_T *argvars;
13260 typval_T *rettv;
13261{
13262 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13263}
13264
13265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 * "isdirectory()" function
13267 */
13268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013270 typval_T *argvars;
13271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274}
13275
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013276/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013277 * "islocked()" function
13278 */
13279 static void
13280f_islocked(argvars, rettv)
13281 typval_T *argvars;
13282 typval_T *rettv;
13283{
13284 lval_T lv;
13285 char_u *end;
13286 dictitem_T *di;
13287
13288 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013289 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13290 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013291 if (end != NULL && lv.ll_name != NULL)
13292 {
13293 if (*end != NUL)
13294 EMSG(_(e_trailing));
13295 else
13296 {
13297 if (lv.ll_tv == NULL)
13298 {
13299 if (check_changedtick(lv.ll_name))
13300 rettv->vval.v_number = 1; /* always locked */
13301 else
13302 {
13303 di = find_var(lv.ll_name, NULL);
13304 if (di != NULL)
13305 {
13306 /* Consider a variable locked when:
13307 * 1. the variable itself is locked
13308 * 2. the value of the variable is locked.
13309 * 3. the List or Dict value is locked.
13310 */
13311 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13312 || tv_islocked(&di->di_tv));
13313 }
13314 }
13315 }
13316 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013317 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013318 else if (lv.ll_newkey != NULL)
13319 EMSG2(_(e_dictkey), lv.ll_newkey);
13320 else if (lv.ll_list != NULL)
13321 /* List item. */
13322 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13323 else
13324 /* Dictionary item. */
13325 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13326 }
13327 }
13328
13329 clear_lval(&lv);
13330}
13331
Bram Moolenaar33570922005-01-25 22:26:29 +000013332static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013333
13334/*
13335 * Turn a dict into a list:
13336 * "what" == 0: list of keys
13337 * "what" == 1: list of values
13338 * "what" == 2: list of items
13339 */
13340 static void
13341dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013342 typval_T *argvars;
13343 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013344 int what;
13345{
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 list_T *l2;
13347 dictitem_T *di;
13348 hashitem_T *hi;
13349 listitem_T *li;
13350 listitem_T *li2;
13351 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013352 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353
Bram Moolenaar8c711452005-01-14 21:53:12 +000013354 if (argvars[0].v_type != VAR_DICT)
13355 {
13356 EMSG(_(e_dictreq));
13357 return;
13358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013359 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013360 return;
13361
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013362 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013363 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013365 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013368 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013370 --todo;
13371 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013372
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013373 li = listitem_alloc();
13374 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013375 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013376 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013377
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013378 if (what == 0)
13379 {
13380 /* keys() */
13381 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013382 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013383 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13384 }
13385 else if (what == 1)
13386 {
13387 /* values() */
13388 copy_tv(&di->di_tv, &li->li_tv);
13389 }
13390 else
13391 {
13392 /* items() */
13393 l2 = list_alloc();
13394 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013395 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013396 li->li_tv.vval.v_list = l2;
13397 if (l2 == NULL)
13398 break;
13399 ++l2->lv_refcount;
13400
13401 li2 = listitem_alloc();
13402 if (li2 == NULL)
13403 break;
13404 list_append(l2, li2);
13405 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013406 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013407 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13408
13409 li2 = listitem_alloc();
13410 if (li2 == NULL)
13411 break;
13412 list_append(l2, li2);
13413 copy_tv(&di->di_tv, &li2->li_tv);
13414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013415 }
13416 }
13417}
13418
13419/*
13420 * "items(dict)" function
13421 */
13422 static void
13423f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 typval_T *argvars;
13425 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013426{
13427 dict_list(argvars, rettv, 2);
13428}
13429
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013431 * "join()" function
13432 */
13433 static void
13434f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013435 typval_T *argvars;
13436 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013437{
13438 garray_T ga;
13439 char_u *sep;
13440
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013441 if (argvars[0].v_type != VAR_LIST)
13442 {
13443 EMSG(_(e_listreq));
13444 return;
13445 }
13446 if (argvars[0].vval.v_list == NULL)
13447 return;
13448 if (argvars[1].v_type == VAR_UNKNOWN)
13449 sep = (char_u *)" ";
13450 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013452
13453 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013454
13455 if (sep != NULL)
13456 {
13457 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013458 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 ga_append(&ga, NUL);
13460 rettv->vval.v_string = (char_u *)ga.ga_data;
13461 }
13462 else
13463 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464}
13465
13466/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013467 * "keys()" function
13468 */
13469 static void
13470f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013471 typval_T *argvars;
13472 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013473{
13474 dict_list(argvars, rettv, 0);
13475}
13476
13477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 * "last_buffer_nr()" function.
13479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013482 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484{
13485 int n = 0;
13486 buf_T *buf;
13487
13488 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13489 if (n < buf->b_fnum)
13490 n = buf->b_fnum;
13491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013493}
13494
13495/*
13496 * "len()" function
13497 */
13498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013502{
13503 switch (argvars[0].v_type)
13504 {
13505 case VAR_STRING:
13506 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507 rettv->vval.v_number = (varnumber_T)STRLEN(
13508 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013509 break;
13510 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013512 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013513 case VAR_DICT:
13514 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13515 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013516 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013517 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013518 break;
13519 }
13520}
13521
Bram Moolenaar33570922005-01-25 22:26:29 +000013522static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523
13524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 typval_T *argvars;
13527 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013528 int type;
13529{
13530#ifdef FEAT_LIBCALL
13531 char_u *string_in;
13532 char_u **string_result;
13533 int nr_result;
13534#endif
13535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013537 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013539
13540 if (check_restricted() || check_secure())
13541 return;
13542
13543#ifdef FEAT_LIBCALL
13544 /* The first two args must be strings, otherwise its meaningless */
13545 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13546 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013547 string_in = NULL;
13548 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549 string_in = argvars[2].vval.v_string;
13550 if (type == VAR_NUMBER)
13551 string_result = NULL;
13552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554 if (mch_libcall(argvars[0].vval.v_string,
13555 argvars[1].vval.v_string,
13556 string_in,
13557 argvars[2].vval.v_number,
13558 string_result,
13559 &nr_result) == OK
13560 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013562 }
13563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564}
13565
13566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013567 * "libcall()" function
13568 */
13569 static void
13570f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013573{
13574 libcall_common(argvars, rettv, VAR_STRING);
13575}
13576
13577/*
13578 * "libcallnr()" function
13579 */
13580 static void
13581f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013582 typval_T *argvars;
13583 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013584{
13585 libcall_common(argvars, rettv, VAR_NUMBER);
13586}
13587
13588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 * "line(string)" function
13590 */
13591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
13596 linenr_T lnum = 0;
13597 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013598 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013600 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 if (fp != NULL)
13602 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604}
13605
13606/*
13607 * "line2byte(lnum)" function
13608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610f_line2byte(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{
13614#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616#else
13617 linenr_T lnum;
13618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13624 if (rettv->vval.v_number >= 0)
13625 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626#endif
13627}
13628
13629/*
13630 * "lispindent(lnum)" function
13631 */
13632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636{
13637#ifdef FEAT_LISP
13638 pos_T pos;
13639 linenr_T lnum;
13640
13641 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13644 {
13645 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 curwin->w_cursor = pos;
13648 }
13649 else
13650#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652}
13653
13654/*
13655 * "localtime()" function
13656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663}
13664
Bram Moolenaar33570922005-01-25 22:26:29 +000013665static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
13667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 int exact;
13672{
13673 char_u *keys;
13674 char_u *which;
13675 char_u buf[NUMBUFLEN];
13676 char_u *keys_buf = NULL;
13677 char_u *rhs;
13678 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013679 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013680 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013681 mapblock_T *mp;
13682 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683
13684 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685 rettv->v_type = VAR_STRING;
13686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 if (*keys == NUL)
13690 return;
13691
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013692 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013695 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013696 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013697 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013698 if (argvars[3].v_type != VAR_UNKNOWN)
13699 get_dict = get_tv_number(&argvars[3]);
13700 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 else
13703 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 if (which == NULL)
13705 return;
13706
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 mode = get_map_mode(&which, 0);
13708
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013709 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013710 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013712
13713 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013715 /* Return a string. */
13716 if (rhs != NULL)
13717 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718
Bram Moolenaarbd743252010-10-20 21:23:33 +020013719 }
13720 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13721 {
13722 /* Return a dictionary. */
13723 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13724 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13725 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726
Bram Moolenaarbd743252010-10-20 21:23:33 +020013727 dict_add_nr_str(dict, "lhs", 0L, lhs);
13728 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13729 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13730 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13731 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13732 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13733 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13734 dict_add_nr_str(dict, "mode", 0L, mapmode);
13735
13736 vim_free(lhs);
13737 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 }
13739}
13740
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013741#ifdef FEAT_FLOAT
13742/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013743 * "log()" function
13744 */
13745 static void
13746f_log(argvars, rettv)
13747 typval_T *argvars;
13748 typval_T *rettv;
13749{
13750 float_T f;
13751
13752 rettv->v_type = VAR_FLOAT;
13753 if (get_float_arg(argvars, &f) == OK)
13754 rettv->vval.v_float = log(f);
13755 else
13756 rettv->vval.v_float = 0.0;
13757}
13758
13759/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013760 * "log10()" function
13761 */
13762 static void
13763f_log10(argvars, rettv)
13764 typval_T *argvars;
13765 typval_T *rettv;
13766{
13767 float_T f;
13768
13769 rettv->v_type = VAR_FLOAT;
13770 if (get_float_arg(argvars, &f) == OK)
13771 rettv->vval.v_float = log10(f);
13772 else
13773 rettv->vval.v_float = 0.0;
13774}
13775#endif
13776
Bram Moolenaar1dced572012-04-05 16:54:08 +020013777#ifdef FEAT_LUA
13778/*
13779 * "luaeval()" function
13780 */
13781 static void
13782f_luaeval(argvars, rettv)
13783 typval_T *argvars;
13784 typval_T *rettv;
13785{
13786 char_u *str;
13787 char_u buf[NUMBUFLEN];
13788
13789 str = get_tv_string_buf(&argvars[0], buf);
13790 do_luaeval(str, argvars + 1, rettv);
13791}
13792#endif
13793
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013795 * "map()" function
13796 */
13797 static void
13798f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 typval_T *argvars;
13800 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013801{
13802 filter_map(argvars, rettv, TRUE);
13803}
13804
13805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 */
13808 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013809f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 typval_T *argvars;
13811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814}
13815
13816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013817 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 */
13819 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013820f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 typval_T *argvars;
13822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013824 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825}
13826
Bram Moolenaar33570922005-01-25 22:26:29 +000013827static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
13829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013831 typval_T *argvars;
13832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 int type;
13834{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013835 char_u *str = NULL;
13836 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 char_u *pat;
13838 regmatch_T regmatch;
13839 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013840 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 char_u *save_cpo;
13842 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013843 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013844 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013845 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013846 list_T *l = NULL;
13847 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013848 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013849 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
13851 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13852 save_cpo = p_cpo;
13853 p_cpo = (char_u *)"";
13854
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013855 rettv->vval.v_number = -1;
13856 if (type == 3)
13857 {
13858 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013859 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013860 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013861 }
13862 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013864 rettv->v_type = VAR_STRING;
13865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013868 if (argvars[0].v_type == VAR_LIST)
13869 {
13870 if ((l = argvars[0].vval.v_list) == NULL)
13871 goto theend;
13872 li = l->lv_first;
13873 }
13874 else
13875 expr = str = get_tv_string(&argvars[0]);
13876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13878 if (pat == NULL)
13879 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013880
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013881 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883 int error = FALSE;
13884
13885 start = get_tv_number_chk(&argvars[2], &error);
13886 if (error)
13887 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013888 if (l != NULL)
13889 {
13890 li = list_find(l, start);
13891 if (li == NULL)
13892 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013893 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013894 }
13895 else
13896 {
13897 if (start < 0)
13898 start = 0;
13899 if (start > (long)STRLEN(str))
13900 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013901 /* When "count" argument is there ignore matches before "start",
13902 * otherwise skip part of the string. Differs when pattern is "^"
13903 * or "\<". */
13904 if (argvars[3].v_type != VAR_UNKNOWN)
13905 startcol = start;
13906 else
13907 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013908 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013909
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013910 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911 nth = get_tv_number_chk(&argvars[3], &error);
13912 if (error)
13913 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 }
13915
13916 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13917 if (regmatch.regprog != NULL)
13918 {
13919 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013920
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013921 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013922 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 if (l != NULL)
13924 {
13925 if (li == NULL)
13926 {
13927 match = FALSE;
13928 break;
13929 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013930 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013931 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013932 if (str == NULL)
13933 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013934 }
13935
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013936 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013938 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013939 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013940 if (l == NULL && !match)
13941 break;
13942
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013943 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013944 if (l != NULL)
13945 {
13946 li = li->li_next;
13947 ++idx;
13948 }
13949 else
13950 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013951#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013952 startcol = (colnr_T)(regmatch.startp[0]
13953 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013954#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013955 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013956#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013957 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013958 }
13959
13960 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013962 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013963 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013964 int i;
13965
13966 /* return list with matched string and submatches */
13967 for (i = 0; i < NSUBEXP; ++i)
13968 {
13969 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013970 {
13971 if (list_append_string(rettv->vval.v_list,
13972 (char_u *)"", 0) == FAIL)
13973 break;
13974 }
13975 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013976 regmatch.startp[i],
13977 (int)(regmatch.endp[i] - regmatch.startp[i]))
13978 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013979 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013980 }
13981 }
13982 else if (type == 2)
13983 {
13984 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013985 if (l != NULL)
13986 copy_tv(&li->li_tv, rettv);
13987 else
13988 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013990 }
13991 else if (l != NULL)
13992 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 else
13994 {
13995 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013996 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 (varnumber_T)(regmatch.startp[0] - str);
13998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013999 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014001 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 }
14003 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014004 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 }
14006
14007theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014008 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 p_cpo = save_cpo;
14010}
14011
14012/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013 * "match()" function
14014 */
14015 static void
14016f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T *argvars;
14018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014019{
14020 find_some_match(argvars, rettv, 1);
14021}
14022
14023/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014024 * "matchadd()" function
14025 */
14026 static void
14027f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014028 typval_T *argvars UNUSED;
14029 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014030{
14031#ifdef FEAT_SEARCH_EXTRA
14032 char_u buf[NUMBUFLEN];
14033 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14034 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14035 int prio = 10; /* default priority */
14036 int id = -1;
14037 int error = FALSE;
14038
14039 rettv->vval.v_number = -1;
14040
14041 if (grp == NULL || pat == NULL)
14042 return;
14043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014044 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014045 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014046 if (argvars[3].v_type != VAR_UNKNOWN)
14047 id = get_tv_number_chk(&argvars[3], &error);
14048 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014049 if (error == TRUE)
14050 return;
14051 if (id >= 1 && id <= 3)
14052 {
14053 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14054 return;
14055 }
14056
14057 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14058#endif
14059}
14060
14061/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014062 * "matcharg()" function
14063 */
14064 static void
14065f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014066 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014067 typval_T *rettv;
14068{
14069 if (rettv_list_alloc(rettv) == OK)
14070 {
14071#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014072 int id = get_tv_number(&argvars[0]);
14073 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014074
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014075 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014076 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014077 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14078 {
14079 list_append_string(rettv->vval.v_list,
14080 syn_id2name(m->hlg_id), -1);
14081 list_append_string(rettv->vval.v_list, m->pattern, -1);
14082 }
14083 else
14084 {
14085 list_append_string(rettv->vval.v_list, NUL, -1);
14086 list_append_string(rettv->vval.v_list, NUL, -1);
14087 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014088 }
14089#endif
14090 }
14091}
14092
14093/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014094 * "matchdelete()" function
14095 */
14096 static void
14097f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014098 typval_T *argvars UNUSED;
14099 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014100{
14101#ifdef FEAT_SEARCH_EXTRA
14102 rettv->vval.v_number = match_delete(curwin,
14103 (int)get_tv_number(&argvars[0]), TRUE);
14104#endif
14105}
14106
14107/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108 * "matchend()" function
14109 */
14110 static void
14111f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114{
14115 find_some_match(argvars, rettv, 0);
14116}
14117
14118/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014119 * "matchlist()" function
14120 */
14121 static void
14122f_matchlist(argvars, rettv)
14123 typval_T *argvars;
14124 typval_T *rettv;
14125{
14126 find_some_match(argvars, rettv, 3);
14127}
14128
14129/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014130 * "matchstr()" function
14131 */
14132 static void
14133f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136{
14137 find_some_match(argvars, rettv, 2);
14138}
14139
Bram Moolenaar33570922005-01-25 22:26:29 +000014140static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014141
14142 static void
14143max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 typval_T *argvars;
14145 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014146 int domax;
14147{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014148 long n = 0;
14149 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014151
14152 if (argvars[0].v_type == VAR_LIST)
14153 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014154 list_T *l;
14155 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014156
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014157 l = argvars[0].vval.v_list;
14158 if (l != NULL)
14159 {
14160 li = l->lv_first;
14161 if (li != NULL)
14162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014163 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014164 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014165 {
14166 li = li->li_next;
14167 if (li == NULL)
14168 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170 if (domax ? i > n : i < n)
14171 n = i;
14172 }
14173 }
14174 }
14175 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014176 else if (argvars[0].v_type == VAR_DICT)
14177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014178 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014179 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014180 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014181 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014182
14183 d = argvars[0].vval.v_dict;
14184 if (d != NULL)
14185 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014186 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014187 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014188 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014189 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014190 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014191 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014193 if (first)
14194 {
14195 n = i;
14196 first = FALSE;
14197 }
14198 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014199 n = i;
14200 }
14201 }
14202 }
14203 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014204 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014205 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014207}
14208
14209/*
14210 * "max()" function
14211 */
14212 static void
14213f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014214 typval_T *argvars;
14215 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014216{
14217 max_min(argvars, rettv, TRUE);
14218}
14219
14220/*
14221 * "min()" function
14222 */
14223 static void
14224f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 typval_T *argvars;
14226 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014227{
14228 max_min(argvars, rettv, FALSE);
14229}
14230
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014231static int mkdir_recurse __ARGS((char_u *dir, int prot));
14232
14233/*
14234 * Create the directory in which "dir" is located, and higher levels when
14235 * needed.
14236 */
14237 static int
14238mkdir_recurse(dir, prot)
14239 char_u *dir;
14240 int prot;
14241{
14242 char_u *p;
14243 char_u *updir;
14244 int r = FAIL;
14245
14246 /* Get end of directory name in "dir".
14247 * We're done when it's "/" or "c:/". */
14248 p = gettail_sep(dir);
14249 if (p <= get_past_head(dir))
14250 return OK;
14251
14252 /* If the directory exists we're done. Otherwise: create it.*/
14253 updir = vim_strnsave(dir, (int)(p - dir));
14254 if (updir == NULL)
14255 return FAIL;
14256 if (mch_isdir(updir))
14257 r = OK;
14258 else if (mkdir_recurse(updir, prot) == OK)
14259 r = vim_mkdir_emsg(updir, prot);
14260 vim_free(updir);
14261 return r;
14262}
14263
14264#ifdef vim_mkdir
14265/*
14266 * "mkdir()" function
14267 */
14268 static void
14269f_mkdir(argvars, rettv)
14270 typval_T *argvars;
14271 typval_T *rettv;
14272{
14273 char_u *dir;
14274 char_u buf[NUMBUFLEN];
14275 int prot = 0755;
14276
14277 rettv->vval.v_number = FAIL;
14278 if (check_restricted() || check_secure())
14279 return;
14280
14281 dir = get_tv_string_buf(&argvars[0], buf);
14282 if (argvars[1].v_type != VAR_UNKNOWN)
14283 {
14284 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 prot = get_tv_number_chk(&argvars[2], NULL);
14286 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014287 mkdir_recurse(dir, prot);
14288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014290}
14291#endif
14292
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 * "mode()" function
14295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014298 typval_T *argvars;
14299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014301 char_u buf[3];
14302
14303 buf[1] = NUL;
14304 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305
14306#ifdef FEAT_VISUAL
14307 if (VIsual_active)
14308 {
14309 if (VIsual_select)
14310 buf[0] = VIsual_mode + 's' - 'v';
14311 else
14312 buf[0] = VIsual_mode;
14313 }
14314 else
14315#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014316 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14317 || State == CONFIRM)
14318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014320 if (State == ASKMORE)
14321 buf[1] = 'm';
14322 else if (State == CONFIRM)
14323 buf[1] = '?';
14324 }
14325 else if (State == EXTERNCMD)
14326 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 else if (State & INSERT)
14328 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329#ifdef FEAT_VREPLACE
14330 if (State & VREPLACE_FLAG)
14331 {
14332 buf[0] = 'R';
14333 buf[1] = 'v';
14334 }
14335 else
14336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 if (State & REPLACE_FLAG)
14338 buf[0] = 'R';
14339 else
14340 buf[0] = 'i';
14341 }
14342 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014345 if (exmode_active)
14346 buf[1] = 'v';
14347 }
14348 else if (exmode_active)
14349 {
14350 buf[0] = 'c';
14351 buf[1] = 'e';
14352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014356 if (finish_op)
14357 buf[1] = 'o';
14358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014360 /* Clear out the minor mode when the argument is not a non-zero number or
14361 * non-empty string. */
14362 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014363 buf[1] = NUL;
14364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_string = vim_strsave(buf);
14366 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
Bram Moolenaar429fa852013-04-15 12:27:36 +020014369#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014370/*
14371 * "mzeval()" function
14372 */
14373 static void
14374f_mzeval(argvars, rettv)
14375 typval_T *argvars;
14376 typval_T *rettv;
14377{
14378 char_u *str;
14379 char_u buf[NUMBUFLEN];
14380
14381 str = get_tv_string_buf(&argvars[0], buf);
14382 do_mzeval(str, rettv);
14383}
Bram Moolenaar75676462013-01-30 14:55:42 +010014384
14385 void
14386mzscheme_call_vim(name, args, rettv)
14387 char_u *name;
14388 typval_T *args;
14389 typval_T *rettv;
14390{
14391 typval_T argvars[3];
14392
14393 argvars[0].v_type = VAR_STRING;
14394 argvars[0].vval.v_string = name;
14395 copy_tv(args, &argvars[1]);
14396 argvars[2].v_type = VAR_UNKNOWN;
14397 f_call(argvars, rettv);
14398 clear_tv(&argvars[1]);
14399}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014400#endif
14401
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014403 * "nextnonblank()" function
14404 */
14405 static void
14406f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *argvars;
14408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409{
14410 linenr_T lnum;
14411
14412 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014415 {
14416 lnum = 0;
14417 break;
14418 }
14419 if (*skipwhite(ml_get(lnum)) != NUL)
14420 break;
14421 }
14422 rettv->vval.v_number = lnum;
14423}
14424
14425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 * "nr2char()" function
14427 */
14428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 typval_T *argvars;
14431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432{
14433 char_u buf[NUMBUFLEN];
14434
14435#ifdef FEAT_MBYTE
14436 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014437 {
14438 int utf8 = 0;
14439
14440 if (argvars[1].v_type != VAR_UNKNOWN)
14441 utf8 = get_tv_number_chk(&argvars[1], NULL);
14442 if (utf8)
14443 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14444 else
14445 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 else
14448#endif
14449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 buf[1] = NUL;
14452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 rettv->v_type = VAR_STRING;
14454 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014455}
14456
14457/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014458 * "or(expr, expr)" function
14459 */
14460 static void
14461f_or(argvars, rettv)
14462 typval_T *argvars;
14463 typval_T *rettv;
14464{
14465 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14466 | get_tv_number_chk(&argvars[1], NULL);
14467}
14468
14469/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014470 * "pathshorten()" function
14471 */
14472 static void
14473f_pathshorten(argvars, rettv)
14474 typval_T *argvars;
14475 typval_T *rettv;
14476{
14477 char_u *p;
14478
14479 rettv->v_type = VAR_STRING;
14480 p = get_tv_string_chk(&argvars[0]);
14481 if (p == NULL)
14482 rettv->vval.v_string = NULL;
14483 else
14484 {
14485 p = vim_strsave(p);
14486 rettv->vval.v_string = p;
14487 if (p != NULL)
14488 shorten_dir(p);
14489 }
14490}
14491
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014492#ifdef FEAT_FLOAT
14493/*
14494 * "pow()" function
14495 */
14496 static void
14497f_pow(argvars, rettv)
14498 typval_T *argvars;
14499 typval_T *rettv;
14500{
14501 float_T fx, fy;
14502
14503 rettv->v_type = VAR_FLOAT;
14504 if (get_float_arg(argvars, &fx) == OK
14505 && get_float_arg(&argvars[1], &fy) == OK)
14506 rettv->vval.v_float = pow(fx, fy);
14507 else
14508 rettv->vval.v_float = 0.0;
14509}
14510#endif
14511
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014513 * "prevnonblank()" function
14514 */
14515 static void
14516f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 typval_T *argvars;
14518 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519{
14520 linenr_T lnum;
14521
14522 lnum = get_tv_lnum(argvars);
14523 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14524 lnum = 0;
14525 else
14526 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14527 --lnum;
14528 rettv->vval.v_number = lnum;
14529}
14530
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014531#ifdef HAVE_STDARG_H
14532/* This dummy va_list is here because:
14533 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14534 * - locally in the function results in a "used before set" warning
14535 * - using va_start() to initialize it gives "function with fixed args" error */
14536static va_list ap;
14537#endif
14538
Bram Moolenaar8c711452005-01-14 21:53:12 +000014539/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014540 * "printf()" function
14541 */
14542 static void
14543f_printf(argvars, rettv)
14544 typval_T *argvars;
14545 typval_T *rettv;
14546{
14547 rettv->v_type = VAR_STRING;
14548 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014549#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014550 {
14551 char_u buf[NUMBUFLEN];
14552 int len;
14553 char_u *s;
14554 int saved_did_emsg = did_emsg;
14555 char *fmt;
14556
14557 /* Get the required length, allocate the buffer and do it for real. */
14558 did_emsg = FALSE;
14559 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014560 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014561 if (!did_emsg)
14562 {
14563 s = alloc(len + 1);
14564 if (s != NULL)
14565 {
14566 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014567 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014568 }
14569 }
14570 did_emsg |= saved_did_emsg;
14571 }
14572#endif
14573}
14574
14575/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014576 * "pumvisible()" function
14577 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014578 static void
14579f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014580 typval_T *argvars UNUSED;
14581 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014582{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014583#ifdef FEAT_INS_EXPAND
14584 if (pum_visible())
14585 rettv->vval.v_number = 1;
14586#endif
14587}
14588
Bram Moolenaardb913952012-06-29 12:54:53 +020014589#ifdef FEAT_PYTHON3
14590/*
14591 * "py3eval()" function
14592 */
14593 static void
14594f_py3eval(argvars, rettv)
14595 typval_T *argvars;
14596 typval_T *rettv;
14597{
14598 char_u *str;
14599 char_u buf[NUMBUFLEN];
14600
14601 str = get_tv_string_buf(&argvars[0], buf);
14602 do_py3eval(str, rettv);
14603}
14604#endif
14605
14606#ifdef FEAT_PYTHON
14607/*
14608 * "pyeval()" function
14609 */
14610 static void
14611f_pyeval(argvars, rettv)
14612 typval_T *argvars;
14613 typval_T *rettv;
14614{
14615 char_u *str;
14616 char_u buf[NUMBUFLEN];
14617
14618 str = get_tv_string_buf(&argvars[0], buf);
14619 do_pyeval(str, rettv);
14620}
14621#endif
14622
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014623/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 * "range()" function
14625 */
14626 static void
14627f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 typval_T *argvars;
14629 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630{
14631 long start;
14632 long end;
14633 long stride = 1;
14634 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014635 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014637 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 if (argvars[1].v_type == VAR_UNKNOWN)
14639 {
14640 end = start - 1;
14641 start = 0;
14642 }
14643 else
14644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014645 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014646 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014648 }
14649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 if (error)
14651 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014652 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014653 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014654 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014655 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014656 else
14657 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014658 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014659 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014660 if (list_append_number(rettv->vval.v_list,
14661 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014662 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014663 }
14664}
14665
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014666/*
14667 * "readfile()" function
14668 */
14669 static void
14670f_readfile(argvars, rettv)
14671 typval_T *argvars;
14672 typval_T *rettv;
14673{
14674 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014675 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676 char_u *fname;
14677 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014678 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14679 int io_size = sizeof(buf);
14680 int readlen; /* size of last fread() */
14681 char_u *prev = NULL; /* previously read bytes, if any */
14682 long prevlen = 0; /* length of data in prev */
14683 long prevsize = 0; /* size of prev buffer */
14684 long maxline = MAXLNUM;
14685 long cnt = 0;
14686 char_u *p; /* position in buf */
14687 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014688
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014689 if (argvars[1].v_type != VAR_UNKNOWN)
14690 {
14691 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14692 binary = TRUE;
14693 if (argvars[2].v_type != VAR_UNKNOWN)
14694 maxline = get_tv_number(&argvars[2]);
14695 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014696
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014697 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014698 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014699
14700 /* Always open the file in binary mode, library functions have a mind of
14701 * their own about CR-LF conversion. */
14702 fname = get_tv_string(&argvars[0]);
14703 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14704 {
14705 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14706 return;
14707 }
14708
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014709 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014710 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014711 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014713 /* This for loop processes what was read, but is also entered at end
14714 * of file so that either:
14715 * - an incomplete line gets written
14716 * - a "binary" file gets an empty line at the end if it ends in a
14717 * newline. */
14718 for (p = buf, start = buf;
14719 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14720 ++p)
14721 {
14722 if (*p == '\n' || readlen <= 0)
14723 {
14724 listitem_T *li;
14725 char_u *s = NULL;
14726 long_u len = p - start;
14727
14728 /* Finished a line. Remove CRs before NL. */
14729 if (readlen > 0 && !binary)
14730 {
14731 while (len > 0 && start[len - 1] == '\r')
14732 --len;
14733 /* removal may cross back to the "prev" string */
14734 if (len == 0)
14735 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14736 --prevlen;
14737 }
14738 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014739 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014740 else
14741 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014742 /* Change "prev" buffer to be the right size. This way
14743 * the bytes are only copied once, and very long lines are
14744 * allocated only once. */
14745 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014746 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014747 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014748 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014749 prev = NULL; /* the list will own the string */
14750 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014751 }
14752 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 if (s == NULL)
14754 {
14755 do_outofmem_msg((long_u) prevlen + len + 1);
14756 failed = TRUE;
14757 break;
14758 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014761 {
14762 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014763 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014764 break;
14765 }
14766 li->li_tv.v_type = VAR_STRING;
14767 li->li_tv.v_lock = 0;
14768 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014769 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014770
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014771 start = p + 1; /* step over newline */
14772 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014773 break;
14774 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775 else if (*p == NUL)
14776 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014777#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014778 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14779 * when finding the BF and check the previous two bytes. */
14780 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014781 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014782 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14783 * + 1, these may be in the "prev" string. */
14784 char_u back1 = p >= buf + 1 ? p[-1]
14785 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14786 char_u back2 = p >= buf + 2 ? p[-2]
14787 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14788 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014789
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014790 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014792 char_u *dest = p - 2;
14793
14794 /* Usually a BOM is at the beginning of a file, and so at
14795 * the beginning of a line; then we can just step over it.
14796 */
14797 if (start == dest)
14798 start = p + 1;
14799 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014800 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014801 /* have to shuffle buf to close gap */
14802 int adjust_prevlen = 0;
14803
14804 if (dest < buf)
14805 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014806 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014807 dest = buf;
14808 }
14809 if (readlen > p - buf + 1)
14810 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14811 readlen -= 3 - adjust_prevlen;
14812 prevlen -= adjust_prevlen;
14813 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014814 }
14815 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014816 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014817#endif
14818 } /* for */
14819
14820 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14821 break;
14822 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014823 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014824 /* There's part of a line in buf, store it in "prev". */
14825 if (p - start + prevlen >= prevsize)
14826 {
14827 /* need bigger "prev" buffer */
14828 char_u *newprev;
14829
14830 /* A common use case is ordinary text files and "prev" gets a
14831 * fragment of a line, so the first allocation is made
14832 * small, to avoid repeatedly 'allocing' large and
14833 * 'reallocing' small. */
14834 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014835 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014836 else
14837 {
14838 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014839 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 prevsize = grow50pc > growmin ? grow50pc : growmin;
14841 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014842 newprev = prev == NULL ? alloc(prevsize)
14843 : vim_realloc(prev, prevsize);
14844 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014845 {
14846 do_outofmem_msg((long_u)prevsize);
14847 failed = TRUE;
14848 break;
14849 }
14850 prev = newprev;
14851 }
14852 /* Add the line part to end of "prev". */
14853 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014854 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014856 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014858 /*
14859 * For a negative line count use only the lines at the end of the file,
14860 * free the rest.
14861 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014862 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014863 while (cnt > -maxline)
14864 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014865 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014866 --cnt;
14867 }
14868
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014869 if (failed)
14870 {
14871 list_free(rettv->vval.v_list, TRUE);
14872 /* readfile doc says an empty list is returned on error */
14873 rettv->vval.v_list = list_alloc();
14874 }
14875
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014876 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014877 fclose(fd);
14878}
14879
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014880#if defined(FEAT_RELTIME)
14881static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14882
14883/*
14884 * Convert a List to proftime_T.
14885 * Return FAIL when there is something wrong.
14886 */
14887 static int
14888list2proftime(arg, tm)
14889 typval_T *arg;
14890 proftime_T *tm;
14891{
14892 long n1, n2;
14893 int error = FALSE;
14894
14895 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14896 || arg->vval.v_list->lv_len != 2)
14897 return FAIL;
14898 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14899 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14900# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014901 tm->HighPart = n1;
14902 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014903# else
14904 tm->tv_sec = n1;
14905 tm->tv_usec = n2;
14906# endif
14907 return error ? FAIL : OK;
14908}
14909#endif /* FEAT_RELTIME */
14910
14911/*
14912 * "reltime()" function
14913 */
14914 static void
14915f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014916 typval_T *argvars UNUSED;
14917 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014918{
14919#ifdef FEAT_RELTIME
14920 proftime_T res;
14921 proftime_T start;
14922
14923 if (argvars[0].v_type == VAR_UNKNOWN)
14924 {
14925 /* No arguments: get current time. */
14926 profile_start(&res);
14927 }
14928 else if (argvars[1].v_type == VAR_UNKNOWN)
14929 {
14930 if (list2proftime(&argvars[0], &res) == FAIL)
14931 return;
14932 profile_end(&res);
14933 }
14934 else
14935 {
14936 /* Two arguments: compute the difference. */
14937 if (list2proftime(&argvars[0], &start) == FAIL
14938 || list2proftime(&argvars[1], &res) == FAIL)
14939 return;
14940 profile_sub(&res, &start);
14941 }
14942
14943 if (rettv_list_alloc(rettv) == OK)
14944 {
14945 long n1, n2;
14946
14947# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014948 n1 = res.HighPart;
14949 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014950# else
14951 n1 = res.tv_sec;
14952 n2 = res.tv_usec;
14953# endif
14954 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14955 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14956 }
14957#endif
14958}
14959
14960/*
14961 * "reltimestr()" function
14962 */
14963 static void
14964f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014965 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014966 typval_T *rettv;
14967{
14968#ifdef FEAT_RELTIME
14969 proftime_T tm;
14970#endif
14971
14972 rettv->v_type = VAR_STRING;
14973 rettv->vval.v_string = NULL;
14974#ifdef FEAT_RELTIME
14975 if (list2proftime(&argvars[0], &tm) == OK)
14976 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14977#endif
14978}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014979
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14981static void make_connection __ARGS((void));
14982static int check_connection __ARGS((void));
14983
14984 static void
14985make_connection()
14986{
14987 if (X_DISPLAY == NULL
14988# ifdef FEAT_GUI
14989 && !gui.in_use
14990# endif
14991 )
14992 {
14993 x_force_connect = TRUE;
14994 setup_term_clip();
14995 x_force_connect = FALSE;
14996 }
14997}
14998
14999 static int
15000check_connection()
15001{
15002 make_connection();
15003 if (X_DISPLAY == NULL)
15004 {
15005 EMSG(_("E240: No connection to Vim server"));
15006 return FAIL;
15007 }
15008 return OK;
15009}
15010#endif
15011
15012#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015013static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014
15015 static void
15016remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015017 typval_T *argvars;
15018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019 int expr;
15020{
15021 char_u *server_name;
15022 char_u *keys;
15023 char_u *r = NULL;
15024 char_u buf[NUMBUFLEN];
15025# ifdef WIN32
15026 HWND w;
15027# else
15028 Window w;
15029# endif
15030
15031 if (check_restricted() || check_secure())
15032 return;
15033
15034# ifdef FEAT_X11
15035 if (check_connection() == FAIL)
15036 return;
15037# endif
15038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 server_name = get_tv_string_chk(&argvars[0]);
15040 if (server_name == NULL)
15041 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015042 keys = get_tv_string_buf(&argvars[1], buf);
15043# ifdef WIN32
15044 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15045# else
15046 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15047 < 0)
15048# endif
15049 {
15050 if (r != NULL)
15051 EMSG(r); /* sending worked but evaluation failed */
15052 else
15053 EMSG2(_("E241: Unable to send to %s"), server_name);
15054 return;
15055 }
15056
15057 rettv->vval.v_string = r;
15058
15059 if (argvars[2].v_type != VAR_UNKNOWN)
15060 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015061 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015062 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015064
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015065 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 v.di_tv.v_type = VAR_STRING;
15067 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 idvar = get_tv_string_chk(&argvars[2]);
15069 if (idvar != NULL)
15070 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 }
15073}
15074#endif
15075
15076/*
15077 * "remote_expr()" function
15078 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015079 static void
15080f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015081 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083{
15084 rettv->v_type = VAR_STRING;
15085 rettv->vval.v_string = NULL;
15086#ifdef FEAT_CLIENTSERVER
15087 remote_common(argvars, rettv, TRUE);
15088#endif
15089}
15090
15091/*
15092 * "remote_foreground()" function
15093 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094 static void
15095f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015096 typval_T *argvars UNUSED;
15097 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099#ifdef FEAT_CLIENTSERVER
15100# ifdef WIN32
15101 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015102 {
15103 char_u *server_name = get_tv_string_chk(&argvars[0]);
15104
15105 if (server_name != NULL)
15106 serverForeground(server_name);
15107 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108# else
15109 /* Send a foreground() expression to the server. */
15110 argvars[1].v_type = VAR_STRING;
15111 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15112 argvars[2].v_type = VAR_UNKNOWN;
15113 remote_common(argvars, rettv, TRUE);
15114 vim_free(argvars[1].vval.v_string);
15115# endif
15116#endif
15117}
15118
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 static void
15120f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123{
15124#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126 char_u *s = NULL;
15127# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015128 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015130 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131
15132 if (check_restricted() || check_secure())
15133 {
15134 rettv->vval.v_number = -1;
15135 return;
15136 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015137 serverid = get_tv_string_chk(&argvars[0]);
15138 if (serverid == NULL)
15139 {
15140 rettv->vval.v_number = -1;
15141 return; /* type error; errmsg already given */
15142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015144 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 if (n == 0)
15146 rettv->vval.v_number = -1;
15147 else
15148 {
15149 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15150 rettv->vval.v_number = (s != NULL);
15151 }
15152# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 if (check_connection() == FAIL)
15154 return;
15155
15156 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015157 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158# endif
15159
15160 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015162 char_u *retvar;
15163
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 v.di_tv.v_type = VAR_STRING;
15165 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 retvar = get_tv_string_chk(&argvars[1]);
15167 if (retvar != NULL)
15168 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170 }
15171#else
15172 rettv->vval.v_number = -1;
15173#endif
15174}
15175
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 static void
15177f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180{
15181 char_u *r = NULL;
15182
15183#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015184 char_u *serverid = get_tv_string_chk(&argvars[0]);
15185
15186 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015187 {
15188# ifdef WIN32
15189 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015190 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015192 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193 if (n != 0)
15194 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15195 if (r == NULL)
15196# else
15197 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015198 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199# endif
15200 EMSG(_("E277: Unable to read a server reply"));
15201 }
15202#endif
15203 rettv->v_type = VAR_STRING;
15204 rettv->vval.v_string = r;
15205}
15206
15207/*
15208 * "remote_send()" function
15209 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210 static void
15211f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015212 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214{
15215 rettv->v_type = VAR_STRING;
15216 rettv->vval.v_string = NULL;
15217#ifdef FEAT_CLIENTSERVER
15218 remote_common(argvars, rettv, FALSE);
15219#endif
15220}
15221
15222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015223 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015224 */
15225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015226f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 typval_T *argvars;
15228 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015229{
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 list_T *l;
15231 listitem_T *item, *item2;
15232 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015234 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015235 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015236 dict_T *d;
15237 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015238 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 if (argvars[0].v_type == VAR_DICT)
15241 {
15242 if (argvars[2].v_type != VAR_UNKNOWN)
15243 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015244 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015245 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 key = get_tv_string_chk(&argvars[1]);
15248 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 di = dict_find(d, key, -1);
15251 if (di == NULL)
15252 EMSG2(_(e_dictkey), key);
15253 else
15254 {
15255 *rettv = di->di_tv;
15256 init_tv(&di->di_tv);
15257 dictitem_remove(d, di);
15258 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015259 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015260 }
15261 }
15262 else if (argvars[0].v_type != VAR_LIST)
15263 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015264 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015265 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 int error = FALSE;
15268
15269 idx = get_tv_number_chk(&argvars[1], &error);
15270 if (error)
15271 ; /* type error: do nothing, errmsg already given */
15272 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273 EMSGN(_(e_listidx), idx);
15274 else
15275 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015276 if (argvars[2].v_type == VAR_UNKNOWN)
15277 {
15278 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015279 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015280 *rettv = item->li_tv;
15281 vim_free(item);
15282 }
15283 else
15284 {
15285 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 end = get_tv_number_chk(&argvars[2], &error);
15287 if (error)
15288 ; /* type error: do nothing */
15289 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015290 EMSGN(_(e_listidx), end);
15291 else
15292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015293 int cnt = 0;
15294
15295 for (li = item; li != NULL; li = li->li_next)
15296 {
15297 ++cnt;
15298 if (li == item2)
15299 break;
15300 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015301 if (li == NULL) /* didn't find "item2" after "item" */
15302 EMSG(_(e_invrange));
15303 else
15304 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015305 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015306 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015307 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015308 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015309 l->lv_first = item;
15310 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015311 item->li_prev = NULL;
15312 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015313 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314 }
15315 }
15316 }
15317 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015318 }
15319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320}
15321
15322/*
15323 * "rename({from}, {to})" function
15324 */
15325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015326f_rename(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 buf[NUMBUFLEN];
15331
15332 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15336 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337}
15338
15339/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015340 * "repeat()" function
15341 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015342 static void
15343f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015344 typval_T *argvars;
15345 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015346{
15347 char_u *p;
15348 int n;
15349 int slen;
15350 int len;
15351 char_u *r;
15352 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015353
15354 n = get_tv_number(&argvars[1]);
15355 if (argvars[0].v_type == VAR_LIST)
15356 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015357 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015358 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015359 if (list_extend(rettv->vval.v_list,
15360 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015361 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015362 }
15363 else
15364 {
15365 p = get_tv_string(&argvars[0]);
15366 rettv->v_type = VAR_STRING;
15367 rettv->vval.v_string = NULL;
15368
15369 slen = (int)STRLEN(p);
15370 len = slen * n;
15371 if (len <= 0)
15372 return;
15373
15374 r = alloc(len + 1);
15375 if (r != NULL)
15376 {
15377 for (i = 0; i < n; i++)
15378 mch_memmove(r + i * slen, p, (size_t)slen);
15379 r[len] = NUL;
15380 }
15381
15382 rettv->vval.v_string = r;
15383 }
15384}
15385
15386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 * "resolve()" function
15388 */
15389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015391 typval_T *argvars;
15392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393{
15394 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015395#ifdef HAVE_READLINK
15396 char_u *buf = NULL;
15397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400#ifdef FEAT_SHORTCUT
15401 {
15402 char_u *v = NULL;
15403
15404 v = mch_resolve_shortcut(p);
15405 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 }
15410#else
15411# ifdef HAVE_READLINK
15412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 char_u *cpy;
15414 int len;
15415 char_u *remain = NULL;
15416 char_u *q;
15417 int is_relative_to_current = FALSE;
15418 int has_trailing_pathsep = FALSE;
15419 int limit = 100;
15420
15421 p = vim_strsave(p);
15422
15423 if (p[0] == '.' && (vim_ispathsep(p[1])
15424 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15425 is_relative_to_current = TRUE;
15426
15427 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015428 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015431 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433
15434 q = getnextcomp(p);
15435 if (*q != NUL)
15436 {
15437 /* Separate the first path component in "p", and keep the
15438 * remainder (beginning with the path separator). */
15439 remain = vim_strsave(q - 1);
15440 q[-1] = NUL;
15441 }
15442
Bram Moolenaard9462e32011-04-11 21:35:11 +020015443 buf = alloc(MAXPATHL + 1);
15444 if (buf == NULL)
15445 goto fail;
15446
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 for (;;)
15448 {
15449 for (;;)
15450 {
15451 len = readlink((char *)p, (char *)buf, MAXPATHL);
15452 if (len <= 0)
15453 break;
15454 buf[len] = NUL;
15455
15456 if (limit-- == 0)
15457 {
15458 vim_free(p);
15459 vim_free(remain);
15460 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 goto fail;
15463 }
15464
15465 /* Ensure that the result will have a trailing path separator
15466 * if the argument has one. */
15467 if (remain == NULL && has_trailing_pathsep)
15468 add_pathsep(buf);
15469
15470 /* Separate the first path component in the link value and
15471 * concatenate the remainders. */
15472 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15473 if (*q != NUL)
15474 {
15475 if (remain == NULL)
15476 remain = vim_strsave(q - 1);
15477 else
15478 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015479 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 if (cpy != NULL)
15481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 vim_free(remain);
15483 remain = cpy;
15484 }
15485 }
15486 q[-1] = NUL;
15487 }
15488
15489 q = gettail(p);
15490 if (q > p && *q == NUL)
15491 {
15492 /* Ignore trailing path separator. */
15493 q[-1] = NUL;
15494 q = gettail(p);
15495 }
15496 if (q > p && !mch_isFullName(buf))
15497 {
15498 /* symlink is relative to directory of argument */
15499 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15500 if (cpy != NULL)
15501 {
15502 STRCPY(cpy, p);
15503 STRCPY(gettail(cpy), buf);
15504 vim_free(p);
15505 p = cpy;
15506 }
15507 }
15508 else
15509 {
15510 vim_free(p);
15511 p = vim_strsave(buf);
15512 }
15513 }
15514
15515 if (remain == NULL)
15516 break;
15517
15518 /* Append the first path component of "remain" to "p". */
15519 q = getnextcomp(remain + 1);
15520 len = q - remain - (*q != NUL);
15521 cpy = vim_strnsave(p, STRLEN(p) + len);
15522 if (cpy != NULL)
15523 {
15524 STRNCAT(cpy, remain, len);
15525 vim_free(p);
15526 p = cpy;
15527 }
15528 /* Shorten "remain". */
15529 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015530 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 else
15532 {
15533 vim_free(remain);
15534 remain = NULL;
15535 }
15536 }
15537
15538 /* If the result is a relative path name, make it explicitly relative to
15539 * the current directory if and only if the argument had this form. */
15540 if (!vim_ispathsep(*p))
15541 {
15542 if (is_relative_to_current
15543 && *p != NUL
15544 && !(p[0] == '.'
15545 && (p[1] == NUL
15546 || vim_ispathsep(p[1])
15547 || (p[1] == '.'
15548 && (p[2] == NUL
15549 || vim_ispathsep(p[2]))))))
15550 {
15551 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015552 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 if (cpy != NULL)
15554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 vim_free(p);
15556 p = cpy;
15557 }
15558 }
15559 else if (!is_relative_to_current)
15560 {
15561 /* Strip leading "./". */
15562 q = p;
15563 while (q[0] == '.' && vim_ispathsep(q[1]))
15564 q += 2;
15565 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015566 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 }
15568 }
15569
15570 /* Ensure that the result will have no trailing path separator
15571 * if the argument had none. But keep "/" or "//". */
15572 if (!has_trailing_pathsep)
15573 {
15574 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015575 if (after_pathsep(p, q))
15576 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 }
15578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 }
15581# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015582 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583# endif
15584#endif
15585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587
15588#ifdef HAVE_READLINK
15589fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015590 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593}
15594
15595/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 */
15598 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015600 typval_T *argvars;
15601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602{
Bram Moolenaar33570922005-01-25 22:26:29 +000015603 list_T *l;
15604 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 if (argvars[0].v_type != VAR_LIST)
15607 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015608 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015609 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610 {
15611 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015612 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015613 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614 while (li != NULL)
15615 {
15616 ni = li->li_prev;
15617 list_append(l, li);
15618 li = ni;
15619 }
15620 rettv->vval.v_list = l;
15621 rettv->v_type = VAR_LIST;
15622 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015623 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625}
15626
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015627#define SP_NOMOVE 0x01 /* don't move cursor */
15628#define SP_REPEAT 0x02 /* repeat to find outer pair */
15629#define SP_RETCOUNT 0x04 /* return matchcount */
15630#define SP_SETPCMARK 0x08 /* set previous context mark */
15631#define SP_START 0x10 /* accept match at start position */
15632#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15633#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015634
Bram Moolenaar33570922005-01-25 22:26:29 +000015635static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636
15637/*
15638 * Get flags for a search function.
15639 * Possibly sets "p_ws".
15640 * Returns BACKWARD, FORWARD or zero (for an error).
15641 */
15642 static int
15643get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645 int *flagsp;
15646{
15647 int dir = FORWARD;
15648 char_u *flags;
15649 char_u nbuf[NUMBUFLEN];
15650 int mask;
15651
15652 if (varp->v_type != VAR_UNKNOWN)
15653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015654 flags = get_tv_string_buf_chk(varp, nbuf);
15655 if (flags == NULL)
15656 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657 while (*flags != NUL)
15658 {
15659 switch (*flags)
15660 {
15661 case 'b': dir = BACKWARD; break;
15662 case 'w': p_ws = TRUE; break;
15663 case 'W': p_ws = FALSE; break;
15664 default: mask = 0;
15665 if (flagsp != NULL)
15666 switch (*flags)
15667 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015668 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015669 case 'e': mask = SP_END; break;
15670 case 'm': mask = SP_RETCOUNT; break;
15671 case 'n': mask = SP_NOMOVE; break;
15672 case 'p': mask = SP_SUBPAT; break;
15673 case 'r': mask = SP_REPEAT; break;
15674 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015675 }
15676 if (mask == 0)
15677 {
15678 EMSG2(_(e_invarg2), flags);
15679 dir = 0;
15680 }
15681 else
15682 *flagsp |= mask;
15683 }
15684 if (dir == 0)
15685 break;
15686 ++flags;
15687 }
15688 }
15689 return dir;
15690}
15691
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015693 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015695 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015696search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015697 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015698 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015699 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015701 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 char_u *pat;
15703 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015704 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 int save_p_ws = p_ws;
15706 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015707 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015709 proftime_T tm;
15710#ifdef FEAT_RELTIME
15711 long time_limit = 0;
15712#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015713 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015714 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015717 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015718 if (dir == 0)
15719 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015720 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015721 if (flags & SP_START)
15722 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015723 if (flags & SP_END)
15724 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015725
Bram Moolenaar76929292008-01-06 19:07:36 +000015726 /* Optional arguments: line number to stop searching and timeout. */
15727 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015728 {
15729 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15730 if (lnum_stop < 0)
15731 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015732#ifdef FEAT_RELTIME
15733 if (argvars[3].v_type != VAR_UNKNOWN)
15734 {
15735 time_limit = get_tv_number_chk(&argvars[3], NULL);
15736 if (time_limit < 0)
15737 goto theend;
15738 }
15739#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015740 }
15741
Bram Moolenaar76929292008-01-06 19:07:36 +000015742#ifdef FEAT_RELTIME
15743 /* Set the time limit, if there is one. */
15744 profile_setlimit(time_limit, &tm);
15745#endif
15746
Bram Moolenaar231334e2005-07-25 20:46:57 +000015747 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015748 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015749 * Check to make sure only those flags are set.
15750 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15751 * flags cannot be set. Check for that condition also.
15752 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015753 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015754 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015757 goto theend;
15758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015760 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015761 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015762 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015763 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015765 if (flags & SP_SUBPAT)
15766 retval = subpatnum;
15767 else
15768 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015769 if (flags & SP_SETPCMARK)
15770 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015772 if (match_pos != NULL)
15773 {
15774 /* Store the match cursor position */
15775 match_pos->lnum = pos.lnum;
15776 match_pos->col = pos.col + 1;
15777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 /* "/$" will put the cursor after the end of the line, may need to
15779 * correct that here */
15780 check_cursor();
15781 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015782
15783 /* If 'n' flag is used: restore cursor position. */
15784 if (flags & SP_NOMOVE)
15785 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015786 else
15787 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015788theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015790
15791 return retval;
15792}
15793
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015794#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015795
15796/*
15797 * round() is not in C90, use ceil() or floor() instead.
15798 */
15799 float_T
15800vim_round(f)
15801 float_T f;
15802{
15803 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15804}
15805
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015806/*
15807 * "round({float})" function
15808 */
15809 static void
15810f_round(argvars, rettv)
15811 typval_T *argvars;
15812 typval_T *rettv;
15813{
15814 float_T f;
15815
15816 rettv->v_type = VAR_FLOAT;
15817 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015818 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015819 else
15820 rettv->vval.v_float = 0.0;
15821}
15822#endif
15823
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015824/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015825 * "screenattr()" function
15826 */
15827 static void
15828f_screenattr(argvars, rettv)
15829 typval_T *argvars UNUSED;
15830 typval_T *rettv;
15831{
15832 int row;
15833 int col;
15834 int c;
15835
15836 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15837 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15838 if (row < 0 || row >= screen_Rows
15839 || col < 0 || col >= screen_Columns)
15840 c = -1;
15841 else
15842 c = ScreenAttrs[LineOffset[row] + col];
15843 rettv->vval.v_number = c;
15844}
15845
15846/*
15847 * "screenchar()" function
15848 */
15849 static void
15850f_screenchar(argvars, rettv)
15851 typval_T *argvars UNUSED;
15852 typval_T *rettv;
15853{
15854 int row;
15855 int col;
15856 int off;
15857 int c;
15858
15859 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15860 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15861 if (row < 0 || row >= screen_Rows
15862 || col < 0 || col >= screen_Columns)
15863 c = -1;
15864 else
15865 {
15866 off = LineOffset[row] + col;
15867#ifdef FEAT_MBYTE
15868 if (enc_utf8 && ScreenLinesUC[off] != 0)
15869 c = ScreenLinesUC[off];
15870 else
15871#endif
15872 c = ScreenLines[off];
15873 }
15874 rettv->vval.v_number = c;
15875}
15876
15877/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015878 * "screencol()" function
15879 *
15880 * First column is 1 to be consistent with virtcol().
15881 */
15882 static void
15883f_screencol(argvars, rettv)
15884 typval_T *argvars UNUSED;
15885 typval_T *rettv;
15886{
15887 rettv->vval.v_number = screen_screencol() + 1;
15888}
15889
15890/*
15891 * "screenrow()" function
15892 */
15893 static void
15894f_screenrow(argvars, rettv)
15895 typval_T *argvars UNUSED;
15896 typval_T *rettv;
15897{
15898 rettv->vval.v_number = screen_screenrow() + 1;
15899}
15900
15901/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015902 * "search()" function
15903 */
15904 static void
15905f_search(argvars, rettv)
15906 typval_T *argvars;
15907 typval_T *rettv;
15908{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015909 int flags = 0;
15910
15911 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912}
15913
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015915 * "searchdecl()" function
15916 */
15917 static void
15918f_searchdecl(argvars, rettv)
15919 typval_T *argvars;
15920 typval_T *rettv;
15921{
15922 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015923 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015924 int error = FALSE;
15925 char_u *name;
15926
15927 rettv->vval.v_number = 1; /* default: FAIL */
15928
15929 name = get_tv_string_chk(&argvars[0]);
15930 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015931 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015932 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015933 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15934 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15935 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015936 if (!error && name != NULL)
15937 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015938 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015939}
15940
15941/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015942 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015944 static int
15945searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015946 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015947 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948{
15949 char_u *spat, *mpat, *epat;
15950 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 int dir;
15953 int flags = 0;
15954 char_u nbuf1[NUMBUFLEN];
15955 char_u nbuf2[NUMBUFLEN];
15956 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015957 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015959 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015962 spat = get_tv_string_chk(&argvars[0]);
15963 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15964 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15965 if (spat == NULL || mpat == NULL || epat == NULL)
15966 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 /* Handle the optional fourth argument: flags */
15969 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015970 if (dir == 0)
15971 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015972
15973 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015974 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15975 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015976 if ((flags & (SP_END | SP_SUBPAT)) != 0
15977 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015978 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015979 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015980 goto theend;
15981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015983 /* Using 'r' implies 'W', otherwise it doesn't work. */
15984 if (flags & SP_REPEAT)
15985 p_ws = FALSE;
15986
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015987 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015988 if (argvars[3].v_type == VAR_UNKNOWN
15989 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 skip = (char_u *)"";
15991 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015994 if (argvars[5].v_type != VAR_UNKNOWN)
15995 {
15996 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15997 if (lnum_stop < 0)
15998 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015999#ifdef FEAT_RELTIME
16000 if (argvars[6].v_type != VAR_UNKNOWN)
16001 {
16002 time_limit = get_tv_number_chk(&argvars[6], NULL);
16003 if (time_limit < 0)
16004 goto theend;
16005 }
16006#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016007 }
16008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 if (skip == NULL)
16010 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016012 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016013 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016014
16015theend:
16016 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016017
16018 return retval;
16019}
16020
16021/*
16022 * "searchpair()" function
16023 */
16024 static void
16025f_searchpair(argvars, rettv)
16026 typval_T *argvars;
16027 typval_T *rettv;
16028{
16029 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16030}
16031
16032/*
16033 * "searchpairpos()" function
16034 */
16035 static void
16036f_searchpairpos(argvars, rettv)
16037 typval_T *argvars;
16038 typval_T *rettv;
16039{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016040 pos_T match_pos;
16041 int lnum = 0;
16042 int col = 0;
16043
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016044 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016045 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016046
16047 if (searchpair_cmn(argvars, &match_pos) > 0)
16048 {
16049 lnum = match_pos.lnum;
16050 col = match_pos.col;
16051 }
16052
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016053 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16054 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016055}
16056
16057/*
16058 * Search for a start/middle/end thing.
16059 * Used by searchpair(), see its documentation for the details.
16060 * Returns 0 or -1 for no match,
16061 */
16062 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016063do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16064 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016065 char_u *spat; /* start pattern */
16066 char_u *mpat; /* middle pattern */
16067 char_u *epat; /* end pattern */
16068 int dir; /* BACKWARD or FORWARD */
16069 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016070 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016071 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016072 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016073 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016074{
16075 char_u *save_cpo;
16076 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16077 long retval = 0;
16078 pos_T pos;
16079 pos_T firstpos;
16080 pos_T foundpos;
16081 pos_T save_cursor;
16082 pos_T save_pos;
16083 int n;
16084 int r;
16085 int nest = 1;
16086 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016087 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016088 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016089
16090 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16091 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016092 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016093
Bram Moolenaar76929292008-01-06 19:07:36 +000016094#ifdef FEAT_RELTIME
16095 /* Set the time limit, if there is one. */
16096 profile_setlimit(time_limit, &tm);
16097#endif
16098
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016099 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16100 * start/middle/end (pat3, for the top pair). */
16101 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16102 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16103 if (pat2 == NULL || pat3 == NULL)
16104 goto theend;
16105 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16106 if (*mpat == NUL)
16107 STRCPY(pat3, pat2);
16108 else
16109 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16110 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016111 if (flags & SP_START)
16112 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016113
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 save_cursor = curwin->w_cursor;
16115 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016116 clearpos(&firstpos);
16117 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 pat = pat3;
16119 for (;;)
16120 {
16121 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016122 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16124 /* didn't find it or found the first match again: FAIL */
16125 break;
16126
16127 if (firstpos.lnum == 0)
16128 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016129 if (equalpos(pos, foundpos))
16130 {
16131 /* Found the same position again. Can happen with a pattern that
16132 * has "\zs" at the end and searching backwards. Advance one
16133 * character and try again. */
16134 if (dir == BACKWARD)
16135 decl(&pos);
16136 else
16137 incl(&pos);
16138 }
16139 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016141 /* clear the start flag to avoid getting stuck here */
16142 options &= ~SEARCH_START;
16143
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 /* If the skip pattern matches, ignore this match. */
16145 if (*skip != NUL)
16146 {
16147 save_pos = curwin->w_cursor;
16148 curwin->w_cursor = pos;
16149 r = eval_to_bool(skip, &err, NULL, FALSE);
16150 curwin->w_cursor = save_pos;
16151 if (err)
16152 {
16153 /* Evaluating {skip} caused an error, break here. */
16154 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016155 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 break;
16157 }
16158 if (r)
16159 continue;
16160 }
16161
16162 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16163 {
16164 /* Found end when searching backwards or start when searching
16165 * forward: nested pair. */
16166 ++nest;
16167 pat = pat2; /* nested, don't search for middle */
16168 }
16169 else
16170 {
16171 /* Found end when searching forward or start when searching
16172 * backward: end of (nested) pair; or found middle in outer pair. */
16173 if (--nest == 1)
16174 pat = pat3; /* outer level, search for middle */
16175 }
16176
16177 if (nest == 0)
16178 {
16179 /* Found the match: return matchcount or line number. */
16180 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016181 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016183 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016184 if (flags & SP_SETPCMARK)
16185 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 curwin->w_cursor = pos;
16187 if (!(flags & SP_REPEAT))
16188 break;
16189 nest = 1; /* search for next unmatched */
16190 }
16191 }
16192
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016193 if (match_pos != NULL)
16194 {
16195 /* Store the match cursor position */
16196 match_pos->lnum = curwin->w_cursor.lnum;
16197 match_pos->col = curwin->w_cursor.col + 1;
16198 }
16199
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016201 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 curwin->w_cursor = save_cursor;
16203
16204theend:
16205 vim_free(pat2);
16206 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016207 if (p_cpo == empty_option)
16208 p_cpo = save_cpo;
16209 else
16210 /* Darn, evaluating the {skip} expression changed the value. */
16211 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016212
16213 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214}
16215
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016216/*
16217 * "searchpos()" function
16218 */
16219 static void
16220f_searchpos(argvars, rettv)
16221 typval_T *argvars;
16222 typval_T *rettv;
16223{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016224 pos_T match_pos;
16225 int lnum = 0;
16226 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016227 int n;
16228 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016229
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016230 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016231 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016232
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016233 n = search_cmn(argvars, &match_pos, &flags);
16234 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016235 {
16236 lnum = match_pos.lnum;
16237 col = match_pos.col;
16238 }
16239
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016240 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16241 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016242 if (flags & SP_SUBPAT)
16243 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016244}
16245
16246
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 static void
16248f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252#ifdef FEAT_CLIENTSERVER
16253 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 char_u *server = get_tv_string_chk(&argvars[0]);
16255 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 if (server == NULL || reply == NULL)
16259 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 if (check_restricted() || check_secure())
16261 return;
16262# ifdef FEAT_X11
16263 if (check_connection() == FAIL)
16264 return;
16265# endif
16266
16267 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016269 EMSG(_("E258: Unable to send to client"));
16270 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016272 rettv->vval.v_number = 0;
16273#else
16274 rettv->vval.v_number = -1;
16275#endif
16276}
16277
Bram Moolenaar0d660222005-01-07 21:51:51 +000016278 static void
16279f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282{
16283 char_u *r = NULL;
16284
16285#ifdef FEAT_CLIENTSERVER
16286# ifdef WIN32
16287 r = serverGetVimNames();
16288# else
16289 make_connection();
16290 if (X_DISPLAY != NULL)
16291 r = serverGetVimNames(X_DISPLAY);
16292# endif
16293#endif
16294 rettv->v_type = VAR_STRING;
16295 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296}
16297
16298/*
16299 * "setbufvar()" function
16300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016302f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016303 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016304 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305{
16306 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 char_u nbuf[NUMBUFLEN];
16311
16312 if (check_restricted() || check_secure())
16313 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16315 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016316 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 varp = &argvars[2];
16318
16319 if (buf != NULL && varname != NULL && varp != NULL)
16320 {
16321 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323
16324 if (*varname == '&')
16325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016326 long numval;
16327 char_u *strval;
16328 int error = FALSE;
16329
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016331 numval = get_tv_number_chk(varp, &error);
16332 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 if (!error && strval != NULL)
16334 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 }
16336 else
16337 {
16338 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16339 if (bufvarname != NULL)
16340 {
16341 STRCPY(bufvarname, "b:");
16342 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016343 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 vim_free(bufvarname);
16345 }
16346 }
16347
16348 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
16353/*
16354 * "setcmdpos()" function
16355 */
16356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 typval_T *argvars;
16359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016361 int pos = (int)get_tv_number(&argvars[0]) - 1;
16362
16363 if (pos >= 0)
16364 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365}
16366
16367/*
16368 * "setline()" function
16369 */
16370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016371f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016372 typval_T *argvars;
16373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374{
16375 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016376 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016377 list_T *l = NULL;
16378 listitem_T *li = NULL;
16379 long added = 0;
16380 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016382 lnum = get_tv_lnum(&argvars[0]);
16383 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016385 l = argvars[1].vval.v_list;
16386 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016388 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016389 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016390
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016391 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016392 for (;;)
16393 {
16394 if (l != NULL)
16395 {
16396 /* list argument, get next string */
16397 if (li == NULL)
16398 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016400 li = li->li_next;
16401 }
16402
16403 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016405 break;
16406 if (lnum <= curbuf->b_ml.ml_line_count)
16407 {
16408 /* existing line, replace it */
16409 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16410 {
16411 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016412 if (lnum == curwin->w_cursor.lnum)
16413 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016414 rettv->vval.v_number = 0; /* OK */
16415 }
16416 }
16417 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16418 {
16419 /* lnum is one past the last line, append the line */
16420 ++added;
16421 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16422 rettv->vval.v_number = 0; /* OK */
16423 }
16424
16425 if (l == NULL) /* only one string argument */
16426 break;
16427 ++lnum;
16428 }
16429
16430 if (added > 0)
16431 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432}
16433
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016434static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16435
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016437 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016438 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016439 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016440set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016441 win_T *wp UNUSED;
16442 typval_T *list_arg UNUSED;
16443 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016444 typval_T *rettv;
16445{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016446#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016447 char_u *act;
16448 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016449#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016450
Bram Moolenaar2641f772005-03-25 21:58:17 +000016451 rettv->vval.v_number = -1;
16452
16453#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016454 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016455 EMSG(_(e_listreq));
16456 else
16457 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016458 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016459
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016460 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016461 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016462 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 if (act == NULL)
16464 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016465 if (*act == 'a' || *act == 'r')
16466 action = *act;
16467 }
16468
Bram Moolenaar81484f42012-12-05 15:16:47 +010016469 if (l != NULL && set_errorlist(wp, l, action,
16470 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016471 rettv->vval.v_number = 0;
16472 }
16473#endif
16474}
16475
16476/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016477 * "setloclist()" function
16478 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016479 static void
16480f_setloclist(argvars, rettv)
16481 typval_T *argvars;
16482 typval_T *rettv;
16483{
16484 win_T *win;
16485
16486 rettv->vval.v_number = -1;
16487
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016488 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016489 if (win != NULL)
16490 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16491}
16492
16493/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016494 * "setmatches()" function
16495 */
16496 static void
16497f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016498 typval_T *argvars UNUSED;
16499 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016500{
16501#ifdef FEAT_SEARCH_EXTRA
16502 list_T *l;
16503 listitem_T *li;
16504 dict_T *d;
16505
16506 rettv->vval.v_number = -1;
16507 if (argvars[0].v_type != VAR_LIST)
16508 {
16509 EMSG(_(e_listreq));
16510 return;
16511 }
16512 if ((l = argvars[0].vval.v_list) != NULL)
16513 {
16514
16515 /* To some extent make sure that we are dealing with a list from
16516 * "getmatches()". */
16517 li = l->lv_first;
16518 while (li != NULL)
16519 {
16520 if (li->li_tv.v_type != VAR_DICT
16521 || (d = li->li_tv.vval.v_dict) == NULL)
16522 {
16523 EMSG(_(e_invarg));
16524 return;
16525 }
16526 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16527 && dict_find(d, (char_u *)"pattern", -1) != NULL
16528 && dict_find(d, (char_u *)"priority", -1) != NULL
16529 && dict_find(d, (char_u *)"id", -1) != NULL))
16530 {
16531 EMSG(_(e_invarg));
16532 return;
16533 }
16534 li = li->li_next;
16535 }
16536
16537 clear_matches(curwin);
16538 li = l->lv_first;
16539 while (li != NULL)
16540 {
16541 d = li->li_tv.vval.v_dict;
16542 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16543 get_dict_string(d, (char_u *)"pattern", FALSE),
16544 (int)get_dict_number(d, (char_u *)"priority"),
16545 (int)get_dict_number(d, (char_u *)"id"));
16546 li = li->li_next;
16547 }
16548 rettv->vval.v_number = 0;
16549 }
16550#endif
16551}
16552
16553/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016554 * "setpos()" function
16555 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016556 static void
16557f_setpos(argvars, rettv)
16558 typval_T *argvars;
16559 typval_T *rettv;
16560{
16561 pos_T pos;
16562 int fnum;
16563 char_u *name;
16564
Bram Moolenaar08250432008-02-13 11:42:46 +000016565 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016566 name = get_tv_string_chk(argvars);
16567 if (name != NULL)
16568 {
16569 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16570 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016571 if (--pos.col < 0)
16572 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016573 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016574 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016575 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016576 if (fnum == curbuf->b_fnum)
16577 {
16578 curwin->w_cursor = pos;
16579 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016580 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016581 }
16582 else
16583 EMSG(_(e_invarg));
16584 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016585 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16586 {
16587 /* set mark */
16588 if (setmark_pos(name[1], &pos, fnum) == OK)
16589 rettv->vval.v_number = 0;
16590 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016591 else
16592 EMSG(_(e_invarg));
16593 }
16594 }
16595}
16596
16597/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016598 * "setqflist()" function
16599 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016600 static void
16601f_setqflist(argvars, rettv)
16602 typval_T *argvars;
16603 typval_T *rettv;
16604{
16605 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16606}
16607
16608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 * "setreg()" function
16610 */
16611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016612f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016613 typval_T *argvars;
16614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615{
16616 int regname;
16617 char_u *strregname;
16618 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016619 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 int append;
16621 char_u yank_type;
16622 long block_len;
16623
16624 block_len = -1;
16625 yank_type = MAUTO;
16626 append = FALSE;
16627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016631 if (strregname == NULL)
16632 return; /* type error; errmsg already given */
16633 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 if (regname == 0 || regname == '@')
16635 regname = '"';
16636 else if (regname == '=')
16637 return;
16638
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016639 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016641 stropt = get_tv_string_chk(&argvars[2]);
16642 if (stropt == NULL)
16643 return; /* type error */
16644 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 switch (*stropt)
16646 {
16647 case 'a': case 'A': /* append */
16648 append = TRUE;
16649 break;
16650 case 'v': case 'c': /* character-wise selection */
16651 yank_type = MCHAR;
16652 break;
16653 case 'V': case 'l': /* line-wise selection */
16654 yank_type = MLINE;
16655 break;
16656#ifdef FEAT_VISUAL
16657 case 'b': case Ctrl_V: /* block-wise selection */
16658 yank_type = MBLOCK;
16659 if (VIM_ISDIGIT(stropt[1]))
16660 {
16661 ++stropt;
16662 block_len = getdigits(&stropt) - 1;
16663 --stropt;
16664 }
16665 break;
16666#endif
16667 }
16668 }
16669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016670 strval = get_tv_string_chk(&argvars[1]);
16671 if (strval != NULL)
16672 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016674 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675}
16676
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016677/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016678 * "settabvar()" function
16679 */
16680 static void
16681f_settabvar(argvars, rettv)
16682 typval_T *argvars;
16683 typval_T *rettv;
16684{
16685 tabpage_T *save_curtab;
16686 char_u *varname, *tabvarname;
16687 typval_T *varp;
16688 tabpage_T *tp;
16689
16690 rettv->vval.v_number = 0;
16691
16692 if (check_restricted() || check_secure())
16693 return;
16694
16695 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16696 varname = get_tv_string_chk(&argvars[1]);
16697 varp = &argvars[2];
16698
16699 if (tp != NULL && varname != NULL && varp != NULL)
16700 {
16701 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016702 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016703
16704 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16705 if (tabvarname != NULL)
16706 {
16707 STRCPY(tabvarname, "t:");
16708 STRCPY(tabvarname + 2, varname);
16709 set_var(tabvarname, varp, TRUE);
16710 vim_free(tabvarname);
16711 }
16712
16713 /* Restore current tabpage */
16714 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016715 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016716 }
16717}
16718
16719/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016720 * "settabwinvar()" function
16721 */
16722 static void
16723f_settabwinvar(argvars, rettv)
16724 typval_T *argvars;
16725 typval_T *rettv;
16726{
16727 setwinvar(argvars, rettv, 1);
16728}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729
16730/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016731 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016734f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016735 typval_T *argvars;
16736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016738 setwinvar(argvars, rettv, 0);
16739}
16740
16741/*
16742 * "setwinvar()" and "settabwinvar()" functions
16743 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016744
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016745 static void
16746setwinvar(argvars, rettv, off)
16747 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016748 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016749 int off;
16750{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 win_T *win;
16752#ifdef FEAT_WINDOWS
16753 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016754 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755#endif
16756 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016757 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016759 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760
16761 if (check_restricted() || check_secure())
16762 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016763
16764#ifdef FEAT_WINDOWS
16765 if (off == 1)
16766 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16767 else
16768 tp = curtab;
16769#endif
16770 win = find_win_by_nr(&argvars[off], tp);
16771 varname = get_tv_string_chk(&argvars[off + 1]);
16772 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773
16774 if (win != NULL && varname != NULL && varp != NULL)
16775 {
16776#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016777 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779#endif
16780
16781 if (*varname == '&')
16782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016783 long numval;
16784 char_u *strval;
16785 int error = FALSE;
16786
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016788 numval = get_tv_number_chk(varp, &error);
16789 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016790 if (!error && strval != NULL)
16791 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 }
16793 else
16794 {
16795 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16796 if (winvarname != NULL)
16797 {
16798 STRCPY(winvarname, "w:");
16799 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016800 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 vim_free(winvarname);
16802 }
16803 }
16804
16805#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016806 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807#endif
16808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809}
16810
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016811#ifdef FEAT_CRYPT
16812/*
16813 * "sha256({string})" function
16814 */
16815 static void
16816f_sha256(argvars, rettv)
16817 typval_T *argvars;
16818 typval_T *rettv;
16819{
16820 char_u *p;
16821
16822 p = get_tv_string(&argvars[0]);
16823 rettv->vval.v_string = vim_strsave(
16824 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16825 rettv->v_type = VAR_STRING;
16826}
16827#endif /* FEAT_CRYPT */
16828
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016830 * "shellescape({string})" function
16831 */
16832 static void
16833f_shellescape(argvars, rettv)
16834 typval_T *argvars;
16835 typval_T *rettv;
16836{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016837 rettv->vval.v_string = vim_strsave_shellescape(
16838 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016839 rettv->v_type = VAR_STRING;
16840}
16841
16842/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016843 * shiftwidth() function
16844 */
16845 static void
16846f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016847 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016848 typval_T *rettv;
16849{
16850 rettv->vval.v_number = get_sw_value();
16851}
16852
16853/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016854 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 */
16856 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 typval_T *argvars;
16859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 p = get_tv_string(&argvars[0]);
16864 rettv->vval.v_string = vim_strsave(p);
16865 simplify_filename(rettv->vval.v_string); /* simplify in place */
16866 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867}
16868
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016869#ifdef FEAT_FLOAT
16870/*
16871 * "sin()" function
16872 */
16873 static void
16874f_sin(argvars, rettv)
16875 typval_T *argvars;
16876 typval_T *rettv;
16877{
16878 float_T f;
16879
16880 rettv->v_type = VAR_FLOAT;
16881 if (get_float_arg(argvars, &f) == OK)
16882 rettv->vval.v_float = sin(f);
16883 else
16884 rettv->vval.v_float = 0.0;
16885}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016886
16887/*
16888 * "sinh()" function
16889 */
16890 static void
16891f_sinh(argvars, rettv)
16892 typval_T *argvars;
16893 typval_T *rettv;
16894{
16895 float_T f;
16896
16897 rettv->v_type = VAR_FLOAT;
16898 if (get_float_arg(argvars, &f) == OK)
16899 rettv->vval.v_float = sinh(f);
16900 else
16901 rettv->vval.v_float = 0.0;
16902}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016903#endif
16904
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905static int
16906#ifdef __BORLANDC__
16907 _RTLENTRYF
16908#endif
16909 item_compare __ARGS((const void *s1, const void *s2));
16910static int
16911#ifdef __BORLANDC__
16912 _RTLENTRYF
16913#endif
16914 item_compare2 __ARGS((const void *s1, const void *s2));
16915
16916static int item_compare_ic;
16917static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016918static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016919static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920#define ITEM_COMPARE_FAIL 999
16921
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 static int
16926#ifdef __BORLANDC__
16927_RTLENTRYF
16928#endif
16929item_compare(s1, s2)
16930 const void *s1;
16931 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 char_u *p1, *p2;
16934 char_u *tofree1, *tofree2;
16935 int res;
16936 char_u numbuf1[NUMBUFLEN];
16937 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016939 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16940 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016941 if (p1 == NULL)
16942 p1 = (char_u *)"";
16943 if (p2 == NULL)
16944 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 if (item_compare_ic)
16946 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 res = STRCMP(p1, p2);
16949 vim_free(tofree1);
16950 vim_free(tofree2);
16951 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952}
16953
16954 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955#ifdef __BORLANDC__
16956_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958item_compare2(s1, s2)
16959 const void *s1;
16960 const void *s2;
16961{
16962 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016963 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016964 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016967 /* shortcut after failure in previous call; compare all items equal */
16968 if (item_compare_func_err)
16969 return 0;
16970
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016971 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16972 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16974 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975
16976 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016977 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016978 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16979 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 clear_tv(&argv[0]);
16981 clear_tv(&argv[1]);
16982
16983 if (res == FAIL)
16984 res = ITEM_COMPARE_FAIL;
16985 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16987 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016988 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 clear_tv(&rettv);
16990 return res;
16991}
16992
16993/*
16994 * "sort({list})" function
16995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 typval_T *argvars;
16999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000{
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 list_T *l;
17002 listitem_T *li;
17003 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 long len;
17005 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007 if (argvars[0].v_type != VAR_LIST)
17008 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 else
17010 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017012 if (l == NULL || tv_check_lock(l->lv_lock,
17013 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 return;
17015 rettv->vval.v_list = l;
17016 rettv->v_type = VAR_LIST;
17017 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 len = list_len(l);
17020 if (len <= 1)
17021 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023 item_compare_ic = FALSE;
17024 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017025 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 if (argvars[1].v_type != VAR_UNKNOWN)
17027 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017028 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017030 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 else
17032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 int error = FALSE;
17034
17035 i = get_tv_number_chk(&argvars[1], &error);
17036 if (error)
17037 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 if (i == 1)
17039 item_compare_ic = TRUE;
17040 else
17041 item_compare_func = get_tv_string(&argvars[1]);
17042 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017043
17044 if (argvars[2].v_type != VAR_UNKNOWN)
17045 {
17046 /* optional third argument: {dict} */
17047 if (argvars[2].v_type != VAR_DICT)
17048 {
17049 EMSG(_(e_dictreq));
17050 return;
17051 }
17052 item_compare_selfdict = argvars[2].vval.v_dict;
17053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017057 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 if (ptrs == NULL)
17059 return;
17060 i = 0;
17061 for (li = l->lv_first; li != NULL; li = li->li_next)
17062 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065 /* test the compare function */
17066 if (item_compare_func != NULL
17067 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17068 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017069 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071 {
17072 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 item_compare_func == NULL ? item_compare : item_compare2);
17075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017076 if (!item_compare_func_err)
17077 {
17078 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017079 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 l->lv_len = 0;
17081 for (i = 0; i < len; ++i)
17082 list_append(l, ptrs[i]);
17083 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084 }
17085
17086 vim_free(ptrs);
17087 }
17088}
17089
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017090/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017091 * "soundfold({word})" function
17092 */
17093 static void
17094f_soundfold(argvars, rettv)
17095 typval_T *argvars;
17096 typval_T *rettv;
17097{
17098 char_u *s;
17099
17100 rettv->v_type = VAR_STRING;
17101 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017102#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017103 rettv->vval.v_string = eval_soundfold(s);
17104#else
17105 rettv->vval.v_string = vim_strsave(s);
17106#endif
17107}
17108
17109/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017110 * "spellbadword()" function
17111 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017112 static void
17113f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017114 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017115 typval_T *rettv;
17116{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017117 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017118 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017119 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017120
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017121 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017122 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017123
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017124#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017125 if (argvars[0].v_type == VAR_UNKNOWN)
17126 {
17127 /* Find the start and length of the badly spelled word. */
17128 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17129 if (len != 0)
17130 word = ml_get_cursor();
17131 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017132 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017133 {
17134 char_u *str = get_tv_string_chk(&argvars[0]);
17135 int capcol = -1;
17136
17137 if (str != NULL)
17138 {
17139 /* Check the argument for spelling. */
17140 while (*str != NUL)
17141 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017142 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017143 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017144 {
17145 word = str;
17146 break;
17147 }
17148 str += len;
17149 }
17150 }
17151 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017152#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017153
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017154 list_append_string(rettv->vval.v_list, word, len);
17155 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017156 attr == HLF_SPB ? "bad" :
17157 attr == HLF_SPR ? "rare" :
17158 attr == HLF_SPL ? "local" :
17159 attr == HLF_SPC ? "caps" :
17160 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017161}
17162
17163/*
17164 * "spellsuggest()" function
17165 */
17166 static void
17167f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017168 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017169 typval_T *rettv;
17170{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017171#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017172 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017173 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017174 int maxcount;
17175 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017176 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017177 listitem_T *li;
17178 int need_capital = FALSE;
17179#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017180
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017181 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017182 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017183
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017184#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017185 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017186 {
17187 str = get_tv_string(&argvars[0]);
17188 if (argvars[1].v_type != VAR_UNKNOWN)
17189 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017190 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017191 if (maxcount <= 0)
17192 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017193 if (argvars[2].v_type != VAR_UNKNOWN)
17194 {
17195 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17196 if (typeerr)
17197 return;
17198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017199 }
17200 else
17201 maxcount = 25;
17202
Bram Moolenaar4770d092006-01-12 23:22:24 +000017203 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017204
17205 for (i = 0; i < ga.ga_len; ++i)
17206 {
17207 str = ((char_u **)ga.ga_data)[i];
17208
17209 li = listitem_alloc();
17210 if (li == NULL)
17211 vim_free(str);
17212 else
17213 {
17214 li->li_tv.v_type = VAR_STRING;
17215 li->li_tv.v_lock = 0;
17216 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017217 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017218 }
17219 }
17220 ga_clear(&ga);
17221 }
17222#endif
17223}
17224
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017226f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 typval_T *argvars;
17228 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229{
17230 char_u *str;
17231 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017232 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233 regmatch_T regmatch;
17234 char_u patbuf[NUMBUFLEN];
17235 char_u *save_cpo;
17236 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017238 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017239 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017240
17241 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17242 save_cpo = p_cpo;
17243 p_cpo = (char_u *)"";
17244
17245 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017246 if (argvars[1].v_type != VAR_UNKNOWN)
17247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017248 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17249 if (pat == NULL)
17250 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017253 }
17254 if (pat == NULL || *pat == NUL)
17255 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017257 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017259 if (typeerr)
17260 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17263 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017265 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017266 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017267 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017268 if (*str == NUL)
17269 match = FALSE; /* empty item at the end */
17270 else
17271 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017272 if (match)
17273 end = regmatch.startp[0];
17274 else
17275 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017276 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17277 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017279 if (list_append_string(rettv->vval.v_list, str,
17280 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017281 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017282 }
17283 if (!match)
17284 break;
17285 /* Advance to just after the match. */
17286 if (regmatch.endp[0] > str)
17287 col = 0;
17288 else
17289 {
17290 /* Don't get stuck at the same match. */
17291#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017292 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293#else
17294 col = 1;
17295#endif
17296 }
17297 str = regmatch.endp[0];
17298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299
Bram Moolenaar473de612013-06-08 18:19:48 +020017300 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302
Bram Moolenaar0d660222005-01-07 21:51:51 +000017303 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304}
17305
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017306#ifdef FEAT_FLOAT
17307/*
17308 * "sqrt()" function
17309 */
17310 static void
17311f_sqrt(argvars, rettv)
17312 typval_T *argvars;
17313 typval_T *rettv;
17314{
17315 float_T f;
17316
17317 rettv->v_type = VAR_FLOAT;
17318 if (get_float_arg(argvars, &f) == OK)
17319 rettv->vval.v_float = sqrt(f);
17320 else
17321 rettv->vval.v_float = 0.0;
17322}
17323
17324/*
17325 * "str2float()" function
17326 */
17327 static void
17328f_str2float(argvars, rettv)
17329 typval_T *argvars;
17330 typval_T *rettv;
17331{
17332 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17333
17334 if (*p == '+')
17335 p = skipwhite(p + 1);
17336 (void)string2float(p, &rettv->vval.v_float);
17337 rettv->v_type = VAR_FLOAT;
17338}
17339#endif
17340
Bram Moolenaar2c932302006-03-18 21:42:09 +000017341/*
17342 * "str2nr()" function
17343 */
17344 static void
17345f_str2nr(argvars, rettv)
17346 typval_T *argvars;
17347 typval_T *rettv;
17348{
17349 int base = 10;
17350 char_u *p;
17351 long n;
17352
17353 if (argvars[1].v_type != VAR_UNKNOWN)
17354 {
17355 base = get_tv_number(&argvars[1]);
17356 if (base != 8 && base != 10 && base != 16)
17357 {
17358 EMSG(_(e_invarg));
17359 return;
17360 }
17361 }
17362
17363 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017364 if (*p == '+')
17365 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017366 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17367 rettv->vval.v_number = n;
17368}
17369
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370#ifdef HAVE_STRFTIME
17371/*
17372 * "strftime({format}[, {time}])" function
17373 */
17374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 typval_T *argvars;
17377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
17379 char_u result_buf[256];
17380 struct tm *curtime;
17381 time_t seconds;
17382 char_u *p;
17383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017384 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017386 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017387 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 seconds = time(NULL);
17389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 curtime = localtime(&seconds);
17392 /* MSVC returns NULL for an invalid value of seconds. */
17393 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 else
17396 {
17397# ifdef FEAT_MBYTE
17398 vimconv_T conv;
17399 char_u *enc;
17400
17401 conv.vc_type = CONV_NONE;
17402 enc = enc_locale();
17403 convert_setup(&conv, p_enc, enc);
17404 if (conv.vc_type != CONV_NONE)
17405 p = string_convert(&conv, p, NULL);
17406# endif
17407 if (p != NULL)
17408 (void)strftime((char *)result_buf, sizeof(result_buf),
17409 (char *)p, curtime);
17410 else
17411 result_buf[0] = NUL;
17412
17413# ifdef FEAT_MBYTE
17414 if (conv.vc_type != CONV_NONE)
17415 vim_free(p);
17416 convert_setup(&conv, enc, p_enc);
17417 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 else
17420# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422
17423# ifdef FEAT_MBYTE
17424 /* Release conversion descriptors */
17425 convert_setup(&conv, NULL, NULL);
17426 vim_free(enc);
17427# endif
17428 }
17429}
17430#endif
17431
17432/*
17433 * "stridx()" function
17434 */
17435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 typval_T *argvars;
17438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439{
17440 char_u buf[NUMBUFLEN];
17441 char_u *needle;
17442 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017443 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017445 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017447 needle = get_tv_string_chk(&argvars[1]);
17448 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017449 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017450 if (needle == NULL || haystack == NULL)
17451 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452
Bram Moolenaar33570922005-01-25 22:26:29 +000017453 if (argvars[2].v_type != VAR_UNKNOWN)
17454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017455 int error = FALSE;
17456
17457 start_idx = get_tv_number_chk(&argvars[2], &error);
17458 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017460 if (start_idx >= 0)
17461 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 }
17463
17464 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17465 if (pos != NULL)
17466 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467}
17468
17469/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017470 * "string()" function
17471 */
17472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 typval_T *argvars;
17475 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017476{
17477 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017478 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017480 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017481 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017482 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017483 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485}
17486
17487/*
17488 * "strlen()" function
17489 */
17490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 typval_T *argvars;
17493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 rettv->vval.v_number = (varnumber_T)(STRLEN(
17496 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497}
17498
17499/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017500 * "strchars()" function
17501 */
17502 static void
17503f_strchars(argvars, rettv)
17504 typval_T *argvars;
17505 typval_T *rettv;
17506{
17507 char_u *s = get_tv_string(&argvars[0]);
17508#ifdef FEAT_MBYTE
17509 varnumber_T len = 0;
17510
17511 while (*s != NUL)
17512 {
17513 mb_cptr2char_adv(&s);
17514 ++len;
17515 }
17516 rettv->vval.v_number = len;
17517#else
17518 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17519#endif
17520}
17521
17522/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017523 * "strdisplaywidth()" function
17524 */
17525 static void
17526f_strdisplaywidth(argvars, rettv)
17527 typval_T *argvars;
17528 typval_T *rettv;
17529{
17530 char_u *s = get_tv_string(&argvars[0]);
17531 int col = 0;
17532
17533 if (argvars[1].v_type != VAR_UNKNOWN)
17534 col = get_tv_number(&argvars[1]);
17535
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017536 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017537}
17538
17539/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017540 * "strwidth()" function
17541 */
17542 static void
17543f_strwidth(argvars, rettv)
17544 typval_T *argvars;
17545 typval_T *rettv;
17546{
17547 char_u *s = get_tv_string(&argvars[0]);
17548
17549 rettv->vval.v_number = (varnumber_T)(
17550#ifdef FEAT_MBYTE
17551 mb_string2cells(s, -1)
17552#else
17553 STRLEN(s)
17554#endif
17555 );
17556}
17557
17558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 * "strpart()" function
17560 */
17561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017562f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017563 typval_T *argvars;
17564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565{
17566 char_u *p;
17567 int n;
17568 int len;
17569 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017570 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 slen = (int)STRLEN(p);
17574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017575 n = get_tv_number_chk(&argvars[1], &error);
17576 if (error)
17577 len = 0;
17578 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 else
17581 len = slen - n; /* default len: all bytes that are available. */
17582
17583 /*
17584 * Only return the overlap between the specified part and the actual
17585 * string.
17586 */
17587 if (n < 0)
17588 {
17589 len += n;
17590 n = 0;
17591 }
17592 else if (n > slen)
17593 n = slen;
17594 if (len < 0)
17595 len = 0;
17596 else if (n + len > slen)
17597 len = slen - n;
17598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 rettv->v_type = VAR_STRING;
17600 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601}
17602
17603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017604 * "strridx()" function
17605 */
17606 static void
17607f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *argvars;
17609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017610{
17611 char_u buf[NUMBUFLEN];
17612 char_u *needle;
17613 char_u *haystack;
17614 char_u *rest;
17615 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017616 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017618 needle = get_tv_string_chk(&argvars[1]);
17619 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017620
17621 rettv->vval.v_number = -1;
17622 if (needle == NULL || haystack == NULL)
17623 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017624
17625 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017626 if (argvars[2].v_type != VAR_UNKNOWN)
17627 {
17628 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017629 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017630 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017631 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017632 }
17633 else
17634 end_idx = haystack_len;
17635
Bram Moolenaar0d660222005-01-07 21:51:51 +000017636 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017637 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017638 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017639 lastmatch = haystack + end_idx;
17640 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017641 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017642 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017643 for (rest = haystack; *rest != '\0'; ++rest)
17644 {
17645 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017646 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017647 break;
17648 lastmatch = rest;
17649 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017650 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017651
17652 if (lastmatch == NULL)
17653 rettv->vval.v_number = -1;
17654 else
17655 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17656}
17657
17658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 * "strtrans()" function
17660 */
17661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017663 typval_T *argvars;
17664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666 rettv->v_type = VAR_STRING;
17667 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668}
17669
17670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017671 * "submatch()" function
17672 */
17673 static void
17674f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017675 typval_T *argvars;
17676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017677{
17678 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 rettv->vval.v_string =
17680 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017681}
17682
17683/*
17684 * "substitute()" function
17685 */
17686 static void
17687f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017688 typval_T *argvars;
17689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017690{
17691 char_u patbuf[NUMBUFLEN];
17692 char_u subbuf[NUMBUFLEN];
17693 char_u flagsbuf[NUMBUFLEN];
17694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017695 char_u *str = get_tv_string_chk(&argvars[0]);
17696 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17697 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17698 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17699
Bram Moolenaar0d660222005-01-07 21:51:51 +000017700 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017701 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17702 rettv->vval.v_string = NULL;
17703 else
17704 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017705}
17706
17707/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017708 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714{
17715 int id = 0;
17716#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017717 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 long col;
17719 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017720 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017722 lnum = get_tv_lnum(argvars); /* -1 on type error */
17723 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17724 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017726 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017727 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017728 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729#endif
17730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017731 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732}
17733
17734/*
17735 * "synIDattr(id, what [, mode])" function
17736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017739 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741{
17742 char_u *p = NULL;
17743#ifdef FEAT_SYN_HL
17744 int id;
17745 char_u *what;
17746 char_u *mode;
17747 char_u modebuf[NUMBUFLEN];
17748 int modec;
17749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750 id = get_tv_number(&argvars[0]);
17751 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017752 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017756 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 modec = 0; /* replace invalid with current */
17758 }
17759 else
17760 {
17761#ifdef FEAT_GUI
17762 if (gui.in_use)
17763 modec = 'g';
17764 else
17765#endif
17766 if (t_colors > 1)
17767 modec = 'c';
17768 else
17769 modec = 't';
17770 }
17771
17772
17773 switch (TOLOWER_ASC(what[0]))
17774 {
17775 case 'b':
17776 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17777 p = highlight_color(id, what, modec);
17778 else /* bold */
17779 p = highlight_has_attr(id, HL_BOLD, modec);
17780 break;
17781
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017782 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 p = highlight_color(id, what, modec);
17784 break;
17785
17786 case 'i':
17787 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17788 p = highlight_has_attr(id, HL_INVERSE, modec);
17789 else /* italic */
17790 p = highlight_has_attr(id, HL_ITALIC, modec);
17791 break;
17792
17793 case 'n': /* name */
17794 p = get_highlight_name(NULL, id - 1);
17795 break;
17796
17797 case 'r': /* reverse */
17798 p = highlight_has_attr(id, HL_INVERSE, modec);
17799 break;
17800
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017801 case 's':
17802 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17803 p = highlight_color(id, what, modec);
17804 else /* standout */
17805 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 break;
17807
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017808 case 'u':
17809 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17810 /* underline */
17811 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17812 else
17813 /* undercurl */
17814 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 break;
17816 }
17817
17818 if (p != NULL)
17819 p = vim_strsave(p);
17820#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017821 rettv->v_type = VAR_STRING;
17822 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823}
17824
17825/*
17826 * "synIDtrans(id)" function
17827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017829f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832{
17833 int id;
17834
17835#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837
17838 if (id > 0)
17839 id = syn_get_final_id(id);
17840 else
17841#endif
17842 id = 0;
17843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017844 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845}
17846
17847/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017848 * "synconcealed(lnum, col)" function
17849 */
17850 static void
17851f_synconcealed(argvars, rettv)
17852 typval_T *argvars UNUSED;
17853 typval_T *rettv;
17854{
17855#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17856 long lnum;
17857 long col;
17858 int syntax_flags = 0;
17859 int cchar;
17860 int matchid = 0;
17861 char_u str[NUMBUFLEN];
17862#endif
17863
17864 rettv->v_type = VAR_LIST;
17865 rettv->vval.v_list = NULL;
17866
17867#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17868 lnum = get_tv_lnum(argvars); /* -1 on type error */
17869 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17870
17871 vim_memset(str, NUL, sizeof(str));
17872
17873 if (rettv_list_alloc(rettv) != FAIL)
17874 {
17875 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17876 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17877 && curwin->w_p_cole > 0)
17878 {
17879 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17880 syntax_flags = get_syntax_info(&matchid);
17881
17882 /* get the conceal character */
17883 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17884 {
17885 cchar = syn_get_sub_char();
17886 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17887 cchar = lcs_conceal;
17888 if (cchar != NUL)
17889 {
17890# ifdef FEAT_MBYTE
17891 if (has_mbyte)
17892 (*mb_char2bytes)(cchar, str);
17893 else
17894# endif
17895 str[0] = cchar;
17896 }
17897 }
17898 }
17899
17900 list_append_number(rettv->vval.v_list,
17901 (syntax_flags & HL_CONCEAL) != 0);
17902 /* -1 to auto-determine strlen */
17903 list_append_string(rettv->vval.v_list, str, -1);
17904 list_append_number(rettv->vval.v_list, matchid);
17905 }
17906#endif
17907}
17908
17909/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017910 * "synstack(lnum, col)" function
17911 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017912 static void
17913f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017914 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017915 typval_T *rettv;
17916{
17917#ifdef FEAT_SYN_HL
17918 long lnum;
17919 long col;
17920 int i;
17921 int id;
17922#endif
17923
17924 rettv->v_type = VAR_LIST;
17925 rettv->vval.v_list = NULL;
17926
17927#ifdef FEAT_SYN_HL
17928 lnum = get_tv_lnum(argvars); /* -1 on type error */
17929 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17930
17931 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017932 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017933 && rettv_list_alloc(rettv) != FAIL)
17934 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017935 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017936 for (i = 0; ; ++i)
17937 {
17938 id = syn_get_stack_item(i);
17939 if (id < 0)
17940 break;
17941 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17942 break;
17943 }
17944 }
17945#endif
17946}
17947
17948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 * "system()" function
17950 */
17951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017952f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017953 typval_T *argvars;
17954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017956 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017958 char_u *infile = NULL;
17959 char_u buf[NUMBUFLEN];
17960 int err = FALSE;
17961 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017963 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017964 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017965
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017966 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017967 {
17968 /*
17969 * Write the string to a temp file, to be used for input of the shell
17970 * command.
17971 */
17972 if ((infile = vim_tempname('i')) == NULL)
17973 {
17974 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017975 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017976 }
17977
17978 fd = mch_fopen((char *)infile, WRITEBIN);
17979 if (fd == NULL)
17980 {
17981 EMSG2(_(e_notopen), infile);
17982 goto done;
17983 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017984 p = get_tv_string_buf_chk(&argvars[1], buf);
17985 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017986 {
17987 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017988 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017989 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017990 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17991 err = TRUE;
17992 if (fclose(fd) != 0)
17993 err = TRUE;
17994 if (err)
17995 {
17996 EMSG(_("E677: Error writing temp file"));
17997 goto done;
17998 }
17999 }
18000
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018001 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18002 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018003
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004#ifdef USE_CR
18005 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018006 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 {
18008 char_u *s;
18009
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018010 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 {
18012 if (*s == CAR)
18013 *s = NL;
18014 }
18015 }
18016#else
18017# ifdef USE_CRNL
18018 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018019 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 {
18021 char_u *s, *d;
18022
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018023 d = res;
18024 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 {
18026 if (s[0] == CAR && s[1] == NL)
18027 ++s;
18028 *d++ = *s;
18029 }
18030 *d = NUL;
18031 }
18032# endif
18033#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018034
18035done:
18036 if (infile != NULL)
18037 {
18038 mch_remove(infile);
18039 vim_free(infile);
18040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018041 rettv->v_type = VAR_STRING;
18042 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043}
18044
18045/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018046 * "tabpagebuflist()" function
18047 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018048 static void
18049f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018050 typval_T *argvars UNUSED;
18051 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018052{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018053#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018054 tabpage_T *tp;
18055 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018056
18057 if (argvars[0].v_type == VAR_UNKNOWN)
18058 wp = firstwin;
18059 else
18060 {
18061 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18062 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018063 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018064 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018065 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018066 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018067 for (; wp != NULL; wp = wp->w_next)
18068 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018069 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018070 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018071 }
18072#endif
18073}
18074
18075
18076/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018077 * "tabpagenr()" function
18078 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018079 static void
18080f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018081 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018082 typval_T *rettv;
18083{
18084 int nr = 1;
18085#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018086 char_u *arg;
18087
18088 if (argvars[0].v_type != VAR_UNKNOWN)
18089 {
18090 arg = get_tv_string_chk(&argvars[0]);
18091 nr = 0;
18092 if (arg != NULL)
18093 {
18094 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018095 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018096 else
18097 EMSG2(_(e_invexpr2), arg);
18098 }
18099 }
18100 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018101 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018102#endif
18103 rettv->vval.v_number = nr;
18104}
18105
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018106
18107#ifdef FEAT_WINDOWS
18108static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18109
18110/*
18111 * Common code for tabpagewinnr() and winnr().
18112 */
18113 static int
18114get_winnr(tp, argvar)
18115 tabpage_T *tp;
18116 typval_T *argvar;
18117{
18118 win_T *twin;
18119 int nr = 1;
18120 win_T *wp;
18121 char_u *arg;
18122
18123 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18124 if (argvar->v_type != VAR_UNKNOWN)
18125 {
18126 arg = get_tv_string_chk(argvar);
18127 if (arg == NULL)
18128 nr = 0; /* type error; errmsg already given */
18129 else if (STRCMP(arg, "$") == 0)
18130 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18131 else if (STRCMP(arg, "#") == 0)
18132 {
18133 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18134 if (twin == NULL)
18135 nr = 0;
18136 }
18137 else
18138 {
18139 EMSG2(_(e_invexpr2), arg);
18140 nr = 0;
18141 }
18142 }
18143
18144 if (nr > 0)
18145 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18146 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018147 {
18148 if (wp == NULL)
18149 {
18150 /* didn't find it in this tabpage */
18151 nr = 0;
18152 break;
18153 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018154 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018155 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018156 return nr;
18157}
18158#endif
18159
18160/*
18161 * "tabpagewinnr()" function
18162 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018163 static void
18164f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018165 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018166 typval_T *rettv;
18167{
18168 int nr = 1;
18169#ifdef FEAT_WINDOWS
18170 tabpage_T *tp;
18171
18172 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18173 if (tp == NULL)
18174 nr = 0;
18175 else
18176 nr = get_winnr(tp, &argvars[1]);
18177#endif
18178 rettv->vval.v_number = nr;
18179}
18180
18181
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018182/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018183 * "tagfiles()" function
18184 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018185 static void
18186f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018187 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018188 typval_T *rettv;
18189{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018190 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018191 tagname_T tn;
18192 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018193
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018194 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018195 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018196 fname = alloc(MAXPATHL);
18197 if (fname == NULL)
18198 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018199
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018200 for (first = TRUE; ; first = FALSE)
18201 if (get_tagfname(&tn, first, fname) == FAIL
18202 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018203 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018204 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018205 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018206}
18207
18208/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018209 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018210 */
18211 static void
18212f_taglist(argvars, rettv)
18213 typval_T *argvars;
18214 typval_T *rettv;
18215{
18216 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018217
18218 tag_pattern = get_tv_string(&argvars[0]);
18219
18220 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018221 if (*tag_pattern == NUL)
18222 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018224 if (rettv_list_alloc(rettv) == OK)
18225 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018226}
18227
18228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 * "tempname()" function
18230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018233 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235{
18236 static int x = 'A';
18237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 rettv->v_type = VAR_STRING;
18239 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240
18241 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18242 * names. Skip 'I' and 'O', they are used for shell redirection. */
18243 do
18244 {
18245 if (x == 'Z')
18246 x = '0';
18247 else if (x == '9')
18248 x = 'A';
18249 else
18250 {
18251#ifdef EBCDIC
18252 if (x == 'I')
18253 x = 'J';
18254 else if (x == 'R')
18255 x = 'S';
18256 else
18257#endif
18258 ++x;
18259 }
18260 } while (x == 'I' || x == 'O');
18261}
18262
18263/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018264 * "test(list)" function: Just checking the walls...
18265 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018266 static void
18267f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018268 typval_T *argvars UNUSED;
18269 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018270{
18271 /* Used for unit testing. Change the code below to your liking. */
18272#if 0
18273 listitem_T *li;
18274 list_T *l;
18275 char_u *bad, *good;
18276
18277 if (argvars[0].v_type != VAR_LIST)
18278 return;
18279 l = argvars[0].vval.v_list;
18280 if (l == NULL)
18281 return;
18282 li = l->lv_first;
18283 if (li == NULL)
18284 return;
18285 bad = get_tv_string(&li->li_tv);
18286 li = li->li_next;
18287 if (li == NULL)
18288 return;
18289 good = get_tv_string(&li->li_tv);
18290 rettv->vval.v_number = test_edit_score(bad, good);
18291#endif
18292}
18293
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018294#ifdef FEAT_FLOAT
18295/*
18296 * "tan()" function
18297 */
18298 static void
18299f_tan(argvars, rettv)
18300 typval_T *argvars;
18301 typval_T *rettv;
18302{
18303 float_T f;
18304
18305 rettv->v_type = VAR_FLOAT;
18306 if (get_float_arg(argvars, &f) == OK)
18307 rettv->vval.v_float = tan(f);
18308 else
18309 rettv->vval.v_float = 0.0;
18310}
18311
18312/*
18313 * "tanh()" function
18314 */
18315 static void
18316f_tanh(argvars, rettv)
18317 typval_T *argvars;
18318 typval_T *rettv;
18319{
18320 float_T f;
18321
18322 rettv->v_type = VAR_FLOAT;
18323 if (get_float_arg(argvars, &f) == OK)
18324 rettv->vval.v_float = tanh(f);
18325 else
18326 rettv->vval.v_float = 0.0;
18327}
18328#endif
18329
Bram Moolenaard52d9742005-08-21 22:20:28 +000018330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 * "tolower(string)" function
18332 */
18333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018335 typval_T *argvars;
18336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337{
18338 char_u *p;
18339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 p = vim_strsave(get_tv_string(&argvars[0]));
18341 rettv->v_type = VAR_STRING;
18342 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343
18344 if (p != NULL)
18345 while (*p != NUL)
18346 {
18347#ifdef FEAT_MBYTE
18348 int l;
18349
18350 if (enc_utf8)
18351 {
18352 int c, lc;
18353
18354 c = utf_ptr2char(p);
18355 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018356 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 /* TODO: reallocate string when byte count changes. */
18358 if (utf_char2len(lc) == l)
18359 utf_char2bytes(lc, p);
18360 p += l;
18361 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018362 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 p += l; /* skip multi-byte character */
18364 else
18365#endif
18366 {
18367 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18368 ++p;
18369 }
18370 }
18371}
18372
18373/*
18374 * "toupper(string)" function
18375 */
18376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018377f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018378 typval_T *argvars;
18379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018382 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383}
18384
18385/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018386 * "tr(string, fromstr, tostr)" function
18387 */
18388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018389f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018390 typval_T *argvars;
18391 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018392{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018393 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018394 char_u *fromstr;
18395 char_u *tostr;
18396 char_u *p;
18397#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018398 int inlen;
18399 int fromlen;
18400 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018401 int idx;
18402 char_u *cpstr;
18403 int cplen;
18404 int first = TRUE;
18405#endif
18406 char_u buf[NUMBUFLEN];
18407 char_u buf2[NUMBUFLEN];
18408 garray_T ga;
18409
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018410 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018411 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18412 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018413
18414 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415 rettv->v_type = VAR_STRING;
18416 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018417 if (fromstr == NULL || tostr == NULL)
18418 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018419 ga_init2(&ga, (int)sizeof(char), 80);
18420
18421#ifdef FEAT_MBYTE
18422 if (!has_mbyte)
18423#endif
18424 /* not multi-byte: fromstr and tostr must be the same length */
18425 if (STRLEN(fromstr) != STRLEN(tostr))
18426 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018427#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018428error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018429#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018430 EMSG2(_(e_invarg2), fromstr);
18431 ga_clear(&ga);
18432 return;
18433 }
18434
18435 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018436 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018437 {
18438#ifdef FEAT_MBYTE
18439 if (has_mbyte)
18440 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018441 inlen = (*mb_ptr2len)(in_str);
18442 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018443 cplen = inlen;
18444 idx = 0;
18445 for (p = fromstr; *p != NUL; p += fromlen)
18446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018447 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018448 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018449 {
18450 for (p = tostr; *p != NUL; p += tolen)
18451 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018452 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018453 if (idx-- == 0)
18454 {
18455 cplen = tolen;
18456 cpstr = p;
18457 break;
18458 }
18459 }
18460 if (*p == NUL) /* tostr is shorter than fromstr */
18461 goto error;
18462 break;
18463 }
18464 ++idx;
18465 }
18466
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018467 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018468 {
18469 /* Check that fromstr and tostr have the same number of
18470 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018471 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018472 first = FALSE;
18473 for (p = tostr; *p != NUL; p += tolen)
18474 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018475 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018476 --idx;
18477 }
18478 if (idx != 0)
18479 goto error;
18480 }
18481
18482 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018483 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018484 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018485
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018486 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018487 }
18488 else
18489#endif
18490 {
18491 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018492 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018493 if (p != NULL)
18494 ga_append(&ga, tostr[p - fromstr]);
18495 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018496 ga_append(&ga, *in_str);
18497 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018498 }
18499 }
18500
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018501 /* add a terminating NUL */
18502 ga_grow(&ga, 1);
18503 ga_append(&ga, NUL);
18504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018506}
18507
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018508#ifdef FEAT_FLOAT
18509/*
18510 * "trunc({float})" function
18511 */
18512 static void
18513f_trunc(argvars, rettv)
18514 typval_T *argvars;
18515 typval_T *rettv;
18516{
18517 float_T f;
18518
18519 rettv->v_type = VAR_FLOAT;
18520 if (get_float_arg(argvars, &f) == OK)
18521 /* trunc() is not in C90, use floor() or ceil() instead. */
18522 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18523 else
18524 rettv->vval.v_float = 0.0;
18525}
18526#endif
18527
Bram Moolenaar8299df92004-07-10 09:47:34 +000018528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 * "type(expr)" function
18530 */
18531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018532f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018533 typval_T *argvars;
18534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018536 int n;
18537
18538 switch (argvars[0].v_type)
18539 {
18540 case VAR_NUMBER: n = 0; break;
18541 case VAR_STRING: n = 1; break;
18542 case VAR_FUNC: n = 2; break;
18543 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018544 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018545#ifdef FEAT_FLOAT
18546 case VAR_FLOAT: n = 5; break;
18547#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018548 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18549 }
18550 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551}
18552
18553/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018554 * "undofile(name)" function
18555 */
18556 static void
18557f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018558 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018559 typval_T *rettv;
18560{
18561 rettv->v_type = VAR_STRING;
18562#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018563 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018564 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018565
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018566 if (*fname == NUL)
18567 {
18568 /* If there is no file name there will be no undo file. */
18569 rettv->vval.v_string = NULL;
18570 }
18571 else
18572 {
18573 char_u *ffname = FullName_save(fname, FALSE);
18574
18575 if (ffname != NULL)
18576 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18577 vim_free(ffname);
18578 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018579 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018580#else
18581 rettv->vval.v_string = NULL;
18582#endif
18583}
18584
18585/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018586 * "undotree()" function
18587 */
18588 static void
18589f_undotree(argvars, rettv)
18590 typval_T *argvars UNUSED;
18591 typval_T *rettv;
18592{
18593 if (rettv_dict_alloc(rettv) == OK)
18594 {
18595 dict_T *dict = rettv->vval.v_dict;
18596 list_T *list;
18597
Bram Moolenaar730cde92010-06-27 05:18:54 +020018598 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018599 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018600 dict_add_nr_str(dict, "save_last",
18601 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018602 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18603 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018604 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018605
18606 list = list_alloc();
18607 if (list != NULL)
18608 {
18609 u_eval_tree(curbuf->b_u_oldhead, list);
18610 dict_add_list(dict, "entries", list);
18611 }
18612 }
18613}
18614
18615/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018616 * "values(dict)" function
18617 */
18618 static void
18619f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018620 typval_T *argvars;
18621 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018622{
18623 dict_list(argvars, rettv, 1);
18624}
18625
18626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 * "virtcol(string)" function
18628 */
18629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 typval_T *argvars;
18632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633{
18634 colnr_T vcol = 0;
18635 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018636 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018638 fp = var2fpos(&argvars[0], FALSE, &fnum);
18639 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18640 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 {
18642 getvvcol(curwin, fp, NULL, NULL, &vcol);
18643 ++vcol;
18644 }
18645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647}
18648
18649/*
18650 * "visualmode()" function
18651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018653f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018654 typval_T *argvars UNUSED;
18655 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656{
18657#ifdef FEAT_VISUAL
18658 char_u str[2];
18659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 str[0] = curbuf->b_visual_mode_eval;
18662 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664
18665 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018666 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668#endif
18669}
18670
18671/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018672 * "wildmenumode()" function
18673 */
18674 static void
18675f_wildmenumode(argvars, rettv)
18676 typval_T *argvars UNUSED;
18677 typval_T *rettv UNUSED;
18678{
18679#ifdef FEAT_WILDMENU
18680 if (wild_menu_showing)
18681 rettv->vval.v_number = 1;
18682#endif
18683}
18684
18685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 * "winbufnr(nr)" function
18687 */
18688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018689f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018690 typval_T *argvars;
18691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692{
18693 win_T *wp;
18694
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018695 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018699 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700}
18701
18702/*
18703 * "wincol()" function
18704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018707 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709{
18710 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712}
18713
18714/*
18715 * "winheight(nr)" function
18716 */
18717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018719 typval_T *argvars;
18720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721{
18722 win_T *wp;
18723
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018724 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729}
18730
18731/*
18732 * "winline()" function
18733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738{
18739 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018740 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741}
18742
18743/*
18744 * "winnr()" function
18745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750{
18751 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018752
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018754 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018756 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757}
18758
18759/*
18760 * "winrestcmd()" function
18761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018763f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018764 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766{
18767#ifdef FEAT_WINDOWS
18768 win_T *wp;
18769 int winnr = 1;
18770 garray_T ga;
18771 char_u buf[50];
18772
18773 ga_init2(&ga, (int)sizeof(char), 70);
18774 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18775 {
18776 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18777 ga_concat(&ga, buf);
18778# ifdef FEAT_VERTSPLIT
18779 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18780 ga_concat(&ga, buf);
18781# endif
18782 ++winnr;
18783 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018784 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791}
18792
18793/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018794 * "winrestview()" function
18795 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018796 static void
18797f_winrestview(argvars, rettv)
18798 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018799 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018800{
18801 dict_T *dict;
18802
18803 if (argvars[0].v_type != VAR_DICT
18804 || (dict = argvars[0].vval.v_dict) == NULL)
18805 EMSG(_(e_invarg));
18806 else
18807 {
18808 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18809 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18810#ifdef FEAT_VIRTUALEDIT
18811 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18812#endif
18813 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018814 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018815
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018816 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018817#ifdef FEAT_DIFF
18818 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18819#endif
18820 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18821 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18822
18823 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018824 win_new_height(curwin, curwin->w_height);
18825# ifdef FEAT_VERTSPLIT
18826 win_new_width(curwin, W_WIDTH(curwin));
18827# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018828 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018829
18830 if (curwin->w_topline == 0)
18831 curwin->w_topline = 1;
18832 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18833 curwin->w_topline = curbuf->b_ml.ml_line_count;
18834#ifdef FEAT_DIFF
18835 check_topfill(curwin, TRUE);
18836#endif
18837 }
18838}
18839
18840/*
18841 * "winsaveview()" function
18842 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018843 static void
18844f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018845 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018846 typval_T *rettv;
18847{
18848 dict_T *dict;
18849
Bram Moolenaara800b422010-06-27 01:15:55 +020018850 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018851 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018852 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018853
18854 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18855 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18856#ifdef FEAT_VIRTUALEDIT
18857 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18858#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018859 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018860 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18861
18862 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18863#ifdef FEAT_DIFF
18864 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18865#endif
18866 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18867 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18868}
18869
18870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 * "winwidth(nr)" function
18872 */
18873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018874f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018875 typval_T *argvars;
18876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877{
18878 win_T *wp;
18879
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018880 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018882 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 else
18884#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018885 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018887 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888#endif
18889}
18890
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018892 * "writefile()" function
18893 */
18894 static void
18895f_writefile(argvars, rettv)
18896 typval_T *argvars;
18897 typval_T *rettv;
18898{
18899 int binary = FALSE;
18900 char_u *fname;
18901 FILE *fd;
18902 listitem_T *li;
18903 char_u *s;
18904 int ret = 0;
18905 int c;
18906
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018907 if (check_restricted() || check_secure())
18908 return;
18909
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018910 if (argvars[0].v_type != VAR_LIST)
18911 {
18912 EMSG2(_(e_listarg), "writefile()");
18913 return;
18914 }
18915 if (argvars[0].vval.v_list == NULL)
18916 return;
18917
18918 if (argvars[2].v_type != VAR_UNKNOWN
18919 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18920 binary = TRUE;
18921
18922 /* Always open the file in binary mode, library functions have a mind of
18923 * their own about CR-LF conversion. */
18924 fname = get_tv_string(&argvars[1]);
18925 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18926 {
18927 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18928 ret = -1;
18929 }
18930 else
18931 {
18932 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18933 li = li->li_next)
18934 {
18935 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18936 {
18937 if (*s == '\n')
18938 c = putc(NUL, fd);
18939 else
18940 c = putc(*s, fd);
18941 if (c == EOF)
18942 {
18943 ret = -1;
18944 break;
18945 }
18946 }
18947 if (!binary || li->li_next != NULL)
18948 if (putc('\n', fd) == EOF)
18949 {
18950 ret = -1;
18951 break;
18952 }
18953 if (ret < 0)
18954 {
18955 EMSG(_(e_write));
18956 break;
18957 }
18958 }
18959 fclose(fd);
18960 }
18961
18962 rettv->vval.v_number = ret;
18963}
18964
18965/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018966 * "xor(expr, expr)" function
18967 */
18968 static void
18969f_xor(argvars, rettv)
18970 typval_T *argvars;
18971 typval_T *rettv;
18972{
18973 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18974 ^ get_tv_number_chk(&argvars[1], NULL);
18975}
18976
18977
18978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018980 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 */
18982 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018983var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018985 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018986 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018988 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018990 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
Bram Moolenaara5525202006-03-02 22:52:09 +000018992 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018993 if (varp->v_type == VAR_LIST)
18994 {
18995 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018996 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018997 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018998 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018999
19000 l = varp->vval.v_list;
19001 if (l == NULL)
19002 return NULL;
19003
19004 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019005 pos.lnum = list_find_nr(l, 0L, &error);
19006 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019007 return NULL; /* invalid line number */
19008
19009 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019010 pos.col = list_find_nr(l, 1L, &error);
19011 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019012 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019013 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019014
19015 /* We accept "$" for the column number: last column. */
19016 li = list_find(l, 1L);
19017 if (li != NULL && li->li_tv.v_type == VAR_STRING
19018 && li->li_tv.vval.v_string != NULL
19019 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19020 pos.col = len + 1;
19021
Bram Moolenaara5525202006-03-02 22:52:09 +000019022 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019023 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019024 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019025 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019026
Bram Moolenaara5525202006-03-02 22:52:09 +000019027#ifdef FEAT_VIRTUALEDIT
19028 /* Get the virtual offset. Defaults to zero. */
19029 pos.coladd = list_find_nr(l, 2L, &error);
19030 if (error)
19031 pos.coladd = 0;
19032#endif
19033
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019034 return &pos;
19035 }
19036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019037 name = get_tv_string_chk(varp);
19038 if (name == NULL)
19039 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019040 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019042#ifdef FEAT_VISUAL
19043 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19044 {
19045 if (VIsual_active)
19046 return &VIsual;
19047 return &curwin->w_cursor;
19048 }
19049#endif
19050 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019052 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19054 return NULL;
19055 return pp;
19056 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019057
19058#ifdef FEAT_VIRTUALEDIT
19059 pos.coladd = 0;
19060#endif
19061
Bram Moolenaar477933c2007-07-17 14:32:23 +000019062 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019063 {
19064 pos.col = 0;
19065 if (name[1] == '0') /* "w0": first visible line */
19066 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019067 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019068 pos.lnum = curwin->w_topline;
19069 return &pos;
19070 }
19071 else if (name[1] == '$') /* "w$": last visible line */
19072 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019073 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019074 pos.lnum = curwin->w_botline - 1;
19075 return &pos;
19076 }
19077 }
19078 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019080 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 {
19082 pos.lnum = curbuf->b_ml.ml_line_count;
19083 pos.col = 0;
19084 }
19085 else
19086 {
19087 pos.lnum = curwin->w_cursor.lnum;
19088 pos.col = (colnr_T)STRLEN(ml_get_curline());
19089 }
19090 return &pos;
19091 }
19092 return NULL;
19093}
19094
19095/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019096 * Convert list in "arg" into a position and optional file number.
19097 * When "fnump" is NULL there is no file number, only 3 items.
19098 * Note that the column is passed on as-is, the caller may want to decrement
19099 * it to use 1 for the first column.
19100 * Return FAIL when conversion is not possible, doesn't check the position for
19101 * validity.
19102 */
19103 static int
19104list2fpos(arg, posp, fnump)
19105 typval_T *arg;
19106 pos_T *posp;
19107 int *fnump;
19108{
19109 list_T *l = arg->vval.v_list;
19110 long i = 0;
19111 long n;
19112
Bram Moolenaarbde35262006-07-23 20:12:24 +000019113 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19114 * when "fnump" isn't NULL and "coladd" is optional. */
19115 if (arg->v_type != VAR_LIST
19116 || l == NULL
19117 || l->lv_len < (fnump == NULL ? 2 : 3)
19118 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019119 return FAIL;
19120
19121 if (fnump != NULL)
19122 {
19123 n = list_find_nr(l, i++, NULL); /* fnum */
19124 if (n < 0)
19125 return FAIL;
19126 if (n == 0)
19127 n = curbuf->b_fnum; /* current buffer */
19128 *fnump = n;
19129 }
19130
19131 n = list_find_nr(l, i++, NULL); /* lnum */
19132 if (n < 0)
19133 return FAIL;
19134 posp->lnum = n;
19135
19136 n = list_find_nr(l, i++, NULL); /* col */
19137 if (n < 0)
19138 return FAIL;
19139 posp->col = n;
19140
19141#ifdef FEAT_VIRTUALEDIT
19142 n = list_find_nr(l, i, NULL);
19143 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019144 posp->coladd = 0;
19145 else
19146 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019147#endif
19148
19149 return OK;
19150}
19151
19152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 * Get the length of an environment variable name.
19154 * Advance "arg" to the first character after the name.
19155 * Return 0 for error.
19156 */
19157 static int
19158get_env_len(arg)
19159 char_u **arg;
19160{
19161 char_u *p;
19162 int len;
19163
19164 for (p = *arg; vim_isIDc(*p); ++p)
19165 ;
19166 if (p == *arg) /* no name found */
19167 return 0;
19168
19169 len = (int)(p - *arg);
19170 *arg = p;
19171 return len;
19172}
19173
19174/*
19175 * Get the length of the name of a function or internal variable.
19176 * "arg" is advanced to the first non-white character after the name.
19177 * Return 0 if something is wrong.
19178 */
19179 static int
19180get_id_len(arg)
19181 char_u **arg;
19182{
19183 char_u *p;
19184 int len;
19185
19186 /* Find the end of the name. */
19187 for (p = *arg; eval_isnamec(*p); ++p)
19188 ;
19189 if (p == *arg) /* no name found */
19190 return 0;
19191
19192 len = (int)(p - *arg);
19193 *arg = skipwhite(p);
19194
19195 return len;
19196}
19197
19198/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019199 * Get the length of the name of a variable or function.
19200 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019202 * Return -1 if curly braces expansion failed.
19203 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 * If the name contains 'magic' {}'s, expand them and return the
19205 * expanded name in an allocated string via 'alias' - caller must free.
19206 */
19207 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019208get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 char_u **arg;
19210 char_u **alias;
19211 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019212 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213{
19214 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 char_u *p;
19216 char_u *expr_start;
19217 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218
19219 *alias = NULL; /* default to no alias */
19220
19221 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19222 && (*arg)[2] == (int)KE_SNR)
19223 {
19224 /* hard coded <SNR>, already translated */
19225 *arg += 3;
19226 return get_id_len(arg) + 3;
19227 }
19228 len = eval_fname_script(*arg);
19229 if (len > 0)
19230 {
19231 /* literal "<SID>", "s:" or "<SNR>" */
19232 *arg += len;
19233 }
19234
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019238 p = find_name_end(*arg, &expr_start, &expr_end,
19239 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 if (expr_start != NULL)
19241 {
19242 char_u *temp_string;
19243
19244 if (!evaluate)
19245 {
19246 len += (int)(p - *arg);
19247 *arg = skipwhite(p);
19248 return len;
19249 }
19250
19251 /*
19252 * Include any <SID> etc in the expanded string:
19253 * Thus the -len here.
19254 */
19255 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19256 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019257 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 *alias = temp_string;
19259 *arg = skipwhite(p);
19260 return (int)STRLEN(temp_string);
19261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262
19263 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019264 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 EMSG2(_(e_invexpr2), *arg);
19266
19267 return len;
19268}
19269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270/*
19271 * Find the end of a variable or function name, taking care of magic braces.
19272 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19273 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019274 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 * Return a pointer to just after the name. Equal to "arg" if there is no
19276 * valid name.
19277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019279find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 char_u *arg;
19281 char_u **expr_start;
19282 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019283 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285 int mb_nest = 0;
19286 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 char_u *p;
19288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019289 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019291 *expr_start = NULL;
19292 *expr_end = NULL;
19293 }
19294
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019295 /* Quick check for valid starting character. */
19296 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19297 return arg;
19298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019299 for (p = arg; *p != NUL
19300 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019301 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019302 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019303 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019304 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019305 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019306 if (*p == '\'')
19307 {
19308 /* skip over 'string' to avoid counting [ and ] inside it. */
19309 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19310 ;
19311 if (*p == NUL)
19312 break;
19313 }
19314 else if (*p == '"')
19315 {
19316 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19317 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19318 if (*p == '\\' && p[1] != NUL)
19319 ++p;
19320 if (*p == NUL)
19321 break;
19322 }
19323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019326 if (*p == '[')
19327 ++br_nest;
19328 else if (*p == ']')
19329 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019332 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019334 if (*p == '{')
19335 {
19336 mb_nest++;
19337 if (expr_start != NULL && *expr_start == NULL)
19338 *expr_start = p;
19339 }
19340 else if (*p == '}')
19341 {
19342 mb_nest--;
19343 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19344 *expr_end = p;
19345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 }
19348
19349 return p;
19350}
19351
19352/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019353 * Expands out the 'magic' {}'s in a variable/function name.
19354 * Note that this can call itself recursively, to deal with
19355 * constructs like foo{bar}{baz}{bam}
19356 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19357 * "in_start" ^
19358 * "expr_start" ^
19359 * "expr_end" ^
19360 * "in_end" ^
19361 *
19362 * Returns a new allocated string, which the caller must free.
19363 * Returns NULL for failure.
19364 */
19365 static char_u *
19366make_expanded_name(in_start, expr_start, expr_end, in_end)
19367 char_u *in_start;
19368 char_u *expr_start;
19369 char_u *expr_end;
19370 char_u *in_end;
19371{
19372 char_u c1;
19373 char_u *retval = NULL;
19374 char_u *temp_result;
19375 char_u *nextcmd = NULL;
19376
19377 if (expr_end == NULL || in_end == NULL)
19378 return NULL;
19379 *expr_start = NUL;
19380 *expr_end = NUL;
19381 c1 = *in_end;
19382 *in_end = NUL;
19383
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019384 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019385 if (temp_result != NULL && nextcmd == NULL)
19386 {
19387 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19388 + (in_end - expr_end) + 1));
19389 if (retval != NULL)
19390 {
19391 STRCPY(retval, in_start);
19392 STRCAT(retval, temp_result);
19393 STRCAT(retval, expr_end + 1);
19394 }
19395 }
19396 vim_free(temp_result);
19397
19398 *in_end = c1; /* put char back for error messages */
19399 *expr_start = '{';
19400 *expr_end = '}';
19401
19402 if (retval != NULL)
19403 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019404 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019405 if (expr_start != NULL)
19406 {
19407 /* Further expansion! */
19408 temp_result = make_expanded_name(retval, expr_start,
19409 expr_end, temp_result);
19410 vim_free(retval);
19411 retval = temp_result;
19412 }
19413 }
19414
19415 return retval;
19416}
19417
19418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019420 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 */
19422 static int
19423eval_isnamec(c)
19424 int c;
19425{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019426 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19427}
19428
19429/*
19430 * Return TRUE if character "c" can be used as the first character in a
19431 * variable or function name (excluding '{' and '}').
19432 */
19433 static int
19434eval_isnamec1(c)
19435 int c;
19436{
19437 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438}
19439
19440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 * Set number v: variable to "val".
19442 */
19443 void
19444set_vim_var_nr(idx, val)
19445 int idx;
19446 long val;
19447{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019448 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
19451/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019452 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 */
19454 long
19455get_vim_var_nr(idx)
19456 int idx;
19457{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019458 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459}
19460
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019461/*
19462 * Get string v: variable value. Uses a static buffer, can only be used once.
19463 */
19464 char_u *
19465get_vim_var_str(idx)
19466 int idx;
19467{
19468 return get_tv_string(&vimvars[idx].vv_tv);
19469}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019470
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019472 * Get List v: variable value. Caller must take care of reference count when
19473 * needed.
19474 */
19475 list_T *
19476get_vim_var_list(idx)
19477 int idx;
19478{
19479 return vimvars[idx].vv_list;
19480}
19481
19482/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019483 * Set v:char to character "c".
19484 */
19485 void
19486set_vim_var_char(c)
19487 int c;
19488{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019489 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019490
19491#ifdef FEAT_MBYTE
19492 if (has_mbyte)
19493 buf[(*mb_char2bytes)(c, buf)] = NUL;
19494 else
19495#endif
19496 {
19497 buf[0] = c;
19498 buf[1] = NUL;
19499 }
19500 set_vim_var_string(VV_CHAR, buf, -1);
19501}
19502
19503/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019504 * Set v:count to "count" and v:count1 to "count1".
19505 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 */
19507 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019508set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 long count;
19510 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019511 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019513 if (set_prevcount)
19514 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019515 vimvars[VV_COUNT].vv_nr = count;
19516 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517}
19518
19519/*
19520 * Set string v: variable to a copy of "val".
19521 */
19522 void
19523set_vim_var_string(idx, val, len)
19524 int idx;
19525 char_u *val;
19526 int len; /* length of "val" to use or -1 (whole string) */
19527{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019528 /* Need to do this (at least) once, since we can't initialize a union.
19529 * Will always be invoked when "v:progname" is set. */
19530 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19531
Bram Moolenaare9a41262005-01-15 22:18:47 +000019532 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019534 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019536 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019538 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539}
19540
19541/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019542 * Set List v: variable to "val".
19543 */
19544 void
19545set_vim_var_list(idx, val)
19546 int idx;
19547 list_T *val;
19548{
19549 list_unref(vimvars[idx].vv_list);
19550 vimvars[idx].vv_list = val;
19551 if (val != NULL)
19552 ++val->lv_refcount;
19553}
19554
19555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 * Set v:register if needed.
19557 */
19558 void
19559set_reg_var(c)
19560 int c;
19561{
19562 char_u regname;
19563
19564 if (c == 0 || c == ' ')
19565 regname = '"';
19566 else
19567 regname = c;
19568 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019569 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 set_vim_var_string(VV_REG, &regname, 1);
19571}
19572
19573/*
19574 * Get or set v:exception. If "oldval" == NULL, return the current value.
19575 * Otherwise, restore the value to "oldval" and return NULL.
19576 * Must always be called in pairs to save and restore v:exception! Does not
19577 * take care of memory allocations.
19578 */
19579 char_u *
19580v_exception(oldval)
19581 char_u *oldval;
19582{
19583 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019584 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585
Bram Moolenaare9a41262005-01-15 22:18:47 +000019586 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 return NULL;
19588}
19589
19590/*
19591 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19592 * Otherwise, restore the value to "oldval" and return NULL.
19593 * Must always be called in pairs to save and restore v:throwpoint! Does not
19594 * take care of memory allocations.
19595 */
19596 char_u *
19597v_throwpoint(oldval)
19598 char_u *oldval;
19599{
19600 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019601 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602
Bram Moolenaare9a41262005-01-15 22:18:47 +000019603 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 return NULL;
19605}
19606
19607#if defined(FEAT_AUTOCMD) || defined(PROTO)
19608/*
19609 * Set v:cmdarg.
19610 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19611 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19612 * Must always be called in pairs!
19613 */
19614 char_u *
19615set_cmdarg(eap, oldarg)
19616 exarg_T *eap;
19617 char_u *oldarg;
19618{
19619 char_u *oldval;
19620 char_u *newval;
19621 unsigned len;
19622
Bram Moolenaare9a41262005-01-15 22:18:47 +000019623 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019624 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019626 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019627 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019628 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 }
19630
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019631 if (eap->force_bin == FORCE_BIN)
19632 len = 6;
19633 else if (eap->force_bin == FORCE_NOBIN)
19634 len = 8;
19635 else
19636 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019637
19638 if (eap->read_edit)
19639 len += 7;
19640
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019641 if (eap->force_ff != 0)
19642 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19643# ifdef FEAT_MBYTE
19644 if (eap->force_enc != 0)
19645 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019646 if (eap->bad_char != 0)
19647 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019648# endif
19649
19650 newval = alloc(len + 1);
19651 if (newval == NULL)
19652 return NULL;
19653
19654 if (eap->force_bin == FORCE_BIN)
19655 sprintf((char *)newval, " ++bin");
19656 else if (eap->force_bin == FORCE_NOBIN)
19657 sprintf((char *)newval, " ++nobin");
19658 else
19659 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019660
19661 if (eap->read_edit)
19662 STRCAT(newval, " ++edit");
19663
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019664 if (eap->force_ff != 0)
19665 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19666 eap->cmd + eap->force_ff);
19667# ifdef FEAT_MBYTE
19668 if (eap->force_enc != 0)
19669 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19670 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019671 if (eap->bad_char == BAD_KEEP)
19672 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19673 else if (eap->bad_char == BAD_DROP)
19674 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19675 else if (eap->bad_char != 0)
19676 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019677# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019678 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019679 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680}
19681#endif
19682
19683/*
19684 * Get the value of internal variable "name".
19685 * Return OK or FAIL.
19686 */
19687 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019688get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 char_u *name;
19690 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019692 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693{
19694 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 typval_T *tv = NULL;
19696 typval_T atv;
19697 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699
19700 /* truncate the name, so that we can use strcmp() */
19701 cc = name[len];
19702 name[len] = NUL;
19703
19704 /*
19705 * Check for "b:changedtick".
19706 */
19707 if (STRCMP(name, "b:changedtick") == 0)
19708 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019709 atv.v_type = VAR_NUMBER;
19710 atv.vval.v_number = curbuf->b_changedtick;
19711 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 }
19713
19714 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 * Check for user-defined variables.
19716 */
19717 else
19718 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019719 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 }
19723
Bram Moolenaare9a41262005-01-15 22:18:47 +000019724 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019726 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019727 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 ret = FAIL;
19729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019731 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732
19733 name[len] = cc;
19734
19735 return ret;
19736}
19737
19738/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019739 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19740 * Also handle function call with Funcref variable: func(expr)
19741 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19742 */
19743 static int
19744handle_subscript(arg, rettv, evaluate, verbose)
19745 char_u **arg;
19746 typval_T *rettv;
19747 int evaluate; /* do more than finding the end */
19748 int verbose; /* give error messages */
19749{
19750 int ret = OK;
19751 dict_T *selfdict = NULL;
19752 char_u *s;
19753 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019754 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019755
19756 while (ret == OK
19757 && (**arg == '['
19758 || (**arg == '.' && rettv->v_type == VAR_DICT)
19759 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19760 && !vim_iswhite(*(*arg - 1)))
19761 {
19762 if (**arg == '(')
19763 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019764 /* need to copy the funcref so that we can clear rettv */
19765 functv = *rettv;
19766 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019767
19768 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019769 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019770 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019771 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19772 &len, evaluate, selfdict);
19773
19774 /* Clear the funcref afterwards, so that deleting it while
19775 * evaluating the arguments is possible (see test55). */
19776 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019777
19778 /* Stop the expression evaluation when immediately aborting on
19779 * error, or when an interrupt occurred or an exception was thrown
19780 * but not caught. */
19781 if (aborting())
19782 {
19783 if (ret == OK)
19784 clear_tv(rettv);
19785 ret = FAIL;
19786 }
19787 dict_unref(selfdict);
19788 selfdict = NULL;
19789 }
19790 else /* **arg == '[' || **arg == '.' */
19791 {
19792 dict_unref(selfdict);
19793 if (rettv->v_type == VAR_DICT)
19794 {
19795 selfdict = rettv->vval.v_dict;
19796 if (selfdict != NULL)
19797 ++selfdict->dv_refcount;
19798 }
19799 else
19800 selfdict = NULL;
19801 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19802 {
19803 clear_tv(rettv);
19804 ret = FAIL;
19805 }
19806 }
19807 }
19808 dict_unref(selfdict);
19809 return ret;
19810}
19811
19812/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019813 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019814 * value).
19815 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019818{
Bram Moolenaar33570922005-01-25 22:26:29 +000019819 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019820}
19821
19822/*
19823 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 * The string "s" must have been allocated, it is consumed.
19825 * Return NULL for out of memory, the variable otherwise.
19826 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 char_u *s;
19830{
Bram Moolenaar33570922005-01-25 22:26:29 +000019831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 rettv = alloc_tv();
19834 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->v_type = VAR_STRING;
19837 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 }
19839 else
19840 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842}
19843
19844/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019845 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019847 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850{
19851 if (varp != NULL)
19852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019853 switch (varp->v_type)
19854 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019855 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019856 func_unref(varp->vval.v_string);
19857 /*FALLTHROUGH*/
19858 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019859 vim_free(varp->vval.v_string);
19860 break;
19861 case VAR_LIST:
19862 list_unref(varp->vval.v_list);
19863 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019864 case VAR_DICT:
19865 dict_unref(varp->vval.v_dict);
19866 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019867 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019868#ifdef FEAT_FLOAT
19869 case VAR_FLOAT:
19870#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019871 case VAR_UNKNOWN:
19872 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019873 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019874 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 break;
19876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 vim_free(varp);
19878 }
19879}
19880
19881/*
19882 * Free the memory for a variable value and set the value to NULL or 0.
19883 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019884 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019885clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887{
19888 if (varp != NULL)
19889 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019892 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019893 func_unref(varp->vval.v_string);
19894 /*FALLTHROUGH*/
19895 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019896 vim_free(varp->vval.v_string);
19897 varp->vval.v_string = NULL;
19898 break;
19899 case VAR_LIST:
19900 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019901 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019902 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019903 case VAR_DICT:
19904 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019905 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019906 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019908 varp->vval.v_number = 0;
19909 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019910#ifdef FEAT_FLOAT
19911 case VAR_FLOAT:
19912 varp->vval.v_float = 0.0;
19913 break;
19914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915 case VAR_UNKNOWN:
19916 break;
19917 default:
19918 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019920 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 }
19922}
19923
19924/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925 * Set the value of a variable to NULL without freeing items.
19926 */
19927 static void
19928init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019929 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930{
19931 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933}
19934
19935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 * Get the number value of a variable.
19937 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019938 * For incompatible types, return 0.
19939 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19940 * caller of incompatible types: it sets *denote to TRUE if "denote"
19941 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 */
19943 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019947 int error = FALSE;
19948
19949 return get_tv_number_chk(varp, &error); /* return 0L on error */
19950}
19951
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019952 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019953get_tv_number_chk(varp, denote)
19954 typval_T *varp;
19955 int *denote;
19956{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019957 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019959 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019961 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019962 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019963#ifdef FEAT_FLOAT
19964 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019965 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019966 break;
19967#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019968 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019969 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019970 break;
19971 case VAR_STRING:
19972 if (varp->vval.v_string != NULL)
19973 vim_str2nr(varp->vval.v_string, NULL, NULL,
19974 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019976 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019977 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019978 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019979 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019980 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019982 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019983 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019986 if (denote == NULL) /* useful for values that must be unsigned */
19987 n = -1;
19988 else
19989 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019990 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991}
19992
19993/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019994 * Get the lnum from the first argument.
19995 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019996 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 */
19998 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020000 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001{
Bram Moolenaar33570922005-01-25 22:26:29 +000020002 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 linenr_T lnum;
20004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020005 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 if (lnum == 0) /* no valid number, try using line() */
20007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008 rettv.v_type = VAR_NUMBER;
20009 f_line(argvars, &rettv);
20010 lnum = rettv.vval.v_number;
20011 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 }
20013 return lnum;
20014}
20015
20016/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020017 * Get the lnum from the first argument.
20018 * Also accepts "$", then "buf" is used.
20019 * Returns 0 on error.
20020 */
20021 static linenr_T
20022get_tv_lnum_buf(argvars, buf)
20023 typval_T *argvars;
20024 buf_T *buf;
20025{
20026 if (argvars[0].v_type == VAR_STRING
20027 && argvars[0].vval.v_string != NULL
20028 && argvars[0].vval.v_string[0] == '$'
20029 && buf != NULL)
20030 return buf->b_ml.ml_line_count;
20031 return get_tv_number_chk(&argvars[0], NULL);
20032}
20033
20034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 * Get the string value of a variable.
20036 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020037 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20038 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 * If the String variable has never been set, return an empty string.
20040 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020041 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20042 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 */
20044 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020046 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047{
20048 static char_u mybuf[NUMBUFLEN];
20049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020050 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051}
20052
20053 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020056 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020058 char_u *res = get_tv_string_buf_chk(varp, buf);
20059
20060 return res != NULL ? res : (char_u *)"";
20061}
20062
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020063 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020064get_tv_string_chk(varp)
20065 typval_T *varp;
20066{
20067 static char_u mybuf[NUMBUFLEN];
20068
20069 return get_tv_string_buf_chk(varp, mybuf);
20070}
20071
20072 static char_u *
20073get_tv_string_buf_chk(varp, buf)
20074 typval_T *varp;
20075 char_u *buf;
20076{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020077 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020079 case VAR_NUMBER:
20080 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20081 return buf;
20082 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020083 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020084 break;
20085 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020086 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020087 break;
20088 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020089 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020091#ifdef FEAT_FLOAT
20092 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020093 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020094 break;
20095#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020096 case VAR_STRING:
20097 if (varp->vval.v_string != NULL)
20098 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020099 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020100 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020104 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105}
20106
20107/*
20108 * Find variable "name" in the list of variables.
20109 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020110 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020111 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020112 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020115find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020120 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121
Bram Moolenaara7043832005-01-21 11:56:39 +000020122 ht = find_var_ht(name, &varname);
20123 if (htp != NULL)
20124 *htp = ht;
20125 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020127 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128}
20129
20130/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020131 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020132 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020135find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020137 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020138 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020139 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020140{
Bram Moolenaar33570922005-01-25 22:26:29 +000020141 hashitem_T *hi;
20142
20143 if (*varname == NUL)
20144 {
20145 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020146 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020148 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 case 'g': return &globvars_var;
20150 case 'v': return &vimvars_var;
20151 case 'b': return &curbuf->b_bufvar;
20152 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020153#ifdef FEAT_WINDOWS
20154 case 't': return &curtab->tp_winvar;
20155#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020156 case 'l': return current_funccal == NULL
20157 ? NULL : &current_funccal->l_vars_var;
20158 case 'a': return current_funccal == NULL
20159 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020160 }
20161 return NULL;
20162 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020163
20164 hi = hash_find(ht, varname);
20165 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020166 {
20167 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020168 * worked find the variable again. Don't auto-load a script if it was
20169 * loaded already, otherwise it would be loaded every time when
20170 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020171 if (ht == &globvarht && !writing)
20172 {
20173 /* Note: script_autoload() may make "hi" invalid. It must either
20174 * be obtained again or not used. */
20175 if (!script_autoload(varname, FALSE) || aborting())
20176 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020177 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020178 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020179 if (HASHITEM_EMPTY(hi))
20180 return NULL;
20181 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020183}
20184
20185/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020187 * Set "varname" to the start of name without ':'.
20188 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020190find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 char_u *name;
20192 char_u **varname;
20193{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020194 hashitem_T *hi;
20195
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 if (name[1] != ':')
20197 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020198 /* The name must not start with a colon or #. */
20199 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 return NULL;
20201 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020202
20203 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020204 hi = hash_find(&compat_hashtab, name);
20205 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020206 return &compat_hashtab;
20207
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 return &globvarht; /* global variable */
20210 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 }
20212 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020213 if (*name == 'g') /* global variable */
20214 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020215 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20216 */
20217 if (vim_strchr(name + 2, ':') != NULL
20218 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020219 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020221 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020223 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020224#ifdef FEAT_WINDOWS
20225 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020226 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020227#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 if (*name == 'v') /* v: variable */
20229 return &vimvarht;
20230 if (*name == 'a' && current_funccal != NULL) /* function argument */
20231 return &current_funccal->l_avars.dv_hashtab;
20232 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20233 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 if (*name == 's' /* script variable */
20235 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20236 return &SCRIPT_VARS(current_SID);
20237 return NULL;
20238}
20239
20240/*
20241 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020242 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 * Returns NULL when it doesn't exist.
20244 */
20245 char_u *
20246get_var_value(name)
20247 char_u *name;
20248{
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250
Bram Moolenaara7043832005-01-21 11:56:39 +000020251 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 if (v == NULL)
20253 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255}
20256
20257/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 * sourcing this script and when executing functions defined in the script.
20260 */
20261 void
20262new_script_vars(id)
20263 scid_T id;
20264{
Bram Moolenaara7043832005-01-21 11:56:39 +000020265 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020266 hashtab_T *ht;
20267 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020268
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20270 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020271 /* Re-allocating ga_data means that an ht_array pointing to
20272 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020273 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020274 for (i = 1; i <= ga_scripts.ga_len; ++i)
20275 {
20276 ht = &SCRIPT_VARS(i);
20277 if (ht->ht_mask == HT_INIT_SIZE - 1)
20278 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020279 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020281 }
20282
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 while (ga_scripts.ga_len < id)
20284 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020285 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020286 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020287 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 }
20290 }
20291}
20292
20293/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020294 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20295 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 */
20297 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020298init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 dict_T *dict;
20300 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020301 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302{
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020304 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020305 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020306 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020307 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020308 dict_var->di_tv.vval.v_dict = dict;
20309 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020310 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20312 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313}
20314
20315/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020316 * Unreference a dictionary initialized by init_var_dict().
20317 */
20318 void
20319unref_var_dict(dict)
20320 dict_T *dict;
20321{
20322 /* Now the dict needs to be freed if no one else is using it, go back to
20323 * normal reference counting. */
20324 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20325 dict_unref(dict);
20326}
20327
20328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020330 * Frees all allocated variables and the value they contain.
20331 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 */
20333 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020334vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020335 hashtab_T *ht;
20336{
20337 vars_clear_ext(ht, TRUE);
20338}
20339
20340/*
20341 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20342 */
20343 static void
20344vars_clear_ext(ht, free_val)
20345 hashtab_T *ht;
20346 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347{
Bram Moolenaara7043832005-01-21 11:56:39 +000020348 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 hashitem_T *hi;
20350 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351
Bram Moolenaar33570922005-01-25 22:26:29 +000020352 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020353 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020354 for (hi = ht->ht_array; todo > 0; ++hi)
20355 {
20356 if (!HASHITEM_EMPTY(hi))
20357 {
20358 --todo;
20359
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020361 * ht_array might change then. hash_clear() takes care of it
20362 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020363 v = HI2DI(hi);
20364 if (free_val)
20365 clear_tv(&v->di_tv);
20366 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20367 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020368 }
20369 }
20370 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020371 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372}
20373
Bram Moolenaara7043832005-01-21 11:56:39 +000020374/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 * Delete a variable from hashtab "ht" at item "hi".
20376 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020379delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020380 hashtab_T *ht;
20381 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382{
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020384
20385 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 clear_tv(&di->di_tv);
20387 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388}
20389
20390/*
20391 * List the value of one internal variable.
20392 */
20393 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020394list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020397 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 char_u *tofree;
20400 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020401 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020402
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020403 current_copyID += COPYID_INC;
20404 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020406 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020407 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408}
20409
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020411list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 char_u *prefix;
20413 char_u *name;
20414 int type;
20415 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020416 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417{
Bram Moolenaar31859182007-08-14 20:41:13 +000020418 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20419 msg_start();
20420 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 if (name != NULL) /* "a:" vars don't have a name stored */
20422 msg_puts(name);
20423 msg_putchar(' ');
20424 msg_advance(22);
20425 if (type == VAR_NUMBER)
20426 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020427 else if (type == VAR_FUNC)
20428 msg_putchar('*');
20429 else if (type == VAR_LIST)
20430 {
20431 msg_putchar('[');
20432 if (*string == '[')
20433 ++string;
20434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020435 else if (type == VAR_DICT)
20436 {
20437 msg_putchar('{');
20438 if (*string == '{')
20439 ++string;
20440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 else
20442 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020443
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020445
20446 if (type == VAR_FUNC)
20447 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020448 if (*first)
20449 {
20450 msg_clr_eos();
20451 *first = FALSE;
20452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453}
20454
20455/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020456 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 * If the variable already exists, the value is updated.
20458 * Otherwise the variable is created.
20459 */
20460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020461set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020463 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020464 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465{
Bram Moolenaar33570922005-01-25 22:26:29 +000020466 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020468 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020470 ht = find_var_ht(name, &varname);
20471 if (ht == NULL || *varname == NUL)
20472 {
20473 EMSG2(_(e_illvar), name);
20474 return;
20475 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020476 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020477
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020478 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20479 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020480
Bram Moolenaar33570922005-01-25 22:26:29 +000020481 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020484 if (var_check_ro(v->di_flags, name)
20485 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020486 return;
20487 if (v->di_tv.v_type != tv->v_type
20488 && !((v->di_tv.v_type == VAR_STRING
20489 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020491 || tv->v_type == VAR_NUMBER))
20492#ifdef FEAT_FLOAT
20493 && !((v->di_tv.v_type == VAR_NUMBER
20494 || v->di_tv.v_type == VAR_FLOAT)
20495 && (tv->v_type == VAR_NUMBER
20496 || tv->v_type == VAR_FLOAT))
20497#endif
20498 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020499 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020500 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020501 return;
20502 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020503
20504 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020505 * Handle setting internal v: variables separately: we don't change
20506 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020507 */
20508 if (ht == &vimvarht)
20509 {
20510 if (v->di_tv.v_type == VAR_STRING)
20511 {
20512 vim_free(v->di_tv.vval.v_string);
20513 if (copy || tv->v_type != VAR_STRING)
20514 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20515 else
20516 {
20517 /* Take over the string to avoid an extra alloc/free. */
20518 v->di_tv.vval.v_string = tv->vval.v_string;
20519 tv->vval.v_string = NULL;
20520 }
20521 }
20522 else if (v->di_tv.v_type != VAR_NUMBER)
20523 EMSG2(_(e_intern2), "set_var()");
20524 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020525 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020526 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020527 if (STRCMP(varname, "searchforward") == 0)
20528 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20529 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020530 return;
20531 }
20532
20533 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 }
20535 else /* add a new variable */
20536 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020537 /* Can't add "v:" variable. */
20538 if (ht == &vimvarht)
20539 {
20540 EMSG2(_(e_illvar), name);
20541 return;
20542 }
20543
Bram Moolenaar92124a32005-06-17 22:03:40 +000020544 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020545 if (!valid_varname(varname))
20546 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020547
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020548 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20549 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020550 if (v == NULL)
20551 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020552 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020555 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 return;
20557 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020558 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020560
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020561 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020562 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020563 else
20564 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020565 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020566 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020567 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020571/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020572 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020573 * Also give an error message.
20574 */
20575 static int
20576var_check_ro(flags, name)
20577 int flags;
20578 char_u *name;
20579{
20580 if (flags & DI_FLAGS_RO)
20581 {
20582 EMSG2(_(e_readonlyvar), name);
20583 return TRUE;
20584 }
20585 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20586 {
20587 EMSG2(_(e_readonlysbx), name);
20588 return TRUE;
20589 }
20590 return FALSE;
20591}
20592
20593/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020594 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20595 * Also give an error message.
20596 */
20597 static int
20598var_check_fixed(flags, name)
20599 int flags;
20600 char_u *name;
20601{
20602 if (flags & DI_FLAGS_FIX)
20603 {
20604 EMSG2(_("E795: Cannot delete variable %s"), name);
20605 return TRUE;
20606 }
20607 return FALSE;
20608}
20609
20610/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020611 * Check if a funcref is assigned to a valid variable name.
20612 * Return TRUE and give an error if not.
20613 */
20614 static int
20615var_check_func_name(name, new_var)
20616 char_u *name; /* points to start of variable name */
20617 int new_var; /* TRUE when creating the variable */
20618{
20619 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20620 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20621 ? name[2] : name[0]))
20622 {
20623 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20624 name);
20625 return TRUE;
20626 }
20627 /* Don't allow hiding a function. When "v" is not NULL we might be
20628 * assigning another function to the same var, the type is checked
20629 * below. */
20630 if (new_var && function_exists(name))
20631 {
20632 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20633 name);
20634 return TRUE;
20635 }
20636 return FALSE;
20637}
20638
20639/*
20640 * Check if a variable name is valid.
20641 * Return FALSE and give an error if not.
20642 */
20643 static int
20644valid_varname(varname)
20645 char_u *varname;
20646{
20647 char_u *p;
20648
20649 for (p = varname; *p != NUL; ++p)
20650 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20651 && *p != AUTOLOAD_CHAR)
20652 {
20653 EMSG2(_(e_illvar), varname);
20654 return FALSE;
20655 }
20656 return TRUE;
20657}
20658
20659/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020660 * Return TRUE if typeval "tv" is set to be locked (immutable).
20661 * Also give an error message, using "name".
20662 */
20663 static int
20664tv_check_lock(lock, name)
20665 int lock;
20666 char_u *name;
20667{
20668 if (lock & VAR_LOCKED)
20669 {
20670 EMSG2(_("E741: Value is locked: %s"),
20671 name == NULL ? (char_u *)_("Unknown") : name);
20672 return TRUE;
20673 }
20674 if (lock & VAR_FIXED)
20675 {
20676 EMSG2(_("E742: Cannot change value of %s"),
20677 name == NULL ? (char_u *)_("Unknown") : name);
20678 return TRUE;
20679 }
20680 return FALSE;
20681}
20682
20683/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020684 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020686 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020687 * It is OK for "from" and "to" to point to the same item. This is used to
20688 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020689 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020690 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020691copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020692 typval_T *from;
20693 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020695 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020696 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020697 switch (from->v_type)
20698 {
20699 case VAR_NUMBER:
20700 to->vval.v_number = from->vval.v_number;
20701 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020702#ifdef FEAT_FLOAT
20703 case VAR_FLOAT:
20704 to->vval.v_float = from->vval.v_float;
20705 break;
20706#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020707 case VAR_STRING:
20708 case VAR_FUNC:
20709 if (from->vval.v_string == NULL)
20710 to->vval.v_string = NULL;
20711 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020713 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020714 if (from->v_type == VAR_FUNC)
20715 func_ref(to->vval.v_string);
20716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020717 break;
20718 case VAR_LIST:
20719 if (from->vval.v_list == NULL)
20720 to->vval.v_list = NULL;
20721 else
20722 {
20723 to->vval.v_list = from->vval.v_list;
20724 ++to->vval.v_list->lv_refcount;
20725 }
20726 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020727 case VAR_DICT:
20728 if (from->vval.v_dict == NULL)
20729 to->vval.v_dict = NULL;
20730 else
20731 {
20732 to->vval.v_dict = from->vval.v_dict;
20733 ++to->vval.v_dict->dv_refcount;
20734 }
20735 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020736 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020737 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020738 break;
20739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740}
20741
20742/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020743 * Make a copy of an item.
20744 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020745 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20746 * reference to an already copied list/dict can be used.
20747 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020748 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020749 static int
20750item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020751 typval_T *from;
20752 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020753 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020754 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020755{
20756 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020757 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020758
Bram Moolenaar33570922005-01-25 22:26:29 +000020759 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020760 {
20761 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020762 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020763 }
20764 ++recurse;
20765
20766 switch (from->v_type)
20767 {
20768 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020769#ifdef FEAT_FLOAT
20770 case VAR_FLOAT:
20771#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020772 case VAR_STRING:
20773 case VAR_FUNC:
20774 copy_tv(from, to);
20775 break;
20776 case VAR_LIST:
20777 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020778 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020779 if (from->vval.v_list == NULL)
20780 to->vval.v_list = NULL;
20781 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20782 {
20783 /* use the copy made earlier */
20784 to->vval.v_list = from->vval.v_list->lv_copylist;
20785 ++to->vval.v_list->lv_refcount;
20786 }
20787 else
20788 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20789 if (to->vval.v_list == NULL)
20790 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020791 break;
20792 case VAR_DICT:
20793 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020794 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020795 if (from->vval.v_dict == NULL)
20796 to->vval.v_dict = NULL;
20797 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20798 {
20799 /* use the copy made earlier */
20800 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20801 ++to->vval.v_dict->dv_refcount;
20802 }
20803 else
20804 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20805 if (to->vval.v_dict == NULL)
20806 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020807 break;
20808 default:
20809 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020810 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020811 }
20812 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020813 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020814}
20815
20816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 * ":echo expr1 ..." print each argument separated with a space, add a
20818 * newline at the end.
20819 * ":echon expr1 ..." print each argument plain.
20820 */
20821 void
20822ex_echo(eap)
20823 exarg_T *eap;
20824{
20825 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020826 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020827 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 char_u *p;
20829 int needclr = TRUE;
20830 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020831 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832
20833 if (eap->skip)
20834 ++emsg_skip;
20835 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20836 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020837 /* If eval1() causes an error message the text from the command may
20838 * still need to be cleared. E.g., "echo 22,44". */
20839 need_clr_eos = needclr;
20840
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020842 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 {
20844 /*
20845 * Report the invalid expression unless the expression evaluation
20846 * has been cancelled due to an aborting error, an interrupt, or an
20847 * exception.
20848 */
20849 if (!aborting())
20850 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020851 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 break;
20853 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020854 need_clr_eos = FALSE;
20855
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 if (!eap->skip)
20857 {
20858 if (atstart)
20859 {
20860 atstart = FALSE;
20861 /* Call msg_start() after eval1(), evaluating the expression
20862 * may cause a message to appear. */
20863 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020864 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020865 /* Mark the saved text as finishing the line, so that what
20866 * follows is displayed on a new line when scrolling back
20867 * at the more prompt. */
20868 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 }
20872 else if (eap->cmdidx == CMD_echo)
20873 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020874 current_copyID += COPYID_INC;
20875 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020876 if (p != NULL)
20877 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020879 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020881 if (*p != TAB && needclr)
20882 {
20883 /* remove any text still there from the command */
20884 msg_clr_eos();
20885 needclr = FALSE;
20886 }
20887 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 }
20889 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020890 {
20891#ifdef FEAT_MBYTE
20892 if (has_mbyte)
20893 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020894 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020895
20896 (void)msg_outtrans_len_attr(p, i, echo_attr);
20897 p += i - 1;
20898 }
20899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020901 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020904 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 arg = skipwhite(arg);
20908 }
20909 eap->nextcmd = check_nextcmd(arg);
20910
20911 if (eap->skip)
20912 --emsg_skip;
20913 else
20914 {
20915 /* remove text that may still be there from the command */
20916 if (needclr)
20917 msg_clr_eos();
20918 if (eap->cmdidx == CMD_echo)
20919 msg_end();
20920 }
20921}
20922
20923/*
20924 * ":echohl {name}".
20925 */
20926 void
20927ex_echohl(eap)
20928 exarg_T *eap;
20929{
20930 int id;
20931
20932 id = syn_name2id(eap->arg);
20933 if (id == 0)
20934 echo_attr = 0;
20935 else
20936 echo_attr = syn_id2attr(id);
20937}
20938
20939/*
20940 * ":execute expr1 ..." execute the result of an expression.
20941 * ":echomsg expr1 ..." Print a message
20942 * ":echoerr expr1 ..." Print an error
20943 * Each gets spaces around each argument and a newline at the end for
20944 * echo commands
20945 */
20946 void
20947ex_execute(eap)
20948 exarg_T *eap;
20949{
20950 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020951 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 int ret = OK;
20953 char_u *p;
20954 garray_T ga;
20955 int len;
20956 int save_did_emsg;
20957
20958 ga_init2(&ga, 1, 80);
20959
20960 if (eap->skip)
20961 ++emsg_skip;
20962 while (*arg != NUL && *arg != '|' && *arg != '\n')
20963 {
20964 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020965 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 {
20967 /*
20968 * Report the invalid expression unless the expression evaluation
20969 * has been cancelled due to an aborting error, an interrupt, or an
20970 * exception.
20971 */
20972 if (!aborting())
20973 EMSG2(_(e_invexpr2), p);
20974 ret = FAIL;
20975 break;
20976 }
20977
20978 if (!eap->skip)
20979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020980 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 len = (int)STRLEN(p);
20982 if (ga_grow(&ga, len + 2) == FAIL)
20983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020984 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 ret = FAIL;
20986 break;
20987 }
20988 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 ga.ga_len += len;
20992 }
20993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 arg = skipwhite(arg);
20996 }
20997
20998 if (ret != FAIL && ga.ga_data != NULL)
20999 {
21000 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021003 out_flush();
21004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 else if (eap->cmdidx == CMD_echoerr)
21006 {
21007 /* We don't want to abort following commands, restore did_emsg. */
21008 save_did_emsg = did_emsg;
21009 EMSG((char_u *)ga.ga_data);
21010 if (!force_abort)
21011 did_emsg = save_did_emsg;
21012 }
21013 else if (eap->cmdidx == CMD_execute)
21014 do_cmdline((char_u *)ga.ga_data,
21015 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21016 }
21017
21018 ga_clear(&ga);
21019
21020 if (eap->skip)
21021 --emsg_skip;
21022
21023 eap->nextcmd = check_nextcmd(arg);
21024}
21025
21026/*
21027 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21028 * "arg" points to the "&" or '+' when called, to "option" when returning.
21029 * Returns NULL when no option name found. Otherwise pointer to the char
21030 * after the option name.
21031 */
21032 static char_u *
21033find_option_end(arg, opt_flags)
21034 char_u **arg;
21035 int *opt_flags;
21036{
21037 char_u *p = *arg;
21038
21039 ++p;
21040 if (*p == 'g' && p[1] == ':')
21041 {
21042 *opt_flags = OPT_GLOBAL;
21043 p += 2;
21044 }
21045 else if (*p == 'l' && p[1] == ':')
21046 {
21047 *opt_flags = OPT_LOCAL;
21048 p += 2;
21049 }
21050 else
21051 *opt_flags = 0;
21052
21053 if (!ASCII_ISALPHA(*p))
21054 return NULL;
21055 *arg = p;
21056
21057 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21058 p += 4; /* termcap option */
21059 else
21060 while (ASCII_ISALPHA(*p))
21061 ++p;
21062 return p;
21063}
21064
21065/*
21066 * ":function"
21067 */
21068 void
21069ex_function(eap)
21070 exarg_T *eap;
21071{
21072 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021073 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 int j;
21075 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 char_u *name = NULL;
21078 char_u *p;
21079 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021080 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 garray_T newargs;
21082 garray_T newlines;
21083 int varargs = FALSE;
21084 int mustend = FALSE;
21085 int flags = 0;
21086 ufunc_T *fp;
21087 int indent;
21088 int nesting;
21089 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021090 dictitem_T *v;
21091 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021092 static int func_nr = 0; /* number for nameless function */
21093 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021094 hashtab_T *ht;
21095 int todo;
21096 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021097 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098
21099 /*
21100 * ":function" without argument: list functions.
21101 */
21102 if (ends_excmd(*eap->arg))
21103 {
21104 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021105 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021106 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021107 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021108 {
21109 if (!HASHITEM_EMPTY(hi))
21110 {
21111 --todo;
21112 fp = HI2UF(hi);
21113 if (!isdigit(*fp->uf_name))
21114 list_func_head(fp, FALSE);
21115 }
21116 }
21117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 eap->nextcmd = check_nextcmd(eap->arg);
21119 return;
21120 }
21121
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021122 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021123 * ":function /pat": list functions matching pattern.
21124 */
21125 if (*eap->arg == '/')
21126 {
21127 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21128 if (!eap->skip)
21129 {
21130 regmatch_T regmatch;
21131
21132 c = *p;
21133 *p = NUL;
21134 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21135 *p = c;
21136 if (regmatch.regprog != NULL)
21137 {
21138 regmatch.rm_ic = p_ic;
21139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021140 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021141 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21142 {
21143 if (!HASHITEM_EMPTY(hi))
21144 {
21145 --todo;
21146 fp = HI2UF(hi);
21147 if (!isdigit(*fp->uf_name)
21148 && vim_regexec(&regmatch, fp->uf_name, 0))
21149 list_func_head(fp, FALSE);
21150 }
21151 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021152 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021153 }
21154 }
21155 if (*p == '/')
21156 ++p;
21157 eap->nextcmd = check_nextcmd(p);
21158 return;
21159 }
21160
21161 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021162 * Get the function name. There are these situations:
21163 * func normal function name
21164 * "name" == func, "fudi.fd_dict" == NULL
21165 * dict.func new dictionary entry
21166 * "name" == NULL, "fudi.fd_dict" set,
21167 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21168 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021169 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021170 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21171 * dict.func existing dict entry that's not a Funcref
21172 * "name" == NULL, "fudi.fd_dict" set,
21173 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021176 name = trans_function_name(&p, eap->skip, 0, &fudi);
21177 paren = (vim_strchr(p, '(') != NULL);
21178 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 {
21180 /*
21181 * Return on an invalid expression in braces, unless the expression
21182 * evaluation has been cancelled due to an aborting error, an
21183 * interrupt, or an exception.
21184 */
21185 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021186 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021187 if (!eap->skip && fudi.fd_newkey != NULL)
21188 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021189 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 else
21193 eap->skip = TRUE;
21194 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021195
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 /* An error in a function call during evaluation of an expression in magic
21197 * braces should not cause the function not to be defined. */
21198 saved_did_emsg = did_emsg;
21199 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200
21201 /*
21202 * ":function func" with only function name: list function.
21203 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021204 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 {
21206 if (!ends_excmd(*skipwhite(p)))
21207 {
21208 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021209 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 }
21211 eap->nextcmd = check_nextcmd(p);
21212 if (eap->nextcmd != NULL)
21213 *p = NUL;
21214 if (!eap->skip && !got_int)
21215 {
21216 fp = find_func(name);
21217 if (fp != NULL)
21218 {
21219 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021220 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021222 if (FUNCLINE(fp, j) == NULL)
21223 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 msg_putchar('\n');
21225 msg_outnum((long)(j + 1));
21226 if (j < 9)
21227 msg_putchar(' ');
21228 if (j < 99)
21229 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021230 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 out_flush(); /* show a line at a time */
21232 ui_breakcheck();
21233 }
21234 if (!got_int)
21235 {
21236 msg_putchar('\n');
21237 msg_puts((char_u *)" endfunction");
21238 }
21239 }
21240 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021241 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021243 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 }
21245
21246 /*
21247 * ":function name(arg1, arg2)" Define function.
21248 */
21249 p = skipwhite(p);
21250 if (*p != '(')
21251 {
21252 if (!eap->skip)
21253 {
21254 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021255 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 }
21257 /* attempt to continue by skipping some text */
21258 if (vim_strchr(p, '(') != NULL)
21259 p = vim_strchr(p, '(');
21260 }
21261 p = skipwhite(p + 1);
21262
21263 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21264 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21265
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021266 if (!eap->skip)
21267 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021268 /* Check the name of the function. Unless it's a dictionary function
21269 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021270 if (name != NULL)
21271 arg = name;
21272 else
21273 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021274 if (arg != NULL && (fudi.fd_di == NULL
21275 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021276 {
21277 if (*arg == K_SPECIAL)
21278 j = 3;
21279 else
21280 j = 0;
21281 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21282 : eval_isnamec(arg[j])))
21283 ++j;
21284 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021285 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021286 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021287 /* Disallow using the g: dict. */
21288 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21289 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021290 }
21291
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 /*
21293 * Isolate the arguments: "arg1, arg2, ...)"
21294 */
21295 while (*p != ')')
21296 {
21297 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21298 {
21299 varargs = TRUE;
21300 p += 3;
21301 mustend = TRUE;
21302 }
21303 else
21304 {
21305 arg = p;
21306 while (ASCII_ISALNUM(*p) || *p == '_')
21307 ++p;
21308 if (arg == p || isdigit(*arg)
21309 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21310 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21311 {
21312 if (!eap->skip)
21313 EMSG2(_("E125: Illegal argument: %s"), arg);
21314 break;
21315 }
21316 if (ga_grow(&newargs, 1) == FAIL)
21317 goto erret;
21318 c = *p;
21319 *p = NUL;
21320 arg = vim_strsave(arg);
21321 if (arg == NULL)
21322 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021323
21324 /* Check for duplicate argument name. */
21325 for (i = 0; i < newargs.ga_len; ++i)
21326 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21327 {
21328 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21329 goto erret;
21330 }
21331
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21333 *p = c;
21334 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 if (*p == ',')
21336 ++p;
21337 else
21338 mustend = TRUE;
21339 }
21340 p = skipwhite(p);
21341 if (mustend && *p != ')')
21342 {
21343 if (!eap->skip)
21344 EMSG2(_(e_invarg2), eap->arg);
21345 break;
21346 }
21347 }
21348 ++p; /* skip the ')' */
21349
Bram Moolenaare9a41262005-01-15 22:18:47 +000021350 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 for (;;)
21352 {
21353 p = skipwhite(p);
21354 if (STRNCMP(p, "range", 5) == 0)
21355 {
21356 flags |= FC_RANGE;
21357 p += 5;
21358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021359 else if (STRNCMP(p, "dict", 4) == 0)
21360 {
21361 flags |= FC_DICT;
21362 p += 4;
21363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 else if (STRNCMP(p, "abort", 5) == 0)
21365 {
21366 flags |= FC_ABORT;
21367 p += 5;
21368 }
21369 else
21370 break;
21371 }
21372
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021373 /* When there is a line break use what follows for the function body.
21374 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21375 if (*p == '\n')
21376 line_arg = p + 1;
21377 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 EMSG(_(e_trailing));
21379
21380 /*
21381 * Read the body of the function, until ":endfunction" is found.
21382 */
21383 if (KeyTyped)
21384 {
21385 /* Check if the function already exists, don't let the user type the
21386 * whole function before telling him it doesn't work! For a script we
21387 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021388 if (!eap->skip && !eap->forceit)
21389 {
21390 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21391 EMSG(_(e_funcdict));
21392 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021393 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021396 if (!eap->skip && did_emsg)
21397 goto erret;
21398
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 msg_putchar('\n'); /* don't overwrite the function name */
21400 cmdline_row = msg_row;
21401 }
21402
21403 indent = 2;
21404 nesting = 0;
21405 for (;;)
21406 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021407 if (KeyTyped)
21408 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021410 sourcing_lnum_off = sourcing_lnum;
21411
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021412 if (line_arg != NULL)
21413 {
21414 /* Use eap->arg, split up in parts by line breaks. */
21415 theline = line_arg;
21416 p = vim_strchr(theline, '\n');
21417 if (p == NULL)
21418 line_arg += STRLEN(line_arg);
21419 else
21420 {
21421 *p = NUL;
21422 line_arg = p + 1;
21423 }
21424 }
21425 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 theline = getcmdline(':', 0L, indent);
21427 else
21428 theline = eap->getline(':', eap->cookie, indent);
21429 if (KeyTyped)
21430 lines_left = Rows - 1;
21431 if (theline == NULL)
21432 {
21433 EMSG(_("E126: Missing :endfunction"));
21434 goto erret;
21435 }
21436
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021437 /* Detect line continuation: sourcing_lnum increased more than one. */
21438 if (sourcing_lnum > sourcing_lnum_off + 1)
21439 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21440 else
21441 sourcing_lnum_off = 0;
21442
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 if (skip_until != NULL)
21444 {
21445 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21446 * don't check for ":endfunc". */
21447 if (STRCMP(theline, skip_until) == 0)
21448 {
21449 vim_free(skip_until);
21450 skip_until = NULL;
21451 }
21452 }
21453 else
21454 {
21455 /* skip ':' and blanks*/
21456 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21457 ;
21458
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021459 /* Check for "endfunction". */
21460 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021462 if (line_arg == NULL)
21463 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 break;
21465 }
21466
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021467 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 * at "end". */
21469 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21470 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021471 else if (STRNCMP(p, "if", 2) == 0
21472 || STRNCMP(p, "wh", 2) == 0
21473 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 || STRNCMP(p, "try", 3) == 0)
21475 indent += 2;
21476
21477 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021478 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021480 if (*p == '!')
21481 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 p += eval_fname_script(p);
21483 if (ASCII_ISALPHA(*p))
21484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021485 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 if (*skipwhite(p) == '(')
21487 {
21488 ++nesting;
21489 indent += 2;
21490 }
21491 }
21492 }
21493
21494 /* Check for ":append" or ":insert". */
21495 p = skip_range(p, NULL);
21496 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21497 || (p[0] == 'i'
21498 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21499 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21500 skip_until = vim_strsave((char_u *)".");
21501
21502 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21503 arg = skipwhite(skiptowhite(p));
21504 if (arg[0] == '<' && arg[1] =='<'
21505 && ((p[0] == 'p' && p[1] == 'y'
21506 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21507 || (p[0] == 'p' && p[1] == 'e'
21508 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21509 || (p[0] == 't' && p[1] == 'c'
21510 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021511 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21512 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21514 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021515 || (p[0] == 'm' && p[1] == 'z'
21516 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 ))
21518 {
21519 /* ":python <<" continues until a dot, like ":append" */
21520 p = skipwhite(arg + 2);
21521 if (*p == NUL)
21522 skip_until = vim_strsave((char_u *)".");
21523 else
21524 skip_until = vim_strsave(p);
21525 }
21526 }
21527
21528 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021529 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021530 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021531 if (line_arg == NULL)
21532 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021534 }
21535
21536 /* Copy the line to newly allocated memory. get_one_sourceline()
21537 * allocates 250 bytes per line, this saves 80% on average. The cost
21538 * is an extra alloc/free. */
21539 p = vim_strsave(theline);
21540 if (p != NULL)
21541 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021542 if (line_arg == NULL)
21543 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021544 theline = p;
21545 }
21546
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021547 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21548
21549 /* Add NULL lines for continuation lines, so that the line count is
21550 * equal to the index in the growarray. */
21551 while (sourcing_lnum_off-- > 0)
21552 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021553
21554 /* Check for end of eap->arg. */
21555 if (line_arg != NULL && *line_arg == NUL)
21556 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 }
21558
21559 /* Don't define the function when skipping commands or when an error was
21560 * detected. */
21561 if (eap->skip || did_emsg)
21562 goto erret;
21563
21564 /*
21565 * If there are no errors, add the function
21566 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021567 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021568 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021569 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021570 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021571 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021572 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021573 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021574 goto erret;
21575 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021577 fp = find_func(name);
21578 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580 if (!eap->forceit)
21581 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021582 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021583 goto erret;
21584 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021585 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021586 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021587 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021588 name);
21589 goto erret;
21590 }
21591 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021592 ga_clear_strings(&(fp->uf_args));
21593 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021594 vim_free(name);
21595 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 }
21598 else
21599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021600 char numbuf[20];
21601
21602 fp = NULL;
21603 if (fudi.fd_newkey == NULL && !eap->forceit)
21604 {
21605 EMSG(_(e_funcdict));
21606 goto erret;
21607 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021608 if (fudi.fd_di == NULL)
21609 {
21610 /* Can't add a function to a locked dictionary */
21611 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21612 goto erret;
21613 }
21614 /* Can't change an existing function if it is locked */
21615 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21616 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021617
21618 /* Give the function a sequential number. Can only be used with a
21619 * Funcref! */
21620 vim_free(name);
21621 sprintf(numbuf, "%d", ++func_nr);
21622 name = vim_strsave((char_u *)numbuf);
21623 if (name == NULL)
21624 goto erret;
21625 }
21626
21627 if (fp == NULL)
21628 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021629 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021630 {
21631 int slen, plen;
21632 char_u *scriptname;
21633
21634 /* Check that the autoload name matches the script name. */
21635 j = FAIL;
21636 if (sourcing_name != NULL)
21637 {
21638 scriptname = autoload_name(name);
21639 if (scriptname != NULL)
21640 {
21641 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021642 plen = (int)STRLEN(p);
21643 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021644 if (slen > plen && fnamecmp(p,
21645 sourcing_name + slen - plen) == 0)
21646 j = OK;
21647 vim_free(scriptname);
21648 }
21649 }
21650 if (j == FAIL)
21651 {
21652 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21653 goto erret;
21654 }
21655 }
21656
21657 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 if (fp == NULL)
21659 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021660
21661 if (fudi.fd_dict != NULL)
21662 {
21663 if (fudi.fd_di == NULL)
21664 {
21665 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021666 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021667 if (fudi.fd_di == NULL)
21668 {
21669 vim_free(fp);
21670 goto erret;
21671 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021672 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21673 {
21674 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021675 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021676 goto erret;
21677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021678 }
21679 else
21680 /* overwrite existing dict entry */
21681 clear_tv(&fudi.fd_di->di_tv);
21682 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021683 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021684 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021685 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021686
21687 /* behave like "dict" was used */
21688 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021689 }
21690
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021692 STRCPY(fp->uf_name, name);
21693 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021695 fp->uf_args = newargs;
21696 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021697#ifdef FEAT_PROFILE
21698 fp->uf_tml_count = NULL;
21699 fp->uf_tml_total = NULL;
21700 fp->uf_tml_self = NULL;
21701 fp->uf_profiling = FALSE;
21702 if (prof_def_func())
21703 func_do_profile(fp);
21704#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021705 fp->uf_varargs = varargs;
21706 fp->uf_flags = flags;
21707 fp->uf_calls = 0;
21708 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021709 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710
21711erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 ga_clear_strings(&newargs);
21713 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021714ret_free:
21715 vim_free(skip_until);
21716 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719}
21720
21721/*
21722 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021723 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021725 * flags:
21726 * TFN_INT: internal function name OK
21727 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 * Advances "pp" to just after the function name (if no error).
21729 */
21730 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021731trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 char_u **pp;
21733 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021734 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021735 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021737 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 char_u *start;
21739 char_u *end;
21740 int lead;
21741 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021744
21745 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021748
21749 /* Check for hard coded <SNR>: already translated function ID (from a user
21750 * command). */
21751 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21752 && (*pp)[2] == (int)KE_SNR)
21753 {
21754 *pp += 3;
21755 len = get_id_len(pp) + 3;
21756 return vim_strnsave(start, len);
21757 }
21758
21759 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21760 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021762 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021764
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021765 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21766 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021767 if (end == start)
21768 {
21769 if (!skip)
21770 EMSG(_("E129: Function name required"));
21771 goto theend;
21772 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021773 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021774 {
21775 /*
21776 * Report an invalid expression in braces, unless the expression
21777 * evaluation has been cancelled due to an aborting error, an
21778 * interrupt, or an exception.
21779 */
21780 if (!aborting())
21781 {
21782 if (end != NULL)
21783 EMSG2(_(e_invarg2), start);
21784 }
21785 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021786 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021787 goto theend;
21788 }
21789
21790 if (lv.ll_tv != NULL)
21791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021792 if (fdp != NULL)
21793 {
21794 fdp->fd_dict = lv.ll_dict;
21795 fdp->fd_newkey = lv.ll_newkey;
21796 lv.ll_newkey = NULL;
21797 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021799 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21800 {
21801 name = vim_strsave(lv.ll_tv->vval.v_string);
21802 *pp = end;
21803 }
21804 else
21805 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021806 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21807 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021808 EMSG(_(e_funcref));
21809 else
21810 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021811 name = NULL;
21812 }
21813 goto theend;
21814 }
21815
21816 if (lv.ll_name == NULL)
21817 {
21818 /* Error found, but continue after the function name. */
21819 *pp = end;
21820 goto theend;
21821 }
21822
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021823 /* Check if the name is a Funcref. If so, use the value. */
21824 if (lv.ll_exp_name != NULL)
21825 {
21826 len = (int)STRLEN(lv.ll_exp_name);
21827 name = deref_func_name(lv.ll_exp_name, &len);
21828 if (name == lv.ll_exp_name)
21829 name = NULL;
21830 }
21831 else
21832 {
21833 len = (int)(end - *pp);
21834 name = deref_func_name(*pp, &len);
21835 if (name == *pp)
21836 name = NULL;
21837 }
21838 if (name != NULL)
21839 {
21840 name = vim_strsave(name);
21841 *pp = end;
21842 goto theend;
21843 }
21844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021845 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021846 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021847 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021848 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21849 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21850 {
21851 /* When there was "s:" already or the name expanded to get a
21852 * leading "s:" then remove it. */
21853 lv.ll_name += 2;
21854 len -= 2;
21855 lead = 2;
21856 }
21857 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021858 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021859 {
21860 if (lead == 2) /* skip over "s:" */
21861 lv.ll_name += 2;
21862 len = (int)(end - lv.ll_name);
21863 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021864
21865 /*
21866 * Copy the function name to allocated memory.
21867 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21868 * Accept <SNR>123_name() outside a script.
21869 */
21870 if (skip)
21871 lead = 0; /* do nothing */
21872 else if (lead > 0)
21873 {
21874 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021875 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21876 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021877 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021878 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021879 if (current_SID <= 0)
21880 {
21881 EMSG(_(e_usingsid));
21882 goto theend;
21883 }
21884 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21885 lead += (int)STRLEN(sid_buf);
21886 }
21887 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021888 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021889 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021890 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021891 goto theend;
21892 }
21893 name = alloc((unsigned)(len + lead + 1));
21894 if (name != NULL)
21895 {
21896 if (lead > 0)
21897 {
21898 name[0] = K_SPECIAL;
21899 name[1] = KS_EXTRA;
21900 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021901 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021902 STRCPY(name + 3, sid_buf);
21903 }
21904 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21905 name[len + lead] = NUL;
21906 }
21907 *pp = end;
21908
21909theend:
21910 clear_lval(&lv);
21911 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912}
21913
21914/*
21915 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21916 * Return 2 if "p" starts with "s:".
21917 * Return 0 otherwise.
21918 */
21919 static int
21920eval_fname_script(p)
21921 char_u *p;
21922{
21923 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21924 || STRNICMP(p + 1, "SNR>", 4) == 0))
21925 return 5;
21926 if (p[0] == 's' && p[1] == ':')
21927 return 2;
21928 return 0;
21929}
21930
21931/*
21932 * Return TRUE if "p" starts with "<SID>" or "s:".
21933 * Only works if eval_fname_script() returned non-zero for "p"!
21934 */
21935 static int
21936eval_fname_sid(p)
21937 char_u *p;
21938{
21939 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21940}
21941
21942/*
21943 * List the head of the function: "name(arg1, arg2)".
21944 */
21945 static void
21946list_func_head(fp, indent)
21947 ufunc_T *fp;
21948 int indent;
21949{
21950 int j;
21951
21952 msg_start();
21953 if (indent)
21954 MSG_PUTS(" ");
21955 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021956 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 {
21958 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021959 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 }
21961 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021962 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021964 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965 {
21966 if (j)
21967 MSG_PUTS(", ");
21968 msg_puts(FUNCARG(fp, j));
21969 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021970 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 {
21972 if (j)
21973 MSG_PUTS(", ");
21974 MSG_PUTS("...");
21975 }
21976 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020021977 if (fp->uf_flags & FC_ABORT)
21978 MSG_PUTS(" abort");
21979 if (fp->uf_flags & FC_RANGE)
21980 MSG_PUTS(" range");
21981 if (fp->uf_flags & FC_DICT)
21982 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021983 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021984 if (p_verbose > 0)
21985 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986}
21987
21988/*
21989 * Find a function by name, return pointer to it in ufuncs.
21990 * Return NULL for unknown function.
21991 */
21992 static ufunc_T *
21993find_func(name)
21994 char_u *name;
21995{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021996 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021998 hi = hash_find(&func_hashtab, name);
21999 if (!HASHITEM_EMPTY(hi))
22000 return HI2UF(hi);
22001 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002}
22003
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022004#if defined(EXITFREE) || defined(PROTO)
22005 void
22006free_all_functions()
22007{
22008 hashitem_T *hi;
22009
22010 /* Need to start all over every time, because func_free() may change the
22011 * hash table. */
22012 while (func_hashtab.ht_used > 0)
22013 for (hi = func_hashtab.ht_array; ; ++hi)
22014 if (!HASHITEM_EMPTY(hi))
22015 {
22016 func_free(HI2UF(hi));
22017 break;
22018 }
22019}
22020#endif
22021
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022022 int
22023translated_function_exists(name)
22024 char_u *name;
22025{
22026 if (builtin_function(name))
22027 return find_internal_func(name) >= 0;
22028 return find_func(name) != NULL;
22029}
22030
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031/*
22032 * Return TRUE if a function "name" exists.
22033 */
22034 static int
22035function_exists(name)
22036 char_u *name;
22037{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022038 char_u *nm = name;
22039 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022040 int n = FALSE;
22041
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022042 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022043 nm = skipwhite(nm);
22044
22045 /* Only accept "funcname", "funcname ", "funcname (..." and
22046 * "funcname(...", not "funcname!...". */
22047 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022048 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022049 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022050 return n;
22051}
22052
Bram Moolenaara1544c02013-05-30 12:35:52 +020022053 char_u *
22054get_expanded_name(name, check)
22055 char_u *name;
22056 int check;
22057{
22058 char_u *nm = name;
22059 char_u *p;
22060
22061 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22062
22063 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022064 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022065 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022066
Bram Moolenaara1544c02013-05-30 12:35:52 +020022067 vim_free(p);
22068 return NULL;
22069}
22070
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022071/*
22072 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022073 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022074 */
22075 static int
22076builtin_function(name)
22077 char_u *name;
22078{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022079 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22080 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022081}
22082
Bram Moolenaar05159a02005-02-26 23:04:13 +000022083#if defined(FEAT_PROFILE) || defined(PROTO)
22084/*
22085 * Start profiling function "fp".
22086 */
22087 static void
22088func_do_profile(fp)
22089 ufunc_T *fp;
22090{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022091 int len = fp->uf_lines.ga_len;
22092
22093 if (len == 0)
22094 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022095 fp->uf_tm_count = 0;
22096 profile_zero(&fp->uf_tm_self);
22097 profile_zero(&fp->uf_tm_total);
22098 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022099 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022100 if (fp->uf_tml_total == NULL)
22101 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022102 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022103 if (fp->uf_tml_self == NULL)
22104 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022105 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022106 fp->uf_tml_idx = -1;
22107 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22108 || fp->uf_tml_self == NULL)
22109 return; /* out of memory */
22110
22111 fp->uf_profiling = TRUE;
22112}
22113
22114/*
22115 * Dump the profiling results for all functions in file "fd".
22116 */
22117 void
22118func_dump_profile(fd)
22119 FILE *fd;
22120{
22121 hashitem_T *hi;
22122 int todo;
22123 ufunc_T *fp;
22124 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022125 ufunc_T **sorttab;
22126 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022128 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022129 if (todo == 0)
22130 return; /* nothing to dump */
22131
Bram Moolenaar73830342005-02-28 22:48:19 +000022132 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22133
Bram Moolenaar05159a02005-02-26 23:04:13 +000022134 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22135 {
22136 if (!HASHITEM_EMPTY(hi))
22137 {
22138 --todo;
22139 fp = HI2UF(hi);
22140 if (fp->uf_profiling)
22141 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022142 if (sorttab != NULL)
22143 sorttab[st_len++] = fp;
22144
Bram Moolenaar05159a02005-02-26 23:04:13 +000022145 if (fp->uf_name[0] == K_SPECIAL)
22146 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22147 else
22148 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22149 if (fp->uf_tm_count == 1)
22150 fprintf(fd, "Called 1 time\n");
22151 else
22152 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22153 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22154 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22155 fprintf(fd, "\n");
22156 fprintf(fd, "count total (s) self (s)\n");
22157
22158 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22159 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022160 if (FUNCLINE(fp, i) == NULL)
22161 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022162 prof_func_line(fd, fp->uf_tml_count[i],
22163 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022164 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22165 }
22166 fprintf(fd, "\n");
22167 }
22168 }
22169 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022170
22171 if (sorttab != NULL && st_len > 0)
22172 {
22173 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22174 prof_total_cmp);
22175 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22176 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22177 prof_self_cmp);
22178 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22179 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022180
22181 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022182}
Bram Moolenaar73830342005-02-28 22:48:19 +000022183
22184 static void
22185prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22186 FILE *fd;
22187 ufunc_T **sorttab;
22188 int st_len;
22189 char *title;
22190 int prefer_self; /* when equal print only self time */
22191{
22192 int i;
22193 ufunc_T *fp;
22194
22195 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22196 fprintf(fd, "count total (s) self (s) function\n");
22197 for (i = 0; i < 20 && i < st_len; ++i)
22198 {
22199 fp = sorttab[i];
22200 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22201 prefer_self);
22202 if (fp->uf_name[0] == K_SPECIAL)
22203 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22204 else
22205 fprintf(fd, " %s()\n", fp->uf_name);
22206 }
22207 fprintf(fd, "\n");
22208}
22209
22210/*
22211 * Print the count and times for one function or function line.
22212 */
22213 static void
22214prof_func_line(fd, count, total, self, prefer_self)
22215 FILE *fd;
22216 int count;
22217 proftime_T *total;
22218 proftime_T *self;
22219 int prefer_self; /* when equal print only self time */
22220{
22221 if (count > 0)
22222 {
22223 fprintf(fd, "%5d ", count);
22224 if (prefer_self && profile_equal(total, self))
22225 fprintf(fd, " ");
22226 else
22227 fprintf(fd, "%s ", profile_msg(total));
22228 if (!prefer_self && profile_equal(total, self))
22229 fprintf(fd, " ");
22230 else
22231 fprintf(fd, "%s ", profile_msg(self));
22232 }
22233 else
22234 fprintf(fd, " ");
22235}
22236
22237/*
22238 * Compare function for total time sorting.
22239 */
22240 static int
22241#ifdef __BORLANDC__
22242_RTLENTRYF
22243#endif
22244prof_total_cmp(s1, s2)
22245 const void *s1;
22246 const void *s2;
22247{
22248 ufunc_T *p1, *p2;
22249
22250 p1 = *(ufunc_T **)s1;
22251 p2 = *(ufunc_T **)s2;
22252 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22253}
22254
22255/*
22256 * Compare function for self time sorting.
22257 */
22258 static int
22259#ifdef __BORLANDC__
22260_RTLENTRYF
22261#endif
22262prof_self_cmp(s1, s2)
22263 const void *s1;
22264 const void *s2;
22265{
22266 ufunc_T *p1, *p2;
22267
22268 p1 = *(ufunc_T **)s1;
22269 p2 = *(ufunc_T **)s2;
22270 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22271}
22272
Bram Moolenaar05159a02005-02-26 23:04:13 +000022273#endif
22274
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022275/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022276 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022277 * Return TRUE if a package was loaded.
22278 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022279 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022280script_autoload(name, reload)
22281 char_u *name;
22282 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022283{
22284 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022285 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022286 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022287 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022288
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022289 /* Return quickly when autoload disabled. */
22290 if (no_autoload)
22291 return FALSE;
22292
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022293 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022294 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022295 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022296 return FALSE;
22297
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022298 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022299
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022300 /* Find the name in the list of previously loaded package names. Skip
22301 * "autoload/", it's always the same. */
22302 for (i = 0; i < ga_loaded.ga_len; ++i)
22303 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22304 break;
22305 if (!reload && i < ga_loaded.ga_len)
22306 ret = FALSE; /* was loaded already */
22307 else
22308 {
22309 /* Remember the name if it wasn't loaded already. */
22310 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22311 {
22312 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22313 tofree = NULL;
22314 }
22315
22316 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022317 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022318 ret = TRUE;
22319 }
22320
22321 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022322 return ret;
22323}
22324
22325/*
22326 * Return the autoload script name for a function or variable name.
22327 * Returns NULL when out of memory.
22328 */
22329 static char_u *
22330autoload_name(name)
22331 char_u *name;
22332{
22333 char_u *p;
22334 char_u *scriptname;
22335
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022336 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022337 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22338 if (scriptname == NULL)
22339 return FALSE;
22340 STRCPY(scriptname, "autoload/");
22341 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022342 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022343 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022344 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022345 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022346 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022347}
22348
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22350
22351/*
22352 * Function given to ExpandGeneric() to obtain the list of user defined
22353 * function names.
22354 */
22355 char_u *
22356get_user_func_name(xp, idx)
22357 expand_T *xp;
22358 int idx;
22359{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022360 static long_u done;
22361 static hashitem_T *hi;
22362 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363
22364 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022366 done = 0;
22367 hi = func_hashtab.ht_array;
22368 }
22369 if (done < func_hashtab.ht_used)
22370 {
22371 if (done++ > 0)
22372 ++hi;
22373 while (HASHITEM_EMPTY(hi))
22374 ++hi;
22375 fp = HI2UF(hi);
22376
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022377 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022378 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022379
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022380 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22381 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382
22383 cat_func_name(IObuff, fp);
22384 if (xp->xp_context != EXPAND_USER_FUNC)
22385 {
22386 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022387 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 STRCAT(IObuff, ")");
22389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 return IObuff;
22391 }
22392 return NULL;
22393}
22394
22395#endif /* FEAT_CMDL_COMPL */
22396
22397/*
22398 * Copy the function name of "fp" to buffer "buf".
22399 * "buf" must be able to hold the function name plus three bytes.
22400 * Takes care of script-local function names.
22401 */
22402 static void
22403cat_func_name(buf, fp)
22404 char_u *buf;
22405 ufunc_T *fp;
22406{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022407 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 {
22409 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022410 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 }
22412 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022413 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414}
22415
22416/*
22417 * ":delfunction {name}"
22418 */
22419 void
22420ex_delfunction(eap)
22421 exarg_T *eap;
22422{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 char_u *p;
22425 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427
22428 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022429 name = trans_function_name(&p, eap->skip, 0, &fudi);
22430 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022432 {
22433 if (fudi.fd_dict != NULL && !eap->skip)
22434 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 if (!ends_excmd(*skipwhite(p)))
22438 {
22439 vim_free(name);
22440 EMSG(_(e_trailing));
22441 return;
22442 }
22443 eap->nextcmd = check_nextcmd(p);
22444 if (eap->nextcmd != NULL)
22445 *p = NUL;
22446
22447 if (!eap->skip)
22448 fp = find_func(name);
22449 vim_free(name);
22450
22451 if (!eap->skip)
22452 {
22453 if (fp == NULL)
22454 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022455 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 return;
22457 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022458 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 {
22460 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22461 return;
22462 }
22463
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022464 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022466 /* Delete the dict item that refers to the function, it will
22467 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022468 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022470 else
22471 func_free(fp);
22472 }
22473}
22474
22475/*
22476 * Free a function and remove it from the list of functions.
22477 */
22478 static void
22479func_free(fp)
22480 ufunc_T *fp;
22481{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022482 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483
22484 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022485 ga_clear_strings(&(fp->uf_args));
22486 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022487#ifdef FEAT_PROFILE
22488 vim_free(fp->uf_tml_count);
22489 vim_free(fp->uf_tml_total);
22490 vim_free(fp->uf_tml_self);
22491#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022493 /* remove the function from the function hashtable */
22494 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22495 if (HASHITEM_EMPTY(hi))
22496 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022497 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022498 hash_remove(&func_hashtab, hi);
22499
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022500 vim_free(fp);
22501}
22502
22503/*
22504 * Unreference a Function: decrement the reference count and free it when it
22505 * becomes zero. Only for numbered functions.
22506 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022507 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022508func_unref(name)
22509 char_u *name;
22510{
22511 ufunc_T *fp;
22512
22513 if (name != NULL && isdigit(*name))
22514 {
22515 fp = find_func(name);
22516 if (fp == NULL)
22517 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022518 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022519 {
22520 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022521 * when "uf_calls" becomes zero. */
22522 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022523 func_free(fp);
22524 }
22525 }
22526}
22527
22528/*
22529 * Count a reference to a Function.
22530 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022531 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022532func_ref(name)
22533 char_u *name;
22534{
22535 ufunc_T *fp;
22536
22537 if (name != NULL && isdigit(*name))
22538 {
22539 fp = find_func(name);
22540 if (fp == NULL)
22541 EMSG2(_(e_intern2), "func_ref()");
22542 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022543 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 }
22545}
22546
22547/*
22548 * Call a user function.
22549 */
22550 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022551call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 ufunc_T *fp; /* pointer to function */
22553 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 typval_T *argvars; /* arguments */
22555 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 linenr_T firstline; /* first line of range */
22557 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559{
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 char_u *save_sourcing_name;
22561 linenr_T save_sourcing_lnum;
22562 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022563 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022564 int save_did_emsg;
22565 static int depth = 0;
22566 dictitem_T *v;
22567 int fixvar_idx = 0; /* index in fixvar[] */
22568 int i;
22569 int ai;
22570 char_u numbuf[NUMBUFLEN];
22571 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022572#ifdef FEAT_PROFILE
22573 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022574 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576
22577 /* If depth of calling is getting too high, don't execute the function */
22578 if (depth >= p_mfd)
22579 {
22580 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022581 rettv->v_type = VAR_NUMBER;
22582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 return;
22584 }
22585 ++depth;
22586
22587 line_breakcheck(); /* check for CTRL-C hit */
22588
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022589 fc = (funccall_T *)alloc(sizeof(funccall_T));
22590 fc->caller = current_funccal;
22591 current_funccal = fc;
22592 fc->func = fp;
22593 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022594 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022595 fc->linenr = 0;
22596 fc->returned = FALSE;
22597 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022599 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22600 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022603 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022604 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22605 * each argument variable and saves a lot of time.
22606 */
22607 /*
22608 * Init l: variables.
22609 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022610 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022611 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022612 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022613 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22614 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022615 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022616 name = v->di_key;
22617 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022618 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022619 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022621 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022622 v->di_tv.vval.v_dict = selfdict;
22623 ++selfdict->dv_refcount;
22624 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022625
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 /*
22627 * Init a: variables.
22628 * Set a:0 to "argcount".
22629 * Set a:000 to a list with room for the "..." arguments.
22630 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022631 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022632 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022633 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022634 /* Use "name" to avoid a warning from some compiler that checks the
22635 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022636 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022637 name = v->di_key;
22638 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022639 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022640 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022641 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022642 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022643 v->di_tv.vval.v_list = &fc->l_varlist;
22644 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22645 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22646 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022647
22648 /*
22649 * Set a:firstline to "firstline" and a:lastline to "lastline".
22650 * Set a:name to named arguments.
22651 * Set a:N to the "..." arguments.
22652 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022653 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022654 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022655 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 (varnumber_T)lastline);
22657 for (i = 0; i < argcount; ++i)
22658 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022659 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 if (ai < 0)
22661 /* named argument a:name */
22662 name = FUNCARG(fp, i);
22663 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 /* "..." argument a:1, a:2, etc. */
22666 sprintf((char *)numbuf, "%d", ai + 1);
22667 name = numbuf;
22668 }
22669 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22670 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022671 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022672 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22673 }
22674 else
22675 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022676 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22677 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022678 if (v == NULL)
22679 break;
22680 v->di_flags = DI_FLAGS_RO;
22681 }
22682 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022683 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022684
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022685 /* Note: the values are copied directly to avoid alloc/free.
22686 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022687 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022688 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022689
22690 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22691 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022692 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22693 fc->l_listitems[ai].li_tv = argvars[i];
22694 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022695 }
22696 }
22697
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 /* Don't redraw while executing the function. */
22699 ++RedrawingDisabled;
22700 save_sourcing_name = sourcing_name;
22701 save_sourcing_lnum = sourcing_lnum;
22702 sourcing_lnum = 1;
22703 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022704 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 if (sourcing_name != NULL)
22706 {
22707 if (save_sourcing_name != NULL
22708 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22709 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22710 else
22711 STRCPY(sourcing_name, "function ");
22712 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22713
22714 if (p_verbose >= 12)
22715 {
22716 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022717 verbose_enter_scroll();
22718
Bram Moolenaar555b2802005-05-19 21:08:39 +000022719 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 if (p_verbose >= 14)
22721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022723 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022724 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022725 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726
22727 msg_puts((char_u *)"(");
22728 for (i = 0; i < argcount; ++i)
22729 {
22730 if (i > 0)
22731 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732 if (argvars[i].v_type == VAR_NUMBER)
22733 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 else
22735 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022736 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22737 if (s != NULL)
22738 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022739 if (vim_strsize(s) > MSG_BUF_CLEN)
22740 {
22741 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22742 s = buf;
22743 }
22744 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022745 vim_free(tofree);
22746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 }
22748 }
22749 msg_puts((char_u *)")");
22750 }
22751 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022752
22753 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 --no_wait_return;
22755 }
22756 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022757#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022758 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022759 {
22760 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22761 func_do_profile(fp);
22762 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022763 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022764 {
22765 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022766 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022767 profile_zero(&fp->uf_tm_children);
22768 }
22769 script_prof_save(&wait_start);
22770 }
22771#endif
22772
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022774 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 save_did_emsg = did_emsg;
22776 did_emsg = FALSE;
22777
22778 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022779 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22781
22782 --RedrawingDisabled;
22783
22784 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022785 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022787 clear_tv(rettv);
22788 rettv->v_type = VAR_NUMBER;
22789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 }
22791
Bram Moolenaar05159a02005-02-26 23:04:13 +000022792#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022793 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022794 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022795 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022796 profile_end(&call_start);
22797 profile_sub_wait(&wait_start, &call_start);
22798 profile_add(&fp->uf_tm_total, &call_start);
22799 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022800 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022801 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022802 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22803 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022804 }
22805 }
22806#endif
22807
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 /* when being verbose, mention the return value */
22809 if (p_verbose >= 12)
22810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022812 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022815 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022816 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022817 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022818 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022821 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022822 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022823 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022824 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022825
Bram Moolenaar555b2802005-05-19 21:08:39 +000022826 /* The value may be very long. Skip the middle part, so that we
22827 * have some idea how it starts and ends. smsg() would always
22828 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022829 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022830 if (s != NULL)
22831 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022832 if (vim_strsize(s) > MSG_BUF_CLEN)
22833 {
22834 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22835 s = buf;
22836 }
22837 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022838 vim_free(tofree);
22839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 }
22841 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022842
22843 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 --no_wait_return;
22845 }
22846
22847 vim_free(sourcing_name);
22848 sourcing_name = save_sourcing_name;
22849 sourcing_lnum = save_sourcing_lnum;
22850 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022851#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022852 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022853 script_prof_restore(&wait_start);
22854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855
22856 if (p_verbose >= 12 && sourcing_name != NULL)
22857 {
22858 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022859 verbose_enter_scroll();
22860
Bram Moolenaar555b2802005-05-19 21:08:39 +000022861 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022863
22864 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 --no_wait_return;
22866 }
22867
22868 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022869 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022871
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022872 /* If the a:000 list and the l: and a: dicts are not referenced we can
22873 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022874 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22875 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22876 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22877 {
22878 free_funccal(fc, FALSE);
22879 }
22880 else
22881 {
22882 hashitem_T *hi;
22883 listitem_T *li;
22884 int todo;
22885
22886 /* "fc" is still in use. This can happen when returning "a:000" or
22887 * assigning "l:" to a global variable.
22888 * Link "fc" in the list for garbage collection later. */
22889 fc->caller = previous_funccal;
22890 previous_funccal = fc;
22891
22892 /* Make a copy of the a: variables, since we didn't do that above. */
22893 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22894 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22895 {
22896 if (!HASHITEM_EMPTY(hi))
22897 {
22898 --todo;
22899 v = HI2DI(hi);
22900 copy_tv(&v->di_tv, &v->di_tv);
22901 }
22902 }
22903
22904 /* Make a copy of the a:000 items, since we didn't do that above. */
22905 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22906 copy_tv(&li->li_tv, &li->li_tv);
22907 }
22908}
22909
22910/*
22911 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022912 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022913 */
22914 static int
22915can_free_funccal(fc, copyID)
22916 funccall_T *fc;
22917 int copyID;
22918{
22919 return (fc->l_varlist.lv_copyID != copyID
22920 && fc->l_vars.dv_copyID != copyID
22921 && fc->l_avars.dv_copyID != copyID);
22922}
22923
22924/*
22925 * Free "fc" and what it contains.
22926 */
22927 static void
22928free_funccal(fc, free_val)
22929 funccall_T *fc;
22930 int free_val; /* a: vars were allocated */
22931{
22932 listitem_T *li;
22933
22934 /* The a: variables typevals may not have been allocated, only free the
22935 * allocated variables. */
22936 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22937
22938 /* free all l: variables */
22939 vars_clear(&fc->l_vars.dv_hashtab);
22940
22941 /* Free the a:000 variables if they were allocated. */
22942 if (free_val)
22943 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22944 clear_tv(&li->li_tv);
22945
22946 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947}
22948
22949/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 * Add a number variable "name" to dict "dp" with value "nr".
22951 */
22952 static void
22953add_nr_var(dp, v, name, nr)
22954 dict_T *dp;
22955 dictitem_T *v;
22956 char *name;
22957 varnumber_T nr;
22958{
22959 STRCPY(v->di_key, name);
22960 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22961 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22962 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022963 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 v->di_tv.vval.v_number = nr;
22965}
22966
22967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 * ":return [expr]"
22969 */
22970 void
22971ex_return(eap)
22972 exarg_T *eap;
22973{
22974 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022975 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 int returning = FALSE;
22977
22978 if (current_funccal == NULL)
22979 {
22980 EMSG(_("E133: :return not inside a function"));
22981 return;
22982 }
22983
22984 if (eap->skip)
22985 ++emsg_skip;
22986
22987 eap->nextcmd = NULL;
22988 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022989 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 {
22991 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022992 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022994 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 }
22996 /* It's safer to return also on error. */
22997 else if (!eap->skip)
22998 {
22999 /*
23000 * Return unless the expression evaluation has been cancelled due to an
23001 * aborting error, an interrupt, or an exception.
23002 */
23003 if (!aborting())
23004 returning = do_return(eap, FALSE, TRUE, NULL);
23005 }
23006
23007 /* When skipping or the return gets pending, advance to the next command
23008 * in this line (!returning). Otherwise, ignore the rest of the line.
23009 * Following lines will be ignored by get_func_line(). */
23010 if (returning)
23011 eap->nextcmd = NULL;
23012 else if (eap->nextcmd == NULL) /* no argument */
23013 eap->nextcmd = check_nextcmd(arg);
23014
23015 if (eap->skip)
23016 --emsg_skip;
23017}
23018
23019/*
23020 * Return from a function. Possibly makes the return pending. Also called
23021 * for a pending return at the ":endtry" or after returning from an extra
23022 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023023 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023024 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 * FALSE when the return gets pending.
23026 */
23027 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023028do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 exarg_T *eap;
23030 int reanimate;
23031 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023032 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033{
23034 int idx;
23035 struct condstack *cstack = eap->cstack;
23036
23037 if (reanimate)
23038 /* Undo the return. */
23039 current_funccal->returned = FALSE;
23040
23041 /*
23042 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23043 * not in its finally clause (which then is to be executed next) is found.
23044 * In this case, make the ":return" pending for execution at the ":endtry".
23045 * Otherwise, return normally.
23046 */
23047 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23048 if (idx >= 0)
23049 {
23050 cstack->cs_pending[idx] = CSTP_RETURN;
23051
23052 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023053 /* A pending return again gets pending. "rettv" points to an
23054 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023056 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 else
23058 {
23059 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023060 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023062 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023064 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 {
23066 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023067 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023068 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069 else
23070 EMSG(_(e_outofmem));
23071 }
23072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023073 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074
23075 if (reanimate)
23076 {
23077 /* The pending return value could be overwritten by a ":return"
23078 * without argument in a finally clause; reset the default
23079 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023080 current_funccal->rettv->v_type = VAR_NUMBER;
23081 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 }
23083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023084 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 }
23086 else
23087 {
23088 current_funccal->returned = TRUE;
23089
23090 /* If the return is carried out now, store the return value. For
23091 * a return immediately after reanimation, the value is already
23092 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023093 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023095 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023096 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023098 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 }
23100 }
23101
23102 return idx < 0;
23103}
23104
23105/*
23106 * Free the variable with a pending return value.
23107 */
23108 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023109discard_pending_return(rettv)
23110 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111{
Bram Moolenaar33570922005-01-25 22:26:29 +000023112 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113}
23114
23115/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023116 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 * is an allocated string. Used by report_pending() for verbose messages.
23118 */
23119 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023120get_return_cmd(rettv)
23121 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023123 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023124 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023125 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023127 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023128 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023129 if (s == NULL)
23130 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023131
23132 STRCPY(IObuff, ":return ");
23133 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23134 if (STRLEN(s) + 8 >= IOSIZE)
23135 STRCPY(IObuff + IOSIZE - 4, "...");
23136 vim_free(tofree);
23137 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138}
23139
23140/*
23141 * Get next function line.
23142 * Called by do_cmdline() to get the next line.
23143 * Returns allocated string, or NULL for end of function.
23144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 char_u *
23146get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023147 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023149 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150{
Bram Moolenaar33570922005-01-25 22:26:29 +000023151 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023152 ufunc_T *fp = fcp->func;
23153 char_u *retval;
23154 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155
23156 /* If breakpoints have been added/deleted need to check for it. */
23157 if (fcp->dbg_tick != debug_tick)
23158 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023159 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 sourcing_lnum);
23161 fcp->dbg_tick = debug_tick;
23162 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023163#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023164 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023165 func_line_end(cookie);
23166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167
Bram Moolenaar05159a02005-02-26 23:04:13 +000023168 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023169 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23170 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 retval = NULL;
23172 else
23173 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023174 /* Skip NULL lines (continuation lines). */
23175 while (fcp->linenr < gap->ga_len
23176 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23177 ++fcp->linenr;
23178 if (fcp->linenr >= gap->ga_len)
23179 retval = NULL;
23180 else
23181 {
23182 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23183 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023184#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023185 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023186 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023187#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 }
23190
23191 /* Did we encounter a breakpoint? */
23192 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23193 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023194 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023196 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 sourcing_lnum);
23198 fcp->dbg_tick = debug_tick;
23199 }
23200
23201 return retval;
23202}
23203
Bram Moolenaar05159a02005-02-26 23:04:13 +000023204#if defined(FEAT_PROFILE) || defined(PROTO)
23205/*
23206 * Called when starting to read a function line.
23207 * "sourcing_lnum" must be correct!
23208 * When skipping lines it may not actually be executed, but we won't find out
23209 * until later and we need to store the time now.
23210 */
23211 void
23212func_line_start(cookie)
23213 void *cookie;
23214{
23215 funccall_T *fcp = (funccall_T *)cookie;
23216 ufunc_T *fp = fcp->func;
23217
23218 if (fp->uf_profiling && sourcing_lnum >= 1
23219 && sourcing_lnum <= fp->uf_lines.ga_len)
23220 {
23221 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023222 /* Skip continuation lines. */
23223 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23224 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023225 fp->uf_tml_execed = FALSE;
23226 profile_start(&fp->uf_tml_start);
23227 profile_zero(&fp->uf_tml_children);
23228 profile_get_wait(&fp->uf_tml_wait);
23229 }
23230}
23231
23232/*
23233 * Called when actually executing a function line.
23234 */
23235 void
23236func_line_exec(cookie)
23237 void *cookie;
23238{
23239 funccall_T *fcp = (funccall_T *)cookie;
23240 ufunc_T *fp = fcp->func;
23241
23242 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23243 fp->uf_tml_execed = TRUE;
23244}
23245
23246/*
23247 * Called when done with a function line.
23248 */
23249 void
23250func_line_end(cookie)
23251 void *cookie;
23252{
23253 funccall_T *fcp = (funccall_T *)cookie;
23254 ufunc_T *fp = fcp->func;
23255
23256 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23257 {
23258 if (fp->uf_tml_execed)
23259 {
23260 ++fp->uf_tml_count[fp->uf_tml_idx];
23261 profile_end(&fp->uf_tml_start);
23262 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023263 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023264 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23265 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023266 }
23267 fp->uf_tml_idx = -1;
23268 }
23269}
23270#endif
23271
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272/*
23273 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023274 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 */
23276 int
23277func_has_ended(cookie)
23278 void *cookie;
23279{
Bram Moolenaar33570922005-01-25 22:26:29 +000023280 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281
23282 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23283 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023284 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 || fcp->returned);
23286}
23287
23288/*
23289 * return TRUE if cookie indicates a function which "abort"s on errors.
23290 */
23291 int
23292func_has_abort(cookie)
23293 void *cookie;
23294{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023295 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296}
23297
23298#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23299typedef enum
23300{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023301 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23302 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23303 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304} var_flavour_T;
23305
23306static var_flavour_T var_flavour __ARGS((char_u *varname));
23307
23308 static var_flavour_T
23309var_flavour(varname)
23310 char_u *varname;
23311{
23312 char_u *p = varname;
23313
23314 if (ASCII_ISUPPER(*p))
23315 {
23316 while (*(++p))
23317 if (ASCII_ISLOWER(*p))
23318 return VAR_FLAVOUR_SESSION;
23319 return VAR_FLAVOUR_VIMINFO;
23320 }
23321 else
23322 return VAR_FLAVOUR_DEFAULT;
23323}
23324#endif
23325
23326#if defined(FEAT_VIMINFO) || defined(PROTO)
23327/*
23328 * Restore global vars that start with a capital from the viminfo file
23329 */
23330 int
23331read_viminfo_varlist(virp, writing)
23332 vir_T *virp;
23333 int writing;
23334{
23335 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023336 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338
23339 if (!writing && (find_viminfo_parameter('!') != NULL))
23340 {
23341 tab = vim_strchr(virp->vir_line + 1, '\t');
23342 if (tab != NULL)
23343 {
23344 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023345 switch (*tab)
23346 {
23347 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023348#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023349 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023350#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023351 case 'D': type = VAR_DICT; break;
23352 case 'L': type = VAR_LIST; break;
23353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354
23355 tab = vim_strchr(tab, '\t');
23356 if (tab != NULL)
23357 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023358 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023359 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023360 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023362#ifdef FEAT_FLOAT
23363 else if (type == VAR_FLOAT)
23364 (void)string2float(tab + 1, &tv.vval.v_float);
23365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023367 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023368 if (type == VAR_DICT || type == VAR_LIST)
23369 {
23370 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23371
23372 if (etv == NULL)
23373 /* Failed to parse back the dict or list, use it as a
23374 * string. */
23375 tv.v_type = VAR_STRING;
23376 else
23377 {
23378 vim_free(tv.vval.v_string);
23379 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023380 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023381 }
23382 }
23383
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023384 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023385
23386 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023387 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023388 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23389 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 }
23391 }
23392 }
23393
23394 return viminfo_readline(virp);
23395}
23396
23397/*
23398 * Write global vars that start with a capital to the viminfo file
23399 */
23400 void
23401write_viminfo_varlist(fp)
23402 FILE *fp;
23403{
Bram Moolenaar33570922005-01-25 22:26:29 +000023404 hashitem_T *hi;
23405 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023406 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023407 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023408 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023409 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023410 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411
23412 if (find_viminfo_parameter('!') == NULL)
23413 return;
23414
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023415 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023417 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023418 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023420 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023422 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 this_var = HI2DI(hi);
23424 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023426 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023427 {
23428 case VAR_STRING: s = "STR"; break;
23429 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023430#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023431 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023432#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023433 case VAR_DICT: s = "DIC"; break;
23434 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023435 default: continue;
23436 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023437 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023438 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023439 if (p != NULL)
23440 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023441 vim_free(tofree);
23442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 }
23444 }
23445}
23446#endif
23447
23448#if defined(FEAT_SESSION) || defined(PROTO)
23449 int
23450store_session_globals(fd)
23451 FILE *fd;
23452{
Bram Moolenaar33570922005-01-25 22:26:29 +000023453 hashitem_T *hi;
23454 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023455 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 char_u *p, *t;
23457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023458 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023459 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023461 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023463 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023464 this_var = HI2DI(hi);
23465 if ((this_var->di_tv.v_type == VAR_NUMBER
23466 || this_var->di_tv.v_type == VAR_STRING)
23467 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023468 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023469 /* Escape special characters with a backslash. Turn a LF and
23470 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023472 (char_u *)"\\\"\n\r");
23473 if (p == NULL) /* out of memory */
23474 break;
23475 for (t = p; *t != NUL; ++t)
23476 if (*t == '\n')
23477 *t = 'n';
23478 else if (*t == '\r')
23479 *t = 'r';
23480 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023481 this_var->di_key,
23482 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23483 : ' ',
23484 p,
23485 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23486 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023487 || put_eol(fd) == FAIL)
23488 {
23489 vim_free(p);
23490 return FAIL;
23491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 vim_free(p);
23493 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023494#ifdef FEAT_FLOAT
23495 else if (this_var->di_tv.v_type == VAR_FLOAT
23496 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23497 {
23498 float_T f = this_var->di_tv.vval.v_float;
23499 int sign = ' ';
23500
23501 if (f < 0)
23502 {
23503 f = -f;
23504 sign = '-';
23505 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023506 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023507 this_var->di_key, sign, f) < 0)
23508 || put_eol(fd) == FAIL)
23509 return FAIL;
23510 }
23511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 }
23513 }
23514 return OK;
23515}
23516#endif
23517
Bram Moolenaar661b1822005-07-28 22:36:45 +000023518/*
23519 * Display script name where an item was last set.
23520 * Should only be invoked when 'verbose' is non-zero.
23521 */
23522 void
23523last_set_msg(scriptID)
23524 scid_T scriptID;
23525{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023526 char_u *p;
23527
Bram Moolenaar661b1822005-07-28 22:36:45 +000023528 if (scriptID != 0)
23529 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023530 p = home_replace_save(NULL, get_scriptname(scriptID));
23531 if (p != NULL)
23532 {
23533 verbose_enter();
23534 MSG_PUTS(_("\n\tLast set from "));
23535 MSG_PUTS(p);
23536 vim_free(p);
23537 verbose_leave();
23538 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023539 }
23540}
23541
Bram Moolenaard812df62008-11-09 12:46:09 +000023542/*
23543 * List v:oldfiles in a nice way.
23544 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023545 void
23546ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023547 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023548{
23549 list_T *l = vimvars[VV_OLDFILES].vv_list;
23550 listitem_T *li;
23551 int nr = 0;
23552
23553 if (l == NULL)
23554 msg((char_u *)_("No old files"));
23555 else
23556 {
23557 msg_start();
23558 msg_scroll = TRUE;
23559 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23560 {
23561 msg_outnum((long)++nr);
23562 MSG_PUTS(": ");
23563 msg_outtrans(get_tv_string(&li->li_tv));
23564 msg_putchar('\n');
23565 out_flush(); /* output one line at a time */
23566 ui_breakcheck();
23567 }
23568 /* Assume "got_int" was set to truncate the listing. */
23569 got_int = FALSE;
23570
23571#ifdef FEAT_BROWSE_CMD
23572 if (cmdmod.browse)
23573 {
23574 quit_more = FALSE;
23575 nr = prompt_for_number(FALSE);
23576 msg_starthere();
23577 if (nr > 0)
23578 {
23579 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23580 (long)nr);
23581
23582 if (p != NULL)
23583 {
23584 p = expand_env_save(p);
23585 eap->arg = p;
23586 eap->cmdidx = CMD_edit;
23587 cmdmod.browse = FALSE;
23588 do_exedit(eap, NULL);
23589 vim_free(p);
23590 }
23591 }
23592 }
23593#endif
23594 }
23595}
23596
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597#endif /* FEAT_EVAL */
23598
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023600#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601
23602#ifdef WIN3264
23603/*
23604 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23605 */
23606static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23607static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23608static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23609
23610/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023611 * Get the short path (8.3) for the filename in "fnamep".
23612 * Only works for a valid file name.
23613 * When the path gets longer "fnamep" is changed and the allocated buffer
23614 * is put in "bufp".
23615 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23616 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 */
23618 static int
23619get_short_pathname(fnamep, bufp, fnamelen)
23620 char_u **fnamep;
23621 char_u **bufp;
23622 int *fnamelen;
23623{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023624 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 char_u *newbuf;
23626
23627 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 l = GetShortPathName(*fnamep, *fnamep, len);
23629 if (l > len - 1)
23630 {
23631 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023632 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 newbuf = vim_strnsave(*fnamep, l);
23634 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636
23637 vim_free(*bufp);
23638 *fnamep = *bufp = newbuf;
23639
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023640 /* Really should always succeed, as the buffer is big enough. */
23641 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 }
23643
23644 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023645 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646}
23647
23648/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023649 * Get the short path (8.3) for the filename in "fname". The converted
23650 * path is returned in "bufp".
23651 *
23652 * Some of the directories specified in "fname" may not exist. This function
23653 * will shorten the existing directories at the beginning of the path and then
23654 * append the remaining non-existing path.
23655 *
23656 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023657 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023658 * bufp - Pointer to an allocated buffer for the filename.
23659 * fnamelen - Length of the filename pointed to by fname
23660 *
23661 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 */
23663 static int
23664shortpath_for_invalid_fname(fname, bufp, fnamelen)
23665 char_u **fname;
23666 char_u **bufp;
23667 int *fnamelen;
23668{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023669 char_u *short_fname, *save_fname, *pbuf_unused;
23670 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023672 int old_len, len;
23673 int new_len, sfx_len;
23674 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675
23676 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023677 old_len = *fnamelen;
23678 save_fname = vim_strnsave(*fname, old_len);
23679 pbuf_unused = NULL;
23680 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023682 endp = save_fname + old_len - 1; /* Find the end of the copy */
23683 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023685 /*
23686 * Try shortening the supplied path till it succeeds by removing one
23687 * directory at a time from the tail of the path.
23688 */
23689 len = 0;
23690 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023692 /* go back one path-separator */
23693 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23694 --endp;
23695 if (endp <= save_fname)
23696 break; /* processed the complete path */
23697
23698 /*
23699 * Replace the path separator with a NUL and try to shorten the
23700 * resulting path.
23701 */
23702 ch = *endp;
23703 *endp = 0;
23704 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023705 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023706 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23707 {
23708 retval = FAIL;
23709 goto theend;
23710 }
23711 *endp = ch; /* preserve the string */
23712
23713 if (len > 0)
23714 break; /* successfully shortened the path */
23715
23716 /* failed to shorten the path. Skip the path separator */
23717 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 }
23719
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023720 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023722 /*
23723 * Succeeded in shortening the path. Now concatenate the shortened
23724 * path with the remaining path at the tail.
23725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023727 /* Compute the length of the new path. */
23728 sfx_len = (int)(save_endp - endp) + 1;
23729 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023731 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023733 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023735 /* There is not enough space in the currently allocated string,
23736 * copy it to a buffer big enough. */
23737 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023739 {
23740 retval = FAIL;
23741 goto theend;
23742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 }
23744 else
23745 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023746 /* Transfer short_fname to the main buffer (it's big enough),
23747 * unless get_short_pathname() did its work in-place. */
23748 *fname = *bufp = save_fname;
23749 if (short_fname != save_fname)
23750 vim_strncpy(save_fname, short_fname, len);
23751 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023753
23754 /* concat the not-shortened part of the path */
23755 vim_strncpy(*fname + len, endp, sfx_len);
23756 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023758
23759theend:
23760 vim_free(pbuf_unused);
23761 vim_free(save_fname);
23762
23763 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764}
23765
23766/*
23767 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023768 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 */
23770 static int
23771shortpath_for_partial(fnamep, bufp, fnamelen)
23772 char_u **fnamep;
23773 char_u **bufp;
23774 int *fnamelen;
23775{
23776 int sepcount, len, tflen;
23777 char_u *p;
23778 char_u *pbuf, *tfname;
23779 int hasTilde;
23780
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023781 /* Count up the path separators from the RHS.. so we know which part
23782 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023784 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 if (vim_ispathsep(*p))
23786 ++sepcount;
23787
23788 /* Need full path first (use expand_env() to remove a "~/") */
23789 hasTilde = (**fnamep == '~');
23790 if (hasTilde)
23791 pbuf = tfname = expand_env_save(*fnamep);
23792 else
23793 pbuf = tfname = FullName_save(*fnamep, FALSE);
23794
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023795 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023797 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799
23800 if (len == 0)
23801 {
23802 /* Don't have a valid filename, so shorten the rest of the
23803 * path if we can. This CAN give us invalid 8.3 filenames, but
23804 * there's not a lot of point in guessing what it might be.
23805 */
23806 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023807 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23808 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 }
23810
23811 /* Count the paths backward to find the beginning of the desired string. */
23812 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023813 {
23814#ifdef FEAT_MBYTE
23815 if (has_mbyte)
23816 p -= mb_head_off(tfname, p);
23817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 if (vim_ispathsep(*p))
23819 {
23820 if (sepcount == 0 || (hasTilde && sepcount == 1))
23821 break;
23822 else
23823 sepcount --;
23824 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 if (hasTilde)
23827 {
23828 --p;
23829 if (p >= tfname)
23830 *p = '~';
23831 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833 }
23834 else
23835 ++p;
23836
23837 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23838 vim_free(*bufp);
23839 *fnamelen = (int)STRLEN(p);
23840 *bufp = pbuf;
23841 *fnamep = p;
23842
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023843 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844}
23845#endif /* WIN3264 */
23846
23847/*
23848 * Adjust a filename, according to a string of modifiers.
23849 * *fnamep must be NUL terminated when called. When returning, the length is
23850 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023851 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 * When there is an error, *fnamep is set to NULL.
23853 */
23854 int
23855modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23856 char_u *src; /* string with modifiers */
23857 int *usedlen; /* characters after src that are used */
23858 char_u **fnamep; /* file name so far */
23859 char_u **bufp; /* buffer for allocated file name or NULL */
23860 int *fnamelen; /* length of fnamep */
23861{
23862 int valid = 0;
23863 char_u *tail;
23864 char_u *s, *p, *pbuf;
23865 char_u dirname[MAXPATHL];
23866 int c;
23867 int has_fullname = 0;
23868#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023869 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870 int has_shortname = 0;
23871#endif
23872
23873repeat:
23874 /* ":p" - full path/file_name */
23875 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23876 {
23877 has_fullname = 1;
23878
23879 valid |= VALID_PATH;
23880 *usedlen += 2;
23881
23882 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23883 if ((*fnamep)[0] == '~'
23884#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23885 && ((*fnamep)[1] == '/'
23886# ifdef BACKSLASH_IN_FILENAME
23887 || (*fnamep)[1] == '\\'
23888# endif
23889 || (*fnamep)[1] == NUL)
23890
23891#endif
23892 )
23893 {
23894 *fnamep = expand_env_save(*fnamep);
23895 vim_free(*bufp); /* free any allocated file name */
23896 *bufp = *fnamep;
23897 if (*fnamep == NULL)
23898 return -1;
23899 }
23900
23901 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023902 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 {
23904 if (vim_ispathsep(*p)
23905 && p[1] == '.'
23906 && (p[2] == NUL
23907 || vim_ispathsep(p[2])
23908 || (p[2] == '.'
23909 && (p[3] == NUL || vim_ispathsep(p[3])))))
23910 break;
23911 }
23912
23913 /* FullName_save() is slow, don't use it when not needed. */
23914 if (*p != NUL || !vim_isAbsName(*fnamep))
23915 {
23916 *fnamep = FullName_save(*fnamep, *p != NUL);
23917 vim_free(*bufp); /* free any allocated file name */
23918 *bufp = *fnamep;
23919 if (*fnamep == NULL)
23920 return -1;
23921 }
23922
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023923#ifdef WIN3264
23924# if _WIN32_WINNT >= 0x0500
23925 if (vim_strchr(*fnamep, '~') != NULL)
23926 {
23927 /* Expand 8.3 filename to full path. Needed to make sure the same
23928 * file does not have two different names.
23929 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23930 p = alloc(_MAX_PATH + 1);
23931 if (p != NULL)
23932 {
23933 if (GetLongPathName(*fnamep, p, MAXPATHL))
23934 {
23935 vim_free(*bufp);
23936 *bufp = *fnamep = p;
23937 }
23938 else
23939 vim_free(p);
23940 }
23941 }
23942# endif
23943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 /* Append a path separator to a directory. */
23945 if (mch_isdir(*fnamep))
23946 {
23947 /* Make room for one or two extra characters. */
23948 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23949 vim_free(*bufp); /* free any allocated file name */
23950 *bufp = *fnamep;
23951 if (*fnamep == NULL)
23952 return -1;
23953 add_pathsep(*fnamep);
23954 }
23955 }
23956
23957 /* ":." - path relative to the current directory */
23958 /* ":~" - path relative to the home directory */
23959 /* ":8" - shortname path - postponed till after */
23960 while (src[*usedlen] == ':'
23961 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23962 {
23963 *usedlen += 2;
23964 if (c == '8')
23965 {
23966#ifdef WIN3264
23967 has_shortname = 1; /* Postpone this. */
23968#endif
23969 continue;
23970 }
23971 pbuf = NULL;
23972 /* Need full path first (use expand_env() to remove a "~/") */
23973 if (!has_fullname)
23974 {
23975 if (c == '.' && **fnamep == '~')
23976 p = pbuf = expand_env_save(*fnamep);
23977 else
23978 p = pbuf = FullName_save(*fnamep, FALSE);
23979 }
23980 else
23981 p = *fnamep;
23982
23983 has_fullname = 0;
23984
23985 if (p != NULL)
23986 {
23987 if (c == '.')
23988 {
23989 mch_dirname(dirname, MAXPATHL);
23990 s = shorten_fname(p, dirname);
23991 if (s != NULL)
23992 {
23993 *fnamep = s;
23994 if (pbuf != NULL)
23995 {
23996 vim_free(*bufp); /* free any allocated file name */
23997 *bufp = pbuf;
23998 pbuf = NULL;
23999 }
24000 }
24001 }
24002 else
24003 {
24004 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24005 /* Only replace it when it starts with '~' */
24006 if (*dirname == '~')
24007 {
24008 s = vim_strsave(dirname);
24009 if (s != NULL)
24010 {
24011 *fnamep = s;
24012 vim_free(*bufp);
24013 *bufp = s;
24014 }
24015 }
24016 }
24017 vim_free(pbuf);
24018 }
24019 }
24020
24021 tail = gettail(*fnamep);
24022 *fnamelen = (int)STRLEN(*fnamep);
24023
24024 /* ":h" - head, remove "/file_name", can be repeated */
24025 /* Don't remove the first "/" or "c:\" */
24026 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24027 {
24028 valid |= VALID_HEAD;
24029 *usedlen += 2;
24030 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024031 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024032 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 *fnamelen = (int)(tail - *fnamep);
24034#ifdef VMS
24035 if (*fnamelen > 0)
24036 *fnamelen += 1; /* the path separator is part of the path */
24037#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024038 if (*fnamelen == 0)
24039 {
24040 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24041 p = vim_strsave((char_u *)".");
24042 if (p == NULL)
24043 return -1;
24044 vim_free(*bufp);
24045 *bufp = *fnamep = tail = p;
24046 *fnamelen = 1;
24047 }
24048 else
24049 {
24050 while (tail > s && !after_pathsep(s, tail))
24051 mb_ptr_back(*fnamep, tail);
24052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 }
24054
24055 /* ":8" - shortname */
24056 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24057 {
24058 *usedlen += 2;
24059#ifdef WIN3264
24060 has_shortname = 1;
24061#endif
24062 }
24063
24064#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024065 /*
24066 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 */
24068 if (has_shortname)
24069 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024070 /* Copy the string if it is shortened by :h and when it wasn't copied
24071 * yet, because we are going to change it in place. Avoids changing
24072 * the buffer name for "%:8". */
24073 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 {
24075 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024076 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 return -1;
24078 vim_free(*bufp);
24079 *bufp = *fnamep = p;
24080 }
24081
24082 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024083 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 if (!has_fullname && !vim_isAbsName(*fnamep))
24085 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024086 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 return -1;
24088 }
24089 else
24090 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024091 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092
Bram Moolenaardc935552011-08-17 15:23:23 +020024093 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024095 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024096 return -1;
24097
24098 if (l == 0)
24099 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024100 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024102 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 return -1;
24104 }
24105 *fnamelen = l;
24106 }
24107 }
24108#endif /* WIN3264 */
24109
24110 /* ":t" - tail, just the basename */
24111 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24112 {
24113 *usedlen += 2;
24114 *fnamelen -= (int)(tail - *fnamep);
24115 *fnamep = tail;
24116 }
24117
24118 /* ":e" - extension, can be repeated */
24119 /* ":r" - root, without extension, can be repeated */
24120 while (src[*usedlen] == ':'
24121 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24122 {
24123 /* find a '.' in the tail:
24124 * - for second :e: before the current fname
24125 * - otherwise: The last '.'
24126 */
24127 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24128 s = *fnamep - 2;
24129 else
24130 s = *fnamep + *fnamelen - 1;
24131 for ( ; s > tail; --s)
24132 if (s[0] == '.')
24133 break;
24134 if (src[*usedlen + 1] == 'e') /* :e */
24135 {
24136 if (s > tail)
24137 {
24138 *fnamelen += (int)(*fnamep - (s + 1));
24139 *fnamep = s + 1;
24140#ifdef VMS
24141 /* cut version from the extension */
24142 s = *fnamep + *fnamelen - 1;
24143 for ( ; s > *fnamep; --s)
24144 if (s[0] == ';')
24145 break;
24146 if (s > *fnamep)
24147 *fnamelen = s - *fnamep;
24148#endif
24149 }
24150 else if (*fnamep <= tail)
24151 *fnamelen = 0;
24152 }
24153 else /* :r */
24154 {
24155 if (s > tail) /* remove one extension */
24156 *fnamelen = (int)(s - *fnamep);
24157 }
24158 *usedlen += 2;
24159 }
24160
24161 /* ":s?pat?foo?" - substitute */
24162 /* ":gs?pat?foo?" - global substitute */
24163 if (src[*usedlen] == ':'
24164 && (src[*usedlen + 1] == 's'
24165 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24166 {
24167 char_u *str;
24168 char_u *pat;
24169 char_u *sub;
24170 int sep;
24171 char_u *flags;
24172 int didit = FALSE;
24173
24174 flags = (char_u *)"";
24175 s = src + *usedlen + 2;
24176 if (src[*usedlen + 1] == 'g')
24177 {
24178 flags = (char_u *)"g";
24179 ++s;
24180 }
24181
24182 sep = *s++;
24183 if (sep)
24184 {
24185 /* find end of pattern */
24186 p = vim_strchr(s, sep);
24187 if (p != NULL)
24188 {
24189 pat = vim_strnsave(s, (int)(p - s));
24190 if (pat != NULL)
24191 {
24192 s = p + 1;
24193 /* find end of substitution */
24194 p = vim_strchr(s, sep);
24195 if (p != NULL)
24196 {
24197 sub = vim_strnsave(s, (int)(p - s));
24198 str = vim_strnsave(*fnamep, *fnamelen);
24199 if (sub != NULL && str != NULL)
24200 {
24201 *usedlen = (int)(p + 1 - src);
24202 s = do_string_sub(str, pat, sub, flags);
24203 if (s != NULL)
24204 {
24205 *fnamep = s;
24206 *fnamelen = (int)STRLEN(s);
24207 vim_free(*bufp);
24208 *bufp = s;
24209 didit = TRUE;
24210 }
24211 }
24212 vim_free(sub);
24213 vim_free(str);
24214 }
24215 vim_free(pat);
24216 }
24217 }
24218 /* after using ":s", repeat all the modifiers */
24219 if (didit)
24220 goto repeat;
24221 }
24222 }
24223
24224 return valid;
24225}
24226
24227/*
24228 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24229 * "flags" can be "g" to do a global substitute.
24230 * Returns an allocated string, NULL for error.
24231 */
24232 char_u *
24233do_string_sub(str, pat, sub, flags)
24234 char_u *str;
24235 char_u *pat;
24236 char_u *sub;
24237 char_u *flags;
24238{
24239 int sublen;
24240 regmatch_T regmatch;
24241 int i;
24242 int do_all;
24243 char_u *tail;
24244 garray_T ga;
24245 char_u *ret;
24246 char_u *save_cpo;
24247
24248 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24249 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024250 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251
24252 ga_init2(&ga, 1, 200);
24253
24254 do_all = (flags[0] == 'g');
24255
24256 regmatch.rm_ic = p_ic;
24257 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24258 if (regmatch.regprog != NULL)
24259 {
24260 tail = str;
24261 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24262 {
24263 /*
24264 * Get some space for a temporary buffer to do the substitution
24265 * into. It will contain:
24266 * - The text up to where the match is.
24267 * - The substituted text.
24268 * - The text after the match.
24269 */
24270 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24271 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24272 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24273 {
24274 ga_clear(&ga);
24275 break;
24276 }
24277
24278 /* copy the text up to where the match is */
24279 i = (int)(regmatch.startp[0] - tail);
24280 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24281 /* add the substituted text */
24282 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24283 + ga.ga_len + i, TRUE, TRUE, FALSE);
24284 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285 /* avoid getting stuck on a match with an empty string */
24286 if (tail == regmatch.endp[0])
24287 {
24288 if (*tail == NUL)
24289 break;
24290 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24291 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 }
24293 else
24294 {
24295 tail = regmatch.endp[0];
24296 if (*tail == NUL)
24297 break;
24298 }
24299 if (!do_all)
24300 break;
24301 }
24302
24303 if (ga.ga_data != NULL)
24304 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24305
Bram Moolenaar473de612013-06-08 18:19:48 +020024306 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 }
24308
24309 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24310 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024311 if (p_cpo == empty_option)
24312 p_cpo = save_cpo;
24313 else
24314 /* Darn, evaluating {sub} expression changed the value. */
24315 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316
24317 return ret;
24318}
24319
24320#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */