blob: 866b40cf6ea73dd4cbaf17e0d2dc0170d49885fa [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200460static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100671static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100691#ifdef FEAT_CRYPT
692static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
693#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000694static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200695static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200699static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000702static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000703static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
707static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
709#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000710static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200711static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712#ifdef HAVE_STRFTIME
713static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
715static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200721static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200722static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000728static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200729static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000732static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000733static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000734static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000735static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000737static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200738#ifdef FEAT_FLOAT
739static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745#ifdef FEAT_FLOAT
746static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200749static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200750static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000811static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200841
842#ifdef EBCDIC
843static int compare_func_name __ARGS((const void *s1, const void *s2));
844static void sortFunctions __ARGS(());
845#endif
846
847
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848/* Character used as separated in autoload function/variable names. */
849#define AUTOLOAD_CHAR '#'
850
Bram Moolenaar33570922005-01-25 22:26:29 +0000851/*
852 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000853 */
854 void
855eval_init()
856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 int i;
858 struct vimvar *p;
859
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200860 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
861 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200862 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865
866 for (i = 0; i < VV_LEN; ++i)
867 {
868 p = &vimvars[i];
869 STRCPY(p->vv_di.di_key, p->vv_name);
870 if (p->vv_flags & VV_RO)
871 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
872 else if (p->vv_flags & VV_RO_SBX)
873 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
874 else
875 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000876
877 /* add to v: scope dict, unless the value is not always available */
878 if (p->vv_type != VAR_UNKNOWN)
879 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000881 /* add to compat scope dict */
882 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000884 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200885 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886
887#ifdef EBCDIC
888 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100889 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890 */
891 sortFunctions();
892#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000893}
894
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895#if defined(EXITFREE) || defined(PROTO)
896 void
897eval_clear()
898{
899 int i;
900 struct vimvar *p;
901
902 for (i = 0; i < VV_LEN; ++i)
903 {
904 p = &vimvars[i];
905 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000907 vim_free(p->vv_str);
908 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000909 }
910 else if (p->vv_di.di_tv.v_type == VAR_LIST)
911 {
912 list_unref(p->vv_list);
913 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 }
916 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000917 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918 hash_clear(&compat_hashtab);
919
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100921# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200922 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100923# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924
925 /* global variables */
926 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000927
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000928 /* autoloaded script names */
929 ga_clear_strings(&ga_loaded);
930
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 /* script-local variables */
932 for (i = 1; i <= ga_scripts.ga_len; ++i)
933 {
934 vars_clear(&SCRIPT_VARS(i));
935 vim_free(SCRIPT_SV(i));
936 }
937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1161 FALSE, FALSE, FALSE, FNE_CHECK_START);
1162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001864 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001867 /*
1868 * ":let" without "=": list variables
1869 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 if (*arg == '[')
1871 EMSG(_(e_invarg));
1872 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_glob_vars(&first);
1879 list_buf_vars(&first);
1880 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001883#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001884 list_script_vars(&first);
1885 list_func_vars(&first);
1886 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 eap->nextcmd = check_nextcmd(arg);
1889 }
1890 else
1891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 op[0] = '=';
1893 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 {
1896 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1897 op[0] = expr[-1]; /* +=, -= or .= */
1898 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002161 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
2250 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
2530 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002531 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 */
2534 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002535get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 typval_T *rettv;
2538 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 int unlet;
2540 int skip;
2541 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 char_u *expr_start, *expr_end;
2546 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 dictitem_T *v;
2548 typval_T var1;
2549 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558
2559 if (skip)
2560 {
2561 /* When skipping just find the end of the name. */
2562 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002563 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 }
2565
2566 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002567 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (expr_start != NULL)
2569 {
2570 /* Don't expand the name when we already know there is an error. */
2571 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2572 && *p != '[' && *p != '.')
2573 {
2574 EMSG(_(e_trailing));
2575 return NULL;
2576 }
2577
2578 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2579 if (lp->ll_exp_name == NULL)
2580 {
2581 /* Report an invalid expression in braces, unless the
2582 * expression evaluation has been cancelled due to an
2583 * aborting error, an interrupt, or an exception. */
2584 if (!aborting() && !quiet)
2585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 EMSG2(_(e_invarg2), name);
2588 return NULL;
2589 }
2590 }
2591 lp->ll_name = lp->ll_exp_name;
2592 }
2593 else
2594 lp->ll_name = name;
2595
2596 /* Without [idx] or .key we are done. */
2597 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2598 return p;
2599
2600 cc = *p;
2601 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002602 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (v == NULL && !quiet)
2604 EMSG2(_(e_undefvar), lp->ll_name);
2605 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 if (v == NULL)
2607 return NULL;
2608
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /*
2610 * Loop until no more [idx] or .key is following.
2611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002612 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2616 && !(lp->ll_tv->v_type == VAR_DICT
2617 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_("E689: Can only index a List or Dictionary"));
2621 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_("E708: [:] must come last"));
2627 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 len = -1;
2631 if (*p == '.')
2632 {
2633 key = p + 1;
2634 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2635 ;
2636 if (len == 0)
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!quiet)
2639 EMSG(_(e_emptykey));
2640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642 p = key + len;
2643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 else
2645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 if (*p == ':')
2649 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 else
2651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 empty1 = FALSE;
2653 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 if (get_tv_string_chk(&var1) == NULL)
2656 {
2657 /* not a number or string */
2658 clear_tv(&var1);
2659 return NULL;
2660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
2662
2663 /* Optionally get the second index [ :expr]. */
2664 if (*p == ':')
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 if (!empty1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (rettv != NULL && (rettv->v_type != VAR_LIST
2675 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = skipwhite(p + 1);
2684 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
2687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 if (get_tv_string_chk(&var2) == NULL)
2696 {
2697 /* not a number or string */
2698 if (!empty1)
2699 clear_tv(&var1);
2700 clear_tv(&var2);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719
2720 /* Skip to past ']'. */
2721 ++p;
2722 }
2723
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
2726 if (len == -1)
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (*key == NUL)
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
2733 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 lp->ll_list = NULL;
2739 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002740 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 /* When assigning to a scope dictionary check that a function and
2743 * variable name is valid (only variable name unless it is l: or
2744 * g: dictionary). Disallow overwriting a builtin function. */
2745 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 int prevval;
2748 int wrong;
2749
2750 if (len != -1)
2751 {
2752 prevval = key[len];
2753 key[len] = NUL;
2754 }
2755 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2756 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002758 || !valid_varname(key);
2759 if (len != -1)
2760 key[len] = prevval;
2761 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 return NULL;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* Can't add "v:" variable. */
2768 if (lp->ll_dict == &vimvardict)
2769 {
2770 EMSG2(_(e_illvar), name);
2771 return NULL;
2772 }
2773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (len == -1)
2780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 p = NULL;
2791 break;
2792 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 /* existing variable, need to check if it can be changed */
2794 else if (var_check_ro(lp->ll_di->di_flags, name))
2795 return NULL;
2796
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 }
2801 else
2802 {
2803 /*
2804 * Get the number and item for the only or first index of the List.
2805 */
2806 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 else
2809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002810 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var1);
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_list = lp->ll_tv->vval.v_list;
2815 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2816 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002818 if (lp->ll_n1 < 0)
2819 {
2820 lp->ll_n1 = 0;
2821 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2822 }
2823 }
2824 if (lp->ll_li == NULL)
2825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002828 if (!quiet)
2829 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832
2833 /*
2834 * May need to find the item or absolute index for the second
2835 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 * When no index given: "lp->ll_empty2" is TRUE.
2837 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 {
2848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2856 if (lp->ll_n1 < 0)
2857 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2858 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 {
2860 if (!quiet)
2861 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002868 }
2869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return p;
2871}
2872
2873/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 */
2876 static void
2877clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879{
2880 vim_free(lp->ll_exp_name);
2881 vim_free(lp->ll_newkey);
2882}
2883
2884/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002887 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 */
2889 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 listitem_T *ri;
2899 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900
2901 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 cc = *endp;
2906 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 if (op != NULL && *op != '=')
2908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002912 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 {
2915 if (tv_op(&tv, rettv, op) == OK)
2916 set_var(lp->ll_name, &tv, FALSE);
2917 clear_tv(&tv);
2918 }
2919 }
2920 else
2921 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 else if (tv_check_lock(lp->ll_newkey == NULL
2926 ? lp->ll_tv->v_lock
2927 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2928 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 else if (lp->ll_range)
2930 {
2931 /*
2932 * Assign the List values to the list items.
2933 */
2934 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2938 else
2939 {
2940 clear_tv(&lp->ll_li->li_tv);
2941 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 ri = ri->li_next;
2944 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2945 break;
2946 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002949 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 ri = NULL;
2952 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002953 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 lp->ll_li = lp->ll_li->li_next;
2956 ++lp->ll_n1;
2957 }
2958 if (ri != NULL)
2959 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 else if (lp->ll_empty2
2961 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 : lp->ll_n1 != lp->ll_n2)
2963 EMSG(_("E711: List value has not enough items"));
2964 }
2965 else
2966 {
2967 /*
2968 * Assign to a List or Dictionary item.
2969 */
2970 if (lp->ll_newkey != NULL)
2971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972 if (op != NULL && *op != '=')
2973 {
2974 EMSG2(_(e_letwrong), op);
2975 return;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002979 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 if (di == NULL)
2981 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002982 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2983 {
2984 vim_free(di);
2985 return;
2986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 else if (op != NULL && *op != '=')
2990 {
2991 tv_op(lp->ll_tv, rettv, op);
2992 return;
2993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 /*
2998 * Assign the value to the variable or list item.
2999 */
3000 if (copy)
3001 copy_tv(rettv, lp->ll_tv);
3002 else
3003 {
3004 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003005 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 }
3008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003009}
3010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3013 * Returns OK or FAIL.
3014 */
3015 static int
3016tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 typval_T *tv1;
3018 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 char_u *op;
3020{
3021 long n;
3022 char_u numbuf[NUMBUFLEN];
3023 char_u *s;
3024
3025 /* Can't do anything with a Funcref or a Dict on the right. */
3026 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3027 {
3028 switch (tv1->v_type)
3029 {
3030 case VAR_DICT:
3031 case VAR_FUNC:
3032 break;
3033
3034 case VAR_LIST:
3035 if (*op != '+' || tv2->v_type != VAR_LIST)
3036 break;
3037 /* List += List */
3038 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3039 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3040 return OK;
3041
3042 case VAR_NUMBER:
3043 case VAR_STRING:
3044 if (tv2->v_type == VAR_LIST)
3045 break;
3046 if (*op == '+' || *op == '-')
3047 {
3048 /* nr += nr or nr -= nr*/
3049 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#ifdef FEAT_FLOAT
3051 if (tv2->v_type == VAR_FLOAT)
3052 {
3053 float_T f = n;
3054
3055 if (*op == '+')
3056 f += tv2->vval.v_float;
3057 else
3058 f -= tv2->vval.v_float;
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_FLOAT;
3061 tv1->vval.v_float = f;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064#endif
3065 {
3066 if (*op == '+')
3067 n += get_tv_number(tv2);
3068 else
3069 n -= get_tv_number(tv2);
3070 clear_tv(tv1);
3071 tv1->v_type = VAR_NUMBER;
3072 tv1->vval.v_number = n;
3073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 }
3075 else
3076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (tv2->v_type == VAR_FLOAT)
3078 break;
3079
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 /* str .= str */
3081 s = get_tv_string(tv1);
3082 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3083 clear_tv(tv1);
3084 tv1->v_type = VAR_STRING;
3085 tv1->vval.v_string = s;
3086 }
3087 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088
3089#ifdef FEAT_FLOAT
3090 case VAR_FLOAT:
3091 {
3092 float_T f;
3093
3094 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3095 && tv2->v_type != VAR_NUMBER
3096 && tv2->v_type != VAR_STRING))
3097 break;
3098 if (tv2->v_type == VAR_FLOAT)
3099 f = tv2->vval.v_float;
3100 else
3101 f = get_tv_number(tv2);
3102 if (*op == '+')
3103 tv1->vval.v_float += f;
3104 else
3105 tv1->vval.v_float -= f;
3106 }
3107 return OK;
3108#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 }
3110 }
3111
3112 EMSG2(_(e_letwrong), op);
3113 return FAIL;
3114}
3115
3116/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 * Add a watcher to a list.
3118 */
3119 static void
3120list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 list_T *l;
3122 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123{
3124 lw->lw_next = l->lv_watch;
3125 l->lv_watch = lw;
3126}
3127
3128/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003129 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 * No warning when it isn't found...
3131 */
3132 static void
3133list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 list_T *l;
3135 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136{
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138
3139 lwp = &l->lv_watch;
3140 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3141 {
3142 if (lw == lwrem)
3143 {
3144 *lwp = lw->lw_next;
3145 break;
3146 }
3147 lwp = &lw->lw_next;
3148 }
3149}
3150
3151/*
3152 * Just before removing an item from a list: advance watchers to the next
3153 * item.
3154 */
3155 static void
3156list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 list_T *l;
3158 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159{
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003161
3162 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3163 if (lw->lw_item == item)
3164 lw->lw_item = item->li_next;
3165}
3166
3167/*
3168 * Evaluate the expression used in a ":for var in expr" command.
3169 * "arg" points to "var".
3170 * Set "*errp" to TRUE for an error, FALSE otherwise;
3171 * Return a pointer that holds the info. Null when there is an error.
3172 */
3173 void *
3174eval_for_line(arg, errp, nextcmdp, skip)
3175 char_u *arg;
3176 int *errp;
3177 char_u **nextcmdp;
3178 int skip;
3179{
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 typval_T tv;
3183 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184
3185 *errp = TRUE; /* default: there is an error */
3186
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 if (fi == NULL)
3189 return NULL;
3190
3191 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3192 if (expr == NULL)
3193 return fi;
3194
3195 expr = skipwhite(expr);
3196 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3197 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003198 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 return fi;
3200 }
3201
3202 if (skip)
3203 ++emsg_skip;
3204 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3205 {
3206 *errp = FALSE;
3207 if (!skip)
3208 {
3209 l = tv.vval.v_list;
3210 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003211 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213 clear_tv(&tv);
3214 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 else
3216 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003217 /* No need to increment the refcount, it's already set for the
3218 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 fi->fi_list = l;
3220 list_add_watch(l, &fi->fi_lw);
3221 fi->fi_lw.lw_item = l->lv_first;
3222 }
3223 }
3224 }
3225 if (skip)
3226 --emsg_skip;
3227
3228 return fi;
3229}
3230
3231/*
3232 * Use the first item in a ":for" list. Advance to the next.
3233 * Assign the values to the variable (list). "arg" points to the first one.
3234 * Return TRUE when a valid item was found, FALSE when at end of list or
3235 * something wrong.
3236 */
3237 int
3238next_for_item(fi_void, arg)
3239 void *fi_void;
3240 char_u *arg;
3241{
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 item = fi->fi_lw.lw_item;
3247 if (item == NULL)
3248 result = FALSE;
3249 else
3250 {
3251 fi->fi_lw.lw_item = item->li_next;
3252 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3253 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3254 }
3255 return result;
3256}
3257
3258/*
3259 * Free the structure used to store info used by ":for".
3260 */
3261 void
3262free_for_info(fi_void)
3263 void *fi_void;
3264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003267 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 list_unref(fi->fi_list);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 vim_free(fi);
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3276
3277 void
3278set_context_for_expression(xp, arg, cmdidx)
3279 expand_T *xp;
3280 char_u *arg;
3281 cmdidx_T cmdidx;
3282{
3283 int got_eq = FALSE;
3284 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (cmdidx == CMD_let)
3288 {
3289 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003290 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 {
3292 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003293 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 {
3295 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (vim_iswhite(*p))
3298 break;
3299 }
3300 return;
3301 }
3302 }
3303 else
3304 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3305 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 while ((xp->xp_pattern = vim_strpbrk(arg,
3307 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3308 {
3309 c = *xp->xp_pattern;
3310 if (c == '&')
3311 {
3312 c = xp->xp_pattern[1];
3313 if (c == '&')
3314 {
3315 ++xp->xp_pattern;
3316 xp->xp_context = cmdidx != CMD_let || got_eq
3317 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3318 }
3319 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003322 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3323 xp->xp_pattern += 2;
3324
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 else if (c == '$')
3328 {
3329 /* environment variable */
3330 xp->xp_context = EXPAND_ENV_VARS;
3331 }
3332 else if (c == '=')
3333 {
3334 got_eq = TRUE;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else if (c == '<'
3338 && xp->xp_context == EXPAND_FUNCTIONS
3339 && vim_strchr(xp->xp_pattern, '(') == NULL)
3340 {
3341 /* Function name can start with "<SNR>" */
3342 break;
3343 }
3344 else if (cmdidx != CMD_let || got_eq)
3345 {
3346 if (c == '"') /* string */
3347 {
3348 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3349 if (c == '\\' && xp->xp_pattern[1] != NUL)
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_NOTHING;
3352 }
3353 else if (c == '\'') /* literal string */
3354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3357 /* skip */ ;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '|')
3361 {
3362 if (xp->xp_pattern[1] == '|')
3363 {
3364 ++xp->xp_pattern;
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
3367 else
3368 xp->xp_context = EXPAND_COMMANDS;
3369 }
3370 else
3371 xp->xp_context = EXPAND_EXPRESSION;
3372 }
3373 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 /* Doesn't look like something valid, expand as an expression
3375 * anyway. */
3376 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 arg = xp->xp_pattern;
3378 if (*arg != NUL)
3379 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3380 /* skip */ ;
3381 }
3382 xp->xp_pattern = arg;
3383}
3384
3385#endif /* FEAT_CMDL_COMPL */
3386
3387/*
3388 * ":1,25call func(arg1, arg2)" function call.
3389 */
3390 void
3391ex_call(eap)
3392 exarg_T *eap;
3393{
3394 char_u *arg = eap->arg;
3395 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003397 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 linenr_T lnum;
3401 int doesrange;
3402 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003405 if (eap->skip)
3406 {
3407 /* trans_function_name() doesn't work well when skipping, use eval0()
3408 * instead to skip to any following command, e.g. for:
3409 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003410 ++emsg_skip;
3411 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3412 clear_tv(&rettv);
3413 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003414 return;
3415 }
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003418 if (fudi.fd_newkey != NULL)
3419 {
3420 /* Still need to give an error message for missing key. */
3421 EMSG2(_(e_dictkey), fudi.fd_newkey);
3422 vim_free(fudi.fd_newkey);
3423 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 if (tofree == NULL)
3425 return;
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 /* Increase refcount on dictionary, it could get deleted when evaluating
3428 * the arguments. */
3429 if (fudi.fd_dict != NULL)
3430 ++fudi.fd_dict->dv_refcount;
3431
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003432 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003433 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar532c7802005-01-27 14:44:31 +00003436 /* Skip white space to allow ":call func ()". Not good, but required for
3437 * backward compatibility. */
3438 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 if (*startarg != '(')
3442 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003443 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 goto end;
3445 }
3446
3447 /*
3448 * When skipping, evaluate the function once, to find the end of the
3449 * arguments.
3450 * When the function takes a range, this is discovered after the first
3451 * call, and the loop is broken.
3452 */
3453 if (eap->skip)
3454 {
3455 ++emsg_skip;
3456 lnum = eap->line2; /* do it once, also with an invalid range */
3457 }
3458 else
3459 lnum = eap->line1;
3460 for ( ; lnum <= eap->line2; ++lnum)
3461 {
3462 if (!eap->skip && eap->addr_count > 0)
3463 {
3464 curwin->w_cursor.lnum = lnum;
3465 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003466#ifdef FEAT_VIRTUALEDIT
3467 curwin->w_cursor.coladd = 0;
3468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 eap->line1, eap->line2, &doesrange,
3473 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 failed = TRUE;
3476 break;
3477 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
3479 /* Handle a function returning a Funcref, Dictionary or List. */
3480 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3481 {
3482 failed = TRUE;
3483 break;
3484 }
3485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (doesrange || eap->skip)
3488 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (aborting())
3495 break;
3496 }
3497 if (eap->skip)
3498 --emsg_skip;
3499
3500 if (!failed)
3501 {
3502 /* Check for trailing illegal characters and a following command. */
3503 if (!ends_excmd(*arg))
3504 {
3505 emsg_severe = TRUE;
3506 EMSG(_(e_trailing));
3507 }
3508 else
3509 eap->nextcmd = check_nextcmd(arg);
3510 }
3511
3512end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003514 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515}
3516
3517/*
3518 * ":unlet[!] var1 ... " command.
3519 */
3520 void
3521ex_unlet(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 ex_unletlock(eap, eap->arg, 0);
3525}
3526
3527/*
3528 * ":lockvar" and ":unlockvar" commands
3529 */
3530 void
3531ex_lockvar(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 int deep = 2;
3536
3537 if (eap->forceit)
3538 deep = -1;
3539 else if (vim_isdigit(*arg))
3540 {
3541 deep = getdigits(&arg);
3542 arg = skipwhite(arg);
3543 }
3544
3545 ex_unletlock(eap, arg, deep);
3546}
3547
3548/*
3549 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3550 */
3551 static void
3552ex_unletlock(eap, argstart, deep)
3553 exarg_T *eap;
3554 char_u *argstart;
3555 int deep;
3556{
3557 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 do
3563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003565 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3566 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 if (lv.ll_name == NULL)
3568 error = TRUE; /* error but continue parsing */
3569 if (name_end == NULL || (!vim_iswhite(*name_end)
3570 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 if (name_end != NULL)
3573 {
3574 emsg_severe = TRUE;
3575 EMSG(_(e_trailing));
3576 }
3577 if (!(eap->skip || error))
3578 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 break;
3580 }
3581
3582 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 {
3584 if (eap->cmdidx == CMD_unlet)
3585 {
3586 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3587 error = TRUE;
3588 }
3589 else
3590 {
3591 if (do_lock_var(&lv, name_end, deep,
3592 eap->cmdidx == CMD_lockvar) == FAIL)
3593 error = TRUE;
3594 }
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 if (!eap->skip)
3598 clear_lval(&lv);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 arg = skipwhite(name_end);
3601 } while (!ends_excmd(*arg));
3602
3603 eap->nextcmd = check_nextcmd(arg);
3604}
3605
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 int forceit;
3611{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 int ret = OK;
3613 int cc;
3614
3615 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 cc = *name_end;
3618 *name_end = NUL;
3619
3620 /* Normal name or expanded name. */
3621 if (check_changedtick(lp->ll_name))
3622 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3628 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 else if (lp->ll_range)
3630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632
3633 /* Delete a range of List items. */
3634 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3635 {
3636 li = lp->ll_li->li_next;
3637 listitem_remove(lp->ll_list, lp->ll_li);
3638 lp->ll_li = li;
3639 ++lp->ll_n1;
3640 }
3641 }
3642 else
3643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 /* unlet a List item. */
3646 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003649 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 }
3651
3652 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653}
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655/*
3656 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
3659 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hashtab_T *ht;
3665 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003667 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar33570922005-01-25 22:26:29 +00003669 ht = find_var_ht(name, &varname);
3670 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003672 hi = hash_find(ht, varname);
3673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003675 di = HI2DI(hi);
3676 if (var_check_fixed(di->di_flags, name)
3677 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
3679 delete_var(ht, hi);
3680 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 if (forceit)
3684 return OK;
3685 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687}
3688
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689/*
3690 * Lock or unlock variable indicated by "lp".
3691 * "deep" is the levels to go (-1 for unlimited);
3692 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3693 */
3694 static int
3695do_lock_var(lp, name_end, deep, lock)
3696 lval_T *lp;
3697 char_u *name_end;
3698 int deep;
3699 int lock;
3700{
3701 int ret = OK;
3702 int cc;
3703 dictitem_T *di;
3704
3705 if (deep == 0) /* nothing to do */
3706 return OK;
3707
3708 if (lp->ll_tv == NULL)
3709 {
3710 cc = *name_end;
3711 *name_end = NUL;
3712
3713 /* Normal name or expanded name. */
3714 if (check_changedtick(lp->ll_name))
3715 ret = FAIL;
3716 else
3717 {
3718 di = find_var(lp->ll_name, NULL);
3719 if (di == NULL)
3720 ret = FAIL;
3721 else
3722 {
3723 if (lock)
3724 di->di_flags |= DI_FLAGS_LOCK;
3725 else
3726 di->di_flags &= ~DI_FLAGS_LOCK;
3727 item_lock(&di->di_tv, deep, lock);
3728 }
3729 }
3730 *name_end = cc;
3731 }
3732 else if (lp->ll_range)
3733 {
3734 listitem_T *li = lp->ll_li;
3735
3736 /* (un)lock a range of List items. */
3737 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3738 {
3739 item_lock(&li->li_tv, deep, lock);
3740 li = li->li_next;
3741 ++lp->ll_n1;
3742 }
3743 }
3744 else if (lp->ll_list != NULL)
3745 /* (un)lock a List item. */
3746 item_lock(&lp->ll_li->li_tv, deep, lock);
3747 else
3748 /* un(lock) a Dictionary item. */
3749 item_lock(&lp->ll_di->di_tv, deep, lock);
3750
3751 return ret;
3752}
3753
3754/*
3755 * Lock or unlock an item. "deep" is nr of levels to go.
3756 */
3757 static void
3758item_lock(tv, deep, lock)
3759 typval_T *tv;
3760 int deep;
3761 int lock;
3762{
3763 static int recurse = 0;
3764 list_T *l;
3765 listitem_T *li;
3766 dict_T *d;
3767 hashitem_T *hi;
3768 int todo;
3769
3770 if (recurse >= DICT_MAXNEST)
3771 {
3772 EMSG(_("E743: variable nested too deep for (un)lock"));
3773 return;
3774 }
3775 if (deep == 0)
3776 return;
3777 ++recurse;
3778
3779 /* lock/unlock the item itself */
3780 if (lock)
3781 tv->v_lock |= VAR_LOCKED;
3782 else
3783 tv->v_lock &= ~VAR_LOCKED;
3784
3785 switch (tv->v_type)
3786 {
3787 case VAR_LIST:
3788 if ((l = tv->vval.v_list) != NULL)
3789 {
3790 if (lock)
3791 l->lv_lock |= VAR_LOCKED;
3792 else
3793 l->lv_lock &= ~VAR_LOCKED;
3794 if (deep < 0 || deep > 1)
3795 /* recursive: lock/unlock the items the List contains */
3796 for (li = l->lv_first; li != NULL; li = li->li_next)
3797 item_lock(&li->li_tv, deep - 1, lock);
3798 }
3799 break;
3800 case VAR_DICT:
3801 if ((d = tv->vval.v_dict) != NULL)
3802 {
3803 if (lock)
3804 d->dv_lock |= VAR_LOCKED;
3805 else
3806 d->dv_lock &= ~VAR_LOCKED;
3807 if (deep < 0 || deep > 1)
3808 {
3809 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3812 {
3813 if (!HASHITEM_EMPTY(hi))
3814 {
3815 --todo;
3816 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3817 }
3818 }
3819 }
3820 }
3821 }
3822 --recurse;
3823}
3824
Bram Moolenaara40058a2005-07-11 22:42:07 +00003825/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003826 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3827 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003828 */
3829 static int
3830tv_islocked(tv)
3831 typval_T *tv;
3832{
3833 return (tv->v_lock & VAR_LOCKED)
3834 || (tv->v_type == VAR_LIST
3835 && tv->vval.v_list != NULL
3836 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3837 || (tv->v_type == VAR_DICT
3838 && tv->vval.v_dict != NULL
3839 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3843/*
3844 * Delete all "menutrans_" variables.
3845 */
3846 void
3847del_menutrans_vars()
3848{
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003853 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 {
3856 if (!HASHITEM_EMPTY(hi))
3857 {
3858 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3860 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 }
3862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864}
3865#endif
3866
3867#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3868
3869/*
3870 * Local string buffer for the next two functions to store a variable name
3871 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3872 * get_user_var_name().
3873 */
3874
3875static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3876
3877static char_u *varnamebuf = NULL;
3878static int varnamebuflen = 0;
3879
3880/*
3881 * Function to concatenate a prefix and a variable name.
3882 */
3883 static char_u *
3884cat_prefix_varname(prefix, name)
3885 int prefix;
3886 char_u *name;
3887{
3888 int len;
3889
3890 len = (int)STRLEN(name) + 3;
3891 if (len > varnamebuflen)
3892 {
3893 vim_free(varnamebuf);
3894 len += 10; /* some additional space */
3895 varnamebuf = alloc(len);
3896 if (varnamebuf == NULL)
3897 {
3898 varnamebuflen = 0;
3899 return NULL;
3900 }
3901 varnamebuflen = len;
3902 }
3903 *varnamebuf = prefix;
3904 varnamebuf[1] = ':';
3905 STRCPY(varnamebuf + 2, name);
3906 return varnamebuf;
3907}
3908
3909/*
3910 * Function given to ExpandGeneric() to obtain the list of user defined
3911 * (global/buffer/window/built-in) variable names.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u *
3914get_user_var_name(xp, idx)
3915 expand_T *xp;
3916 int idx;
3917{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003918 static long_u gdone;
3919 static long_u bdone;
3920 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 static long_u tdone;
3923#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003924 static int vidx;
3925 static hashitem_T *hi;
3926 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003930 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 tdone = 0;
3933#endif
3934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* Global variables */
3937 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 else
3942 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 while (HASHITEM_EMPTY(hi))
3944 ++hi;
3945 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3946 return cat_prefix_varname('g', hi->hi_key);
3947 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949
3950 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003951 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003956 else
3957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 while (HASHITEM_EMPTY(hi))
3959 ++hi;
3960 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return (char_u *)"b:changedtick";
3966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967
3968 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003969 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003972 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003974 else
3975 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981#ifdef FEAT_WINDOWS
3982 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003983 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003984 if (tdone < ht->ht_used)
3985 {
3986 if (tdone++ == 0)
3987 hi = ht->ht_array;
3988 else
3989 ++hi;
3990 while (HASHITEM_EMPTY(hi))
3991 ++hi;
3992 return cat_prefix_varname('t', hi->hi_key);
3993 }
3994#endif
3995
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 /* v: variables */
3997 if (vidx < VV_LEN)
3998 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 vim_free(varnamebuf);
4001 varnamebuf = NULL;
4002 varnamebuflen = 0;
4003 return NULL;
4004}
4005
4006#endif /* FEAT_CMDL_COMPL */
4007
4008/*
4009 * types for expressions.
4010 */
4011typedef enum
4012{
4013 TYPE_UNKNOWN = 0
4014 , TYPE_EQUAL /* == */
4015 , TYPE_NEQUAL /* != */
4016 , TYPE_GREATER /* > */
4017 , TYPE_GEQUAL /* >= */
4018 , TYPE_SMALLER /* < */
4019 , TYPE_SEQUAL /* <= */
4020 , TYPE_MATCH /* =~ */
4021 , TYPE_NOMATCH /* !~ */
4022} exptype_T;
4023
4024/*
4025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4028 */
4029
4030/*
4031 * Handle zero level expression.
4032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004034 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * Return OK or FAIL.
4036 */
4037 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 char_u **nextcmd;
4042 int evaluate;
4043{
4044 int ret;
4045 char_u *p;
4046
4047 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (ret == FAIL || !ends_excmd(*p))
4050 {
4051 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /*
4054 * Report the invalid expression unless the expression evaluation has
4055 * been cancelled due to an aborting error, an interrupt, or an
4056 * exception.
4057 */
4058 if (!aborting())
4059 EMSG2(_(e_invexpr2), arg);
4060 ret = FAIL;
4061 }
4062 if (nextcmd != NULL)
4063 *nextcmd = check_nextcmd(p);
4064
4065 return ret;
4066}
4067
4068/*
4069 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004070 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 *
4072 * "arg" must point to the first non-white of the expression.
4073 * "arg" is advanced to the next non-white after the recognized expression.
4074 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004075 * Note: "rettv.v_lock" is not set.
4076 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
4085 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /*
4089 * Get the first variable.
4090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093
4094 if ((*arg)[0] == '?')
4095 {
4096 result = FALSE;
4097 if (evaluate)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 int error = FALSE;
4100
4101 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (error)
4105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107
4108 /*
4109 * Get the second variable.
4110 */
4111 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114
4115 /*
4116 * Check for the ":".
4117 */
4118 if ((*arg)[0] != ':')
4119 {
4120 EMSG(_("E109: Missing ':' after '?'"));
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125
4126 /*
4127 * Get the third variable.
4128 */
4129 *arg = skipwhite(*arg + 1);
4130 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4131 {
4132 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135 }
4136 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139
4140 return OK;
4141}
4142
4143/*
4144 * Handle first level expression:
4145 * expr2 || expr2 || expr2 logical OR
4146 *
4147 * "arg" must point to the first non-white of the expression.
4148 * "arg" is advanced to the next non-white after the recognized expression.
4149 *
4150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
Bram Moolenaar33570922005-01-25 22:26:29 +00004158 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 long result;
4160 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163 /*
4164 * Get the first variable.
4165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168
4169 /*
4170 * Repeat until there is no following "||".
4171 */
4172 first = TRUE;
4173 result = FALSE;
4174 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4175 {
4176 if (evaluate && first)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 if (error)
4182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 first = FALSE;
4184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 2);
4190 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4191 return FAIL;
4192
4193 /*
4194 * Compute the result.
4195 */
4196 if (evaluate && !result)
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (error)
4202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 if (evaluate)
4205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 rettv->v_type = VAR_NUMBER;
4207 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209 }
4210
4211 return OK;
4212}
4213
4214/*
4215 * Handle second level expression:
4216 * expr3 && expr3 && expr3 logical AND
4217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 long result;
4231 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat until there is no following "&&".
4242 */
4243 first = TRUE;
4244 result = TRUE;
4245 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4246 {
4247 if (evaluate && first)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 first = FALSE;
4255 }
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 2);
4261 if (eval4(arg, &var2, evaluate && result) == FAIL)
4262 return FAIL;
4263
4264 /*
4265 * Compute the result.
4266 */
4267 if (evaluate && result)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 if (evaluate)
4276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle third level expression:
4287 * var1 == var2
4288 * var1 =~ var2
4289 * var1 != var2
4290 * var1 !~ var2
4291 * var1 > var2
4292 * var1 >= var2
4293 * var1 < var2
4294 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 * var1 is var2
4296 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int evaluate;
4308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 char_u *p;
4311 int i;
4312 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004313 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int len = 2;
4315 long n1, n2;
4316 char_u *s1, *s2;
4317 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4318 regmatch_T regmatch;
4319 int ic;
4320 char_u *save_cpo;
4321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 p = *arg;
4329 switch (p[0])
4330 {
4331 case '=': if (p[1] == '=')
4332 type = TYPE_EQUAL;
4333 else if (p[1] == '~')
4334 type = TYPE_MATCH;
4335 break;
4336 case '!': if (p[1] == '=')
4337 type = TYPE_NEQUAL;
4338 else if (p[1] == '~')
4339 type = TYPE_NOMATCH;
4340 break;
4341 case '>': if (p[1] != '=')
4342 {
4343 type = TYPE_GREATER;
4344 len = 1;
4345 }
4346 else
4347 type = TYPE_GEQUAL;
4348 break;
4349 case '<': if (p[1] != '=')
4350 {
4351 type = TYPE_SMALLER;
4352 len = 1;
4353 }
4354 else
4355 type = TYPE_SEQUAL;
4356 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004357 case 'i': if (p[1] == 's')
4358 {
4359 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4360 len = 5;
4361 if (!vim_isIDc(p[len]))
4362 {
4363 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4364 type_is = TRUE;
4365 }
4366 }
4367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369
4370 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 if (type != TYPE_UNKNOWN)
4374 {
4375 /* extra question mark appended: ignore case */
4376 if (p[len] == '?')
4377 {
4378 ic = TRUE;
4379 ++len;
4380 }
4381 /* extra '#' appended: match case */
4382 else if (p[len] == '#')
4383 {
4384 ic = FALSE;
4385 ++len;
4386 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 ic = p_ic;
4390
4391 /*
4392 * Get the second variable.
4393 */
4394 *arg = skipwhite(p + len);
4395 if (eval5(arg, &var2, evaluate) == FAIL)
4396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 return FAIL;
4399 }
4400
4401 if (evaluate)
4402 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 if (type_is && rettv->v_type != var2.v_type)
4404 {
4405 /* For "is" a different type always means FALSE, for "notis"
4406 * it means TRUE. */
4407 n1 = (type == TYPE_NEQUAL);
4408 }
4409 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4410 {
4411 if (type_is)
4412 {
4413 n1 = (rettv->v_type == var2.v_type
4414 && rettv->vval.v_list == var2.vval.v_list);
4415 if (type == TYPE_NEQUAL)
4416 n1 = !n1;
4417 }
4418 else if (rettv->v_type != var2.v_type
4419 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4420 {
4421 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004422 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004424 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
4429 else
4430 {
4431 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004432 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4433 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 }
4438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004439 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4440 {
4441 if (type_is)
4442 {
4443 n1 = (rettv->v_type == var2.v_type
4444 && rettv->vval.v_dict == var2.vval.v_dict);
4445 if (type == TYPE_NEQUAL)
4446 n1 = !n1;
4447 }
4448 else if (rettv->v_type != var2.v_type
4449 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4450 {
4451 if (rettv->v_type != var2.v_type)
4452 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4453 else
4454 EMSG(_("E736: Invalid operation for Dictionary"));
4455 clear_tv(rettv);
4456 clear_tv(&var2);
4457 return FAIL;
4458 }
4459 else
4460 {
4461 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004462 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4463 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004464 if (type == TYPE_NEQUAL)
4465 n1 = !n1;
4466 }
4467 }
4468
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4470 {
4471 if (rettv->v_type != var2.v_type
4472 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4473 {
4474 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004475 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004477 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 clear_tv(rettv);
4479 clear_tv(&var2);
4480 return FAIL;
4481 }
4482 else
4483 {
4484 /* Compare two Funcrefs for being equal or unequal. */
4485 if (rettv->vval.v_string == NULL
4486 || var2.vval.v_string == NULL)
4487 n1 = FALSE;
4488 else
4489 n1 = STRCMP(rettv->vval.v_string,
4490 var2.vval.v_string) == 0;
4491 if (type == TYPE_NEQUAL)
4492 n1 = !n1;
4493 }
4494 }
4495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496#ifdef FEAT_FLOAT
4497 /*
4498 * If one of the two variables is a float, compare as a float.
4499 * When using "=~" or "!~", always compare as string.
4500 */
4501 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4502 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4503 {
4504 float_T f1, f2;
4505
4506 if (rettv->v_type == VAR_FLOAT)
4507 f1 = rettv->vval.v_float;
4508 else
4509 f1 = get_tv_number(rettv);
4510 if (var2.v_type == VAR_FLOAT)
4511 f2 = var2.vval.v_float;
4512 else
4513 f2 = get_tv_number(&var2);
4514 n1 = FALSE;
4515 switch (type)
4516 {
4517 case TYPE_EQUAL: n1 = (f1 == f2); break;
4518 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4519 case TYPE_GREATER: n1 = (f1 > f2); break;
4520 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4521 case TYPE_SMALLER: n1 = (f1 < f2); break;
4522 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4523 case TYPE_UNKNOWN:
4524 case TYPE_MATCH:
4525 case TYPE_NOMATCH: break; /* avoid gcc warning */
4526 }
4527 }
4528#endif
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /*
4531 * If one of the two variables is a number, compare as a number.
4532 * When using "=~" or "!~", always compare as string.
4533 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 n1 = get_tv_number(rettv);
4538 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 switch (type)
4540 {
4541 case TYPE_EQUAL: n1 = (n1 == n2); break;
4542 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4543 case TYPE_GREATER: n1 = (n1 > n2); break;
4544 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4545 case TYPE_SMALLER: n1 = (n1 < n2); break;
4546 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4547 case TYPE_UNKNOWN:
4548 case TYPE_MATCH:
4549 case TYPE_NOMATCH: break; /* avoid gcc warning */
4550 }
4551 }
4552 else
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 s1 = get_tv_string_buf(rettv, buf1);
4555 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4557 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4558 else
4559 i = 0;
4560 n1 = FALSE;
4561 switch (type)
4562 {
4563 case TYPE_EQUAL: n1 = (i == 0); break;
4564 case TYPE_NEQUAL: n1 = (i != 0); break;
4565 case TYPE_GREATER: n1 = (i > 0); break;
4566 case TYPE_GEQUAL: n1 = (i >= 0); break;
4567 case TYPE_SMALLER: n1 = (i < 0); break;
4568 case TYPE_SEQUAL: n1 = (i <= 0); break;
4569
4570 case TYPE_MATCH:
4571 case TYPE_NOMATCH:
4572 /* avoid 'l' flag in 'cpoptions' */
4573 save_cpo = p_cpo;
4574 p_cpo = (char_u *)"";
4575 regmatch.regprog = vim_regcomp(s2,
4576 RE_MAGIC + RE_STRING);
4577 regmatch.rm_ic = ic;
4578 if (regmatch.regprog != NULL)
4579 {
4580 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4581 vim_free(regmatch.regprog);
4582 if (type == TYPE_NOMATCH)
4583 n1 = !n1;
4584 }
4585 p_cpo = save_cpo;
4586 break;
4587
4588 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4589 }
4590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597
4598 return OK;
4599}
4600
4601/*
4602 * Handle fourth level expression:
4603 * + number addition
4604 * - number subtraction
4605 * . string concatenation
4606 *
4607 * "arg" must point to the first non-white of the expression.
4608 * "arg" is advanced to the next non-white after the recognized expression.
4609 *
4610 * Return OK or FAIL.
4611 */
4612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 int evaluate;
4617{
Bram Moolenaar33570922005-01-25 22:26:29 +00004618 typval_T var2;
4619 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int op;
4621 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622#ifdef FEAT_FLOAT
4623 float_T f1 = 0, f2 = 0;
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u *s1, *s2;
4626 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4627 char_u *p;
4628
4629 /*
4630 * Get the first variable.
4631 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004632 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return FAIL;
4634
4635 /*
4636 * Repeat computing, until no '+', '-' or '.' is following.
4637 */
4638 for (;;)
4639 {
4640 op = **arg;
4641 if (op != '+' && op != '-' && op != '.')
4642 break;
4643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004644 if ((op != '+' || rettv->v_type != VAR_LIST)
4645#ifdef FEAT_FLOAT
4646 && (op == '.' || rettv->v_type != VAR_FLOAT)
4647#endif
4648 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 {
4650 /* For "list + ...", an illegal use of the first operand as
4651 * a number cannot be determined before evaluating the 2nd
4652 * operand: if this is also a list, all is ok.
4653 * For "something . ...", "something - ..." or "non-list + ...",
4654 * we know that the first operand needs to be a string or number
4655 * without evaluating the 2nd operand. So check before to avoid
4656 * side effects after an error. */
4657 if (evaluate && get_tv_string_chk(rettv) == NULL)
4658 {
4659 clear_tv(rettv);
4660 return FAIL;
4661 }
4662 }
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /*
4665 * Get the second variable.
4666 */
4667 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004668 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return FAIL;
4672 }
4673
4674 if (evaluate)
4675 {
4676 /*
4677 * Compute the result.
4678 */
4679 if (op == '.')
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4682 s2 = get_tv_string_buf_chk(&var2, buf2);
4683 if (s2 == NULL) /* type error ? */
4684 {
4685 clear_tv(rettv);
4686 clear_tv(&var2);
4687 return FAIL;
4688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004689 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 rettv->v_type = VAR_STRING;
4692 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004694 else if (op == '+' && rettv->v_type == VAR_LIST
4695 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004696 {
4697 /* concatenate Lists */
4698 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4699 &var3) == FAIL)
4700 {
4701 clear_tv(rettv);
4702 clear_tv(&var2);
4703 return FAIL;
4704 }
4705 clear_tv(rettv);
4706 *rettv = var3;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 else
4709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 int error = FALSE;
4711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 f1 = rettv->vval.v_float;
4716 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 else
4719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721 n1 = get_tv_number_chk(rettv, &error);
4722 if (error)
4723 {
4724 /* This can only happen for "list + non-list". For
4725 * "non-list + ..." or "something - ...", we returned
4726 * before evaluating the 2nd operand. */
4727 clear_tv(rettv);
4728 return FAIL;
4729 }
4730#ifdef FEAT_FLOAT
4731 if (var2.v_type == VAR_FLOAT)
4732 f1 = n1;
4733#endif
4734 }
4735#ifdef FEAT_FLOAT
4736 if (var2.v_type == VAR_FLOAT)
4737 {
4738 f2 = var2.vval.v_float;
4739 n2 = 0;
4740 }
4741 else
4742#endif
4743 {
4744 n2 = get_tv_number_chk(&var2, &error);
4745 if (error)
4746 {
4747 clear_tv(rettv);
4748 clear_tv(&var2);
4749 return FAIL;
4750 }
4751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
4753 f2 = n2;
4754#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757
4758#ifdef FEAT_FLOAT
4759 /* If there is a float on either side the result is a float. */
4760 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4761 {
4762 if (op == '+')
4763 f1 = f1 + f2;
4764 else
4765 f1 = f1 - f2;
4766 rettv->v_type = VAR_FLOAT;
4767 rettv->vval.v_float = f1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#endif
4771 {
4772 if (op == '+')
4773 n1 = n1 + n2;
4774 else
4775 n1 = n1 - n2;
4776 rettv->v_type = VAR_NUMBER;
4777 rettv->vval.v_number = n1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 }
4783 return OK;
4784}
4785
4786/*
4787 * Handle fifth level expression:
4788 * * number multiplication
4789 * / number division
4790 * % number modulo
4791 *
4792 * "arg" must point to the first non-white of the expression.
4793 * "arg" is advanced to the next non-white after the recognized expression.
4794 *
4795 * Return OK or FAIL.
4796 */
4797 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 int op;
4806 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807#ifdef FEAT_FLOAT
4808 int use_float = FALSE;
4809 float_T f1 = 0, f2;
4810#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 /*
4814 * Get the first variable.
4815 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004816 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
4818
4819 /*
4820 * Repeat computing, until no '*', '/' or '%' is following.
4821 */
4822 for (;;)
4823 {
4824 op = **arg;
4825 if (op != '*' && op != '/' && op != '%')
4826 break;
4827
4828 if (evaluate)
4829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#ifdef FEAT_FLOAT
4831 if (rettv->v_type == VAR_FLOAT)
4832 {
4833 f1 = rettv->vval.v_float;
4834 use_float = TRUE;
4835 n1 = 0;
4836 }
4837 else
4838#endif
4839 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 if (error)
4842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 n1 = 0;
4846
4847 /*
4848 * Get the second variable.
4849 */
4850 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004851 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return FAIL;
4853
4854 if (evaluate)
4855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856#ifdef FEAT_FLOAT
4857 if (var2.v_type == VAR_FLOAT)
4858 {
4859 if (!use_float)
4860 {
4861 f1 = n1;
4862 use_float = TRUE;
4863 }
4864 f2 = var2.vval.v_float;
4865 n2 = 0;
4866 }
4867 else
4868#endif
4869 {
4870 n2 = get_tv_number_chk(&var2, &error);
4871 clear_tv(&var2);
4872 if (error)
4873 return FAIL;
4874#ifdef FEAT_FLOAT
4875 if (use_float)
4876 f2 = n2;
4877#endif
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884#ifdef FEAT_FLOAT
4885 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 if (op == '*')
4888 f1 = f1 * f2;
4889 else if (op == '/')
4890 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891# ifdef VMS
4892 /* VMS crashes on divide by zero, work around it */
4893 if (f2 == 0.0)
4894 {
4895 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004896 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004898 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004899 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004900 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901 }
4902 else
4903 f1 = f1 / f2;
4904# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 /* We rely on the floating point library to handle divide
4906 * by zero to result in "inf" and not a crash. */
4907 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004912 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 return FAIL;
4914 }
4915 rettv->v_type = VAR_FLOAT;
4916 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 n1 = n1 * n2;
4923 else if (op == '/')
4924 {
4925 if (n2 == 0) /* give an error message? */
4926 {
4927 if (n1 == 0)
4928 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4929 else if (n1 < 0)
4930 n1 = -0x7fffffffL;
4931 else
4932 n1 = 0x7fffffffL;
4933 }
4934 else
4935 n1 = n1 / n2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938 {
4939 if (n2 == 0) /* give an error message? */
4940 n1 = 0;
4941 else
4942 n1 = n1 % n2;
4943 }
4944 rettv->v_type = VAR_NUMBER;
4945 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 }
4949
4950 return OK;
4951}
4952
4953/*
4954 * Handle sixth level expression:
4955 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004956 * "string" string constant
4957 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 * &option-name option value
4959 * @r register contents
4960 * identifier variable value
4961 * function() function call
4962 * $VAR environment variable
4963 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004964 * [expr, expr] List
4965 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 *
4967 * Also handle:
4968 * ! in front logical NOT
4969 * - in front unary minus
4970 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * trailing [] subscript in String or List
4972 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * "arg" must point to the first non-white of the expression.
4975 * "arg" is advanced to the next non-white after the recognized expression.
4976 *
4977 * Return OK or FAIL.
4978 */
4979 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004980eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004984 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 long n;
4987 int len;
4988 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u *start_leader, *end_leader;
4990 int ret = OK;
4991 char_u *alias;
4992
4993 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 /*
5000 * Skip '!' and '-' characters. They are handled later.
5001 */
5002 start_leader = *arg;
5003 while (**arg == '!' || **arg == '-' || **arg == '+')
5004 *arg = skipwhite(*arg + 1);
5005 end_leader = *arg;
5006
5007 switch (**arg)
5008 {
5009 /*
5010 * Number constant.
5011 */
5012 case '0':
5013 case '1':
5014 case '2':
5015 case '3':
5016 case '4':
5017 case '5':
5018 case '6':
5019 case '7':
5020 case '8':
5021 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 {
5023#ifdef FEAT_FLOAT
5024 char_u *p = skipdigits(*arg + 1);
5025 int get_float = FALSE;
5026
5027 /* We accept a float when the format matches
5028 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005029 * strict to avoid backwards compatibility problems.
5030 * Don't look for a float after the "." operator, so that
5031 * ":let vers = 1.2.3" doesn't fail. */
5032 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 get_float = TRUE;
5035 p = skipdigits(p + 2);
5036 if (*p == 'e' || *p == 'E')
5037 {
5038 ++p;
5039 if (*p == '-' || *p == '+')
5040 ++p;
5041 if (!vim_isdigit(*p))
5042 get_float = FALSE;
5043 else
5044 p = skipdigits(p + 1);
5045 }
5046 if (ASCII_ISALPHA(*p) || *p == '.')
5047 get_float = FALSE;
5048 }
5049 if (get_float)
5050 {
5051 float_T f;
5052
5053 *arg += string2float(*arg, &f);
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_FLOAT;
5057 rettv->vval.v_float = f;
5058 }
5059 }
5060 else
5061#endif
5062 {
5063 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5064 *arg += len;
5065 if (evaluate)
5066 {
5067 rettv->v_type = VAR_NUMBER;
5068 rettv->vval.v_number = n;
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * String constant: "string".
5076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079
5080 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 break;
5085
5086 /*
5087 * List: [expr, expr]
5088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Dictionary: {key: val, key: val}
5094 */
5095 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5096 break;
5097
5098 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005099 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005101 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /*
5105 * Environment variable: $VAR.
5106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /*
5111 * Register contents: @r.
5112 */
5113 case '@': ++*arg;
5114 if (evaluate)
5115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005117 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 if (**arg != NUL)
5120 ++*arg;
5121 break;
5122
5123 /*
5124 * nested expression: (expression).
5125 */
5126 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (**arg == ')')
5129 ++*arg;
5130 else if (ret == OK)
5131 {
5132 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 ret = FAIL;
5135 }
5136 break;
5137
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141
5142 if (ret == NOTDONE)
5143 {
5144 /*
5145 * Must be a variable or function name.
5146 * Can also be a curly-braces kind of name: {expr}.
5147 */
5148 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 if (alias != NULL)
5151 s = alias;
5152
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 ret = FAIL;
5155 else
5156 {
5157 if (**arg == '(') /* recursive! */
5158 {
5159 /* If "s" is the name of a variable of type VAR_FUNC
5160 * use its contents. */
5161 s = deref_func_name(s, &len);
5162
5163 /* Invoke the function. */
5164 ret = get_func_tv(s, len, rettv, arg,
5165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005167
5168 /* If evaluate is FALSE rettv->v_type was not set in
5169 * get_func_tv, but it's needed in handle_subscript() to parse
5170 * what follows. So set it here. */
5171 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5172 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005173 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005174 rettv->v_type = VAR_FUNC;
5175 }
5176
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 /* Stop the expression evaluation when immediately
5178 * aborting on error, or when an interrupt occurred or
5179 * an exception was thrown but not caught. */
5180 if (aborting())
5181 {
5182 if (ret == OK)
5183 clear_tv(rettv);
5184 ret = FAIL;
5185 }
5186 }
5187 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005189 else
5190 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005192 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 }
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 *arg = skipwhite(*arg);
5196
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005197 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5198 * expr(expr). */
5199 if (ret == OK)
5200 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 /*
5203 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5204 */
5205 if (ret == OK && evaluate && end_leader > start_leader)
5206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005208 int val = 0;
5209#ifdef FEAT_FLOAT
5210 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 if (rettv->v_type == VAR_FLOAT)
5213 f = rettv->vval.v_float;
5214 else
5215#endif
5216 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 clear_tv(rettv);
5220 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else
5223 {
5224 while (end_leader > start_leader)
5225 {
5226 --end_leader;
5227 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005228 {
5229#ifdef FEAT_FLOAT
5230 if (rettv->v_type == VAR_FLOAT)
5231 f = !f;
5232 else
5233#endif
5234 val = !val;
5235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005236 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005237 {
5238#ifdef FEAT_FLOAT
5239 if (rettv->v_type == VAR_FLOAT)
5240 f = -f;
5241 else
5242#endif
5243 val = -val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246#ifdef FEAT_FLOAT
5247 if (rettv->v_type == VAR_FLOAT)
5248 {
5249 clear_tv(rettv);
5250 rettv->vval.v_float = f;
5251 }
5252 else
5253#endif
5254 {
5255 clear_tv(rettv);
5256 rettv->v_type = VAR_NUMBER;
5257 rettv->vval.v_number = val;
5258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
5262 return ret;
5263}
5264
5265/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005266 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5267 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5269 */
5270 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005273 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276{
5277 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 long len = -1;
5281 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 if (rettv->v_type == VAR_FUNC
5286#ifdef FEAT_FLOAT
5287 || rettv->v_type == VAR_FLOAT
5288#endif
5289 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 if (verbose)
5292 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 return FAIL;
5294 }
5295
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 /*
5299 * dict.name
5300 */
5301 key = *arg + 1;
5302 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5303 ;
5304 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 }
5308 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 /*
5311 * something[idx]
5312 *
5313 * Get the (first) variable from inside the [].
5314 */
5315 *arg = skipwhite(*arg + 1);
5316 if (**arg == ':')
5317 empty1 = TRUE;
5318 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5319 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5321 {
5322 /* not a number or string */
5323 clear_tv(&var1);
5324 return FAIL;
5325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326
5327 /*
5328 * Get the second variable from inside the [:].
5329 */
5330 if (**arg == ':')
5331 {
5332 range = TRUE;
5333 *arg = skipwhite(*arg + 1);
5334 if (**arg == ']')
5335 empty2 = TRUE;
5336 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 if (!empty1)
5339 clear_tv(&var1);
5340 return FAIL;
5341 }
5342 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5343 {
5344 /* not a number or string */
5345 if (!empty1)
5346 clear_tv(&var1);
5347 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 return FAIL;
5349 }
5350 }
5351
5352 /* Check for the ']'. */
5353 if (**arg != ']')
5354 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 if (verbose)
5356 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 clear_tv(&var1);
5358 if (range)
5359 clear_tv(&var2);
5360 return FAIL;
5361 }
5362 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 }
5364
5365 if (evaluate)
5366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 n1 = 0;
5368 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 n1 = get_tv_number(&var1);
5371 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 }
5373 if (range)
5374 {
5375 if (empty2)
5376 n2 = -1;
5377 else
5378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 n2 = get_tv_number(&var2);
5380 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 }
5382 }
5383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
5386 case VAR_NUMBER:
5387 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 len = (long)STRLEN(s);
5390 if (range)
5391 {
5392 /* The resulting variable is a substring. If the indexes
5393 * are out of range the result is empty. */
5394 if (n1 < 0)
5395 {
5396 n1 = len + n1;
5397 if (n1 < 0)
5398 n1 = 0;
5399 }
5400 if (n2 < 0)
5401 n2 = len + n2;
5402 else if (n2 >= len)
5403 n2 = len;
5404 if (n1 >= len || n2 < 0 || n1 > n2)
5405 s = NULL;
5406 else
5407 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5408 }
5409 else
5410 {
5411 /* The resulting variable is a string of a single
5412 * character. If the index is too big or negative the
5413 * result is empty. */
5414 if (n1 >= len || n1 < 0)
5415 s = NULL;
5416 else
5417 s = vim_strnsave(s + n1, 1);
5418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 clear_tv(rettv);
5420 rettv->v_type = VAR_STRING;
5421 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 break;
5423
5424 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 if (n1 < 0)
5427 n1 = len + n1;
5428 if (!empty1 && (n1 < 0 || n1 >= len))
5429 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005430 /* For a range we allow invalid values and return an empty
5431 * list. A list index out of range is an error. */
5432 if (!range)
5433 {
5434 if (verbose)
5435 EMSGN(_(e_listidx), n1);
5436 return FAIL;
5437 }
5438 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440 if (range)
5441 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 list_T *l;
5443 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444
5445 if (n2 < 0)
5446 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005447 else if (n2 >= len)
5448 n2 = len - 1;
5449 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005450 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 l = list_alloc();
5452 if (l == NULL)
5453 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 n1 <= n2; ++n1)
5456 {
5457 if (list_append_tv(l, &item->li_tv) == FAIL)
5458 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005459 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 return FAIL;
5461 }
5462 item = item->li_next;
5463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(rettv);
5465 rettv->v_type = VAR_LIST;
5466 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005467 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 else
5470 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005471 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 clear_tv(rettv);
5473 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476
5477 case VAR_DICT:
5478 if (range)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 if (len == -1)
5483 clear_tv(&var1);
5484 return FAIL;
5485 }
5486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
5489 if (len == -1)
5490 {
5491 key = get_tv_string(&var1);
5492 if (*key == NUL)
5493 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005494 if (verbose)
5495 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 clear_tv(&var1);
5497 return FAIL;
5498 }
5499 }
5500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005501 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005503 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005504 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005505 if (len == -1)
5506 clear_tv(&var1);
5507 if (item == NULL)
5508 return FAIL;
5509
5510 copy_tv(&item->di_tv, &var1);
5511 clear_tv(rettv);
5512 *rettv = var1;
5513 }
5514 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 }
5516 }
5517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 return OK;
5519}
5520
5521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 * Get an option value.
5523 * "arg" points to the '&' or '+' before the option name.
5524 * "arg" is advanced to character after the option name.
5525 * Return OK or FAIL.
5526 */
5527 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 int evaluate;
5532{
5533 char_u *option_end;
5534 long numval;
5535 char_u *stringval;
5536 int opt_type;
5537 int c;
5538 int working = (**arg == '+'); /* has("+option") */
5539 int ret = OK;
5540 int opt_flags;
5541
5542 /*
5543 * Isolate the option name and find its value.
5544 */
5545 option_end = find_option_end(arg, &opt_flags);
5546 if (option_end == NULL)
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 EMSG2(_("E112: Option name missing: %s"), *arg);
5550 return FAIL;
5551 }
5552
5553 if (!evaluate)
5554 {
5555 *arg = option_end;
5556 return OK;
5557 }
5558
5559 c = *option_end;
5560 *option_end = NUL;
5561 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if (opt_type == -3) /* invalid name */
5565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 EMSG2(_("E113: Unknown option: %s"), *arg);
5568 ret = FAIL;
5569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
5572 if (opt_type == -2) /* hidden string option */
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv->v_type = VAR_STRING;
5575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else if (opt_type == -1) /* hidden number option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_NUMBER;
5580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == 1) /* number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else /* string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 }
5593 else if (working && (opt_type == -2 || opt_type == -1))
5594 ret = FAIL;
5595
5596 *option_end = c; /* put back for error messages */
5597 *arg = option_end;
5598
5599 return ret;
5600}
5601
5602/*
5603 * Allocate a variable for a string constant.
5604 * Return OK or FAIL.
5605 */
5606 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 int evaluate;
5611{
5612 char_u *p;
5613 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 int extra = 0;
5615
5616 /*
5617 * Find the end of the string, skipping backslashed characters.
5618 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (*p == '\\' && p[1] != NUL)
5622 {
5623 ++p;
5624 /* A "\<x>" form occupies at least 4 characters, and produces up
5625 * to 6 characters: reserve space for 2 extra */
5626 if (*p == '<')
5627 extra += 2;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630
5631 if (*p != '"')
5632 {
5633 EMSG2(_("E114: Missing quote: %s"), *arg);
5634 return FAIL;
5635 }
5636
5637 /* If only parsing, set *arg and return here */
5638 if (!evaluate)
5639 {
5640 *arg = p + 1;
5641 return OK;
5642 }
5643
5644 /*
5645 * Copy the string into allocated memory, handling backslashed
5646 * characters.
5647 */
5648 name = alloc((unsigned)(p - *arg + extra));
5649 if (name == NULL)
5650 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 rettv->v_type = VAR_STRING;
5652 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 if (*p == '\\')
5657 {
5658 switch (*++p)
5659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 case 'b': *name++ = BS; ++p; break;
5661 case 'e': *name++ = ESC; ++p; break;
5662 case 'f': *name++ = FF; ++p; break;
5663 case 'n': *name++ = NL; ++p; break;
5664 case 'r': *name++ = CAR; ++p; break;
5665 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 case 'X': /* hex: "\x1", "\x12" */
5668 case 'x':
5669 case 'u': /* Unicode: "\u0023" */
5670 case 'U':
5671 if (vim_isxdigit(p[1]))
5672 {
5673 int n, nr;
5674 int c = toupper(*p);
5675
5676 if (c == 'X')
5677 n = 2;
5678 else
5679 n = 4;
5680 nr = 0;
5681 while (--n >= 0 && vim_isxdigit(p[1]))
5682 {
5683 ++p;
5684 nr = (nr << 4) + hex2nr(*p);
5685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687#ifdef FEAT_MBYTE
5688 /* For "\u" store the number according to
5689 * 'encoding'. */
5690 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
5693#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 break;
5697
5698 /* octal: "\1", "\12", "\123" */
5699 case '0':
5700 case '1':
5701 case '2':
5702 case '3':
5703 case '4':
5704 case '5':
5705 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 case '7': *name = *p++ - '0';
5707 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name = (*name << 3) + *p++ - '0';
5710 if (*p >= '0' && *p <= '7')
5711 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715
5716 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 if (extra != 0)
5719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 break;
5722 }
5723 /* FALLTHROUGH */
5724
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 }
5729 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 *arg = p + 1;
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 return OK;
5737}
5738
5739/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * Return OK or FAIL.
5742 */
5743 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005744get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 int evaluate;
5748{
5749 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 int reduce = 0;
5752
5753 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 */
5756 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 break;
5762 ++reduce;
5763 ++p;
5764 }
5765 }
5766
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 return FAIL;
5771 }
5772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 if (!evaluate)
5775 {
5776 *arg = p + 1;
5777 return OK;
5778 }
5779
5780 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 */
5783 str = alloc((unsigned)((p - *arg) - reduce));
5784 if (str == NULL)
5785 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 rettv->v_type = VAR_STRING;
5787 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 break;
5795 ++p;
5796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 *arg = p + 1;
5801
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 return OK;
5803}
5804
5805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * Allocate a variable for a List and fill it from "*arg".
5807 * Return OK or FAIL.
5808 */
5809 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 int evaluate;
5814{
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 list_T *l = NULL;
5816 typval_T tv;
5817 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818
5819 if (evaluate)
5820 {
5821 l = list_alloc();
5822 if (l == NULL)
5823 return FAIL;
5824 }
5825
5826 *arg = skipwhite(*arg + 1);
5827 while (**arg != ']' && **arg != NUL)
5828 {
5829 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5830 goto failret;
5831 if (evaluate)
5832 {
5833 item = listitem_alloc();
5834 if (item != NULL)
5835 {
5836 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005837 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 list_append(l, item);
5839 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005840 else
5841 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 }
5843
5844 if (**arg == ']')
5845 break;
5846 if (**arg != ',')
5847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 goto failret;
5850 }
5851 *arg = skipwhite(*arg + 1);
5852 }
5853
5854 if (**arg != ']')
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857failret:
5858 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005859 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 return FAIL;
5861 }
5862
5863 *arg = skipwhite(*arg + 1);
5864 if (evaluate)
5865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005866 rettv->v_type = VAR_LIST;
5867 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 ++l->lv_refcount;
5869 }
5870
5871 return OK;
5872}
5873
5874/*
5875 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005876 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005878 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879list_alloc()
5880{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881 list_T *l;
5882
5883 l = (list_T *)alloc_clear(sizeof(list_T));
5884 if (l != NULL)
5885 {
5886 /* Prepend the list to the list of lists for garbage collection. */
5887 if (first_list != NULL)
5888 first_list->lv_used_prev = l;
5889 l->lv_used_prev = NULL;
5890 l->lv_used_next = first_list;
5891 first_list = l;
5892 }
5893 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894}
5895
5896/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005897 * Allocate an empty list for a return value.
5898 * Returns OK or FAIL.
5899 */
5900 static int
5901rettv_list_alloc(rettv)
5902 typval_T *rettv;
5903{
5904 list_T *l = list_alloc();
5905
5906 if (l == NULL)
5907 return FAIL;
5908
5909 rettv->vval.v_list = l;
5910 rettv->v_type = VAR_LIST;
5911 ++l->lv_refcount;
5912 return OK;
5913}
5914
5915/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 * Unreference a list: decrement the reference count and free it when it
5917 * becomes zero.
5918 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005919 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005923 if (l != NULL && --l->lv_refcount <= 0)
5924 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925}
5926
5927/*
5928 * Free a list, including all items it points to.
5929 * Ignores the reference count.
5930 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005931 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005932list_free(l, recurse)
5933 list_T *l;
5934 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935{
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005938 /* Remove the list from the list of lists for garbage collection. */
5939 if (l->lv_used_prev == NULL)
5940 first_list = l->lv_used_next;
5941 else
5942 l->lv_used_prev->lv_used_next = l->lv_used_next;
5943 if (l->lv_used_next != NULL)
5944 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5945
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005948 /* Remove the item before deleting it. */
5949 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005950 if (recurse || (item->li_tv.v_type != VAR_LIST
5951 && item->li_tv.v_type != VAR_DICT))
5952 clear_tv(&item->li_tv);
5953 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 }
5955 vim_free(l);
5956}
5957
5958/*
5959 * Allocate a list item.
5960 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005961 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962listitem_alloc()
5963{
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965}
5966
5967/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005968 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
5970 static void
5971listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 vim_free(item);
5976}
5977
5978/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005979 * Remove a list item from a List and free it. Also clears the value.
5980 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005981 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005982listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 list_T *l;
5984 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005985{
5986 list_remove(l, item, item);
5987 listitem_free(item);
5988}
5989
5990/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 * Get the number of items in a list.
5992 */
5993 static long
5994list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 if (l == NULL)
5998 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000}
6001
6002/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003 * Return TRUE when two lists have exactly the same values.
6004 */
6005 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 list_T *l1;
6008 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006014 if (l1 == NULL || l2 == NULL)
6015 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006016 if (l1 == l2)
6017 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 if (list_len(l1) != list_len(l2))
6019 return FALSE;
6020
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 for (item1 = l1->lv_first, item2 = l2->lv_first;
6022 item1 != NULL && item2 != NULL;
6023 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006024 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 return FALSE;
6026 return item1 == NULL && item2 == NULL;
6027}
6028
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006029#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6030 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006031/*
6032 * Return the dictitem that an entry in a hashtable points to.
6033 */
6034 dictitem_T *
6035dict_lookup(hi)
6036 hashitem_T *hi;
6037{
6038 return HI2DI(hi);
6039}
6040#endif
6041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 * Return TRUE when two dictionaries have exactly the same key/values.
6044 */
6045 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006046dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 dict_T *d1;
6048 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006050 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051{
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 hashitem_T *hi;
6053 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006056 if (d1 == NULL || d2 == NULL)
6057 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006058 if (d1 == d2)
6059 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 if (dict_len(d1) != dict_len(d2))
6061 return FALSE;
6062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006063 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 if (!HASHITEM_EMPTY(hi))
6067 {
6068 item2 = dict_find(d2, hi->hi_key, -1);
6069 if (item2 == NULL)
6070 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006072 return FALSE;
6073 --todo;
6074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 }
6076 return TRUE;
6077}
6078
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079static int tv_equal_recurse_limit;
6080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006084 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085 */
6086 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 typval_T *tv1;
6089 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090 int ic; /* ignore case */
6091 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092{
6093 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006094 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006096 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 * recursiveness to a limit. We guess they are equal then.
6103 * A fixed limit has the problem of still taking an awful long time.
6104 * Reduce the limit every time running into it. That should work fine for
6105 * deeply linked structures that are not recursively linked and catch
6106 * recursiveness quickly. */
6107 if (!recursive)
6108 tv_equal_recurse_limit = 1000;
6109 if (recursive_cnt >= tv_equal_recurse_limit)
6110 {
6111 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006117 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 ++recursive_cnt;
6119 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6120 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006121 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122
6123 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 ++recursive_cnt;
6125 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6126 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006127 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128
6129 case VAR_FUNC:
6130 return (tv1->vval.v_string != NULL
6131 && tv2->vval.v_string != NULL
6132 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6133
6134 case VAR_NUMBER:
6135 return tv1->vval.v_number == tv2->vval.v_number;
6136
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006137#ifdef FEAT_FLOAT
6138 case VAR_FLOAT:
6139 return tv1->vval.v_float == tv2->vval.v_float;
6140#endif
6141
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006142 case VAR_STRING:
6143 s1 = get_tv_string_buf(tv1, buf1);
6144 s2 = get_tv_string_buf(tv2, buf2);
6145 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006146 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147
6148 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006149 return TRUE;
6150}
6151
6152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 * Locate item with index "n" in list "l" and return it.
6154 * A negative index is counted from the end; -1 is the last item.
6155 * Returns NULL when "n" is out of range.
6156 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006157 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 long n;
6161{
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 long idx;
6164
6165 if (l == NULL)
6166 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006167
6168 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006170 n = l->lv_len + n;
6171
6172 /* Check for index out of range. */
6173 if (n < 0 || n >= l->lv_len)
6174 return NULL;
6175
6176 /* When there is a cached index may start search from there. */
6177 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006179 if (n < l->lv_idx / 2)
6180 {
6181 /* closest to the start of the list */
6182 item = l->lv_first;
6183 idx = 0;
6184 }
6185 else if (n > (l->lv_idx + l->lv_len) / 2)
6186 {
6187 /* closest to the end of the list */
6188 item = l->lv_last;
6189 idx = l->lv_len - 1;
6190 }
6191 else
6192 {
6193 /* closest to the cached index */
6194 item = l->lv_idx_item;
6195 idx = l->lv_idx;
6196 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 }
6198 else
6199 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 if (n < l->lv_len / 2)
6201 {
6202 /* closest to the start of the list */
6203 item = l->lv_first;
6204 idx = 0;
6205 }
6206 else
6207 {
6208 /* closest to the end of the list */
6209 item = l->lv_last;
6210 idx = l->lv_len - 1;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006213
6214 while (n > idx)
6215 {
6216 /* search forward */
6217 item = item->li_next;
6218 ++idx;
6219 }
6220 while (n < idx)
6221 {
6222 /* search backward */
6223 item = item->li_prev;
6224 --idx;
6225 }
6226
6227 /* cache the used index */
6228 l->lv_idx = idx;
6229 l->lv_idx_item = item;
6230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 return item;
6232}
6233
6234/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006235 * Get list item "l[idx]" as a number.
6236 */
6237 static long
6238list_find_nr(l, idx, errorp)
6239 list_T *l;
6240 long idx;
6241 int *errorp; /* set to TRUE when something wrong */
6242{
6243 listitem_T *li;
6244
6245 li = list_find(l, idx);
6246 if (li == NULL)
6247 {
6248 if (errorp != NULL)
6249 *errorp = TRUE;
6250 return -1L;
6251 }
6252 return get_tv_number_chk(&li->li_tv, errorp);
6253}
6254
6255/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006256 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6257 */
6258 char_u *
6259list_find_str(l, idx)
6260 list_T *l;
6261 long idx;
6262{
6263 listitem_T *li;
6264
6265 li = list_find(l, idx - 1);
6266 if (li == NULL)
6267 {
6268 EMSGN(_(e_listidx), idx);
6269 return NULL;
6270 }
6271 return get_tv_string(&li->li_tv);
6272}
6273
6274/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275 * Locate "item" list "l" and return its index.
6276 * Returns -1 when "item" is not in the list.
6277 */
6278 static long
6279list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 list_T *l;
6281 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006282{
6283 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006285
6286 if (l == NULL)
6287 return -1;
6288 idx = 0;
6289 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6290 ++idx;
6291 if (li == NULL)
6292 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006293 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006294}
6295
6296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 * Append item "item" to the end of list "l".
6298 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006299 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 list_T *l;
6302 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303{
6304 if (l->lv_last == NULL)
6305 {
6306 /* empty list */
6307 l->lv_first = item;
6308 l->lv_last = item;
6309 item->li_prev = NULL;
6310 }
6311 else
6312 {
6313 l->lv_last->li_next = item;
6314 item->li_prev = l->lv_last;
6315 l->lv_last = item;
6316 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 item->li_next = NULL;
6319}
6320
6321/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006323 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006325 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *l;
6328 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331
Bram Moolenaar05159a02005-02-26 23:04:13 +00006332 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 copy_tv(tv, &li->li_tv);
6335 list_append(l, li);
6336 return OK;
6337}
6338
6339/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006340 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006341 * Return FAIL when out of memory.
6342 */
6343 int
6344list_append_dict(list, dict)
6345 list_T *list;
6346 dict_T *dict;
6347{
6348 listitem_T *li = listitem_alloc();
6349
6350 if (li == NULL)
6351 return FAIL;
6352 li->li_tv.v_type = VAR_DICT;
6353 li->li_tv.v_lock = 0;
6354 li->li_tv.vval.v_dict = dict;
6355 list_append(list, li);
6356 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 return OK;
6358}
6359
6360/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363 * Returns FAIL when out of memory.
6364 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006365 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006366list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006367 list_T *l;
6368 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006369 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006370{
6371 listitem_T *li = listitem_alloc();
6372
6373 if (li == NULL)
6374 return FAIL;
6375 list_append(l, li);
6376 li->li_tv.v_type = VAR_STRING;
6377 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006378 if (str == NULL)
6379 li->li_tv.vval.v_string = NULL;
6380 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006381 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 return FAIL;
6383 return OK;
6384}
6385
6386/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006387 * Append "n" to list "l".
6388 * Returns FAIL when out of memory.
6389 */
6390 static int
6391list_append_number(l, n)
6392 list_T *l;
6393 varnumber_T n;
6394{
6395 listitem_T *li;
6396
6397 li = listitem_alloc();
6398 if (li == NULL)
6399 return FAIL;
6400 li->li_tv.v_type = VAR_NUMBER;
6401 li->li_tv.v_lock = 0;
6402 li->li_tv.vval.v_number = n;
6403 list_append(l, li);
6404 return OK;
6405}
6406
6407/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 * If "item" is NULL append at the end.
6410 * Return FAIL when out of memory.
6411 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006412 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 list_T *l;
6415 typval_T *tv;
6416 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417{
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419
6420 if (ni == NULL)
6421 return FAIL;
6422 copy_tv(tv, &ni->li_tv);
6423 if (item == NULL)
6424 /* Append new item at end of list. */
6425 list_append(l, ni);
6426 else
6427 {
6428 /* Insert new item before existing item. */
6429 ni->li_prev = item->li_prev;
6430 ni->li_next = item;
6431 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 ++l->lv_idx;
6435 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006437 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439 l->lv_idx_item = NULL;
6440 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 }
6444 return OK;
6445}
6446
6447/*
6448 * Extend "l1" with "l2".
6449 * If "bef" is NULL append at the end, otherwise insert before this item.
6450 * Returns FAIL when out of memory.
6451 */
6452 static int
6453list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 list_T *l1;
6455 list_T *l2;
6456 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006459 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006461 /* We also quit the loop when we have inserted the original item count of
6462 * the list, avoid a hang when we extend a list with itself. */
6463 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6465 return FAIL;
6466 return OK;
6467}
6468
6469/*
6470 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6471 * Return FAIL when out of memory.
6472 */
6473 static int
6474list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 list_T *l1;
6476 list_T *l2;
6477 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478{
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006481 if (l1 == NULL || l2 == NULL)
6482 return FAIL;
6483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486 if (l == NULL)
6487 return FAIL;
6488 tv->v_type = VAR_LIST;
6489 tv->vval.v_list = l;
6490
6491 /* append all items from the second list */
6492 return list_extend(l, l2, NULL);
6493}
6494
6495/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499 * Returns NULL when out of memory.
6500 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006502list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 list_T *copy;
6508 listitem_T *item;
6509 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510
6511 if (orig == NULL)
6512 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513
6514 copy = list_alloc();
6515 if (copy != NULL)
6516 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 if (copyID != 0)
6518 {
6519 /* Do this before adding the items, because one of the items may
6520 * refer back to this list. */
6521 orig->lv_copyID = copyID;
6522 orig->lv_copylist = copy;
6523 }
6524 for (item = orig->lv_first; item != NULL && !got_int;
6525 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 {
6527 ni = listitem_alloc();
6528 if (ni == NULL)
6529 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 {
6532 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6533 {
6534 vim_free(ni);
6535 break;
6536 }
6537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006539 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 list_append(copy, ni);
6541 }
6542 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 if (item != NULL)
6544 {
6545 list_unref(copy);
6546 copy = NULL;
6547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 }
6549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 return copy;
6551}
6552
6553/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006555 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006557 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006558list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l;
6560 listitem_T *item;
6561 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562{
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 /* notify watchers */
6566 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 list_fix_watch(l, ip);
6570 if (ip == item2)
6571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573
6574 if (item2->li_next == NULL)
6575 l->lv_last = item->li_prev;
6576 else
6577 item2->li_next->li_prev = item->li_prev;
6578 if (item->li_prev == NULL)
6579 l->lv_first = item2->li_next;
6580 else
6581 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583}
6584
6585/*
6586 * Return an allocated string with the string representation of a list.
6587 * May return NULL.
6588 */
6589 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006592 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
6594 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 if (tv->vval.v_list == NULL)
6597 return NULL;
6598 ga_init2(&ga, (int)sizeof(char), 80);
6599 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006600 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 {
6602 vim_free(ga.ga_data);
6603 return NULL;
6604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 ga_append(&ga, ']');
6606 ga_append(&ga, NUL);
6607 return (char_u *)ga.ga_data;
6608}
6609
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006610typedef struct join_S {
6611 char_u *s;
6612 char_u *tofree;
6613} join_T;
6614
6615 static int
6616list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6617 garray_T *gap; /* to store the result in */
6618 list_T *l;
6619 char_u *sep;
6620 int echo_style;
6621 int copyID;
6622 garray_T *join_gap; /* to keep each list item string */
6623{
6624 int i;
6625 join_T *p;
6626 int len;
6627 int sumlen = 0;
6628 int first = TRUE;
6629 char_u *tofree;
6630 char_u numbuf[NUMBUFLEN];
6631 listitem_T *item;
6632 char_u *s;
6633
6634 /* Stringify each item in the list. */
6635 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6636 {
6637 if (echo_style)
6638 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6639 else
6640 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6641 if (s == NULL)
6642 return FAIL;
6643
6644 len = (int)STRLEN(s);
6645 sumlen += len;
6646
6647 ga_grow(join_gap, 1);
6648 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6649 if (tofree != NULL || s != numbuf)
6650 {
6651 p->s = s;
6652 p->tofree = tofree;
6653 }
6654 else
6655 {
6656 p->s = vim_strnsave(s, len);
6657 p->tofree = p->s;
6658 }
6659
6660 line_breakcheck();
6661 }
6662
6663 /* Allocate result buffer with its total size, avoid re-allocation and
6664 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6665 if (join_gap->ga_len >= 2)
6666 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6667 if (ga_grow(gap, sumlen + 2) == FAIL)
6668 return FAIL;
6669
6670 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6671 {
6672 if (first)
6673 first = FALSE;
6674 else
6675 ga_concat(gap, sep);
6676 p = ((join_T *)join_gap->ga_data) + i;
6677
6678 if (p->s != NULL)
6679 ga_concat(gap, p->s);
6680 line_breakcheck();
6681 }
6682
6683 return OK;
6684}
6685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006688 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006692list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006694 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006696 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006697 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699 garray_T join_ga;
6700 int retval;
6701 join_T *p;
6702 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6705 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6706
6707 /* Dispose each item in join_ga. */
6708 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710 p = (join_T *)join_ga.ga_data;
6711 for (i = 0; i < join_ga.ga_len; ++i)
6712 {
6713 vim_free(p->tofree);
6714 ++p;
6715 }
6716 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718
6719 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720}
6721
6722/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006723 * Garbage collection for lists and dictionaries.
6724 *
6725 * We use reference counts to be able to free most items right away when they
6726 * are no longer used. But for composite items it's possible that it becomes
6727 * unused while the reference count is > 0: When there is a recursive
6728 * reference. Example:
6729 * :let l = [1, 2, 3]
6730 * :let d = {9: l}
6731 * :let l[1] = d
6732 *
6733 * Since this is quite unusual we handle this with garbage collection: every
6734 * once in a while find out which lists and dicts are not referenced from any
6735 * variable.
6736 *
6737 * Here is a good reference text about garbage collection (refers to Python
6738 * but it applies to all reference-counting mechanisms):
6739 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006740 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741
6742/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 * Do garbage collection for lists and dicts.
6744 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 int
6747garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006748{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 buf_T *buf;
6751 win_T *wp;
6752 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006753 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006754 int did_free;
6755 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006756#ifdef FEAT_WINDOWS
6757 tabpage_T *tp;
6758#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006760 /* Only do this once. */
6761 want_garbage_collect = FALSE;
6762 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006763 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006764
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006765 /* We advance by two because we add one for items referenced through
6766 * previous_funccal. */
6767 current_copyID += COPYID_INC;
6768 copyID = current_copyID;
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /*
6771 * 1. Go through all accessible variables and mark all lists and dicts
6772 * with copyID.
6773 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774
6775 /* Don't free variables in the previous_funccal list unless they are only
6776 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006777 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006778 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6779 {
6780 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6781 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6782 }
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /* script-local variables */
6785 for (i = 1; i <= ga_scripts.ga_len; ++i)
6786 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6787
6788 /* buffer-local variables */
6789 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006790 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791
6792 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006794 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006795
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006796#ifdef FEAT_WINDOWS
6797 /* tabpage-local variables */
6798 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006799 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006800#endif
6801
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006802 /* global variables */
6803 set_ref_in_ht(&globvarht, copyID);
6804
6805 /* function-local variables */
6806 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6807 {
6808 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6809 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6810 }
6811
Bram Moolenaard812df62008-11-09 12:46:09 +00006812 /* v: vars */
6813 set_ref_in_ht(&vimvarht, copyID);
6814
Bram Moolenaar1dced572012-04-05 16:54:08 +02006815#ifdef FEAT_LUA
6816 set_ref_in_lua(copyID);
6817#endif
6818
Bram Moolenaardb913952012-06-29 12:54:53 +02006819#ifdef FEAT_PYTHON
6820 set_ref_in_python(copyID);
6821#endif
6822
6823#ifdef FEAT_PYTHON3
6824 set_ref_in_python3(copyID);
6825#endif
6826
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006827 /*
6828 * 2. Free lists and dictionaries that are not referenced.
6829 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 did_free = free_unref_items(copyID);
6831
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006832 /*
6833 * 3. Check if any funccal can be freed now.
6834 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 for (pfc = &previous_funccal; *pfc != NULL; )
6836 {
6837 if (can_free_funccal(*pfc, copyID))
6838 {
6839 fc = *pfc;
6840 *pfc = fc->caller;
6841 free_funccal(fc, TRUE);
6842 did_free = TRUE;
6843 did_free_funccal = TRUE;
6844 }
6845 else
6846 pfc = &(*pfc)->caller;
6847 }
6848 if (did_free_funccal)
6849 /* When a funccal was freed some more items might be garbage
6850 * collected, so run again. */
6851 (void)garbage_collect();
6852
6853 return did_free;
6854}
6855
6856/*
6857 * Free lists and dictionaries that are no longer referenced.
6858 */
6859 static int
6860free_unref_items(copyID)
6861 int copyID;
6862{
6863 dict_T *dd;
6864 list_T *ll;
6865 int did_free = FALSE;
6866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 */
6870 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006873 /* Free the Dictionary and ordinary items it contains, but don't
6874 * recurse into Lists and Dictionaries, they will be in the list
6875 * of dicts or list of lists. */
6876 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 did_free = TRUE;
6878
6879 /* restart, next dict may also have been freed */
6880 dd = first_dict;
6881 }
6882 else
6883 dd = dd->dv_used_next;
6884
6885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of lists and free items without the copyID.
6887 * But don't free a list that has a watcher (used in a for loop), these
6888 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 */
6890 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6892 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006894 /* Free the List and ordinary items it contains, but don't recurse
6895 * into Lists and Dictionaries, they will be in the list of dicts
6896 * or list of lists. */
6897 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 did_free = TRUE;
6899
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006900 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 ll = first_list;
6902 }
6903 else
6904 ll = ll->lv_used_next;
6905
6906 return did_free;
6907}
6908
6909/*
6910 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6911 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006912 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913set_ref_in_ht(ht, copyID)
6914 hashtab_T *ht;
6915 int copyID;
6916{
6917 int todo;
6918 hashitem_T *hi;
6919
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006920 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 for (hi = ht->ht_array; todo > 0; ++hi)
6922 if (!HASHITEM_EMPTY(hi))
6923 {
6924 --todo;
6925 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6926 }
6927}
6928
6929/*
6930 * Mark all lists and dicts referenced through list "l" with "copyID".
6931 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006932 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933set_ref_in_list(l, copyID)
6934 list_T *l;
6935 int copyID;
6936{
6937 listitem_T *li;
6938
6939 for (li = l->lv_first; li != NULL; li = li->li_next)
6940 set_ref_in_item(&li->li_tv, copyID);
6941}
6942
6943/*
6944 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6945 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006946 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947set_ref_in_item(tv, copyID)
6948 typval_T *tv;
6949 int copyID;
6950{
6951 dict_T *dd;
6952 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953
6954 switch (tv->v_type)
6955 {
6956 case VAR_DICT:
6957 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006958 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /* Didn't see this dict yet. */
6961 dd->dv_copyID = copyID;
6962 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965
6966 case VAR_LIST:
6967 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006968 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006969 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 /* Didn't see this list yet. */
6971 ll->lv_copyID = copyID;
6972 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977}
6978
6979/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980 * Allocate an empty header for a dictionary.
6981 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006982 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983dict_alloc()
6984{
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006989 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006990 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 if (first_dict != NULL)
6992 first_dict->dv_used_prev = d;
6993 d->dv_used_next = first_dict;
6994 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006995 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996
Bram Moolenaar33570922005-01-25 22:26:29 +00006997 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006998 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006999 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007003 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004}
7005
7006/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007007 * Allocate an empty dict for a return value.
7008 * Returns OK or FAIL.
7009 */
7010 static int
7011rettv_dict_alloc(rettv)
7012 typval_T *rettv;
7013{
7014 dict_T *d = dict_alloc();
7015
7016 if (d == NULL)
7017 return FAIL;
7018
7019 rettv->vval.v_dict = d;
7020 rettv->v_type = VAR_DICT;
7021 ++d->dv_refcount;
7022 return OK;
7023}
7024
7025
7026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 * Unreference a Dictionary: decrement the reference count and free it when it
7028 * becomes zero.
7029 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007030 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007034 if (d != NULL && --d->dv_refcount <= 0)
7035 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036}
7037
7038/*
7039 * Free a Dictionary, including all items it contains.
7040 * Ignores the reference count.
7041 */
7042 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007043dict_free(d, recurse)
7044 dict_T *d;
7045 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007046{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 /* Remove the dict from the list of dicts for garbage collection. */
7052 if (d->dv_used_prev == NULL)
7053 first_dict = d->dv_used_next;
7054 else
7055 d->dv_used_prev->dv_used_next = d->dv_used_next;
7056 if (d->dv_used_next != NULL)
7057 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7058
7059 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007060 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007061 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064 if (!HASHITEM_EMPTY(hi))
7065 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007066 /* Remove the item before deleting it, just in case there is
7067 * something recursive causing trouble. */
7068 di = HI2DI(hi);
7069 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007070 if (recurse || (di->di_tv.v_type != VAR_LIST
7071 && di->di_tv.v_type != VAR_DICT))
7072 clear_tv(&di->di_tv);
7073 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 --todo;
7075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 vim_free(d);
7079}
7080
7081/*
7082 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 * The "key" is copied to the new item.
7084 * Note that the value of the item "di_tv" still needs to be initialized!
7085 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007087 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088dictitem_alloc(key)
7089 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090{
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007093 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007097 di->di_flags = 0;
7098 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100}
7101
7102/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 * Make a copy of a Dictionary item.
7104 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7112 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113 if (di != NULL)
7114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007116 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 copy_tv(&org->di_tv, &di->di_tv);
7118 }
7119 return di;
7120}
7121
7122/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 * Remove item "item" from Dictionary "dict" and free it.
7124 */
7125 static void
7126dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dict_T *dict;
7128 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129{
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 if (HASHITEM_EMPTY(hi))
7134 EMSG2(_(e_intern2), "dictitem_remove()");
7135 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 dictitem_free(item);
7138}
7139
7140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 * Free a dict item. Also clears the value.
7142 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007143 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 clear_tv(&item->di_tv);
7148 vim_free(item);
7149}
7150
7151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7153 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007154 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 * Returns NULL when out of memory.
7156 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007161 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162{
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dict_T *copy;
7164 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167
7168 if (orig == NULL)
7169 return NULL;
7170
7171 copy = dict_alloc();
7172 if (copy != NULL)
7173 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007174 if (copyID != 0)
7175 {
7176 orig->dv_copyID = copyID;
7177 orig->dv_copydict = copy;
7178 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007179 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 --todo;
7185
7186 di = dictitem_alloc(hi->hi_key);
7187 if (di == NULL)
7188 break;
7189 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 {
7191 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7192 copyID) == FAIL)
7193 {
7194 vim_free(di);
7195 break;
7196 }
7197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 else
7199 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7200 if (dict_add(copy, di) == FAIL)
7201 {
7202 dictitem_free(di);
7203 break;
7204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007209 if (todo > 0)
7210 {
7211 dict_unref(copy);
7212 copy = NULL;
7213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 }
7215
7216 return copy;
7217}
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007223 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 dict_T *d;
7226 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229}
7230
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007232 * Add a number or string entry to dictionary "d".
7233 * When "str" is NULL use number "nr", otherwise use "str".
7234 * Returns FAIL when out of memory and when key already exists.
7235 */
7236 int
7237dict_add_nr_str(d, key, nr, str)
7238 dict_T *d;
7239 char *key;
7240 long nr;
7241 char_u *str;
7242{
7243 dictitem_T *item;
7244
7245 item = dictitem_alloc((char_u *)key);
7246 if (item == NULL)
7247 return FAIL;
7248 item->di_tv.v_lock = 0;
7249 if (str == NULL)
7250 {
7251 item->di_tv.v_type = VAR_NUMBER;
7252 item->di_tv.vval.v_number = nr;
7253 }
7254 else
7255 {
7256 item->di_tv.v_type = VAR_STRING;
7257 item->di_tv.vval.v_string = vim_strsave(str);
7258 }
7259 if (dict_add(d, item) == FAIL)
7260 {
7261 dictitem_free(item);
7262 return FAIL;
7263 }
7264 return OK;
7265}
7266
7267/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007268 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007269 * Returns FAIL when out of memory and when key already exists.
7270 */
7271 int
7272dict_add_list(d, key, list)
7273 dict_T *d;
7274 char *key;
7275 list_T *list;
7276{
7277 dictitem_T *item;
7278
7279 item = dictitem_alloc((char_u *)key);
7280 if (item == NULL)
7281 return FAIL;
7282 item->di_tv.v_lock = 0;
7283 item->di_tv.v_type = VAR_LIST;
7284 item->di_tv.vval.v_list = list;
7285 if (dict_add(d, item) == FAIL)
7286 {
7287 dictitem_free(item);
7288 return FAIL;
7289 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007290 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007291 return OK;
7292}
7293
7294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 * Get the number of items in a Dictionary.
7296 */
7297 static long
7298dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 if (d == NULL)
7302 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304}
7305
7306/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 * Find item "key[len]" in Dictionary "d".
7308 * If "len" is negative use strlen(key).
7309 * Returns NULL when not found.
7310 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007311 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 char_u *key;
7315 int len;
7316{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317#define AKEYLEN 200
7318 char_u buf[AKEYLEN];
7319 char_u *akey;
7320 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (len < 0)
7324 akey = key;
7325 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007326 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 tofree = akey = vim_strnsave(key, len);
7328 if (akey == NULL)
7329 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007330 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 else
7332 {
7333 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007334 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 akey = buf;
7336 }
7337
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 vim_free(tofree);
7340 if (HASHITEM_EMPTY(hi))
7341 return NULL;
7342 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343}
7344
7345/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 * Get a string item from a dictionary.
7347 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007348 * Returns NULL if the entry doesn't exist or out of memory.
7349 */
7350 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352 dict_T *d;
7353 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355{
7356 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007357 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007358
7359 di = dict_find(d, key, -1);
7360 if (di == NULL)
7361 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007362 s = get_tv_string(&di->di_tv);
7363 if (save && s != NULL)
7364 s = vim_strsave(s);
7365 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366}
7367
7368/*
7369 * Get a number item from a dictionary.
7370 * Returns 0 if the entry doesn't exist or out of memory.
7371 */
7372 long
7373get_dict_number(d, key)
7374 dict_T *d;
7375 char_u *key;
7376{
7377 dictitem_T *di;
7378
7379 di = dict_find(d, key, -1);
7380 if (di == NULL)
7381 return 0;
7382 return get_tv_number(&di->di_tv);
7383}
7384
7385/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 * Return an allocated string with the string representation of a Dictionary.
7387 * May return NULL.
7388 */
7389 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393{
7394 garray_T ga;
7395 int first = TRUE;
7396 char_u *tofree;
7397 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 return NULL;
7405 ga_init2(&ga, (int)sizeof(char), 80);
7406 ga_append(&ga, '{');
7407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007408 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007409 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413 --todo;
7414
7415 if (first)
7416 first = FALSE;
7417 else
7418 ga_concat(&ga, (char_u *)", ");
7419
7420 tofree = string_quote(hi->hi_key, FALSE);
7421 if (tofree != NULL)
7422 {
7423 ga_concat(&ga, tofree);
7424 vim_free(tofree);
7425 }
7426 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007427 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428 if (s != NULL)
7429 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007431 if (s == NULL)
7432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007435 if (todo > 0)
7436 {
7437 vim_free(ga.ga_data);
7438 return NULL;
7439 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440
7441 ga_append(&ga, '}');
7442 ga_append(&ga, NUL);
7443 return (char_u *)ga.ga_data;
7444}
7445
7446/*
7447 * Allocate a variable for a Dictionary and fill it from "*arg".
7448 * Return OK or FAIL. Returns NOTDONE for {expr}.
7449 */
7450 static int
7451get_dict_tv(arg, rettv, evaluate)
7452 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 int evaluate;
7455{
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 dict_T *d = NULL;
7457 typval_T tvkey;
7458 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007459 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 /*
7465 * First check if it's not a curly-braces thing: {expr}.
7466 * Must do this without evaluating, otherwise a function may be called
7467 * twice. Unfortunately this means we need to call eval1() twice for the
7468 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 if (*start != '}')
7472 {
7473 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7474 return FAIL;
7475 if (*start == '}')
7476 return NOTDONE;
7477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478
7479 if (evaluate)
7480 {
7481 d = dict_alloc();
7482 if (d == NULL)
7483 return FAIL;
7484 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 tvkey.v_type = VAR_UNKNOWN;
7486 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 *arg = skipwhite(*arg + 1);
7489 while (**arg != '}' && **arg != NUL)
7490 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 goto failret;
7493 if (**arg != ':')
7494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007495 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 goto failret;
7498 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007499 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 key = get_tv_string_buf_chk(&tvkey, buf);
7502 if (key == NULL || *key == NUL)
7503 {
7504 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7505 if (key != NULL)
7506 EMSG(_(e_emptykey));
7507 clear_tv(&tvkey);
7508 goto failret;
7509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511
7512 *arg = skipwhite(*arg + 1);
7513 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7514 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007515 if (evaluate)
7516 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 goto failret;
7518 }
7519 if (evaluate)
7520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 if (item != NULL)
7523 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007524 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 clear_tv(&tv);
7527 goto failret;
7528 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 item = dictitem_alloc(key);
7530 clear_tv(&tvkey);
7531 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007534 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 if (dict_add(d, item) == FAIL)
7536 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 }
7538 }
7539
7540 if (**arg == '}')
7541 break;
7542 if (**arg != ',')
7543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007544 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 goto failret;
7546 }
7547 *arg = skipwhite(*arg + 1);
7548 }
7549
7550 if (**arg != '}')
7551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007552 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553failret:
7554 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007555 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 return FAIL;
7557 }
7558
7559 *arg = skipwhite(*arg + 1);
7560 if (evaluate)
7561 {
7562 rettv->v_type = VAR_DICT;
7563 rettv->vval.v_dict = d;
7564 ++d->dv_refcount;
7565 }
7566
7567 return OK;
7568}
7569
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 * Return a string with the string representation of a variable.
7572 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007573 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007576 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007577 */
7578 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007579echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007582 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 static int recurse = 0;
7586 char_u *r = NULL;
7587
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007590 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 *tofree = NULL;
7592 return NULL;
7593 }
7594 ++recurse;
7595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007596 switch (tv->v_type)
7597 {
7598 case VAR_FUNC:
7599 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 r = tv->vval.v_string;
7601 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007603 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604 if (tv->vval.v_list == NULL)
7605 {
7606 *tofree = NULL;
7607 r = NULL;
7608 }
7609 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7610 {
7611 *tofree = NULL;
7612 r = (char_u *)"[...]";
7613 }
7614 else
7615 {
7616 tv->vval.v_list->lv_copyID = copyID;
7617 *tofree = list2string(tv, copyID);
7618 r = *tofree;
7619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007621
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007623 if (tv->vval.v_dict == NULL)
7624 {
7625 *tofree = NULL;
7626 r = NULL;
7627 }
7628 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7629 {
7630 *tofree = NULL;
7631 r = (char_u *)"{...}";
7632 }
7633 else
7634 {
7635 tv->vval.v_dict->dv_copyID = copyID;
7636 *tofree = dict2string(tv, copyID);
7637 r = *tofree;
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 case VAR_STRING:
7642 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 *tofree = NULL;
7644 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007646
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007647#ifdef FEAT_FLOAT
7648 case VAR_FLOAT:
7649 *tofree = NULL;
7650 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7651 r = numbuf;
7652 break;
7653#endif
7654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659
7660 --recurse;
7661 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662}
7663
7664/*
7665 * Return a string with the string representation of a variable.
7666 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7667 * "numbuf" is used for a number.
7668 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007669 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 */
7671 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007672tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 char_u **tofree;
7675 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677{
7678 switch (tv->v_type)
7679 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 case VAR_FUNC:
7681 *tofree = string_quote(tv->vval.v_string, TRUE);
7682 return *tofree;
7683 case VAR_STRING:
7684 *tofree = string_quote(tv->vval.v_string, FALSE);
7685 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007686#ifdef FEAT_FLOAT
7687 case VAR_FLOAT:
7688 *tofree = NULL;
7689 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7690 return numbuf;
7691#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007699 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700}
7701
7702/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 * Return string "str" in ' quotes, doubling ' characters.
7704 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 */
7707 static char_u *
7708string_quote(str, function)
7709 char_u *str;
7710 int function;
7711{
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 char_u *p, *r, *s;
7714
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 len = (function ? 13 : 3);
7716 if (str != NULL)
7717 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007718 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 for (p = str; *p != NUL; mb_ptr_adv(p))
7720 if (*p == '\'')
7721 ++len;
7722 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 s = r = alloc(len);
7724 if (r != NULL)
7725 {
7726 if (function)
7727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 r += 10;
7730 }
7731 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 if (str != NULL)
7734 for (p = str; *p != NUL; )
7735 {
7736 if (*p == '\'')
7737 *r++ = '\'';
7738 MB_COPY_CHAR(p, r);
7739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 if (function)
7742 *r++ = ')';
7743 *r++ = NUL;
7744 }
7745 return s;
7746}
7747
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007748#ifdef FEAT_FLOAT
7749/*
7750 * Convert the string "text" to a floating point number.
7751 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7752 * this always uses a decimal point.
7753 * Returns the length of the text that was consumed.
7754 */
7755 static int
7756string2float(text, value)
7757 char_u *text;
7758 float_T *value; /* result stored here */
7759{
7760 char *s = (char *)text;
7761 float_T f;
7762
7763 f = strtod(s, &s);
7764 *value = f;
7765 return (int)((char_u *)s - text);
7766}
7767#endif
7768
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 * Get the value of an environment variable.
7771 * "arg" is pointing to the '$'. It is advanced to after the name.
7772 * If the environment variable was not set, silently assume it is empty.
7773 * Always return OK.
7774 */
7775 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 int evaluate;
7780{
7781 char_u *string = NULL;
7782 int len;
7783 int cc;
7784 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
7787 ++*arg;
7788 name = *arg;
7789 len = get_env_len(arg);
7790 if (evaluate)
7791 {
7792 if (len != 0)
7793 {
7794 cc = name[len];
7795 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 /* first try vim_getenv(), fast for normal environment vars */
7797 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799 {
7800 if (!mustfree)
7801 string = vim_strsave(string);
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
7804 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007805 if (mustfree)
7806 vim_free(string);
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 /* next try expanding things like $VIM and ${HOME} */
7809 string = expand_env_save(name - 1);
7810 if (string != NULL && *string == '$')
7811 {
7812 vim_free(string);
7813 string = NULL;
7814 }
7815 }
7816 name[len] = cc;
7817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818 rettv->v_type = VAR_STRING;
7819 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821
7822 return OK;
7823}
7824
7825/*
7826 * Array with names and number of arguments of all internal functions
7827 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7828 */
7829static struct fst
7830{
7831 char *f_name; /* function name */
7832 char f_min_argc; /* minimal number of arguments */
7833 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007835 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836} functions[] =
7837{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
7839 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007840 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007841#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007842 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007843 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"append", 2, 2, f_append},
7845 {"argc", 0, 0, f_argc},
7846 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007847 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007851 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007854 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"bufexists", 1, 1, f_bufexists},
7856 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7857 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7858 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7859 {"buflisted", 1, 1, f_buflisted},
7860 {"bufloaded", 1, 1, f_bufloaded},
7861 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007862 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"bufwinnr", 1, 1, f_bufwinnr},
7864 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007865 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#ifdef FEAT_FLOAT
7868 {"ceil", 1, 1, f_ceil},
7869#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007870 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007871 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007873 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007875#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007876 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007877 {"complete_add", 1, 1, f_complete_add},
7878 {"complete_check", 0, 0, f_complete_check},
7879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#ifdef FEAT_FLOAT
7883 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007884 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007888 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007889 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"delete", 1, 1, f_delete},
7891 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007892 {"diff_filler", 1, 1, f_diff_filler},
7893 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007894 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"eventhandler", 0, 0, f_eventhandler},
7898 {"executable", 1, 1, f_executable},
7899 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007900#ifdef FEAT_FLOAT
7901 {"exp", 1, 1, f_exp},
7902#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007903 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007904 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007905 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7907 {"filereadable", 1, 1, f_filereadable},
7908 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007910 {"finddir", 1, 3, f_finddir},
7911 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#ifdef FEAT_FLOAT
7913 {"float2nr", 1, 1, f_float2nr},
7914 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007915 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007917 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"fnamemodify", 2, 2, f_fnamemodify},
7919 {"foldclosed", 1, 1, f_foldclosed},
7920 {"foldclosedend", 1, 1, f_foldclosedend},
7921 {"foldlevel", 1, 1, f_foldlevel},
7922 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007923 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007926 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007928 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007929 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"getchar", 0, 1, f_getchar},
7931 {"getcharmod", 0, 0, f_getcharmod},
7932 {"getcmdline", 0, 0, f_getcmdline},
7933 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007934 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007936 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007937 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"getfsize", 1, 1, f_getfsize},
7939 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007940 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007941 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007942 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007943 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007944 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007945 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007946 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007947 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007949 {"gettabvar", 2, 3, f_gettabvar},
7950 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"getwinposx", 0, 0, f_getwinposx},
7952 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007953 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007954 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007955 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007957 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007958 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007959 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7961 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7962 {"histadd", 2, 2, f_histadd},
7963 {"histdel", 1, 2, f_histdel},
7964 {"histget", 1, 2, f_histget},
7965 {"histnr", 1, 1, f_histnr},
7966 {"hlID", 1, 1, f_hlID},
7967 {"hlexists", 1, 1, f_hlexists},
7968 {"hostname", 0, 0, f_hostname},
7969 {"iconv", 3, 3, f_iconv},
7970 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007972 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007974 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"inputrestore", 0, 0, f_inputrestore},
7976 {"inputsave", 0, 0, f_inputsave},
7977 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007979 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007981 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007984 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007986 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"libcall", 3, 3, f_libcall},
7988 {"libcallnr", 3, 3, f_libcallnr},
7989 {"line", 1, 1, f_line},
7990 {"line2byte", 1, 1, f_line2byte},
7991 {"lispindent", 1, 1, f_lispindent},
7992 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007994 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007995 {"log10", 1, 1, f_log10},
7996#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007997#ifdef FEAT_LUA
7998 {"luaeval", 1, 2, f_luaeval},
7999#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008001 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008002 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008003 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008004 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008005 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008007 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008008 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008009 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008010 {"max", 1, 1, f_max},
8011 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008012#ifdef vim_mkdir
8013 {"mkdir", 1, 3, f_mkdir},
8014#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008016#ifdef FEAT_MZSCHEME
8017 {"mzeval", 1, 1, f_mzeval},
8018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008020 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008021 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008022 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#ifdef FEAT_FLOAT
8024 {"pow", 2, 2, f_pow},
8025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008027 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008029#ifdef FEAT_PYTHON3
8030 {"py3eval", 1, 1, f_py3eval},
8031#endif
8032#ifdef FEAT_PYTHON
8033 {"pyeval", 1, 1, f_pyeval},
8034#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008036 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008037 {"reltime", 0, 2, f_reltime},
8038 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"remote_expr", 2, 3, f_remote_expr},
8040 {"remote_foreground", 1, 1, f_remote_foreground},
8041 {"remote_peek", 1, 2, f_remote_peek},
8042 {"remote_read", 1, 1, f_remote_read},
8043 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008044 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008046 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008048 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008049#ifdef FEAT_FLOAT
8050 {"round", 1, 1, f_round},
8051#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008052 {"screencol", 0, 0, f_screencol},
8053 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008054 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008055 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008056 {"searchpair", 3, 7, f_searchpair},
8057 {"searchpairpos", 3, 7, f_searchpairpos},
8058 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"server2client", 2, 2, f_server2client},
8060 {"serverlist", 0, 0, f_serverlist},
8061 {"setbufvar", 3, 3, f_setbufvar},
8062 {"setcmdpos", 1, 1, f_setcmdpos},
8063 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008064 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008065 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008066 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008067 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008069 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008070 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008072#ifdef FEAT_CRYPT
8073 {"sha256", 1, 1, f_sha256},
8074#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008075 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008076 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008080 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008082 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008083 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008084 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008085 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008086 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087#ifdef FEAT_FLOAT
8088 {"sqrt", 1, 1, f_sqrt},
8089 {"str2float", 1, 1, f_str2float},
8090#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008091 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008092 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008093 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#ifdef HAVE_STRFTIME
8095 {"strftime", 1, 2, f_strftime},
8096#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"strlen", 1, 1, f_strlen},
8100 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008101 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008103 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"submatch", 1, 1, f_submatch},
8105 {"substitute", 4, 4, f_substitute},
8106 {"synID", 3, 3, f_synID},
8107 {"synIDattr", 2, 3, f_synIDattr},
8108 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008109 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008110 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008111 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008112 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008113 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008114 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008115 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008116 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008117#ifdef FEAT_FLOAT
8118 {"tan", 1, 1, f_tan},
8119 {"tanh", 1, 1, f_tanh},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008122 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"tolower", 1, 1, f_tolower},
8124 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008125 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#ifdef FEAT_FLOAT
8127 {"trunc", 1, 1, f_trunc},
8128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008130 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008131 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"virtcol", 1, 1, f_virtcol},
8134 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008135 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"winbufnr", 1, 1, f_winbufnr},
8137 {"wincol", 0, 0, f_wincol},
8138 {"winheight", 1, 1, f_winheight},
8139 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008140 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008142 {"winrestview", 1, 1, f_winrestview},
8143 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008145 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008146 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147};
8148
8149#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8150
8151/*
8152 * Function given to ExpandGeneric() to obtain the list of internal
8153 * or user defined function names.
8154 */
8155 char_u *
8156get_function_name(xp, idx)
8157 expand_T *xp;
8158 int idx;
8159{
8160 static int intidx = -1;
8161 char_u *name;
8162
8163 if (idx == 0)
8164 intidx = -1;
8165 if (intidx < 0)
8166 {
8167 name = get_user_func_name(xp, idx);
8168 if (name != NULL)
8169 return name;
8170 }
8171 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8172 {
8173 STRCPY(IObuff, functions[intidx].f_name);
8174 STRCAT(IObuff, "(");
8175 if (functions[intidx].f_max_argc == 0)
8176 STRCAT(IObuff, ")");
8177 return IObuff;
8178 }
8179
8180 return NULL;
8181}
8182
8183/*
8184 * Function given to ExpandGeneric() to obtain the list of internal or
8185 * user defined variable or function names.
8186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 char_u *
8188get_expr_name(xp, idx)
8189 expand_T *xp;
8190 int idx;
8191{
8192 static int intidx = -1;
8193 char_u *name;
8194
8195 if (idx == 0)
8196 intidx = -1;
8197 if (intidx < 0)
8198 {
8199 name = get_function_name(xp, idx);
8200 if (name != NULL)
8201 return name;
8202 }
8203 return get_user_var_name(xp, ++intidx);
8204}
8205
8206#endif /* FEAT_CMDL_COMPL */
8207
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008208#if defined(EBCDIC) || defined(PROTO)
8209/*
8210 * Compare struct fst by function name.
8211 */
8212 static int
8213compare_func_name(s1, s2)
8214 const void *s1;
8215 const void *s2;
8216{
8217 struct fst *p1 = (struct fst *)s1;
8218 struct fst *p2 = (struct fst *)s2;
8219
8220 return STRCMP(p1->f_name, p2->f_name);
8221}
8222
8223/*
8224 * Sort the function table by function name.
8225 * The sorting of the table above is ASCII dependant.
8226 * On machines using EBCDIC we have to sort it.
8227 */
8228 static void
8229sortFunctions()
8230{
8231 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8232
8233 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8234}
8235#endif
8236
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238/*
8239 * Find internal function in table above.
8240 * Return index, or -1 if not found
8241 */
8242 static int
8243find_internal_func(name)
8244 char_u *name; /* name of the function */
8245{
8246 int first = 0;
8247 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8248 int cmp;
8249 int x;
8250
8251 /*
8252 * Find the function name in the table. Binary search.
8253 */
8254 while (first <= last)
8255 {
8256 x = first + ((unsigned)(last - first) >> 1);
8257 cmp = STRCMP(name, functions[x].f_name);
8258 if (cmp < 0)
8259 last = x - 1;
8260 else if (cmp > 0)
8261 first = x + 1;
8262 else
8263 return x;
8264 }
8265 return -1;
8266}
8267
8268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8270 * name it contains, otherwise return "name".
8271 */
8272 static char_u *
8273deref_func_name(name, lenp)
8274 char_u *name;
8275 int *lenp;
8276{
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278 int cc;
8279
8280 cc = name[*lenp];
8281 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008282 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287 {
8288 *lenp = 0;
8289 return (char_u *)""; /* just in case */
8290 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008291 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 }
8294
8295 return name;
8296}
8297
8298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * Allocate a variable for the result of a function.
8300 * Return OK or FAIL.
8301 */
8302 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8304 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 char_u *name; /* name of the function */
8306 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 char_u **arg; /* argument, pointing to the '(' */
8309 linenr_T firstline; /* first line of range */
8310 linenr_T lastline; /* last line of range */
8311 int *doesrange; /* return: function handled range */
8312 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 char_u *argp;
8316 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 int argcount = 0; /* number of arguments found */
8319
8320 /*
8321 * Get the arguments.
8322 */
8323 argp = *arg;
8324 while (argcount < MAX_FUNC_ARGS)
8325 {
8326 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8327 if (*argp == ')' || *argp == ',' || *argp == NUL)
8328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8330 {
8331 ret = FAIL;
8332 break;
8333 }
8334 ++argcount;
8335 if (*argp != ',')
8336 break;
8337 }
8338 if (*argp == ')')
8339 ++argp;
8340 else
8341 ret = FAIL;
8342
8343 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 {
8348 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008349 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008351 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 *arg = skipwhite(argp);
8358 return ret;
8359}
8360
8361
8362/*
8363 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008364 * Return OK when the function can't be called, FAIL otherwise.
8365 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 */
8367 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008368call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008370 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008374 typval_T *argvars; /* vars for arguments, must have "argcount"
8375 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 linenr_T firstline; /* first line of range */
8377 linenr_T lastline; /* last line of range */
8378 int *doesrange; /* return: function handled range */
8379 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#define ERROR_UNKNOWN 0
8384#define ERROR_TOOMANY 1
8385#define ERROR_TOOFEW 2
8386#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387#define ERROR_DICT 4
8388#define ERROR_NONE 5
8389#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int error = ERROR_NONE;
8391 int i;
8392 int llen;
8393 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#define FLEN_FIXED 40
8395 char_u fname_buf[FLEN_FIXED + 1];
8396 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008397 char_u *name;
8398
8399 /* Make a copy of the name, if it comes from a funcref variable it could
8400 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008401 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008402 if (name == NULL)
8403 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 /*
8406 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8407 * Change <SNR>123_name() to K_SNR 123_name().
8408 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 llen = eval_fname_script(name);
8411 if (llen > 0)
8412 {
8413 fname_buf[0] = K_SPECIAL;
8414 fname_buf[1] = KS_EXTRA;
8415 fname_buf[2] = (int)KE_SNR;
8416 i = 3;
8417 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8418 {
8419 if (current_SID <= 0)
8420 error = ERROR_SCRIPT;
8421 else
8422 {
8423 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8424 i = (int)STRLEN(fname_buf);
8425 }
8426 }
8427 if (i + STRLEN(name + llen) < FLEN_FIXED)
8428 {
8429 STRCPY(fname_buf + i, name + llen);
8430 fname = fname_buf;
8431 }
8432 else
8433 {
8434 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8435 if (fname == NULL)
8436 error = ERROR_OTHER;
8437 else
8438 {
8439 mch_memmove(fname, fname_buf, (size_t)i);
8440 STRCPY(fname + i, name + llen);
8441 }
8442 }
8443 }
8444 else
8445 fname = name;
8446
8447 *doesrange = FALSE;
8448
8449
8450 /* execute the function if no errors detected and executing */
8451 if (evaluate && error == ERROR_NONE)
8452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008453 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 error = ERROR_UNKNOWN;
8456
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 /*
8460 * User defined function.
8461 */
8462 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* Trigger FuncUndefined event, may load the function. */
8466 if (fp == NULL
8467 && apply_autocmds(EVENT_FUNCUNDEFINED,
8468 fname, fname, TRUE, NULL)
8469 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 fp = find_func(fname);
8473 }
8474#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008476 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 {
8478 /* loaded a package, search for the function again */
8479 fp = find_func(fname);
8480 }
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (fp != NULL)
8483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008484 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008488 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else
8493 {
8494 /*
8495 * Call the user function.
8496 * Save and restore search patterns, script variables and
8497 * redo buffer.
8498 */
8499 save_search_patterns();
8500 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008501 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008504 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8505 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8506 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008507 /* Function was unreferenced while being used, free it
8508 * now. */
8509 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 restoreRedobuff();
8511 restore_search_patterns();
8512 error = ERROR_NONE;
8513 }
8514 }
8515 }
8516 else
8517 {
8518 /*
8519 * Find the function name in the table, call its implementation.
8520 */
8521 i = find_internal_func(fname);
8522 if (i >= 0)
8523 {
8524 if (argcount < functions[i].f_min_argc)
8525 error = ERROR_TOOFEW;
8526 else if (argcount > functions[i].f_max_argc)
8527 error = ERROR_TOOMANY;
8528 else
8529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 error = ERROR_NONE;
8533 }
8534 }
8535 }
8536 /*
8537 * The function call (or "FuncUndefined" autocommand sequence) might
8538 * have been aborted by an error, an interrupt, or an explicitly thrown
8539 * exception that has not been caught so far. This situation can be
8540 * tested for by calling aborting(). For an error in an internal
8541 * function or for the "E132" error in call_user_func(), however, the
8542 * throw point at which the "force_abort" flag (temporarily reset by
8543 * emsg()) is normally updated has not been reached yet. We need to
8544 * update that flag first to make aborting() reliable.
8545 */
8546 update_force_abort();
8547 }
8548 if (error == ERROR_NONE)
8549 ret = OK;
8550
8551 /*
8552 * Report an error unless the argument evaluation or function call has been
8553 * cancelled due to an aborting error, an interrupt, or an exception.
8554 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 if (!aborting())
8556 {
8557 switch (error)
8558 {
8559 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 break;
8562 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008564 break;
8565 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008566 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 name);
8568 break;
8569 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008570 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008571 name);
8572 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008573 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008574 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 name);
8576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 }
8578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (fname != name && fname != fname_buf)
8581 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008582 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
8584 return ret;
8585}
8586
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008587/*
8588 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008589 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008590 */
8591 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008592emsg_funcname(ermsg, name)
8593 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 char_u *name;
8595{
8596 char_u *p;
8597
8598 if (*name == K_SPECIAL)
8599 p = concat_str((char_u *)"<SNR>", name + 3);
8600 else
8601 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008602 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008603 if (p != name)
8604 vim_free(p);
8605}
8606
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008607/*
8608 * Return TRUE for a non-zero Number and a non-empty String.
8609 */
8610 static int
8611non_zero_arg(argvars)
8612 typval_T *argvars;
8613{
8614 return ((argvars[0].v_type == VAR_NUMBER
8615 && argvars[0].vval.v_number != 0)
8616 || (argvars[0].v_type == VAR_STRING
8617 && argvars[0].vval.v_string != NULL
8618 && *argvars[0].vval.v_string != NUL));
8619}
8620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*********************************************
8622 * Implementation of the built-in functions
8623 */
8624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008626static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8627
8628/*
8629 * Get the float value of "argvars[0]" into "f".
8630 * Returns FAIL when the argument is not a Number or Float.
8631 */
8632 static int
8633get_float_arg(argvars, f)
8634 typval_T *argvars;
8635 float_T *f;
8636{
8637 if (argvars[0].v_type == VAR_FLOAT)
8638 {
8639 *f = argvars[0].vval.v_float;
8640 return OK;
8641 }
8642 if (argvars[0].v_type == VAR_NUMBER)
8643 {
8644 *f = (float_T)argvars[0].vval.v_number;
8645 return OK;
8646 }
8647 EMSG(_("E808: Number or Float required"));
8648 return FAIL;
8649}
8650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651/*
8652 * "abs(expr)" function
8653 */
8654 static void
8655f_abs(argvars, rettv)
8656 typval_T *argvars;
8657 typval_T *rettv;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 rettv->v_type = VAR_FLOAT;
8662 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8663 }
8664 else
8665 {
8666 varnumber_T n;
8667 int error = FALSE;
8668
8669 n = get_tv_number_chk(&argvars[0], &error);
8670 if (error)
8671 rettv->vval.v_number = -1;
8672 else if (n > 0)
8673 rettv->vval.v_number = n;
8674 else
8675 rettv->vval.v_number = -n;
8676 }
8677}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008678
8679/*
8680 * "acos()" function
8681 */
8682 static void
8683f_acos(argvars, rettv)
8684 typval_T *argvars;
8685 typval_T *rettv;
8686{
8687 float_T f;
8688
8689 rettv->v_type = VAR_FLOAT;
8690 if (get_float_arg(argvars, &f) == OK)
8691 rettv->vval.v_float = acos(f);
8692 else
8693 rettv->vval.v_float = 0.0;
8694}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695#endif
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 */
8700 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008701f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *argvars;
8703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008710 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008711 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008712 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 }
8715 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008716 EMSG(_(e_listreq));
8717}
8718
8719/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008720 * "and(expr, expr)" function
8721 */
8722 static void
8723f_and(argvars, rettv)
8724 typval_T *argvars;
8725 typval_T *rettv;
8726{
8727 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8728 & get_tv_number_chk(&argvars[1], NULL);
8729}
8730
8731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 * "append(lnum, string/list)" function
8733 */
8734 static void
8735f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738{
8739 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 list_T *l = NULL;
8742 listitem_T *li = NULL;
8743 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 long added = 0;
8745
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 lnum = get_tv_lnum(argvars);
8747 if (lnum >= 0
8748 && lnum <= curbuf->b_ml.ml_line_count
8749 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008750 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008751 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753 l = argvars[1].vval.v_list;
8754 if (l == NULL)
8755 return;
8756 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008758 for (;;)
8759 {
8760 if (l == NULL)
8761 tv = &argvars[1]; /* append a string */
8762 else if (li == NULL)
8763 break; /* end of list */
8764 else
8765 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 line = get_tv_string_chk(tv);
8767 if (line == NULL) /* type error */
8768 {
8769 rettv->vval.v_number = 1; /* Failed */
8770 break;
8771 }
8772 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008773 ++added;
8774 if (l == NULL)
8775 break;
8776 li = li->li_next;
8777 }
8778
8779 appended_lines_mark(lnum, added);
8780 if (curwin->w_cursor.lnum > lnum)
8781 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 else
8784 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787/*
8788 * "argc()" function
8789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008792 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796}
8797
8798/*
8799 * "argidx()" function
8800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809/*
8810 * "argv(nr)" function
8811 */
8812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 typval_T *argvars;
8815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 int idx;
8818
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008819 if (argvars[0].v_type != VAR_UNKNOWN)
8820 {
8821 idx = get_tv_number_chk(&argvars[0], NULL);
8822 if (idx >= 0 && idx < ARGCOUNT)
8823 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8824 else
8825 rettv->vval.v_string = NULL;
8826 rettv->v_type = VAR_STRING;
8827 }
8828 else if (rettv_list_alloc(rettv) == OK)
8829 for (idx = 0; idx < ARGCOUNT; ++idx)
8830 list_append_string(rettv->vval.v_list,
8831 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008835/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008836 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008837 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008838 static void
8839f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008840 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008841 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008843 float_T f;
8844
8845 rettv->v_type = VAR_FLOAT;
8846 if (get_float_arg(argvars, &f) == OK)
8847 rettv->vval.v_float = asin(f);
8848 else
8849 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850}
8851
8852/*
8853 * "atan()" function
8854 */
8855 static void
8856f_atan(argvars, rettv)
8857 typval_T *argvars;
8858 typval_T *rettv;
8859{
8860 float_T f;
8861
8862 rettv->v_type = VAR_FLOAT;
8863 if (get_float_arg(argvars, &f) == OK)
8864 rettv->vval.v_float = atan(f);
8865 else
8866 rettv->vval.v_float = 0.0;
8867}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868
8869/*
8870 * "atan2()" function
8871 */
8872 static void
8873f_atan2(argvars, rettv)
8874 typval_T *argvars;
8875 typval_T *rettv;
8876{
8877 float_T fx, fy;
8878
8879 rettv->v_type = VAR_FLOAT;
8880 if (get_float_arg(argvars, &fx) == OK
8881 && get_float_arg(&argvars[1], &fy) == OK)
8882 rettv->vval.v_float = atan2(fx, fy);
8883 else
8884 rettv->vval.v_float = 0.0;
8885}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#endif
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888/*
8889 * "browse(save, title, initdir, default)" function
8890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008893 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896#ifdef FEAT_BROWSE
8897 int save;
8898 char_u *title;
8899 char_u *initdir;
8900 char_u *defname;
8901 char_u buf[NUMBUFLEN];
8902 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 save = get_tv_number_chk(&argvars[0], &error);
8906 title = get_tv_string_chk(&argvars[1]);
8907 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8908 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 if (error || title == NULL || initdir == NULL || defname == NULL)
8911 rettv->vval.v_string = NULL;
8912 else
8913 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008914 do_browse(save ? BROWSE_SAVE : 0,
8915 title, defname, NULL, initdir, NULL, curbuf);
8916#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008918#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008920}
8921
8922/*
8923 * "browsedir(title, initdir)" function
8924 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008928 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008929{
8930#ifdef FEAT_BROWSE
8931 char_u *title;
8932 char_u *initdir;
8933 char_u buf[NUMBUFLEN];
8934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 title = get_tv_string_chk(&argvars[0]);
8936 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 if (title == NULL || initdir == NULL)
8939 rettv->vval.v_string = NULL;
8940 else
8941 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008942 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947}
8948
Bram Moolenaar33570922005-01-25 22:26:29 +00008949static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951/*
8952 * Find a buffer by number or exact name.
8953 */
8954 static buf_T *
8955find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008956 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957{
8958 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008960 if (avar->v_type == VAR_NUMBER)
8961 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008962 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008965 if (buf == NULL)
8966 {
8967 /* No full path name match, try a match with a URL or a "nofile"
8968 * buffer, these don't use the full path. */
8969 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8970 if (buf->b_fname != NULL
8971 && (path_with_url(buf->b_fname)
8972#ifdef FEAT_QUICKFIX
8973 || bt_nofile(buf)
8974#endif
8975 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008977 break;
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 }
8980 return buf;
8981}
8982
8983/*
8984 * "bufexists(expr)" function
8985 */
8986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992}
8993
8994/*
8995 * "buflisted(expr)" function
8996 */
8997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 buf_T *buf;
9003
9004 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008/*
9009 * "bufloaded(expr)" function
9010 */
9011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
9016 buf_T *buf;
9017
9018 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020}
9021
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009022static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024/*
9025 * Get buffer by number or pattern.
9026 */
9027 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009028get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009030 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 int save_magic;
9034 char_u *save_cpo;
9035 buf_T *buf;
9036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037 if (tv->v_type == VAR_NUMBER)
9038 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009039 if (tv->v_type != VAR_STRING)
9040 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 if (name == NULL || *name == NUL)
9042 return curbuf;
9043 if (name[0] == '$' && name[1] == NUL)
9044 return lastbuf;
9045
9046 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9047 save_magic = p_magic;
9048 p_magic = TRUE;
9049 save_cpo = p_cpo;
9050 p_cpo = (char_u *)"";
9051
9052 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009053 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
9055 p_magic = save_magic;
9056 p_cpo = save_cpo;
9057
9058 /* If not found, try expanding the name, like done for bufexists(). */
9059 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 return buf;
9063}
9064
9065/*
9066 * "bufname(expr)" function
9067 */
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 typval_T *argvars;
9071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073 buf_T *buf;
9074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009077 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 --emsg_off;
9084}
9085
9086/*
9087 * "bufnr(expr)" function
9088 */
9089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 typval_T *argvars;
9092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
9094 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009095 int error = FALSE;
9096 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009100 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009101 --emsg_off;
9102
9103 /* If the buffer isn't found and the second argument is not zero create a
9104 * new buffer. */
9105 if (buf == NULL
9106 && argvars[1].v_type != VAR_UNKNOWN
9107 && get_tv_number_chk(&argvars[1], &error) != 0
9108 && !error
9109 && (name = get_tv_string_chk(&argvars[0])) != NULL
9110 && !error)
9111 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117}
9118
9119/*
9120 * "bufwinnr(nr)" function
9121 */
9122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 typval_T *argvars;
9125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
9127#ifdef FEAT_WINDOWS
9128 win_T *wp;
9129 int winnr = 0;
9130#endif
9131 buf_T *buf;
9132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009133 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009135 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136#ifdef FEAT_WINDOWS
9137 for (wp = firstwin; wp; wp = wp->w_next)
9138 {
9139 ++winnr;
9140 if (wp->w_buffer == buf)
9141 break;
9142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146#endif
9147 --emsg_off;
9148}
9149
9150/*
9151 * "byte2line(byte)" function
9152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009155 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160#else
9161 long boff = 0;
9162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 (linenr_T)0, &boff);
9169#endif
9170}
9171
9172/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009173 * "byteidx()" function
9174 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009177 typval_T *argvars;
9178 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009179{
9180#ifdef FEAT_MBYTE
9181 char_u *t;
9182#endif
9183 char_u *str;
9184 long idx;
9185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 str = get_tv_string_chk(&argvars[0]);
9187 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009190 return;
9191
9192#ifdef FEAT_MBYTE
9193 t = str;
9194 for ( ; idx > 0; idx--)
9195 {
9196 if (*t == NUL) /* EOL reached */
9197 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009198 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009199 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009200 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009201#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009202 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009204#endif
9205}
9206
Bram Moolenaardb913952012-06-29 12:54:53 +02009207 int
9208func_call(name, args, selfdict, rettv)
9209 char_u *name;
9210 typval_T *args;
9211 dict_T *selfdict;
9212 typval_T *rettv;
9213{
9214 listitem_T *item;
9215 typval_T argv[MAX_FUNC_ARGS + 1];
9216 int argc = 0;
9217 int dummy;
9218 int r = 0;
9219
9220 for (item = args->vval.v_list->lv_first; item != NULL;
9221 item = item->li_next)
9222 {
9223 if (argc == MAX_FUNC_ARGS)
9224 {
9225 EMSG(_("E699: Too many arguments"));
9226 break;
9227 }
9228 /* Make a copy of each argument. This is needed to be able to set
9229 * v_lock to VAR_FIXED in the copy without changing the original list.
9230 */
9231 copy_tv(&item->li_tv, &argv[argc++]);
9232 }
9233
9234 if (item == NULL)
9235 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9236 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9237 &dummy, TRUE, selfdict);
9238
9239 /* Free the arguments. */
9240 while (argc > 0)
9241 clear_tv(&argv[--argc]);
9242
9243 return r;
9244}
9245
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009246/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009247 * "call(func, arglist)" function
9248 */
9249 static void
9250f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009251 typval_T *argvars;
9252 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009253{
9254 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009255 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009256
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009257 if (argvars[1].v_type != VAR_LIST)
9258 {
9259 EMSG(_(e_listreq));
9260 return;
9261 }
9262 if (argvars[1].vval.v_list == NULL)
9263 return;
9264
9265 if (argvars[0].v_type == VAR_FUNC)
9266 func = argvars[0].vval.v_string;
9267 else
9268 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 if (*func == NUL)
9270 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009271
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 if (argvars[2].v_type != VAR_UNKNOWN)
9273 {
9274 if (argvars[2].v_type != VAR_DICT)
9275 {
9276 EMSG(_(e_dictreq));
9277 return;
9278 }
9279 selfdict = argvars[2].vval.v_dict;
9280 }
9281
Bram Moolenaardb913952012-06-29 12:54:53 +02009282 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009283}
9284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009285#ifdef FEAT_FLOAT
9286/*
9287 * "ceil({float})" function
9288 */
9289 static void
9290f_ceil(argvars, rettv)
9291 typval_T *argvars;
9292 typval_T *rettv;
9293{
9294 float_T f;
9295
9296 rettv->v_type = VAR_FLOAT;
9297 if (get_float_arg(argvars, &f) == OK)
9298 rettv->vval.v_float = ceil(f);
9299 else
9300 rettv->vval.v_float = 0.0;
9301}
9302#endif
9303
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009304/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009305 * "changenr()" function
9306 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009307 static void
9308f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009309 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009310 typval_T *rettv;
9311{
9312 rettv->vval.v_number = curbuf->b_u_seq_cur;
9313}
9314
9315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 * "char2nr(string)" function
9317 */
9318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009320 typval_T *argvars;
9321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322{
9323#ifdef FEAT_MBYTE
9324 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009325 {
9326 int utf8 = 0;
9327
9328 if (argvars[1].v_type != VAR_UNKNOWN)
9329 utf8 = get_tv_number_chk(&argvars[1], NULL);
9330
9331 if (utf8)
9332 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9333 else
9334 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 else
9337#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009338 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339}
9340
9341/*
9342 * "cindent(lnum)" function
9343 */
9344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349#ifdef FEAT_CINDENT
9350 pos_T pos;
9351 linenr_T lnum;
9352
9353 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9356 {
9357 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 curwin->w_cursor = pos;
9360 }
9361 else
9362#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364}
9365
9366/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009367 * "clearmatches()" function
9368 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009369 static void
9370f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009371 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009372 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009373{
9374#ifdef FEAT_SEARCH_EXTRA
9375 clear_matches(curwin);
9376#endif
9377}
9378
9379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 * "col(string)" function
9381 */
9382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *argvars;
9385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 colnr_T col = 0;
9388 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009389 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009391 fp = var2fpos(&argvars[0], FALSE, &fnum);
9392 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 {
9394 if (fp->col == MAXCOL)
9395 {
9396 /* '> can be MAXCOL, get the length of the line then */
9397 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009398 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 else
9400 col = MAXCOL;
9401 }
9402 else
9403 {
9404 col = fp->col + 1;
9405#ifdef FEAT_VIRTUALEDIT
9406 /* col(".") when the cursor is on the NUL at the end of the line
9407 * because of "coladd" can be seen as an extra column. */
9408 if (virtual_active() && fp == &curwin->w_cursor)
9409 {
9410 char_u *p = ml_get_cursor();
9411
9412 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9413 curwin->w_virtcol - curwin->w_cursor.coladd))
9414 {
9415# ifdef FEAT_MBYTE
9416 int l;
9417
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009418 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 col += l;
9420# else
9421 if (*p != NUL && p[1] == NUL)
9422 ++col;
9423# endif
9424 }
9425 }
9426#endif
9427 }
9428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
Bram Moolenaar572cb562005-08-05 21:35:02 +00009432#if defined(FEAT_INS_EXPAND)
9433/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009434 * "complete()" function
9435 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009436 static void
9437f_complete(argvars, rettv)
9438 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009439 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009440{
9441 int startcol;
9442
9443 if ((State & INSERT) == 0)
9444 {
9445 EMSG(_("E785: complete() can only be used in Insert mode"));
9446 return;
9447 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009448
9449 /* Check for undo allowed here, because if something was already inserted
9450 * the line was already saved for undo and this check isn't done. */
9451 if (!undo_allowed())
9452 return;
9453
Bram Moolenaarade00832006-03-10 21:46:58 +00009454 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9455 {
9456 EMSG(_(e_invarg));
9457 return;
9458 }
9459
9460 startcol = get_tv_number_chk(&argvars[0], NULL);
9461 if (startcol <= 0)
9462 return;
9463
9464 set_completion(startcol - 1, argvars[1].vval.v_list);
9465}
9466
9467/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009468 * "complete_add()" function
9469 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009470 static void
9471f_complete_add(argvars, rettv)
9472 typval_T *argvars;
9473 typval_T *rettv;
9474{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009475 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009476}
9477
9478/*
9479 * "complete_check()" function
9480 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009481 static void
9482f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009483 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009484 typval_T *rettv;
9485{
9486 int saved = RedrawingDisabled;
9487
9488 RedrawingDisabled = 0;
9489 ins_compl_check_keys(0);
9490 rettv->vval.v_number = compl_interrupted;
9491 RedrawingDisabled = saved;
9492}
9493#endif
9494
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495/*
9496 * "confirm(message, buttons[, default [, type]])" function
9497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009500 typval_T *argvars UNUSED;
9501 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
9503#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9504 char_u *message;
9505 char_u *buttons = NULL;
9506 char_u buf[NUMBUFLEN];
9507 char_u buf2[NUMBUFLEN];
9508 int def = 1;
9509 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 char_u *typestr;
9511 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 message = get_tv_string_chk(&argvars[0]);
9514 if (message == NULL)
9515 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9519 if (buttons == NULL)
9520 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009526 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9527 if (typestr == NULL)
9528 error = TRUE;
9529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 switch (TOUPPER_ASC(*typestr))
9532 {
9533 case 'E': type = VIM_ERROR; break;
9534 case 'Q': type = VIM_QUESTION; break;
9535 case 'I': type = VIM_INFO; break;
9536 case 'W': type = VIM_WARNING; break;
9537 case 'G': type = VIM_GENERIC; break;
9538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540 }
9541 }
9542 }
9543
9544 if (buttons == NULL || *buttons == NUL)
9545 buttons = (char_u *)_("&Ok");
9546
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009547 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009549 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550#endif
9551}
9552
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009553/*
9554 * "copy()" function
9555 */
9556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009558 typval_T *argvars;
9559 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009561 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009564#ifdef FEAT_FLOAT
9565/*
9566 * "cos()" function
9567 */
9568 static void
9569f_cos(argvars, rettv)
9570 typval_T *argvars;
9571 typval_T *rettv;
9572{
9573 float_T f;
9574
9575 rettv->v_type = VAR_FLOAT;
9576 if (get_float_arg(argvars, &f) == OK)
9577 rettv->vval.v_float = cos(f);
9578 else
9579 rettv->vval.v_float = 0.0;
9580}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009581
9582/*
9583 * "cosh()" function
9584 */
9585 static void
9586f_cosh(argvars, rettv)
9587 typval_T *argvars;
9588 typval_T *rettv;
9589{
9590 float_T f;
9591
9592 rettv->v_type = VAR_FLOAT;
9593 if (get_float_arg(argvars, &f) == OK)
9594 rettv->vval.v_float = cosh(f);
9595 else
9596 rettv->vval.v_float = 0.0;
9597}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598#endif
9599
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601 * "count()" function
9602 */
9603 static void
9604f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 typval_T *argvars;
9606 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009608 long n = 0;
9609 int ic = FALSE;
9610
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009612 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 listitem_T *li;
9614 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009616
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 if ((l = argvars[0].vval.v_list) != NULL)
9618 {
9619 li = l->lv_first;
9620 if (argvars[2].v_type != VAR_UNKNOWN)
9621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 int error = FALSE;
9623
9624 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 if (argvars[3].v_type != VAR_UNKNOWN)
9626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 idx = get_tv_number_chk(&argvars[3], &error);
9628 if (!error)
9629 {
9630 li = list_find(l, idx);
9631 if (li == NULL)
9632 EMSGN(_(e_listidx), idx);
9633 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 if (error)
9636 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637 }
9638
9639 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009640 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 ++n;
9642 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 else if (argvars[0].v_type == VAR_DICT)
9645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009646 int todo;
9647 dict_T *d;
9648 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009650 if ((d = argvars[0].vval.v_dict) != NULL)
9651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 int error = FALSE;
9653
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 if (argvars[2].v_type != VAR_UNKNOWN)
9655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657 if (argvars[3].v_type != VAR_UNKNOWN)
9658 EMSG(_(e_invarg));
9659 }
9660
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009661 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009662 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009663 {
9664 if (!HASHITEM_EMPTY(hi))
9665 {
9666 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009667 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009668 ++n;
9669 }
9670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009671 }
9672 }
9673 else
9674 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009675 rettv->vval.v_number = n;
9676}
9677
9678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9680 *
9681 * Checks the existence of a cscope connection.
9682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009685 typval_T *argvars UNUSED;
9686 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688#ifdef FEAT_CSCOPE
9689 int num = 0;
9690 char_u *dbpath = NULL;
9691 char_u *prepend = NULL;
9692 char_u buf[NUMBUFLEN];
9693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009694 if (argvars[0].v_type != VAR_UNKNOWN
9695 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 num = (int)get_tv_number(&argvars[0]);
9698 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 }
9702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704#endif
9705}
9706
9707/*
9708 * "cursor(lnum, col)" function
9709 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009710 * Moves the cursor to the specified line and column.
9711 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 typval_T *argvars;
9716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009719#ifdef FEAT_VIRTUALEDIT
9720 long coladd = 0;
9721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009723 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009724 if (argvars[1].v_type == VAR_UNKNOWN)
9725 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009726 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009727
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009728 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009729 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009730 line = pos.lnum;
9731 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009732#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009733 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009734#endif
9735 }
9736 else
9737 {
9738 line = get_tv_lnum(argvars);
9739 col = get_tv_number_chk(&argvars[1], NULL);
9740#ifdef FEAT_VIRTUALEDIT
9741 if (argvars[2].v_type != VAR_UNKNOWN)
9742 coladd = get_tv_number_chk(&argvars[2], NULL);
9743#endif
9744 }
9745 if (line < 0 || col < 0
9746#ifdef FEAT_VIRTUALEDIT
9747 || coladd < 0
9748#endif
9749 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (line > 0)
9752 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 if (col > 0)
9754 curwin->w_cursor.col = col - 1;
9755#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009756 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#endif
9758
9759 /* Make sure the cursor is in a valid position. */
9760 check_cursor();
9761#ifdef FEAT_MBYTE
9762 /* Correct cursor for multi-byte character. */
9763 if (has_mbyte)
9764 mb_adjust_cursor();
9765#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009766
9767 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009768 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009772 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 */
9774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009779 int noref = 0;
9780
9781 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009782 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009783 if (noref < 0 || noref > 1)
9784 EMSG(_(e_invarg));
9785 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009786 {
9787 current_copyID += COPYID_INC;
9788 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790}
9791
9792/*
9793 * "delete()" function
9794 */
9795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009797 typval_T *argvars;
9798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804}
9805
9806/*
9807 * "did_filetype()" function
9808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009811 typval_T *argvars UNUSED;
9812 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813{
9814#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816#endif
9817}
9818
9819/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009820 * "diff_filler()" function
9821 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009824 typval_T *argvars UNUSED;
9825 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009826{
9827#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829#endif
9830}
9831
9832/*
9833 * "diff_hlID()" function
9834 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009837 typval_T *argvars UNUSED;
9838 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009839{
9840#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 static linenr_T prev_lnum = 0;
9843 static int changedtick = 0;
9844 static int fnum = 0;
9845 static int change_start = 0;
9846 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009847 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009848 int filler_lines;
9849 int col;
9850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 if (lnum < 0) /* ignore type error in {lnum} arg */
9852 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009853 if (lnum != prev_lnum
9854 || changedtick != curbuf->b_changedtick
9855 || fnum != curbuf->b_fnum)
9856 {
9857 /* New line, buffer, change: need to get the values. */
9858 filler_lines = diff_check(curwin, lnum);
9859 if (filler_lines < 0)
9860 {
9861 if (filler_lines == -1)
9862 {
9863 change_start = MAXCOL;
9864 change_end = -1;
9865 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9866 hlID = HLF_ADD; /* added line */
9867 else
9868 hlID = HLF_CHD; /* changed line */
9869 }
9870 else
9871 hlID = HLF_ADD; /* added line */
9872 }
9873 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009874 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 prev_lnum = lnum;
9876 changedtick = curbuf->b_changedtick;
9877 fnum = curbuf->b_fnum;
9878 }
9879
9880 if (hlID == HLF_CHD || hlID == HLF_TXD)
9881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883 if (col >= change_start && col <= change_end)
9884 hlID = HLF_TXD; /* changed text */
9885 else
9886 hlID = HLF_CHD; /* changed line */
9887 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009888 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009889#endif
9890}
9891
9892/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009893 * "empty({expr})" function
9894 */
9895 static void
9896f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 typval_T *argvars;
9898 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009899{
9900 int n;
9901
9902 switch (argvars[0].v_type)
9903 {
9904 case VAR_STRING:
9905 case VAR_FUNC:
9906 n = argvars[0].vval.v_string == NULL
9907 || *argvars[0].vval.v_string == NUL;
9908 break;
9909 case VAR_NUMBER:
9910 n = argvars[0].vval.v_number == 0;
9911 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009912#ifdef FEAT_FLOAT
9913 case VAR_FLOAT:
9914 n = argvars[0].vval.v_float == 0.0;
9915 break;
9916#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009917 case VAR_LIST:
9918 n = argvars[0].vval.v_list == NULL
9919 || argvars[0].vval.v_list->lv_first == NULL;
9920 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 case VAR_DICT:
9922 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009923 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009925 default:
9926 EMSG2(_(e_intern2), "f_empty()");
9927 n = 0;
9928 }
9929
9930 rettv->vval.v_number = n;
9931}
9932
9933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 * "escape({string}, {chars})" function
9935 */
9936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 typval_T *argvars;
9939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 char_u buf[NUMBUFLEN];
9942
Bram Moolenaar758711c2005-02-02 23:11:38 +00009943 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9944 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946}
9947
9948/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009949 * "eval()" function
9950 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009951 static void
9952f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009953 typval_T *argvars;
9954 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955{
9956 char_u *s;
9957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 s = get_tv_string_chk(&argvars[0]);
9959 if (s != NULL)
9960 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009962 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9963 {
9964 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009965 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009967 else if (*s != NUL)
9968 EMSG(_(e_trailing));
9969}
9970
9971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 * "eventhandler()" function
9973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009976 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982/*
9983 * "executable()" function
9984 */
9985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991}
9992
9993/*
9994 * "exists()" function
9995 */
9996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009998 typval_T *argvars;
9999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001 char_u *p;
10002 char_u *name;
10003 int n = FALSE;
10004 int len = 0;
10005
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010006 no_autoload = TRUE;
10007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 if (*p == '$') /* environment variable */
10010 {
10011 /* first try "normal" environment variables (fast) */
10012 if (mch_getenv(p + 1) != NULL)
10013 n = TRUE;
10014 else
10015 {
10016 /* try expanding things like $VIM and ${HOME} */
10017 p = expand_env_save(p);
10018 if (p != NULL && *p != '$')
10019 n = TRUE;
10020 vim_free(p);
10021 }
10022 }
10023 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010026 if (*skipwhite(p) != NUL)
10027 n = FALSE; /* trailing garbage */
10028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 else if (*p == '*') /* internal or user defined function */
10030 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010031 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 }
10033 else if (*p == ':')
10034 {
10035 n = cmd_exists(p + 1);
10036 }
10037 else if (*p == '#')
10038 {
10039#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010040 if (p[1] == '#')
10041 n = autocmd_supported(p + 2);
10042 else
10043 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#endif
10045 }
10046 else /* internal variable */
10047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010048 char_u *tofree;
10049 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010051 /* get_name_len() takes care of expanding curly braces */
10052 name = p;
10053 len = get_name_len(&p, &tofree, TRUE, FALSE);
10054 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010056 if (tofree != NULL)
10057 name = tofree;
10058 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10059 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010061 /* handle d.key, l[idx], f(expr) */
10062 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10063 if (n)
10064 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 }
10066 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010067 if (*p != NUL)
10068 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010070 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 }
10072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010074
10075 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076}
10077
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010078#ifdef FEAT_FLOAT
10079/*
10080 * "exp()" function
10081 */
10082 static void
10083f_exp(argvars, rettv)
10084 typval_T *argvars;
10085 typval_T *rettv;
10086{
10087 float_T f;
10088
10089 rettv->v_type = VAR_FLOAT;
10090 if (get_float_arg(argvars, &f) == OK)
10091 rettv->vval.v_float = exp(f);
10092 else
10093 rettv->vval.v_float = 0.0;
10094}
10095#endif
10096
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097/*
10098 * "expand()" function
10099 */
10100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010102 typval_T *argvars;
10103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105 char_u *s;
10106 int len;
10107 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010108 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010111 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010114 if (argvars[1].v_type != VAR_UNKNOWN
10115 && argvars[2].v_type != VAR_UNKNOWN
10116 && get_tv_number_chk(&argvars[2], &error)
10117 && !error)
10118 {
10119 rettv->v_type = VAR_LIST;
10120 rettv->vval.v_list = NULL;
10121 }
10122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 if (*s == '%' || *s == '#' || *s == '<')
10125 {
10126 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010127 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010129 if (rettv->v_type == VAR_LIST)
10130 {
10131 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10132 list_append_string(rettv->vval.v_list, result, -1);
10133 else
10134 vim_free(result);
10135 }
10136 else
10137 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139 else
10140 {
10141 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010142 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 if (argvars[1].v_type != VAR_UNKNOWN
10144 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010145 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010146 if (!error)
10147 {
10148 ExpandInit(&xpc);
10149 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010150 if (p_wic)
10151 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010152 if (rettv->v_type == VAR_STRING)
10153 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10154 options, WILD_ALL);
10155 else if (rettv_list_alloc(rettv) != FAIL)
10156 {
10157 int i;
10158
10159 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10160 for (i = 0; i < xpc.xp_numfiles; i++)
10161 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10162 ExpandCleanup(&xpc);
10163 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010164 }
10165 else
10166 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 }
10168}
10169
10170/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010171 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010173 */
10174 static void
10175f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010176 typval_T *argvars;
10177 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010178{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010179 char *arg_errmsg = N_("extend() argument");
10180
Bram Moolenaare9a41262005-01-15 22:18:47 +000010181 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010182 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010183 list_T *l1, *l2;
10184 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010185 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010187
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 l1 = argvars[0].vval.v_list;
10189 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010190 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010191 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 {
10193 if (argvars[2].v_type != VAR_UNKNOWN)
10194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 before = get_tv_number_chk(&argvars[2], &error);
10196 if (error)
10197 return; /* type error; errmsg already given */
10198
Bram Moolenaar758711c2005-02-02 23:11:38 +000010199 if (before == l1->lv_len)
10200 item = NULL;
10201 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010202 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010203 item = list_find(l1, before);
10204 if (item == NULL)
10205 {
10206 EMSGN(_(e_listidx), before);
10207 return;
10208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 }
10210 }
10211 else
10212 item = NULL;
10213 list_extend(l1, l2, item);
10214
Bram Moolenaare9a41262005-01-15 22:18:47 +000010215 copy_tv(&argvars[0], rettv);
10216 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010217 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10219 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010220 dict_T *d1, *d2;
10221 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 char_u *action;
10223 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010225 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010226
10227 d1 = argvars[0].vval.v_dict;
10228 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010229 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010230 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010231 {
10232 /* Check the third argument. */
10233 if (argvars[2].v_type != VAR_UNKNOWN)
10234 {
10235 static char *(av[]) = {"keep", "force", "error"};
10236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 action = get_tv_string_chk(&argvars[2]);
10238 if (action == NULL)
10239 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 for (i = 0; i < 3; ++i)
10241 if (STRCMP(action, av[i]) == 0)
10242 break;
10243 if (i == 3)
10244 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010245 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010246 return;
10247 }
10248 }
10249 else
10250 action = (char_u *)"force";
10251
10252 /* Go over all entries in the second dict and add them to the
10253 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010254 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010256 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010257 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010259 --todo;
10260 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010261 if (d1->dv_scope != 0)
10262 {
10263 /* Disallow replacing a builtin function in l: and g:.
10264 * Check the key to be valid when adding to any
10265 * scope. */
10266 if (d1->dv_scope == VAR_DEF_SCOPE
10267 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10268 && var_check_func_name(hi2->hi_key,
10269 di1 == NULL))
10270 break;
10271 if (!valid_varname(hi2->hi_key))
10272 break;
10273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010274 if (di1 == NULL)
10275 {
10276 di1 = dictitem_copy(HI2DI(hi2));
10277 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10278 dictitem_free(di1);
10279 }
10280 else if (*action == 'e')
10281 {
10282 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10283 break;
10284 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010285 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010286 {
10287 clear_tv(&di1->di_tv);
10288 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10289 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 }
10291 }
10292
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 copy_tv(&argvars[0], rettv);
10294 }
10295 }
10296 else
10297 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010298}
10299
10300/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010301 * "feedkeys()" function
10302 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010303 static void
10304f_feedkeys(argvars, rettv)
10305 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010306 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010307{
10308 int remap = TRUE;
10309 char_u *keys, *flags;
10310 char_u nbuf[NUMBUFLEN];
10311 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010312 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010313
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010314 /* This is not allowed in the sandbox. If the commands would still be
10315 * executed in the sandbox it would be OK, but it probably happens later,
10316 * when "sandbox" is no longer set. */
10317 if (check_secure())
10318 return;
10319
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010320 keys = get_tv_string(&argvars[0]);
10321 if (*keys != NUL)
10322 {
10323 if (argvars[1].v_type != VAR_UNKNOWN)
10324 {
10325 flags = get_tv_string_buf(&argvars[1], nbuf);
10326 for ( ; *flags != NUL; ++flags)
10327 {
10328 switch (*flags)
10329 {
10330 case 'n': remap = FALSE; break;
10331 case 'm': remap = TRUE; break;
10332 case 't': typed = TRUE; break;
10333 }
10334 }
10335 }
10336
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010337 /* Need to escape K_SPECIAL and CSI before putting the string in the
10338 * typeahead buffer. */
10339 keys_esc = vim_strsave_escape_csi(keys);
10340 if (keys_esc != NULL)
10341 {
10342 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010343 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010344 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010345 if (vgetc_busy)
10346 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010347 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010348 }
10349}
10350
10351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 * "filereadable()" function
10353 */
10354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 typval_T *argvars;
10357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010359 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 char_u *p;
10361 int n;
10362
Bram Moolenaarc236c162008-07-13 17:41:49 +000010363#ifndef O_NONBLOCK
10364# define O_NONBLOCK 0
10365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010367 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10368 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 {
10370 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010371 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373 else
10374 n = FALSE;
10375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377}
10378
10379/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010380 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 * rights to write into.
10382 */
10383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010384f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 typval_T *argvars;
10386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010388 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389}
10390
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010391static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010392
10393 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010394findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010395 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010397 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010398{
10399#ifdef FEAT_SEARCHPATH
10400 char_u *fname;
10401 char_u *fresult = NULL;
10402 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10403 char_u *p;
10404 char_u pathbuf[NUMBUFLEN];
10405 int count = 1;
10406 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010407 int error = FALSE;
10408#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010409
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010410 rettv->vval.v_string = NULL;
10411 rettv->v_type = VAR_STRING;
10412
10413#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010415
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010416 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10419 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010420 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 else
10422 {
10423 if (*p != NUL)
10424 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010427 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010429 }
10430
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010431 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10432 error = TRUE;
10433
10434 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 do
10437 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010438 if (rettv->v_type == VAR_STRING)
10439 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 fresult = find_file_in_path_option(first ? fname : NULL,
10441 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010442 0, first, path,
10443 find_what,
10444 curbuf->b_ffname,
10445 find_what == FINDFILE_DIR
10446 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010448
10449 if (fresult != NULL && rettv->v_type == VAR_LIST)
10450 list_append_string(rettv->vval.v_list, fresult, -1);
10451
10452 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010454
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010455 if (rettv->v_type == VAR_STRING)
10456 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010458}
10459
Bram Moolenaar33570922005-01-25 22:26:29 +000010460static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10461static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010462
10463/*
10464 * Implementation of map() and filter().
10465 */
10466 static void
10467filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 typval_T *argvars;
10469 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010470 int map;
10471{
10472 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 listitem_T *li, *nli;
10475 list_T *l = NULL;
10476 dictitem_T *di;
10477 hashtab_T *ht;
10478 hashitem_T *hi;
10479 dict_T *d = NULL;
10480 typval_T save_val;
10481 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010483 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010484 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10485 char *arg_errmsg = (map ? N_("map() argument")
10486 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010487 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010488 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010489
Bram Moolenaare9a41262005-01-15 22:18:47 +000010490 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010492 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010493 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 return;
10495 }
10496 else if (argvars[0].v_type == VAR_DICT)
10497 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010498 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010499 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 return;
10501 }
10502 else
10503 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010504 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 return;
10506 }
10507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 expr = get_tv_string_buf_chk(&argvars[1], buf);
10509 /* On type errors, the preceding call has already displayed an error
10510 * message. Avoid a misleading error message for an empty string that
10511 * was not passed as argument. */
10512 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 prepare_vimvar(VV_VAL, &save_val);
10515 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010516
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010517 /* We reset "did_emsg" to be able to detect whether an error
10518 * occurred during evaluation of the expression. */
10519 save_did_emsg = did_emsg;
10520 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010521
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010522 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 vimvars[VV_KEY].vv_type = VAR_STRING;
10526
10527 ht = &d->dv_hashtab;
10528 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010529 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 if (!HASHITEM_EMPTY(hi))
10533 {
10534 --todo;
10535 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010536 if (tv_check_lock(di->di_tv.v_lock,
10537 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 break;
10539 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010540 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010541 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 break;
10543 if (!map && rem)
10544 dictitem_remove(d, di);
10545 clear_tv(&vimvars[VV_KEY].vv_tv);
10546 }
10547 }
10548 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 }
10550 else
10551 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010552 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 for (li = l->lv_first; li != NULL; li = nli)
10555 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010556 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010557 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010559 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010560 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010561 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010562 break;
10563 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010565 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010566 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010567 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010568
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010569 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010571
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010572 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574
10575 copy_tv(&argvars[0], rettv);
10576}
10577
10578 static int
10579filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 char_u *expr;
10582 int map;
10583 int *remp;
10584{
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010587 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 s = expr;
10591 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010592 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 if (*s != NUL) /* check for trailing chars after expr */
10594 {
10595 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010596 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 }
10598 if (map)
10599 {
10600 /* map(): replace the list item value */
10601 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010602 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 *tv = rettv;
10604 }
10605 else
10606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 int error = FALSE;
10608
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 /* On type error, nothing has been removed; return FAIL to stop the
10613 * loop. The error message was given by get_tv_number_chk(). */
10614 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010615 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010617 retval = OK;
10618theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010619 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010620 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621}
10622
10623/*
10624 * "filter()" function
10625 */
10626 static void
10627f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 typval_T *argvars;
10629 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010630{
10631 filter_map(argvars, rettv, FALSE);
10632}
10633
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635 * "finddir({fname}[, {path}[, {count}]])" function
10636 */
10637 static void
10638f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010639 typval_T *argvars;
10640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010642 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010643}
10644
10645/*
10646 * "findfile({fname}[, {path}[, {count}]])" function
10647 */
10648 static void
10649f_findfile(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_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654}
10655
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010656#ifdef FEAT_FLOAT
10657/*
10658 * "float2nr({float})" function
10659 */
10660 static void
10661f_float2nr(argvars, rettv)
10662 typval_T *argvars;
10663 typval_T *rettv;
10664{
10665 float_T f;
10666
10667 if (get_float_arg(argvars, &f) == OK)
10668 {
10669 if (f < -0x7fffffff)
10670 rettv->vval.v_number = -0x7fffffff;
10671 else if (f > 0x7fffffff)
10672 rettv->vval.v_number = 0x7fffffff;
10673 else
10674 rettv->vval.v_number = (varnumber_T)f;
10675 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010676}
10677
10678/*
10679 * "floor({float})" function
10680 */
10681 static void
10682f_floor(argvars, rettv)
10683 typval_T *argvars;
10684 typval_T *rettv;
10685{
10686 float_T f;
10687
10688 rettv->v_type = VAR_FLOAT;
10689 if (get_float_arg(argvars, &f) == OK)
10690 rettv->vval.v_float = floor(f);
10691 else
10692 rettv->vval.v_float = 0.0;
10693}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010694
10695/*
10696 * "fmod()" function
10697 */
10698 static void
10699f_fmod(argvars, rettv)
10700 typval_T *argvars;
10701 typval_T *rettv;
10702{
10703 float_T fx, fy;
10704
10705 rettv->v_type = VAR_FLOAT;
10706 if (get_float_arg(argvars, &fx) == OK
10707 && get_float_arg(&argvars[1], &fy) == OK)
10708 rettv->vval.v_float = fmod(fx, fy);
10709 else
10710 rettv->vval.v_float = 0.0;
10711}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010712#endif
10713
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010715 * "fnameescape({string})" function
10716 */
10717 static void
10718f_fnameescape(argvars, rettv)
10719 typval_T *argvars;
10720 typval_T *rettv;
10721{
10722 rettv->vval.v_string = vim_strsave_fnameescape(
10723 get_tv_string(&argvars[0]), FALSE);
10724 rettv->v_type = VAR_STRING;
10725}
10726
10727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 * "fnamemodify({fname}, {mods})" function
10729 */
10730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010732 typval_T *argvars;
10733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
10735 char_u *fname;
10736 char_u *mods;
10737 int usedlen = 0;
10738 int len;
10739 char_u *fbuf = NULL;
10740 char_u buf[NUMBUFLEN];
10741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 fname = get_tv_string_chk(&argvars[0]);
10743 mods = get_tv_string_buf_chk(&argvars[1], buf);
10744 if (fname == NULL || mods == NULL)
10745 fname = NULL;
10746 else
10747 {
10748 len = (int)STRLEN(fname);
10749 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 vim_free(fbuf);
10758}
10759
Bram Moolenaar33570922005-01-25 22:26:29 +000010760static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761
10762/*
10763 * "foldclosed()" function
10764 */
10765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010767 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010769 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
10771#ifdef FEAT_FOLDING
10772 linenr_T lnum;
10773 linenr_T first, last;
10774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10777 {
10778 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10779 {
10780 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 return;
10785 }
10786 }
10787#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789}
10790
10791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792 * "foldclosed()" function
10793 */
10794 static void
10795f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 typval_T *argvars;
10797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798{
10799 foldclosed_both(argvars, rettv, FALSE);
10800}
10801
10802/*
10803 * "foldclosedend()" function
10804 */
10805 static void
10806f_foldclosedend(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, TRUE);
10811}
10812
10813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 * "foldlevel()" function
10815 */
10816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010818 typval_T *argvars UNUSED;
10819 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821#ifdef FEAT_FOLDING
10822 linenr_T lnum;
10823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * "foldtext()" function
10832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010835 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
10838#ifdef FEAT_FOLDING
10839 linenr_T lnum;
10840 char_u *s;
10841 char_u *r;
10842 int len;
10843 char *txt;
10844#endif
10845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->v_type = VAR_STRING;
10847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10850 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10851 <= curbuf->b_ml.ml_line_count
10852 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 {
10854 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10856 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 {
10858 if (!linewhite(lnum))
10859 break;
10860 ++lnum;
10861 }
10862
10863 /* Find interesting text in this line. */
10864 s = skipwhite(ml_get(lnum));
10865 /* skip C comment-start */
10866 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010869 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010871 {
10872 s = skipwhite(ml_get(lnum + 1));
10873 if (*s == '*')
10874 s = skipwhite(s + 1);
10875 }
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 txt = _("+-%s%3ld lines: ");
10878 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010879 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 + 20 /* for %3ld */
10881 + STRLEN(s))); /* concatenated */
10882 if (r != NULL)
10883 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010884 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10885 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10886 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 len = (int)STRLEN(r);
10888 STRCAT(r, s);
10889 /* remove 'foldmarker' and 'commentstring' */
10890 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 }
10893 }
10894#endif
10895}
10896
10897/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010898 * "foldtextresult(lnum)" function
10899 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010901f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010904{
10905#ifdef FEAT_FOLDING
10906 linenr_T lnum;
10907 char_u *text;
10908 char_u buf[51];
10909 foldinfo_T foldinfo;
10910 int fold_count;
10911#endif
10912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->v_type = VAR_STRING;
10914 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010915#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 /* treat illegal types and illegal string values for {lnum} the same */
10918 if (lnum < 0)
10919 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010920 fold_count = foldedCount(curwin, lnum, &foldinfo);
10921 if (fold_count > 0)
10922 {
10923 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10924 &foldinfo, buf);
10925 if (text == buf)
10926 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010928 }
10929#endif
10930}
10931
10932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 * "foreground()" function
10934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010937 typval_T *argvars UNUSED;
10938 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940#ifdef FEAT_GUI
10941 if (gui.in_use)
10942 gui_mch_set_foreground();
10943#else
10944# ifdef WIN32
10945 win32_set_foreground();
10946# endif
10947#endif
10948}
10949
10950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 * "function()" function
10952 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *argvars;
10956 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957{
10958 char_u *s;
10959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010961 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010963 /* Don't check an autoload name for existence here. */
10964 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010965 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010966 else
10967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968 rettv->vval.v_string = vim_strsave(s);
10969 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010970 }
10971}
10972
10973/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010974 * "garbagecollect()" function
10975 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010976 static void
10977f_garbagecollect(argvars, rettv)
10978 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010979 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010980{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010981 /* This is postponed until we are back at the toplevel, because we may be
10982 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10983 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010984
10985 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10986 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010987}
10988
10989/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990 * "get()" function
10991 */
10992 static void
10993f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010994 typval_T *argvars;
10995 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010996{
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 listitem_T *li;
10998 list_T *l;
10999 dictitem_T *di;
11000 dict_T *d;
11001 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011002
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011004 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011007 int error = FALSE;
11008
11009 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11010 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 else if (argvars[0].v_type == VAR_DICT)
11015 {
11016 if ((d = argvars[0].vval.v_dict) != NULL)
11017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011018 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 if (di != NULL)
11020 tv = &di->di_tv;
11021 }
11022 }
11023 else
11024 EMSG2(_(e_listdictarg), "get()");
11025
11026 if (tv == NULL)
11027 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 copy_tv(&argvars[2], rettv);
11030 }
11031 else
11032 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011033}
11034
Bram Moolenaar342337a2005-07-21 21:11:17 +000011035static 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 +000011036
11037/*
11038 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011039 * Return a range (from start to end) of lines in rettv from the specified
11040 * buffer.
11041 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011042 */
11043 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011044get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011045 buf_T *buf;
11046 linenr_T start;
11047 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011048 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011049 typval_T *rettv;
11050{
11051 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011052
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011053 if (retlist && rettv_list_alloc(rettv) == FAIL)
11054 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011055
11056 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11057 return;
11058
11059 if (!retlist)
11060 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011061 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11062 p = ml_get_buf(buf, start, FALSE);
11063 else
11064 p = (char_u *)"";
11065
11066 rettv->v_type = VAR_STRING;
11067 rettv->vval.v_string = vim_strsave(p);
11068 }
11069 else
11070 {
11071 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011072 return;
11073
11074 if (start < 1)
11075 start = 1;
11076 if (end > buf->b_ml.ml_line_count)
11077 end = buf->b_ml.ml_line_count;
11078 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011079 if (list_append_string(rettv->vval.v_list,
11080 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011081 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011082 }
11083}
11084
11085/*
11086 * "getbufline()" function
11087 */
11088 static void
11089f_getbufline(argvars, rettv)
11090 typval_T *argvars;
11091 typval_T *rettv;
11092{
11093 linenr_T lnum;
11094 linenr_T end;
11095 buf_T *buf;
11096
11097 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11098 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011099 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011100 --emsg_off;
11101
Bram Moolenaar661b1822005-07-28 22:36:45 +000011102 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011103 if (argvars[2].v_type == VAR_UNKNOWN)
11104 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011105 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011106 end = get_tv_lnum_buf(&argvars[2], buf);
11107
Bram Moolenaar342337a2005-07-21 21:11:17 +000011108 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011109}
11110
Bram Moolenaar0d660222005-01-07 21:51:51 +000011111/*
11112 * "getbufvar()" function
11113 */
11114 static void
11115f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118{
11119 buf_T *buf;
11120 buf_T *save_curbuf;
11121 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011123 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11126 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011128 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011130 rettv->v_type = VAR_STRING;
11131 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132
11133 if (buf != NULL && varname != NULL)
11134 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011135 /* set curbuf to be our buf, temporarily */
11136 save_curbuf = curbuf;
11137 curbuf = buf;
11138
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011140 {
11141 if (get_option_tv(&varname, rettv, TRUE) == OK)
11142 done = TRUE;
11143 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011144 else if (STRCMP(varname, "changedtick") == 0)
11145 {
11146 rettv->v_type = VAR_NUMBER;
11147 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011148 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 else
11151 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011152 /* Look up the variable. */
11153 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11154 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11155 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011157 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011159 done = TRUE;
11160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011162
11163 /* restore previous notion of curbuf */
11164 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 }
11166
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011167 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11168 /* use the default value */
11169 copy_tv(&argvars[2], rettv);
11170
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171 --emsg_off;
11172}
11173
11174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 * "getchar()" function
11176 */
11177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011179 typval_T *argvars;
11180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181{
11182 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011185 /* Position the cursor. Needed after a message that ends in a space. */
11186 windgoto(msg_row, msg_col);
11187
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 ++no_mapping;
11189 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011190 for (;;)
11191 {
11192 if (argvars[0].v_type == VAR_UNKNOWN)
11193 /* getchar(): blocking wait. */
11194 n = safe_vgetc();
11195 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11196 /* getchar(1): only check if char avail */
11197 n = vpeekc();
11198 else if (error || vpeekc() == NUL)
11199 /* illegal argument or getchar(0) and no char avail: return zero */
11200 n = 0;
11201 else
11202 /* getchar(0) and char avail: return char */
11203 n = safe_vgetc();
11204 if (n == K_IGNORE)
11205 continue;
11206 break;
11207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 --no_mapping;
11209 --allow_keys;
11210
Bram Moolenaar219b8702006-11-01 14:32:36 +000011211 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11212 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11213 vimvars[VV_MOUSE_COL].vv_nr = 0;
11214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 if (IS_SPECIAL(n) || mod_mask != 0)
11217 {
11218 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11219 int i = 0;
11220
11221 /* Turn a special key into three bytes, plus modifier. */
11222 if (mod_mask != 0)
11223 {
11224 temp[i++] = K_SPECIAL;
11225 temp[i++] = KS_MODIFIER;
11226 temp[i++] = mod_mask;
11227 }
11228 if (IS_SPECIAL(n))
11229 {
11230 temp[i++] = K_SPECIAL;
11231 temp[i++] = K_SECOND(n);
11232 temp[i++] = K_THIRD(n);
11233 }
11234#ifdef FEAT_MBYTE
11235 else if (has_mbyte)
11236 i += (*mb_char2bytes)(n, temp + i);
11237#endif
11238 else
11239 temp[i++] = n;
11240 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 rettv->v_type = VAR_STRING;
11242 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011243
11244#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011245 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011246 {
11247 int row = mouse_row;
11248 int col = mouse_col;
11249 win_T *win;
11250 linenr_T lnum;
11251# ifdef FEAT_WINDOWS
11252 win_T *wp;
11253# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011254 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011255
11256 if (row >= 0 && col >= 0)
11257 {
11258 /* Find the window at the mouse coordinates and compute the
11259 * text position. */
11260 win = mouse_find_win(&row, &col);
11261 (void)mouse_comp_pos(win, &row, &col, &lnum);
11262# ifdef FEAT_WINDOWS
11263 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011264 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011265# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011266 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011267 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11268 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11269 }
11270 }
11271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 }
11273}
11274
11275/*
11276 * "getcharmod()" function
11277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284}
11285
11286/*
11287 * "getcmdline()" function
11288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->v_type = VAR_STRING;
11295 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
11298/*
11299 * "getcmdpos()" function
11300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011303 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307}
11308
11309/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011310 * "getcmdtype()" function
11311 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011312 static void
11313f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011314 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011315 typval_T *rettv;
11316{
11317 rettv->v_type = VAR_STRING;
11318 rettv->vval.v_string = alloc(2);
11319 if (rettv->vval.v_string != NULL)
11320 {
11321 rettv->vval.v_string[0] = get_cmdline_type();
11322 rettv->vval.v_string[1] = NUL;
11323 }
11324}
11325
11326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 * "getcwd()" function
11328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011334 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011337 rettv->vval.v_string = NULL;
11338 cwd = alloc(MAXPATHL);
11339 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011341 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11342 {
11343 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011345 if (rettv->vval.v_string != NULL)
11346 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011348 }
11349 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 }
11351}
11352
11353/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011354 * "getfontname()" function
11355 */
11356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->v_type = VAR_STRING;
11362 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011363#ifdef FEAT_GUI
11364 if (gui.in_use)
11365 {
11366 GuiFont font;
11367 char_u *name = NULL;
11368
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011369 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011370 {
11371 /* Get the "Normal" font. Either the name saved by
11372 * hl_set_font_name() or from the font ID. */
11373 font = gui.norm_font;
11374 name = hl_get_font_name();
11375 }
11376 else
11377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011379 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11380 return;
11381 font = gui_mch_get_font(name, FALSE);
11382 if (font == NOFONT)
11383 return; /* Invalid font name, return empty string. */
11384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011386 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011387 gui_mch_free_font(font);
11388 }
11389#endif
11390}
11391
11392/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011393 * "getfperm({fname})" function
11394 */
11395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *argvars;
11398 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399{
11400 char_u *fname;
11401 struct stat st;
11402 char_u *perm = NULL;
11403 char_u flags[] = "rwx";
11404 int i;
11405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011409 if (mch_stat((char *)fname, &st) >= 0)
11410 {
11411 perm = vim_strsave((char_u *)"---------");
11412 if (perm != NULL)
11413 {
11414 for (i = 0; i < 9; i++)
11415 {
11416 if (st.st_mode & (1 << (8 - i)))
11417 perm[i] = flags[i % 3];
11418 }
11419 }
11420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011422}
11423
11424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 * "getfsize({fname})" function
11426 */
11427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *argvars;
11430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431{
11432 char_u *fname;
11433 struct stat st;
11434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
11439 if (mch_stat((char *)fname, &st) >= 0)
11440 {
11441 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011446
11447 /* non-perfect check for overflow */
11448 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11449 rettv->vval.v_number = -2;
11450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 }
11452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454}
11455
11456/*
11457 * "getftime({fname})" function
11458 */
11459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T *argvars;
11462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
11464 char_u *fname;
11465 struct stat st;
11466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
11469 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473}
11474
11475/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011476 * "getftype({fname})" function
11477 */
11478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011480 typval_T *argvars;
11481 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011482{
11483 char_u *fname;
11484 struct stat st;
11485 char_u *type = NULL;
11486 char *t;
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491 if (mch_lstat((char *)fname, &st) >= 0)
11492 {
11493#ifdef S_ISREG
11494 if (S_ISREG(st.st_mode))
11495 t = "file";
11496 else if (S_ISDIR(st.st_mode))
11497 t = "dir";
11498# ifdef S_ISLNK
11499 else if (S_ISLNK(st.st_mode))
11500 t = "link";
11501# endif
11502# ifdef S_ISBLK
11503 else if (S_ISBLK(st.st_mode))
11504 t = "bdev";
11505# endif
11506# ifdef S_ISCHR
11507 else if (S_ISCHR(st.st_mode))
11508 t = "cdev";
11509# endif
11510# ifdef S_ISFIFO
11511 else if (S_ISFIFO(st.st_mode))
11512 t = "fifo";
11513# endif
11514# ifdef S_ISSOCK
11515 else if (S_ISSOCK(st.st_mode))
11516 t = "fifo";
11517# endif
11518 else
11519 t = "other";
11520#else
11521# ifdef S_IFMT
11522 switch (st.st_mode & S_IFMT)
11523 {
11524 case S_IFREG: t = "file"; break;
11525 case S_IFDIR: t = "dir"; break;
11526# ifdef S_IFLNK
11527 case S_IFLNK: t = "link"; break;
11528# endif
11529# ifdef S_IFBLK
11530 case S_IFBLK: t = "bdev"; break;
11531# endif
11532# ifdef S_IFCHR
11533 case S_IFCHR: t = "cdev"; break;
11534# endif
11535# ifdef S_IFIFO
11536 case S_IFIFO: t = "fifo"; break;
11537# endif
11538# ifdef S_IFSOCK
11539 case S_IFSOCK: t = "socket"; break;
11540# endif
11541 default: t = "other";
11542 }
11543# else
11544 if (mch_isdir(fname))
11545 t = "dir";
11546 else
11547 t = "file";
11548# endif
11549#endif
11550 type = vim_strsave((char_u *)t);
11551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011553}
11554
11555/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011556 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557 */
11558 static void
11559f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 typval_T *argvars;
11561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011562{
11563 linenr_T lnum;
11564 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011565 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011566
11567 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011568 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011569 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011570 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 retlist = FALSE;
11572 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011573 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011574 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011575 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011576 retlist = TRUE;
11577 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011578
Bram Moolenaar342337a2005-07-21 21:11:17 +000011579 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580}
11581
11582/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011583 * "getmatches()" function
11584 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011585 static void
11586f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011587 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011588 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011589{
11590#ifdef FEAT_SEARCH_EXTRA
11591 dict_T *dict;
11592 matchitem_T *cur = curwin->w_match_head;
11593
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011594 if (rettv_list_alloc(rettv) == OK)
11595 {
11596 while (cur != NULL)
11597 {
11598 dict = dict_alloc();
11599 if (dict == NULL)
11600 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011601 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11602 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11603 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11604 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11605 list_append_dict(rettv->vval.v_list, dict);
11606 cur = cur->next;
11607 }
11608 }
11609#endif
11610}
11611
11612/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011613 * "getpid()" function
11614 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011615 static void
11616f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011617 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011618 typval_T *rettv;
11619{
11620 rettv->vval.v_number = mch_get_pid();
11621}
11622
11623/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011624 * "getpos(string)" function
11625 */
11626 static void
11627f_getpos(argvars, rettv)
11628 typval_T *argvars;
11629 typval_T *rettv;
11630{
11631 pos_T *fp;
11632 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011633 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011634
11635 if (rettv_list_alloc(rettv) == OK)
11636 {
11637 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011638 fp = var2fpos(&argvars[0], TRUE, &fnum);
11639 if (fnum != -1)
11640 list_append_number(l, (varnumber_T)fnum);
11641 else
11642 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011643 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11644 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011645 list_append_number(l, (fp != NULL)
11646 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011647 : (varnumber_T)0);
11648 list_append_number(l,
11649#ifdef FEAT_VIRTUALEDIT
11650 (fp != NULL) ? (varnumber_T)fp->coladd :
11651#endif
11652 (varnumber_T)0);
11653 }
11654 else
11655 rettv->vval.v_number = FALSE;
11656}
11657
11658/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011659 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011660 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011661 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011662f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011663 typval_T *argvars UNUSED;
11664 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011665{
11666#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011667 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011668#endif
11669
Bram Moolenaar2641f772005-03-25 21:58:17 +000011670#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011671 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011672 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011673 wp = NULL;
11674 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11675 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011676 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011677 if (wp == NULL)
11678 return;
11679 }
11680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011681 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011682 }
11683#endif
11684}
11685
11686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 * "getreg()" function
11688 */
11689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 typval_T *argvars;
11692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693{
11694 char_u *strregname;
11695 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011696 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011699 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 strregname = get_tv_string_chk(&argvars[0]);
11702 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011703 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011707 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 regname = (strregname == NULL ? '"' : *strregname);
11709 if (regname == 0)
11710 regname = '"';
11711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 rettv->vval.v_string = error ? NULL :
11714 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715}
11716
11717/*
11718 * "getregtype()" function
11719 */
11720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011722 typval_T *argvars;
11723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724{
11725 char_u *strregname;
11726 int regname;
11727 char_u buf[NUMBUFLEN + 2];
11728 long reglen = 0;
11729
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011730 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 {
11732 strregname = get_tv_string_chk(&argvars[0]);
11733 if (strregname == NULL) /* type error; errmsg already given */
11734 {
11735 rettv->v_type = VAR_STRING;
11736 rettv->vval.v_string = NULL;
11737 return;
11738 }
11739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 else
11741 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743
11744 regname = (strregname == NULL ? '"' : *strregname);
11745 if (regname == 0)
11746 regname = '"';
11747
11748 buf[0] = NUL;
11749 buf[1] = NUL;
11750 switch (get_reg_type(regname, &reglen))
11751 {
11752 case MLINE: buf[0] = 'V'; break;
11753 case MCHAR: buf[0] = 'v'; break;
11754#ifdef FEAT_VISUAL
11755 case MBLOCK:
11756 buf[0] = Ctrl_V;
11757 sprintf((char *)buf + 1, "%ld", reglen + 1);
11758 break;
11759#endif
11760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->v_type = VAR_STRING;
11762 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763}
11764
11765/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011766 * "gettabvar()" function
11767 */
11768 static void
11769f_gettabvar(argvars, rettv)
11770 typval_T *argvars;
11771 typval_T *rettv;
11772{
11773 tabpage_T *tp;
11774 dictitem_T *v;
11775 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011776 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011777
11778 rettv->v_type = VAR_STRING;
11779 rettv->vval.v_string = NULL;
11780
11781 varname = get_tv_string_chk(&argvars[1]);
11782 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11783 if (tp != NULL && varname != NULL)
11784 {
11785 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011786 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011787 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011788 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011789 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011790 done = TRUE;
11791 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011792 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011793
11794 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011795 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011796}
11797
11798/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011799 * "gettabwinvar()" function
11800 */
11801 static void
11802f_gettabwinvar(argvars, rettv)
11803 typval_T *argvars;
11804 typval_T *rettv;
11805{
11806 getwinvar(argvars, rettv, 1);
11807}
11808
11809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 * "getwinposx()" function
11811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818#ifdef FEAT_GUI
11819 if (gui.in_use)
11820 {
11821 int x, y;
11822
11823 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 }
11826#endif
11827}
11828
11829/*
11830 * "getwinposy()" function
11831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011834 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838#ifdef FEAT_GUI
11839 if (gui.in_use)
11840 {
11841 int x, y;
11842
11843 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 }
11846#endif
11847}
11848
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011849/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011850 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011851 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011852 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011853find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011854 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011855 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011856{
11857#ifdef FEAT_WINDOWS
11858 win_T *wp;
11859#endif
11860 int nr;
11861
11862 nr = get_tv_number_chk(vp, NULL);
11863
11864#ifdef FEAT_WINDOWS
11865 if (nr < 0)
11866 return NULL;
11867 if (nr == 0)
11868 return curwin;
11869
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011870 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11871 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011872 if (--nr <= 0)
11873 break;
11874 return wp;
11875#else
11876 if (nr == 0 || nr == 1)
11877 return curwin;
11878 return NULL;
11879#endif
11880}
11881
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882/*
11883 * "getwinvar()" function
11884 */
11885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011887 typval_T *argvars;
11888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011890 getwinvar(argvars, rettv, 0);
11891}
11892
11893/*
11894 * getwinvar() and gettabwinvar()
11895 */
11896 static void
11897getwinvar(argvars, rettv, off)
11898 typval_T *argvars;
11899 typval_T *rettv;
11900 int off; /* 1 for gettabwinvar() */
11901{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 win_T *win, *oldcurwin;
11903 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011905 tabpage_T *tp;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011906 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011908#ifdef FEAT_WINDOWS
11909 if (off == 1)
11910 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11911 else
11912 tp = curtab;
11913#endif
11914 win = find_win_by_nr(&argvars[off], tp);
11915 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011918 rettv->v_type = VAR_STRING;
11919 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920
11921 if (win != NULL && varname != NULL)
11922 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011923 /* Set curwin to be our win, temporarily. Also set curbuf, so
11924 * that we can get buffer-local options. */
11925 oldcurwin = curwin;
11926 curwin = win;
11927 curbuf = win->w_buffer;
11928
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011930 {
11931 if (get_option_tv(&varname, rettv, 1) == OK)
11932 done = TRUE;
11933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 else
11935 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011936 /* Look up the variable. */
11937 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11938 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011940 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011942 done = TRUE;
11943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011945
11946 /* restore previous notion of curwin */
11947 curwin = oldcurwin;
11948 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 }
11950
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011951 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11952 /* use the default return value */
11953 copy_tv(&argvars[off + 2], rettv);
11954
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 --emsg_off;
11956}
11957
11958/*
11959 * "glob()" function
11960 */
11961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011963 typval_T *argvars;
11964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011966 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011968 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011970 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011971 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011973 if (argvars[1].v_type != VAR_UNKNOWN)
11974 {
11975 if (get_tv_number_chk(&argvars[1], &error))
11976 options |= WILD_KEEP_ALL;
11977 if (argvars[2].v_type != VAR_UNKNOWN
11978 && get_tv_number_chk(&argvars[2], &error))
11979 {
11980 rettv->v_type = VAR_LIST;
11981 rettv->vval.v_list = NULL;
11982 }
11983 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011984 if (!error)
11985 {
11986 ExpandInit(&xpc);
11987 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011988 if (p_wic)
11989 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011990 if (rettv->v_type == VAR_STRING)
11991 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011992 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011993 else if (rettv_list_alloc(rettv) != FAIL)
11994 {
11995 int i;
11996
11997 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11998 NULL, options, WILD_ALL_KEEP);
11999 for (i = 0; i < xpc.xp_numfiles; i++)
12000 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12001
12002 ExpandCleanup(&xpc);
12003 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012004 }
12005 else
12006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007}
12008
12009/*
12010 * "globpath()" function
12011 */
12012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012014 typval_T *argvars;
12015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012017 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012019 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012020 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012022 /* When the optional second argument is non-zero, don't remove matches
12023 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12024 if (argvars[2].v_type != VAR_UNKNOWN
12025 && get_tv_number_chk(&argvars[2], &error))
12026 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012028 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029 rettv->vval.v_string = NULL;
12030 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012031 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12032 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033}
12034
12035/*
12036 * "has()" function
12037 */
12038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039f_has(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{
12043 int i;
12044 char_u *name;
12045 int n = FALSE;
12046 static char *(has_list[]) =
12047 {
12048#ifdef AMIGA
12049 "amiga",
12050# ifdef FEAT_ARP
12051 "arp",
12052# endif
12053#endif
12054#ifdef __BEOS__
12055 "beos",
12056#endif
12057#ifdef MSDOS
12058# ifdef DJGPP
12059 "dos32",
12060# else
12061 "dos16",
12062# endif
12063#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012064#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 "mac",
12066#endif
12067#if defined(MACOS_X_UNIX)
12068 "macunix",
12069#endif
12070#ifdef OS2
12071 "os2",
12072#endif
12073#ifdef __QNX__
12074 "qnx",
12075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076#ifdef UNIX
12077 "unix",
12078#endif
12079#ifdef VMS
12080 "vms",
12081#endif
12082#ifdef WIN16
12083 "win16",
12084#endif
12085#ifdef WIN32
12086 "win32",
12087#endif
12088#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12089 "win32unix",
12090#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012091#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 "win64",
12093#endif
12094#ifdef EBCDIC
12095 "ebcdic",
12096#endif
12097#ifndef CASE_INSENSITIVE_FILENAME
12098 "fname_case",
12099#endif
12100#ifdef FEAT_ARABIC
12101 "arabic",
12102#endif
12103#ifdef FEAT_AUTOCMD
12104 "autocmd",
12105#endif
12106#ifdef FEAT_BEVAL
12107 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012108# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12109 "balloon_multiline",
12110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111#endif
12112#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12113 "builtin_terms",
12114# ifdef ALL_BUILTIN_TCAPS
12115 "all_builtin_terms",
12116# endif
12117#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012118#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12119 || defined(FEAT_GUI_W32) \
12120 || defined(FEAT_GUI_MOTIF))
12121 "browsefilter",
12122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123#ifdef FEAT_BYTEOFF
12124 "byte_offset",
12125#endif
12126#ifdef FEAT_CINDENT
12127 "cindent",
12128#endif
12129#ifdef FEAT_CLIENTSERVER
12130 "clientserver",
12131#endif
12132#ifdef FEAT_CLIPBOARD
12133 "clipboard",
12134#endif
12135#ifdef FEAT_CMDL_COMPL
12136 "cmdline_compl",
12137#endif
12138#ifdef FEAT_CMDHIST
12139 "cmdline_hist",
12140#endif
12141#ifdef FEAT_COMMENTS
12142 "comments",
12143#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012144#ifdef FEAT_CONCEAL
12145 "conceal",
12146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147#ifdef FEAT_CRYPT
12148 "cryptv",
12149#endif
12150#ifdef FEAT_CSCOPE
12151 "cscope",
12152#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012153#ifdef FEAT_CURSORBIND
12154 "cursorbind",
12155#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012156#ifdef CURSOR_SHAPE
12157 "cursorshape",
12158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159#ifdef DEBUG
12160 "debug",
12161#endif
12162#ifdef FEAT_CON_DIALOG
12163 "dialog_con",
12164#endif
12165#ifdef FEAT_GUI_DIALOG
12166 "dialog_gui",
12167#endif
12168#ifdef FEAT_DIFF
12169 "diff",
12170#endif
12171#ifdef FEAT_DIGRAPHS
12172 "digraphs",
12173#endif
12174#ifdef FEAT_DND
12175 "dnd",
12176#endif
12177#ifdef FEAT_EMACS_TAGS
12178 "emacs_tags",
12179#endif
12180 "eval", /* always present, of course! */
12181#ifdef FEAT_EX_EXTRA
12182 "ex_extra",
12183#endif
12184#ifdef FEAT_SEARCH_EXTRA
12185 "extra_search",
12186#endif
12187#ifdef FEAT_FKMAP
12188 "farsi",
12189#endif
12190#ifdef FEAT_SEARCHPATH
12191 "file_in_path",
12192#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012193#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012194 "filterpipe",
12195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196#ifdef FEAT_FIND_ID
12197 "find_in_path",
12198#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012199#ifdef FEAT_FLOAT
12200 "float",
12201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202#ifdef FEAT_FOLDING
12203 "folding",
12204#endif
12205#ifdef FEAT_FOOTER
12206 "footer",
12207#endif
12208#if !defined(USE_SYSTEM) && defined(UNIX)
12209 "fork",
12210#endif
12211#ifdef FEAT_GETTEXT
12212 "gettext",
12213#endif
12214#ifdef FEAT_GUI
12215 "gui",
12216#endif
12217#ifdef FEAT_GUI_ATHENA
12218# ifdef FEAT_GUI_NEXTAW
12219 "gui_neXtaw",
12220# else
12221 "gui_athena",
12222# endif
12223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224#ifdef FEAT_GUI_GTK
12225 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012228#ifdef FEAT_GUI_GNOME
12229 "gui_gnome",
12230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231#ifdef FEAT_GUI_MAC
12232 "gui_mac",
12233#endif
12234#ifdef FEAT_GUI_MOTIF
12235 "gui_motif",
12236#endif
12237#ifdef FEAT_GUI_PHOTON
12238 "gui_photon",
12239#endif
12240#ifdef FEAT_GUI_W16
12241 "gui_win16",
12242#endif
12243#ifdef FEAT_GUI_W32
12244 "gui_win32",
12245#endif
12246#ifdef FEAT_HANGULIN
12247 "hangul_input",
12248#endif
12249#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12250 "iconv",
12251#endif
12252#ifdef FEAT_INS_EXPAND
12253 "insert_expand",
12254#endif
12255#ifdef FEAT_JUMPLIST
12256 "jumplist",
12257#endif
12258#ifdef FEAT_KEYMAP
12259 "keymap",
12260#endif
12261#ifdef FEAT_LANGMAP
12262 "langmap",
12263#endif
12264#ifdef FEAT_LIBCALL
12265 "libcall",
12266#endif
12267#ifdef FEAT_LINEBREAK
12268 "linebreak",
12269#endif
12270#ifdef FEAT_LISP
12271 "lispindent",
12272#endif
12273#ifdef FEAT_LISTCMDS
12274 "listcmds",
12275#endif
12276#ifdef FEAT_LOCALMAP
12277 "localmap",
12278#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012279#ifdef FEAT_LUA
12280# ifndef DYNAMIC_LUA
12281 "lua",
12282# endif
12283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284#ifdef FEAT_MENU
12285 "menu",
12286#endif
12287#ifdef FEAT_SESSION
12288 "mksession",
12289#endif
12290#ifdef FEAT_MODIFY_FNAME
12291 "modify_fname",
12292#endif
12293#ifdef FEAT_MOUSE
12294 "mouse",
12295#endif
12296#ifdef FEAT_MOUSESHAPE
12297 "mouseshape",
12298#endif
12299#if defined(UNIX) || defined(VMS)
12300# ifdef FEAT_MOUSE_DEC
12301 "mouse_dec",
12302# endif
12303# ifdef FEAT_MOUSE_GPM
12304 "mouse_gpm",
12305# endif
12306# ifdef FEAT_MOUSE_JSB
12307 "mouse_jsbterm",
12308# endif
12309# ifdef FEAT_MOUSE_NET
12310 "mouse_netterm",
12311# endif
12312# ifdef FEAT_MOUSE_PTERM
12313 "mouse_pterm",
12314# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012315# ifdef FEAT_MOUSE_SGR
12316 "mouse_sgr",
12317# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012318# ifdef FEAT_SYSMOUSE
12319 "mouse_sysmouse",
12320# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012321# ifdef FEAT_MOUSE_URXVT
12322 "mouse_urxvt",
12323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324# ifdef FEAT_MOUSE_XTERM
12325 "mouse_xterm",
12326# endif
12327#endif
12328#ifdef FEAT_MBYTE
12329 "multi_byte",
12330#endif
12331#ifdef FEAT_MBYTE_IME
12332 "multi_byte_ime",
12333#endif
12334#ifdef FEAT_MULTI_LANG
12335 "multi_lang",
12336#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012337#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012338#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012339 "mzscheme",
12340#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342#ifdef FEAT_OLE
12343 "ole",
12344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345#ifdef FEAT_PATH_EXTRA
12346 "path_extra",
12347#endif
12348#ifdef FEAT_PERL
12349#ifndef DYNAMIC_PERL
12350 "perl",
12351#endif
12352#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012353#ifdef FEAT_PERSISTENT_UNDO
12354 "persistent_undo",
12355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356#ifdef FEAT_PYTHON
12357#ifndef DYNAMIC_PYTHON
12358 "python",
12359#endif
12360#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012361#ifdef FEAT_PYTHON3
12362#ifndef DYNAMIC_PYTHON3
12363 "python3",
12364#endif
12365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366#ifdef FEAT_POSTSCRIPT
12367 "postscript",
12368#endif
12369#ifdef FEAT_PRINTER
12370 "printer",
12371#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012372#ifdef FEAT_PROFILE
12373 "profile",
12374#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012375#ifdef FEAT_RELTIME
12376 "reltime",
12377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378#ifdef FEAT_QUICKFIX
12379 "quickfix",
12380#endif
12381#ifdef FEAT_RIGHTLEFT
12382 "rightleft",
12383#endif
12384#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12385 "ruby",
12386#endif
12387#ifdef FEAT_SCROLLBIND
12388 "scrollbind",
12389#endif
12390#ifdef FEAT_CMDL_INFO
12391 "showcmd",
12392 "cmdline_info",
12393#endif
12394#ifdef FEAT_SIGNS
12395 "signs",
12396#endif
12397#ifdef FEAT_SMARTINDENT
12398 "smartindent",
12399#endif
12400#ifdef FEAT_SNIFF
12401 "sniff",
12402#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012403#ifdef STARTUPTIME
12404 "startuptime",
12405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406#ifdef FEAT_STL_OPT
12407 "statusline",
12408#endif
12409#ifdef FEAT_SUN_WORKSHOP
12410 "sun_workshop",
12411#endif
12412#ifdef FEAT_NETBEANS_INTG
12413 "netbeans_intg",
12414#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012415#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012416 "spell",
12417#endif
12418#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 "syntax",
12420#endif
12421#if defined(USE_SYSTEM) || !defined(UNIX)
12422 "system",
12423#endif
12424#ifdef FEAT_TAG_BINS
12425 "tag_binary",
12426#endif
12427#ifdef FEAT_TAG_OLDSTATIC
12428 "tag_old_static",
12429#endif
12430#ifdef FEAT_TAG_ANYWHITE
12431 "tag_any_white",
12432#endif
12433#ifdef FEAT_TCL
12434# ifndef DYNAMIC_TCL
12435 "tcl",
12436# endif
12437#endif
12438#ifdef TERMINFO
12439 "terminfo",
12440#endif
12441#ifdef FEAT_TERMRESPONSE
12442 "termresponse",
12443#endif
12444#ifdef FEAT_TEXTOBJ
12445 "textobjects",
12446#endif
12447#ifdef HAVE_TGETENT
12448 "tgetent",
12449#endif
12450#ifdef FEAT_TITLE
12451 "title",
12452#endif
12453#ifdef FEAT_TOOLBAR
12454 "toolbar",
12455#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012456#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12457 "unnamedplus",
12458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459#ifdef FEAT_USR_CMDS
12460 "user-commands", /* was accidentally included in 5.4 */
12461 "user_commands",
12462#endif
12463#ifdef FEAT_VIMINFO
12464 "viminfo",
12465#endif
12466#ifdef FEAT_VERTSPLIT
12467 "vertsplit",
12468#endif
12469#ifdef FEAT_VIRTUALEDIT
12470 "virtualedit",
12471#endif
12472#ifdef FEAT_VISUAL
12473 "visual",
12474#endif
12475#ifdef FEAT_VISUALEXTRA
12476 "visualextra",
12477#endif
12478#ifdef FEAT_VREPLACE
12479 "vreplace",
12480#endif
12481#ifdef FEAT_WILDIGN
12482 "wildignore",
12483#endif
12484#ifdef FEAT_WILDMENU
12485 "wildmenu",
12486#endif
12487#ifdef FEAT_WINDOWS
12488 "windows",
12489#endif
12490#ifdef FEAT_WAK
12491 "winaltkeys",
12492#endif
12493#ifdef FEAT_WRITEBACKUP
12494 "writebackup",
12495#endif
12496#ifdef FEAT_XIM
12497 "xim",
12498#endif
12499#ifdef FEAT_XFONTSET
12500 "xfontset",
12501#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012502#ifdef FEAT_XPM_W32
12503 "xpm_w32",
12504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505#ifdef USE_XSMP
12506 "xsmp",
12507#endif
12508#ifdef USE_XSMP_INTERACT
12509 "xsmp_interact",
12510#endif
12511#ifdef FEAT_XCLIPBOARD
12512 "xterm_clipboard",
12513#endif
12514#ifdef FEAT_XTERM_SAVE
12515 "xterm_save",
12516#endif
12517#if defined(UNIX) && defined(FEAT_X11)
12518 "X11",
12519#endif
12520 NULL
12521 };
12522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012523 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 for (i = 0; has_list[i] != NULL; ++i)
12525 if (STRICMP(name, has_list[i]) == 0)
12526 {
12527 n = TRUE;
12528 break;
12529 }
12530
12531 if (n == FALSE)
12532 {
12533 if (STRNICMP(name, "patch", 5) == 0)
12534 n = has_patch(atoi((char *)name + 5));
12535 else if (STRICMP(name, "vim_starting") == 0)
12536 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012537#ifdef FEAT_MBYTE
12538 else if (STRICMP(name, "multi_byte_encoding") == 0)
12539 n = has_mbyte;
12540#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012541#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12542 else if (STRICMP(name, "balloon_multiline") == 0)
12543 n = multiline_balloon_available();
12544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545#ifdef DYNAMIC_TCL
12546 else if (STRICMP(name, "tcl") == 0)
12547 n = tcl_enabled(FALSE);
12548#endif
12549#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12550 else if (STRICMP(name, "iconv") == 0)
12551 n = iconv_enabled(FALSE);
12552#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012553#ifdef DYNAMIC_LUA
12554 else if (STRICMP(name, "lua") == 0)
12555 n = lua_enabled(FALSE);
12556#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012557#ifdef DYNAMIC_MZSCHEME
12558 else if (STRICMP(name, "mzscheme") == 0)
12559 n = mzscheme_enabled(FALSE);
12560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561#ifdef DYNAMIC_RUBY
12562 else if (STRICMP(name, "ruby") == 0)
12563 n = ruby_enabled(FALSE);
12564#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012565#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566#ifdef DYNAMIC_PYTHON
12567 else if (STRICMP(name, "python") == 0)
12568 n = python_enabled(FALSE);
12569#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012570#endif
12571#ifdef FEAT_PYTHON3
12572#ifdef DYNAMIC_PYTHON3
12573 else if (STRICMP(name, "python3") == 0)
12574 n = python3_enabled(FALSE);
12575#endif
12576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577#ifdef DYNAMIC_PERL
12578 else if (STRICMP(name, "perl") == 0)
12579 n = perl_enabled(FALSE);
12580#endif
12581#ifdef FEAT_GUI
12582 else if (STRICMP(name, "gui_running") == 0)
12583 n = (gui.in_use || gui.starting);
12584# ifdef FEAT_GUI_W32
12585 else if (STRICMP(name, "gui_win32s") == 0)
12586 n = gui_is_win32s();
12587# endif
12588# ifdef FEAT_BROWSE
12589 else if (STRICMP(name, "browse") == 0)
12590 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12591# endif
12592#endif
12593#ifdef FEAT_SYN_HL
12594 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012595 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#endif
12597#if defined(WIN3264)
12598 else if (STRICMP(name, "win95") == 0)
12599 n = mch_windows95();
12600#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012601#ifdef FEAT_NETBEANS_INTG
12602 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012603 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 }
12606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608}
12609
12610/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012611 * "has_key()" function
12612 */
12613 static void
12614f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012615 typval_T *argvars;
12616 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012617{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012618 if (argvars[0].v_type != VAR_DICT)
12619 {
12620 EMSG(_(e_dictreq));
12621 return;
12622 }
12623 if (argvars[0].vval.v_dict == NULL)
12624 return;
12625
12626 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012627 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012628}
12629
12630/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012631 * "haslocaldir()" function
12632 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012633 static void
12634f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012635 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012636 typval_T *rettv;
12637{
12638 rettv->vval.v_number = (curwin->w_localdir != NULL);
12639}
12640
12641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642 * "hasmapto()" function
12643 */
12644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012646 typval_T *argvars;
12647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648{
12649 char_u *name;
12650 char_u *mode;
12651 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012652 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012655 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 mode = (char_u *)"nvo";
12657 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012660 if (argvars[2].v_type != VAR_UNKNOWN)
12661 abbr = get_tv_number(&argvars[2]);
12662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
Bram Moolenaar2c932302006-03-18 21:42:09 +000012664 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668}
12669
12670/*
12671 * "histadd()" function
12672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012675 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677{
12678#ifdef FEAT_CMDHIST
12679 int histype;
12680 char_u *str;
12681 char_u buf[NUMBUFLEN];
12682#endif
12683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 if (check_restricted() || check_secure())
12686 return;
12687#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12689 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 if (histype >= 0)
12691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 if (*str != NUL)
12694 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012695 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 return;
12699 }
12700 }
12701#endif
12702}
12703
12704/*
12705 * "histdel()" function
12706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012709 typval_T *argvars UNUSED;
12710 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711{
12712#ifdef FEAT_CMDHIST
12713 int n;
12714 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012715 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12718 if (str == NULL)
12719 n = 0;
12720 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012723 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 else
12728 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 get_tv_string_buf(&argvars[1], buf));
12731 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732#endif
12733}
12734
12735/*
12736 * "histget()" function
12737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742{
12743#ifdef FEAT_CMDHIST
12744 int type;
12745 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12749 if (str == NULL)
12750 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 {
12753 type = get_histtype(str);
12754 if (argvars[1].v_type == VAR_UNKNOWN)
12755 idx = get_history_idx(type);
12756 else
12757 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12758 /* -1 on type error */
12759 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765}
12766
12767/*
12768 * "histnr()" function
12769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012772 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
12775 int i;
12776
12777#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 char_u *history = get_tv_string_chk(&argvars[0]);
12779
12780 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 if (i >= HIST_CMD && i < HIST_COUNT)
12782 i = get_history_idx(i);
12783 else
12784#endif
12785 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787}
12788
12789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 * "highlightID(name)" function
12791 */
12792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012794 typval_T *argvars;
12795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
12800/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801 * "highlight_exists()" function
12802 */
12803 static void
12804f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 typval_T *argvars;
12806 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012807{
12808 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12809}
12810
12811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 * "hostname()" function
12813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818{
12819 char_u hostname[256];
12820
12821 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 rettv->v_type = VAR_STRING;
12823 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
12827 * iconv() function
12828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012831 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833{
12834#ifdef FEAT_MBYTE
12835 char_u buf1[NUMBUFLEN];
12836 char_u buf2[NUMBUFLEN];
12837 char_u *from, *to, *str;
12838 vimconv_T vimconv;
12839#endif
12840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->v_type = VAR_STRING;
12842 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
12844#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 str = get_tv_string(&argvars[0]);
12846 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12847 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 vimconv.vc_type = CONV_NONE;
12849 convert_setup(&vimconv, from, to);
12850
12851 /* If the encodings are equal, no conversion needed. */
12852 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
12857 convert_setup(&vimconv, NULL, NULL);
12858 vim_free(from);
12859 vim_free(to);
12860#endif
12861}
12862
12863/*
12864 * "indent()" function
12865 */
12866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 typval_T *argvars;
12869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870{
12871 linenr_T lnum;
12872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012880/*
12881 * "index()" function
12882 */
12883 static void
12884f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *argvars;
12886 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012887{
Bram Moolenaar33570922005-01-25 22:26:29 +000012888 list_T *l;
12889 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012890 long idx = 0;
12891 int ic = FALSE;
12892
12893 rettv->vval.v_number = -1;
12894 if (argvars[0].v_type != VAR_LIST)
12895 {
12896 EMSG(_(e_listreq));
12897 return;
12898 }
12899 l = argvars[0].vval.v_list;
12900 if (l != NULL)
12901 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012902 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012905 int error = FALSE;
12906
Bram Moolenaar758711c2005-02-02 23:11:38 +000012907 /* Start at specified item. Use the cached index that list_find()
12908 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012909 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012910 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012911 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012912 ic = get_tv_number_chk(&argvars[3], &error);
12913 if (error)
12914 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012915 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012916
Bram Moolenaar758711c2005-02-02 23:11:38 +000012917 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012918 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012919 {
12920 rettv->vval.v_number = idx;
12921 break;
12922 }
12923 }
12924}
12925
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926static int inputsecret_flag = 0;
12927
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012928static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12929
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012931 * This function is used by f_input() and f_inputdialog() functions. The third
12932 * argument to f_input() specifies the type of completion to use at the
12933 * prompt. The third argument to f_inputdialog() specifies the value to return
12934 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 */
12936 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012937get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012938 typval_T *argvars;
12939 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012940 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 char_u *p = NULL;
12944 int c;
12945 char_u buf[NUMBUFLEN];
12946 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012947 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012948 int xp_type = EXPAND_NOTHING;
12949 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953
12954#ifdef NO_CONSOLE_INPUT
12955 /* While starting up, there is no place to enter text. */
12956 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958#endif
12959
12960 cmd_silent = FALSE; /* Want to see the prompt. */
12961 if (prompt != NULL)
12962 {
12963 /* Only the part of the message after the last NL is considered as
12964 * prompt for the command line */
12965 p = vim_strrchr(prompt, '\n');
12966 if (p == NULL)
12967 p = prompt;
12968 else
12969 {
12970 ++p;
12971 c = *p;
12972 *p = NUL;
12973 msg_start();
12974 msg_clr_eos();
12975 msg_puts_attr(prompt, echo_attr);
12976 msg_didout = FALSE;
12977 msg_starthere();
12978 *p = c;
12979 }
12980 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012982 if (argvars[1].v_type != VAR_UNKNOWN)
12983 {
12984 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12985 if (defstr != NULL)
12986 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012988 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012989 {
12990 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012991 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012992 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012993
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012994 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012995 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012996
Bram Moolenaar4463f292005-09-25 22:20:24 +000012997 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12998 if (xp_name == NULL)
12999 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013001 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013002
Bram Moolenaar4463f292005-09-25 22:20:24 +000013003 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13004 &xp_arg) == FAIL)
13005 return;
13006 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013007 }
13008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013009 if (defstr != NULL)
13010 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013011 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13012 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013013 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013014 && argvars[1].v_type != VAR_UNKNOWN
13015 && argvars[2].v_type != VAR_UNKNOWN)
13016 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13017 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013018
13019 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 /* since the user typed this, no need to wait for return */
13022 need_wait_return = FALSE;
13023 msg_didout = FALSE;
13024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 cmd_silent = cmd_silent_save;
13026}
13027
13028/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013029 * "input()" function
13030 * Also handles inputsecret() when inputsecret is set.
13031 */
13032 static void
13033f_input(argvars, rettv)
13034 typval_T *argvars;
13035 typval_T *rettv;
13036{
13037 get_user_input(argvars, rettv, FALSE);
13038}
13039
13040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 * "inputdialog()" function
13042 */
13043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
13048#if defined(FEAT_GUI_TEXTDIALOG)
13049 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13050 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13051 {
13052 char_u *message;
13053 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 message = get_tv_string_chk(&argvars[0]);
13057 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013058 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013059 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 else
13061 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 if (message != NULL && defstr != NULL
13063 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013064 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 else
13067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068 if (message != NULL && defstr != NULL
13069 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013070 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 rettv->vval.v_string = vim_strsave(
13072 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 }
13078 else
13079#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013080 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081}
13082
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013083/*
13084 * "inputlist()" function
13085 */
13086 static void
13087f_inputlist(argvars, rettv)
13088 typval_T *argvars;
13089 typval_T *rettv;
13090{
13091 listitem_T *li;
13092 int selected;
13093 int mouse_used;
13094
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013095#ifdef NO_CONSOLE_INPUT
13096 /* While starting up, there is no place to enter text. */
13097 if (no_console_input())
13098 return;
13099#endif
13100 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13101 {
13102 EMSG2(_(e_listarg), "inputlist()");
13103 return;
13104 }
13105
13106 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013107 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013108 lines_left = Rows; /* avoid more prompt */
13109 msg_scroll = TRUE;
13110 msg_clr_eos();
13111
13112 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13113 {
13114 msg_puts(get_tv_string(&li->li_tv));
13115 msg_putchar('\n');
13116 }
13117
13118 /* Ask for choice. */
13119 selected = prompt_for_number(&mouse_used);
13120 if (mouse_used)
13121 selected -= lines_left;
13122
13123 rettv->vval.v_number = selected;
13124}
13125
13126
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13128
13129/*
13130 * "inputrestore()" function
13131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136{
13137 if (ga_userinput.ga_len > 0)
13138 {
13139 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13141 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013142 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 }
13144 else if (p_verbose > 1)
13145 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013146 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 }
13149}
13150
13151/*
13152 * "inputsave()" function
13153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013159 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 if (ga_grow(&ga_userinput, 1) == OK)
13161 {
13162 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13163 + ga_userinput.ga_len);
13164 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013165 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 }
13167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169}
13170
13171/*
13172 * "inputsecret()" function
13173 */
13174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013176 typval_T *argvars;
13177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178{
13179 ++cmdline_star;
13180 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 --cmdline_star;
13183 --inputsecret_flag;
13184}
13185
13186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187 * "insert()" function
13188 */
13189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013191 typval_T *argvars;
13192 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013193{
13194 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 listitem_T *item;
13196 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198
13199 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013201 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013202 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 {
13204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 before = get_tv_number_chk(&argvars[2], &error);
13206 if (error)
13207 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208
Bram Moolenaar758711c2005-02-02 23:11:38 +000013209 if (before == l->lv_len)
13210 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 else
13212 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013213 item = list_find(l, before);
13214 if (item == NULL)
13215 {
13216 EMSGN(_(e_listidx), before);
13217 l = NULL;
13218 }
13219 }
13220 if (l != NULL)
13221 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013222 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013223 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224 }
13225 }
13226}
13227
13228/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013229 * "invert(expr)" function
13230 */
13231 static void
13232f_invert(argvars, rettv)
13233 typval_T *argvars;
13234 typval_T *rettv;
13235{
13236 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13237}
13238
13239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 * "isdirectory()" function
13241 */
13242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013244 typval_T *argvars;
13245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248}
13249
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013250/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013251 * "islocked()" function
13252 */
13253 static void
13254f_islocked(argvars, rettv)
13255 typval_T *argvars;
13256 typval_T *rettv;
13257{
13258 lval_T lv;
13259 char_u *end;
13260 dictitem_T *di;
13261
13262 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013263 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13264 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013265 if (end != NULL && lv.ll_name != NULL)
13266 {
13267 if (*end != NUL)
13268 EMSG(_(e_trailing));
13269 else
13270 {
13271 if (lv.ll_tv == NULL)
13272 {
13273 if (check_changedtick(lv.ll_name))
13274 rettv->vval.v_number = 1; /* always locked */
13275 else
13276 {
13277 di = find_var(lv.ll_name, NULL);
13278 if (di != NULL)
13279 {
13280 /* Consider a variable locked when:
13281 * 1. the variable itself is locked
13282 * 2. the value of the variable is locked.
13283 * 3. the List or Dict value is locked.
13284 */
13285 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13286 || tv_islocked(&di->di_tv));
13287 }
13288 }
13289 }
13290 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013291 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013292 else if (lv.ll_newkey != NULL)
13293 EMSG2(_(e_dictkey), lv.ll_newkey);
13294 else if (lv.ll_list != NULL)
13295 /* List item. */
13296 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13297 else
13298 /* Dictionary item. */
13299 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13300 }
13301 }
13302
13303 clear_lval(&lv);
13304}
13305
Bram Moolenaar33570922005-01-25 22:26:29 +000013306static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013307
13308/*
13309 * Turn a dict into a list:
13310 * "what" == 0: list of keys
13311 * "what" == 1: list of values
13312 * "what" == 2: list of items
13313 */
13314 static void
13315dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013316 typval_T *argvars;
13317 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013318 int what;
13319{
Bram Moolenaar33570922005-01-25 22:26:29 +000013320 list_T *l2;
13321 dictitem_T *di;
13322 hashitem_T *hi;
13323 listitem_T *li;
13324 listitem_T *li2;
13325 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013326 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013327
Bram Moolenaar8c711452005-01-14 21:53:12 +000013328 if (argvars[0].v_type != VAR_DICT)
13329 {
13330 EMSG(_(e_dictreq));
13331 return;
13332 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013333 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013334 return;
13335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013336 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013337 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013339 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013341 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013342 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013344 --todo;
13345 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013347 li = listitem_alloc();
13348 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013349 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013350 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013351
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013352 if (what == 0)
13353 {
13354 /* keys() */
13355 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013356 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013357 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13358 }
13359 else if (what == 1)
13360 {
13361 /* values() */
13362 copy_tv(&di->di_tv, &li->li_tv);
13363 }
13364 else
13365 {
13366 /* items() */
13367 l2 = list_alloc();
13368 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013369 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013370 li->li_tv.vval.v_list = l2;
13371 if (l2 == NULL)
13372 break;
13373 ++l2->lv_refcount;
13374
13375 li2 = listitem_alloc();
13376 if (li2 == NULL)
13377 break;
13378 list_append(l2, li2);
13379 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013380 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013381 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13382
13383 li2 = listitem_alloc();
13384 if (li2 == NULL)
13385 break;
13386 list_append(l2, li2);
13387 copy_tv(&di->di_tv, &li2->li_tv);
13388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013389 }
13390 }
13391}
13392
13393/*
13394 * "items(dict)" function
13395 */
13396 static void
13397f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013400{
13401 dict_list(argvars, rettv, 2);
13402}
13403
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013405 * "join()" function
13406 */
13407 static void
13408f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411{
13412 garray_T ga;
13413 char_u *sep;
13414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013415 if (argvars[0].v_type != VAR_LIST)
13416 {
13417 EMSG(_(e_listreq));
13418 return;
13419 }
13420 if (argvars[0].vval.v_list == NULL)
13421 return;
13422 if (argvars[1].v_type == VAR_UNKNOWN)
13423 sep = (char_u *)" ";
13424 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013426
13427 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013428
13429 if (sep != NULL)
13430 {
13431 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013432 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 ga_append(&ga, NUL);
13434 rettv->vval.v_string = (char_u *)ga.ga_data;
13435 }
13436 else
13437 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013438}
13439
13440/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013441 * "keys()" function
13442 */
13443 static void
13444f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 typval_T *argvars;
13446 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013447{
13448 dict_list(argvars, rettv, 0);
13449}
13450
13451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 * "last_buffer_nr()" function.
13453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013456 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459 int n = 0;
13460 buf_T *buf;
13461
13462 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13463 if (n < buf->b_fnum)
13464 n = buf->b_fnum;
13465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013467}
13468
13469/*
13470 * "len()" function
13471 */
13472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *argvars;
13475 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013476{
13477 switch (argvars[0].v_type)
13478 {
13479 case VAR_STRING:
13480 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 rettv->vval.v_number = (varnumber_T)STRLEN(
13482 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013483 break;
13484 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013487 case VAR_DICT:
13488 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13489 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013490 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013491 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 break;
13493 }
13494}
13495
Bram Moolenaar33570922005-01-25 22:26:29 +000013496static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013497
13498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013502 int type;
13503{
13504#ifdef FEAT_LIBCALL
13505 char_u *string_in;
13506 char_u **string_result;
13507 int nr_result;
13508#endif
13509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013511 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513
13514 if (check_restricted() || check_secure())
13515 return;
13516
13517#ifdef FEAT_LIBCALL
13518 /* The first two args must be strings, otherwise its meaningless */
13519 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13520 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013521 string_in = NULL;
13522 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523 string_in = argvars[2].vval.v_string;
13524 if (type == VAR_NUMBER)
13525 string_result = NULL;
13526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013528 if (mch_libcall(argvars[0].vval.v_string,
13529 argvars[1].vval.v_string,
13530 string_in,
13531 argvars[2].vval.v_number,
13532 string_result,
13533 &nr_result) == OK
13534 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013536 }
13537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538}
13539
13540/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013541 * "libcall()" function
13542 */
13543 static void
13544f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013545 typval_T *argvars;
13546 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013547{
13548 libcall_common(argvars, rettv, VAR_STRING);
13549}
13550
13551/*
13552 * "libcallnr()" function
13553 */
13554 static void
13555f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013558{
13559 libcall_common(argvars, rettv, VAR_NUMBER);
13560}
13561
13562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 * "line(string)" function
13564 */
13565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569{
13570 linenr_T lnum = 0;
13571 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013572 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013574 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 if (fp != NULL)
13576 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578}
13579
13580/*
13581 * "line2byte(lnum)" function
13582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013585 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587{
13588#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590#else
13591 linenr_T lnum;
13592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13598 if (rettv->vval.v_number >= 0)
13599 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600#endif
13601}
13602
13603/*
13604 * "lispindent(lnum)" function
13605 */
13606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013608 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
13611#ifdef FEAT_LISP
13612 pos_T pos;
13613 linenr_T lnum;
13614
13615 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13618 {
13619 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 curwin->w_cursor = pos;
13622 }
13623 else
13624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626}
13627
13628/*
13629 * "localtime()" function
13630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013633 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637}
13638
Bram Moolenaar33570922005-01-25 22:26:29 +000013639static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640
13641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *argvars;
13644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 int exact;
13646{
13647 char_u *keys;
13648 char_u *which;
13649 char_u buf[NUMBUFLEN];
13650 char_u *keys_buf = NULL;
13651 char_u *rhs;
13652 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013653 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013654 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013655 mapblock_T *mp;
13656 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657
13658 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 rettv->v_type = VAR_STRING;
13660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 if (*keys == NUL)
13664 return;
13665
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013666 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013669 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013670 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013671 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013672 if (argvars[3].v_type != VAR_UNKNOWN)
13673 get_dict = get_tv_number(&argvars[3]);
13674 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 else
13677 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 if (which == NULL)
13679 return;
13680
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 mode = get_map_mode(&which, 0);
13682
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013683 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013684 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013686
13687 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013689 /* Return a string. */
13690 if (rhs != NULL)
13691 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692
Bram Moolenaarbd743252010-10-20 21:23:33 +020013693 }
13694 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13695 {
13696 /* Return a dictionary. */
13697 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13698 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13699 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaarbd743252010-10-20 21:23:33 +020013701 dict_add_nr_str(dict, "lhs", 0L, lhs);
13702 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13703 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13704 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13705 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13706 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13707 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13708 dict_add_nr_str(dict, "mode", 0L, mapmode);
13709
13710 vim_free(lhs);
13711 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 }
13713}
13714
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013715#ifdef FEAT_FLOAT
13716/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013717 * "log()" function
13718 */
13719 static void
13720f_log(argvars, rettv)
13721 typval_T *argvars;
13722 typval_T *rettv;
13723{
13724 float_T f;
13725
13726 rettv->v_type = VAR_FLOAT;
13727 if (get_float_arg(argvars, &f) == OK)
13728 rettv->vval.v_float = log(f);
13729 else
13730 rettv->vval.v_float = 0.0;
13731}
13732
13733/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013734 * "log10()" function
13735 */
13736 static void
13737f_log10(argvars, rettv)
13738 typval_T *argvars;
13739 typval_T *rettv;
13740{
13741 float_T f;
13742
13743 rettv->v_type = VAR_FLOAT;
13744 if (get_float_arg(argvars, &f) == OK)
13745 rettv->vval.v_float = log10(f);
13746 else
13747 rettv->vval.v_float = 0.0;
13748}
13749#endif
13750
Bram Moolenaar1dced572012-04-05 16:54:08 +020013751#ifdef FEAT_LUA
13752/*
13753 * "luaeval()" function
13754 */
13755 static void
13756f_luaeval(argvars, rettv)
13757 typval_T *argvars;
13758 typval_T *rettv;
13759{
13760 char_u *str;
13761 char_u buf[NUMBUFLEN];
13762
13763 str = get_tv_string_buf(&argvars[0], buf);
13764 do_luaeval(str, argvars + 1, rettv);
13765}
13766#endif
13767
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013769 * "map()" function
13770 */
13771 static void
13772f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013775{
13776 filter_map(argvars, rettv, TRUE);
13777}
13778
13779/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013780 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 */
13782 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *argvars;
13785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788}
13789
13790/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 */
13793 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013794f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013795 typval_T *argvars;
13796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799}
13800
Bram Moolenaar33570922005-01-25 22:26:29 +000013801static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
13803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013804find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013805 typval_T *argvars;
13806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 int type;
13808{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013809 char_u *str = NULL;
13810 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 char_u *pat;
13812 regmatch_T regmatch;
13813 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013814 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 char_u *save_cpo;
13816 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013817 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013818 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013819 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 list_T *l = NULL;
13821 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013823 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824
13825 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13826 save_cpo = p_cpo;
13827 p_cpo = (char_u *)"";
13828
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013829 rettv->vval.v_number = -1;
13830 if (type == 3)
13831 {
13832 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013833 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013834 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013835 }
13836 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013838 rettv->v_type = VAR_STRING;
13839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013842 if (argvars[0].v_type == VAR_LIST)
13843 {
13844 if ((l = argvars[0].vval.v_list) == NULL)
13845 goto theend;
13846 li = l->lv_first;
13847 }
13848 else
13849 expr = str = get_tv_string(&argvars[0]);
13850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13852 if (pat == NULL)
13853 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 int error = FALSE;
13858
13859 start = get_tv_number_chk(&argvars[2], &error);
13860 if (error)
13861 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013862 if (l != NULL)
13863 {
13864 li = list_find(l, start);
13865 if (li == NULL)
13866 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013867 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013868 }
13869 else
13870 {
13871 if (start < 0)
13872 start = 0;
13873 if (start > (long)STRLEN(str))
13874 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013875 /* When "count" argument is there ignore matches before "start",
13876 * otherwise skip part of the string. Differs when pattern is "^"
13877 * or "\<". */
13878 if (argvars[3].v_type != VAR_UNKNOWN)
13879 startcol = start;
13880 else
13881 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013882 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013883
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013884 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 nth = get_tv_number_chk(&argvars[3], &error);
13886 if (error)
13887 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 }
13889
13890 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13891 if (regmatch.regprog != NULL)
13892 {
13893 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013894
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013895 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013896 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013897 if (l != NULL)
13898 {
13899 if (li == NULL)
13900 {
13901 match = FALSE;
13902 break;
13903 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013904 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013905 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013906 if (str == NULL)
13907 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013908 }
13909
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013910 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013911
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013912 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013913 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013914 if (l == NULL && !match)
13915 break;
13916
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013917 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013918 if (l != NULL)
13919 {
13920 li = li->li_next;
13921 ++idx;
13922 }
13923 else
13924 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013925#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013926 startcol = (colnr_T)(regmatch.startp[0]
13927 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013928#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013929 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013930#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013931 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013932 }
13933
13934 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013936 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013938 int i;
13939
13940 /* return list with matched string and submatches */
13941 for (i = 0; i < NSUBEXP; ++i)
13942 {
13943 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013944 {
13945 if (list_append_string(rettv->vval.v_list,
13946 (char_u *)"", 0) == FAIL)
13947 break;
13948 }
13949 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013950 regmatch.startp[i],
13951 (int)(regmatch.endp[i] - regmatch.startp[i]))
13952 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013953 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013954 }
13955 }
13956 else if (type == 2)
13957 {
13958 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013959 if (l != NULL)
13960 copy_tv(&li->li_tv, rettv);
13961 else
13962 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013964 }
13965 else if (l != NULL)
13966 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 else
13968 {
13969 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 (varnumber_T)(regmatch.startp[0] - str);
13972 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013975 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 }
13977 }
13978 vim_free(regmatch.regprog);
13979 }
13980
13981theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013982 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 p_cpo = save_cpo;
13984}
13985
13986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013987 * "match()" function
13988 */
13989 static void
13990f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013991 typval_T *argvars;
13992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993{
13994 find_some_match(argvars, rettv, 1);
13995}
13996
13997/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013998 * "matchadd()" function
13999 */
14000 static void
14001f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014002 typval_T *argvars UNUSED;
14003 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014004{
14005#ifdef FEAT_SEARCH_EXTRA
14006 char_u buf[NUMBUFLEN];
14007 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14008 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14009 int prio = 10; /* default priority */
14010 int id = -1;
14011 int error = FALSE;
14012
14013 rettv->vval.v_number = -1;
14014
14015 if (grp == NULL || pat == NULL)
14016 return;
14017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014018 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014019 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014020 if (argvars[3].v_type != VAR_UNKNOWN)
14021 id = get_tv_number_chk(&argvars[3], &error);
14022 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014023 if (error == TRUE)
14024 return;
14025 if (id >= 1 && id <= 3)
14026 {
14027 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14028 return;
14029 }
14030
14031 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14032#endif
14033}
14034
14035/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014036 * "matcharg()" function
14037 */
14038 static void
14039f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014040 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014041 typval_T *rettv;
14042{
14043 if (rettv_list_alloc(rettv) == OK)
14044 {
14045#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014046 int id = get_tv_number(&argvars[0]);
14047 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014048
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014049 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014050 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014051 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14052 {
14053 list_append_string(rettv->vval.v_list,
14054 syn_id2name(m->hlg_id), -1);
14055 list_append_string(rettv->vval.v_list, m->pattern, -1);
14056 }
14057 else
14058 {
14059 list_append_string(rettv->vval.v_list, NUL, -1);
14060 list_append_string(rettv->vval.v_list, NUL, -1);
14061 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014062 }
14063#endif
14064 }
14065}
14066
14067/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014068 * "matchdelete()" function
14069 */
14070 static void
14071f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014072 typval_T *argvars UNUSED;
14073 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014074{
14075#ifdef FEAT_SEARCH_EXTRA
14076 rettv->vval.v_number = match_delete(curwin,
14077 (int)get_tv_number(&argvars[0]), TRUE);
14078#endif
14079}
14080
14081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 * "matchend()" function
14083 */
14084 static void
14085f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088{
14089 find_some_match(argvars, rettv, 0);
14090}
14091
14092/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014093 * "matchlist()" function
14094 */
14095 static void
14096f_matchlist(argvars, rettv)
14097 typval_T *argvars;
14098 typval_T *rettv;
14099{
14100 find_some_match(argvars, rettv, 3);
14101}
14102
14103/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104 * "matchstr()" function
14105 */
14106 static void
14107f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014108 typval_T *argvars;
14109 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110{
14111 find_some_match(argvars, rettv, 2);
14112}
14113
Bram Moolenaar33570922005-01-25 22:26:29 +000014114static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014115
14116 static void
14117max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014118 typval_T *argvars;
14119 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014120 int domax;
14121{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014122 long n = 0;
14123 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014125
14126 if (argvars[0].v_type == VAR_LIST)
14127 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 list_T *l;
14129 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014130
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014131 l = argvars[0].vval.v_list;
14132 if (l != NULL)
14133 {
14134 li = l->lv_first;
14135 if (li != NULL)
14136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014137 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014138 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014139 {
14140 li = li->li_next;
14141 if (li == NULL)
14142 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014144 if (domax ? i > n : i < n)
14145 n = i;
14146 }
14147 }
14148 }
14149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014150 else if (argvars[0].v_type == VAR_DICT)
14151 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014153 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014154 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014155 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014156
14157 d = argvars[0].vval.v_dict;
14158 if (d != NULL)
14159 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014160 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014162 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014163 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014164 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014165 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014167 if (first)
14168 {
14169 n = i;
14170 first = FALSE;
14171 }
14172 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014173 n = i;
14174 }
14175 }
14176 }
14177 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014178 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014179 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014181}
14182
14183/*
14184 * "max()" function
14185 */
14186 static void
14187f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *argvars;
14189 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014190{
14191 max_min(argvars, rettv, TRUE);
14192}
14193
14194/*
14195 * "min()" function
14196 */
14197 static void
14198f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 typval_T *argvars;
14200 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014201{
14202 max_min(argvars, rettv, FALSE);
14203}
14204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014205static int mkdir_recurse __ARGS((char_u *dir, int prot));
14206
14207/*
14208 * Create the directory in which "dir" is located, and higher levels when
14209 * needed.
14210 */
14211 static int
14212mkdir_recurse(dir, prot)
14213 char_u *dir;
14214 int prot;
14215{
14216 char_u *p;
14217 char_u *updir;
14218 int r = FAIL;
14219
14220 /* Get end of directory name in "dir".
14221 * We're done when it's "/" or "c:/". */
14222 p = gettail_sep(dir);
14223 if (p <= get_past_head(dir))
14224 return OK;
14225
14226 /* If the directory exists we're done. Otherwise: create it.*/
14227 updir = vim_strnsave(dir, (int)(p - dir));
14228 if (updir == NULL)
14229 return FAIL;
14230 if (mch_isdir(updir))
14231 r = OK;
14232 else if (mkdir_recurse(updir, prot) == OK)
14233 r = vim_mkdir_emsg(updir, prot);
14234 vim_free(updir);
14235 return r;
14236}
14237
14238#ifdef vim_mkdir
14239/*
14240 * "mkdir()" function
14241 */
14242 static void
14243f_mkdir(argvars, rettv)
14244 typval_T *argvars;
14245 typval_T *rettv;
14246{
14247 char_u *dir;
14248 char_u buf[NUMBUFLEN];
14249 int prot = 0755;
14250
14251 rettv->vval.v_number = FAIL;
14252 if (check_restricted() || check_secure())
14253 return;
14254
14255 dir = get_tv_string_buf(&argvars[0], buf);
14256 if (argvars[1].v_type != VAR_UNKNOWN)
14257 {
14258 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 prot = get_tv_number_chk(&argvars[2], NULL);
14260 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014261 mkdir_recurse(dir, prot);
14262 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014264}
14265#endif
14266
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 * "mode()" function
14269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 typval_T *argvars;
14273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014275 char_u buf[3];
14276
14277 buf[1] = NUL;
14278 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279
14280#ifdef FEAT_VISUAL
14281 if (VIsual_active)
14282 {
14283 if (VIsual_select)
14284 buf[0] = VIsual_mode + 's' - 'v';
14285 else
14286 buf[0] = VIsual_mode;
14287 }
14288 else
14289#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014290 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14291 || State == CONFIRM)
14292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014294 if (State == ASKMORE)
14295 buf[1] = 'm';
14296 else if (State == CONFIRM)
14297 buf[1] = '?';
14298 }
14299 else if (State == EXTERNCMD)
14300 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 else if (State & INSERT)
14302 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014303#ifdef FEAT_VREPLACE
14304 if (State & VREPLACE_FLAG)
14305 {
14306 buf[0] = 'R';
14307 buf[1] = 'v';
14308 }
14309 else
14310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 if (State & REPLACE_FLAG)
14312 buf[0] = 'R';
14313 else
14314 buf[0] = 'i';
14315 }
14316 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014319 if (exmode_active)
14320 buf[1] = 'v';
14321 }
14322 else if (exmode_active)
14323 {
14324 buf[0] = 'c';
14325 buf[1] = 'e';
14326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014330 if (finish_op)
14331 buf[1] = 'o';
14332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014334 /* Clear out the minor mode when the argument is not a non-zero number or
14335 * non-empty string. */
14336 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014337 buf[1] = NUL;
14338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 rettv->vval.v_string = vim_strsave(buf);
14340 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341}
14342
Bram Moolenaar429fa852013-04-15 12:27:36 +020014343#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014344/*
14345 * "mzeval()" function
14346 */
14347 static void
14348f_mzeval(argvars, rettv)
14349 typval_T *argvars;
14350 typval_T *rettv;
14351{
14352 char_u *str;
14353 char_u buf[NUMBUFLEN];
14354
14355 str = get_tv_string_buf(&argvars[0], buf);
14356 do_mzeval(str, rettv);
14357}
Bram Moolenaar75676462013-01-30 14:55:42 +010014358
14359 void
14360mzscheme_call_vim(name, args, rettv)
14361 char_u *name;
14362 typval_T *args;
14363 typval_T *rettv;
14364{
14365 typval_T argvars[3];
14366
14367 argvars[0].v_type = VAR_STRING;
14368 argvars[0].vval.v_string = name;
14369 copy_tv(args, &argvars[1]);
14370 argvars[2].v_type = VAR_UNKNOWN;
14371 f_call(argvars, rettv);
14372 clear_tv(&argvars[1]);
14373}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014374#endif
14375
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377 * "nextnonblank()" function
14378 */
14379 static void
14380f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014381 typval_T *argvars;
14382 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383{
14384 linenr_T lnum;
14385
14386 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 {
14390 lnum = 0;
14391 break;
14392 }
14393 if (*skipwhite(ml_get(lnum)) != NUL)
14394 break;
14395 }
14396 rettv->vval.v_number = lnum;
14397}
14398
14399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 * "nr2char()" function
14401 */
14402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 typval_T *argvars;
14405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406{
14407 char_u buf[NUMBUFLEN];
14408
14409#ifdef FEAT_MBYTE
14410 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014411 {
14412 int utf8 = 0;
14413
14414 if (argvars[1].v_type != VAR_UNKNOWN)
14415 utf8 = get_tv_number_chk(&argvars[1], NULL);
14416 if (utf8)
14417 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14418 else
14419 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 else
14422#endif
14423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 buf[1] = NUL;
14426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 rettv->v_type = VAR_STRING;
14428 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014429}
14430
14431/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014432 * "or(expr, expr)" function
14433 */
14434 static void
14435f_or(argvars, rettv)
14436 typval_T *argvars;
14437 typval_T *rettv;
14438{
14439 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14440 | get_tv_number_chk(&argvars[1], NULL);
14441}
14442
14443/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014444 * "pathshorten()" function
14445 */
14446 static void
14447f_pathshorten(argvars, rettv)
14448 typval_T *argvars;
14449 typval_T *rettv;
14450{
14451 char_u *p;
14452
14453 rettv->v_type = VAR_STRING;
14454 p = get_tv_string_chk(&argvars[0]);
14455 if (p == NULL)
14456 rettv->vval.v_string = NULL;
14457 else
14458 {
14459 p = vim_strsave(p);
14460 rettv->vval.v_string = p;
14461 if (p != NULL)
14462 shorten_dir(p);
14463 }
14464}
14465
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014466#ifdef FEAT_FLOAT
14467/*
14468 * "pow()" function
14469 */
14470 static void
14471f_pow(argvars, rettv)
14472 typval_T *argvars;
14473 typval_T *rettv;
14474{
14475 float_T fx, fy;
14476
14477 rettv->v_type = VAR_FLOAT;
14478 if (get_float_arg(argvars, &fx) == OK
14479 && get_float_arg(&argvars[1], &fy) == OK)
14480 rettv->vval.v_float = pow(fx, fy);
14481 else
14482 rettv->vval.v_float = 0.0;
14483}
14484#endif
14485
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014486/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014487 * "prevnonblank()" function
14488 */
14489 static void
14490f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *argvars;
14492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493{
14494 linenr_T lnum;
14495
14496 lnum = get_tv_lnum(argvars);
14497 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14498 lnum = 0;
14499 else
14500 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14501 --lnum;
14502 rettv->vval.v_number = lnum;
14503}
14504
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014505#ifdef HAVE_STDARG_H
14506/* This dummy va_list is here because:
14507 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14508 * - locally in the function results in a "used before set" warning
14509 * - using va_start() to initialize it gives "function with fixed args" error */
14510static va_list ap;
14511#endif
14512
Bram Moolenaar8c711452005-01-14 21:53:12 +000014513/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014514 * "printf()" function
14515 */
14516 static void
14517f_printf(argvars, rettv)
14518 typval_T *argvars;
14519 typval_T *rettv;
14520{
14521 rettv->v_type = VAR_STRING;
14522 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014523#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014524 {
14525 char_u buf[NUMBUFLEN];
14526 int len;
14527 char_u *s;
14528 int saved_did_emsg = did_emsg;
14529 char *fmt;
14530
14531 /* Get the required length, allocate the buffer and do it for real. */
14532 did_emsg = FALSE;
14533 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014534 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014535 if (!did_emsg)
14536 {
14537 s = alloc(len + 1);
14538 if (s != NULL)
14539 {
14540 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014541 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014542 }
14543 }
14544 did_emsg |= saved_did_emsg;
14545 }
14546#endif
14547}
14548
14549/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014550 * "pumvisible()" function
14551 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014552 static void
14553f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014554 typval_T *argvars UNUSED;
14555 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014556{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014557#ifdef FEAT_INS_EXPAND
14558 if (pum_visible())
14559 rettv->vval.v_number = 1;
14560#endif
14561}
14562
Bram Moolenaardb913952012-06-29 12:54:53 +020014563#ifdef FEAT_PYTHON3
14564/*
14565 * "py3eval()" function
14566 */
14567 static void
14568f_py3eval(argvars, rettv)
14569 typval_T *argvars;
14570 typval_T *rettv;
14571{
14572 char_u *str;
14573 char_u buf[NUMBUFLEN];
14574
14575 str = get_tv_string_buf(&argvars[0], buf);
14576 do_py3eval(str, rettv);
14577}
14578#endif
14579
14580#ifdef FEAT_PYTHON
14581/*
14582 * "pyeval()" function
14583 */
14584 static void
14585f_pyeval(argvars, rettv)
14586 typval_T *argvars;
14587 typval_T *rettv;
14588{
14589 char_u *str;
14590 char_u buf[NUMBUFLEN];
14591
14592 str = get_tv_string_buf(&argvars[0], buf);
14593 do_pyeval(str, rettv);
14594}
14595#endif
14596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014597/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014598 * "range()" function
14599 */
14600 static void
14601f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 typval_T *argvars;
14603 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604{
14605 long start;
14606 long end;
14607 long stride = 1;
14608 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612 if (argvars[1].v_type == VAR_UNKNOWN)
14613 {
14614 end = start - 1;
14615 start = 0;
14616 }
14617 else
14618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 }
14623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 if (error)
14625 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014627 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014628 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014629 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630 else
14631 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014632 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014633 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014634 if (list_append_number(rettv->vval.v_list,
14635 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014636 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014637 }
14638}
14639
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014640/*
14641 * "readfile()" function
14642 */
14643 static void
14644f_readfile(argvars, rettv)
14645 typval_T *argvars;
14646 typval_T *rettv;
14647{
14648 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014649 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014650 char_u *fname;
14651 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014652 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14653 int io_size = sizeof(buf);
14654 int readlen; /* size of last fread() */
14655 char_u *prev = NULL; /* previously read bytes, if any */
14656 long prevlen = 0; /* length of data in prev */
14657 long prevsize = 0; /* size of prev buffer */
14658 long maxline = MAXLNUM;
14659 long cnt = 0;
14660 char_u *p; /* position in buf */
14661 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014663 if (argvars[1].v_type != VAR_UNKNOWN)
14664 {
14665 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14666 binary = TRUE;
14667 if (argvars[2].v_type != VAR_UNKNOWN)
14668 maxline = get_tv_number(&argvars[2]);
14669 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014670
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014671 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014672 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014673
14674 /* Always open the file in binary mode, library functions have a mind of
14675 * their own about CR-LF conversion. */
14676 fname = get_tv_string(&argvars[0]);
14677 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14678 {
14679 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14680 return;
14681 }
14682
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014683 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014684 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014685 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014686
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014687 /* This for loop processes what was read, but is also entered at end
14688 * of file so that either:
14689 * - an incomplete line gets written
14690 * - a "binary" file gets an empty line at the end if it ends in a
14691 * newline. */
14692 for (p = buf, start = buf;
14693 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14694 ++p)
14695 {
14696 if (*p == '\n' || readlen <= 0)
14697 {
14698 listitem_T *li;
14699 char_u *s = NULL;
14700 long_u len = p - start;
14701
14702 /* Finished a line. Remove CRs before NL. */
14703 if (readlen > 0 && !binary)
14704 {
14705 while (len > 0 && start[len - 1] == '\r')
14706 --len;
14707 /* removal may cross back to the "prev" string */
14708 if (len == 0)
14709 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14710 --prevlen;
14711 }
14712 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014713 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 else
14715 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014716 /* Change "prev" buffer to be the right size. This way
14717 * the bytes are only copied once, and very long lines are
14718 * allocated only once. */
14719 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014720 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014721 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014723 prev = NULL; /* the list will own the string */
14724 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725 }
14726 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014727 if (s == NULL)
14728 {
14729 do_outofmem_msg((long_u) prevlen + len + 1);
14730 failed = TRUE;
14731 break;
14732 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014733
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014734 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014735 {
14736 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014737 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738 break;
14739 }
14740 li->li_tv.v_type = VAR_STRING;
14741 li->li_tv.v_lock = 0;
14742 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014743 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014744
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014745 start = p + 1; /* step over newline */
14746 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014747 break;
14748 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014749 else if (*p == NUL)
14750 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014751#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014752 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14753 * when finding the BF and check the previous two bytes. */
14754 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014755 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014756 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14757 * + 1, these may be in the "prev" string. */
14758 char_u back1 = p >= buf + 1 ? p[-1]
14759 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14760 char_u back2 = p >= buf + 2 ? p[-2]
14761 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14762 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014763
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014764 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014765 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014766 char_u *dest = p - 2;
14767
14768 /* Usually a BOM is at the beginning of a file, and so at
14769 * the beginning of a line; then we can just step over it.
14770 */
14771 if (start == dest)
14772 start = p + 1;
14773 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014774 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775 /* have to shuffle buf to close gap */
14776 int adjust_prevlen = 0;
14777
14778 if (dest < buf)
14779 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014780 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014781 dest = buf;
14782 }
14783 if (readlen > p - buf + 1)
14784 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14785 readlen -= 3 - adjust_prevlen;
14786 prevlen -= adjust_prevlen;
14787 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014788 }
14789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014790 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014791#endif
14792 } /* for */
14793
14794 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14795 break;
14796 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014797 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014798 /* There's part of a line in buf, store it in "prev". */
14799 if (p - start + prevlen >= prevsize)
14800 {
14801 /* need bigger "prev" buffer */
14802 char_u *newprev;
14803
14804 /* A common use case is ordinary text files and "prev" gets a
14805 * fragment of a line, so the first allocation is made
14806 * small, to avoid repeatedly 'allocing' large and
14807 * 'reallocing' small. */
14808 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014809 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014810 else
14811 {
14812 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014813 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014814 prevsize = grow50pc > growmin ? grow50pc : growmin;
14815 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014816 newprev = prev == NULL ? alloc(prevsize)
14817 : vim_realloc(prev, prevsize);
14818 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014819 {
14820 do_outofmem_msg((long_u)prevsize);
14821 failed = TRUE;
14822 break;
14823 }
14824 prev = newprev;
14825 }
14826 /* Add the line part to end of "prev". */
14827 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014828 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014829 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014831
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014832 /*
14833 * For a negative line count use only the lines at the end of the file,
14834 * free the rest.
14835 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014836 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014837 while (cnt > -maxline)
14838 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014839 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014840 --cnt;
14841 }
14842
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014843 if (failed)
14844 {
14845 list_free(rettv->vval.v_list, TRUE);
14846 /* readfile doc says an empty list is returned on error */
14847 rettv->vval.v_list = list_alloc();
14848 }
14849
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014850 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851 fclose(fd);
14852}
14853
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014854#if defined(FEAT_RELTIME)
14855static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14856
14857/*
14858 * Convert a List to proftime_T.
14859 * Return FAIL when there is something wrong.
14860 */
14861 static int
14862list2proftime(arg, tm)
14863 typval_T *arg;
14864 proftime_T *tm;
14865{
14866 long n1, n2;
14867 int error = FALSE;
14868
14869 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14870 || arg->vval.v_list->lv_len != 2)
14871 return FAIL;
14872 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14873 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14874# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014875 tm->HighPart = n1;
14876 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014877# else
14878 tm->tv_sec = n1;
14879 tm->tv_usec = n2;
14880# endif
14881 return error ? FAIL : OK;
14882}
14883#endif /* FEAT_RELTIME */
14884
14885/*
14886 * "reltime()" function
14887 */
14888 static void
14889f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014890 typval_T *argvars UNUSED;
14891 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014892{
14893#ifdef FEAT_RELTIME
14894 proftime_T res;
14895 proftime_T start;
14896
14897 if (argvars[0].v_type == VAR_UNKNOWN)
14898 {
14899 /* No arguments: get current time. */
14900 profile_start(&res);
14901 }
14902 else if (argvars[1].v_type == VAR_UNKNOWN)
14903 {
14904 if (list2proftime(&argvars[0], &res) == FAIL)
14905 return;
14906 profile_end(&res);
14907 }
14908 else
14909 {
14910 /* Two arguments: compute the difference. */
14911 if (list2proftime(&argvars[0], &start) == FAIL
14912 || list2proftime(&argvars[1], &res) == FAIL)
14913 return;
14914 profile_sub(&res, &start);
14915 }
14916
14917 if (rettv_list_alloc(rettv) == OK)
14918 {
14919 long n1, n2;
14920
14921# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014922 n1 = res.HighPart;
14923 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014924# else
14925 n1 = res.tv_sec;
14926 n2 = res.tv_usec;
14927# endif
14928 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14929 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14930 }
14931#endif
14932}
14933
14934/*
14935 * "reltimestr()" function
14936 */
14937 static void
14938f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014939 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014940 typval_T *rettv;
14941{
14942#ifdef FEAT_RELTIME
14943 proftime_T tm;
14944#endif
14945
14946 rettv->v_type = VAR_STRING;
14947 rettv->vval.v_string = NULL;
14948#ifdef FEAT_RELTIME
14949 if (list2proftime(&argvars[0], &tm) == OK)
14950 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14951#endif
14952}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014953
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14955static void make_connection __ARGS((void));
14956static int check_connection __ARGS((void));
14957
14958 static void
14959make_connection()
14960{
14961 if (X_DISPLAY == NULL
14962# ifdef FEAT_GUI
14963 && !gui.in_use
14964# endif
14965 )
14966 {
14967 x_force_connect = TRUE;
14968 setup_term_clip();
14969 x_force_connect = FALSE;
14970 }
14971}
14972
14973 static int
14974check_connection()
14975{
14976 make_connection();
14977 if (X_DISPLAY == NULL)
14978 {
14979 EMSG(_("E240: No connection to Vim server"));
14980 return FAIL;
14981 }
14982 return OK;
14983}
14984#endif
14985
14986#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014987static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988
14989 static void
14990remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014991 typval_T *argvars;
14992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 int expr;
14994{
14995 char_u *server_name;
14996 char_u *keys;
14997 char_u *r = NULL;
14998 char_u buf[NUMBUFLEN];
14999# ifdef WIN32
15000 HWND w;
15001# else
15002 Window w;
15003# endif
15004
15005 if (check_restricted() || check_secure())
15006 return;
15007
15008# ifdef FEAT_X11
15009 if (check_connection() == FAIL)
15010 return;
15011# endif
15012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015013 server_name = get_tv_string_chk(&argvars[0]);
15014 if (server_name == NULL)
15015 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016 keys = get_tv_string_buf(&argvars[1], buf);
15017# ifdef WIN32
15018 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15019# else
15020 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15021 < 0)
15022# endif
15023 {
15024 if (r != NULL)
15025 EMSG(r); /* sending worked but evaluation failed */
15026 else
15027 EMSG2(_("E241: Unable to send to %s"), server_name);
15028 return;
15029 }
15030
15031 rettv->vval.v_string = r;
15032
15033 if (argvars[2].v_type != VAR_UNKNOWN)
15034 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015036 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015037 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015039 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 v.di_tv.v_type = VAR_STRING;
15041 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015042 idvar = get_tv_string_chk(&argvars[2]);
15043 if (idvar != NULL)
15044 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 }
15047}
15048#endif
15049
15050/*
15051 * "remote_expr()" function
15052 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015053 static void
15054f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015055 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015056 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057{
15058 rettv->v_type = VAR_STRING;
15059 rettv->vval.v_string = NULL;
15060#ifdef FEAT_CLIENTSERVER
15061 remote_common(argvars, rettv, TRUE);
15062#endif
15063}
15064
15065/*
15066 * "remote_foreground()" function
15067 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 static void
15069f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015070 typval_T *argvars UNUSED;
15071 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073#ifdef FEAT_CLIENTSERVER
15074# ifdef WIN32
15075 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 {
15077 char_u *server_name = get_tv_string_chk(&argvars[0]);
15078
15079 if (server_name != NULL)
15080 serverForeground(server_name);
15081 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082# else
15083 /* Send a foreground() expression to the server. */
15084 argvars[1].v_type = VAR_STRING;
15085 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15086 argvars[2].v_type = VAR_UNKNOWN;
15087 remote_common(argvars, rettv, TRUE);
15088 vim_free(argvars[1].vval.v_string);
15089# endif
15090#endif
15091}
15092
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 static void
15094f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097{
15098#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015099 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 char_u *s = NULL;
15101# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015102 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015104 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105
15106 if (check_restricted() || check_secure())
15107 {
15108 rettv->vval.v_number = -1;
15109 return;
15110 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015111 serverid = get_tv_string_chk(&argvars[0]);
15112 if (serverid == NULL)
15113 {
15114 rettv->vval.v_number = -1;
15115 return; /* type error; errmsg already given */
15116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015118 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 if (n == 0)
15120 rettv->vval.v_number = -1;
15121 else
15122 {
15123 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15124 rettv->vval.v_number = (s != NULL);
15125 }
15126# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 if (check_connection() == FAIL)
15128 return;
15129
15130 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015131 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132# endif
15133
15134 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015136 char_u *retvar;
15137
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 v.di_tv.v_type = VAR_STRING;
15139 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 retvar = get_tv_string_chk(&argvars[1]);
15141 if (retvar != NULL)
15142 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015143 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 }
15145#else
15146 rettv->vval.v_number = -1;
15147#endif
15148}
15149
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 static void
15151f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154{
15155 char_u *r = NULL;
15156
15157#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015158 char_u *serverid = get_tv_string_chk(&argvars[0]);
15159
15160 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161 {
15162# ifdef WIN32
15163 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015164 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015166 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 if (n != 0)
15168 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15169 if (r == NULL)
15170# else
15171 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015172 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173# endif
15174 EMSG(_("E277: Unable to read a server reply"));
15175 }
15176#endif
15177 rettv->v_type = VAR_STRING;
15178 rettv->vval.v_string = r;
15179}
15180
15181/*
15182 * "remote_send()" function
15183 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 static void
15185f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015187 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188{
15189 rettv->v_type = VAR_STRING;
15190 rettv->vval.v_string = NULL;
15191#ifdef FEAT_CLIENTSERVER
15192 remote_common(argvars, rettv, FALSE);
15193#endif
15194}
15195
15196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015197 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015198 */
15199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 typval_T *argvars;
15202 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015203{
Bram Moolenaar33570922005-01-25 22:26:29 +000015204 list_T *l;
15205 listitem_T *item, *item2;
15206 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015208 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015209 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 dict_T *d;
15211 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015212 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015213
Bram Moolenaar8c711452005-01-14 21:53:12 +000015214 if (argvars[0].v_type == VAR_DICT)
15215 {
15216 if (argvars[2].v_type != VAR_UNKNOWN)
15217 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015218 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015219 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015221 key = get_tv_string_chk(&argvars[1]);
15222 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 di = dict_find(d, key, -1);
15225 if (di == NULL)
15226 EMSG2(_(e_dictkey), key);
15227 else
15228 {
15229 *rettv = di->di_tv;
15230 init_tv(&di->di_tv);
15231 dictitem_remove(d, di);
15232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015233 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015234 }
15235 }
15236 else if (argvars[0].v_type != VAR_LIST)
15237 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015238 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015239 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015241 int error = FALSE;
15242
15243 idx = get_tv_number_chk(&argvars[1], &error);
15244 if (error)
15245 ; /* type error: do nothing, errmsg already given */
15246 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015247 EMSGN(_(e_listidx), idx);
15248 else
15249 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015250 if (argvars[2].v_type == VAR_UNKNOWN)
15251 {
15252 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015253 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015254 *rettv = item->li_tv;
15255 vim_free(item);
15256 }
15257 else
15258 {
15259 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 end = get_tv_number_chk(&argvars[2], &error);
15261 if (error)
15262 ; /* type error: do nothing */
15263 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015264 EMSGN(_(e_listidx), end);
15265 else
15266 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015267 int cnt = 0;
15268
15269 for (li = item; li != NULL; li = li->li_next)
15270 {
15271 ++cnt;
15272 if (li == item2)
15273 break;
15274 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 if (li == NULL) /* didn't find "item2" after "item" */
15276 EMSG(_(e_invrange));
15277 else
15278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015279 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015280 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015281 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015283 l->lv_first = item;
15284 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015285 item->li_prev = NULL;
15286 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015287 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015288 }
15289 }
15290 }
15291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015292 }
15293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294}
15295
15296/*
15297 * "rename({from}, {to})" function
15298 */
15299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015301 typval_T *argvars;
15302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303{
15304 char_u buf[NUMBUFLEN];
15305
15306 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015307 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15310 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311}
15312
15313/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314 * "repeat()" function
15315 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015316 static void
15317f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 typval_T *argvars;
15319 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320{
15321 char_u *p;
15322 int n;
15323 int slen;
15324 int len;
15325 char_u *r;
15326 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015327
15328 n = get_tv_number(&argvars[1]);
15329 if (argvars[0].v_type == VAR_LIST)
15330 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015331 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015332 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015333 if (list_extend(rettv->vval.v_list,
15334 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015335 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015336 }
15337 else
15338 {
15339 p = get_tv_string(&argvars[0]);
15340 rettv->v_type = VAR_STRING;
15341 rettv->vval.v_string = NULL;
15342
15343 slen = (int)STRLEN(p);
15344 len = slen * n;
15345 if (len <= 0)
15346 return;
15347
15348 r = alloc(len + 1);
15349 if (r != NULL)
15350 {
15351 for (i = 0; i < n; i++)
15352 mch_memmove(r + i * slen, p, (size_t)slen);
15353 r[len] = NUL;
15354 }
15355
15356 rettv->vval.v_string = r;
15357 }
15358}
15359
15360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 * "resolve()" function
15362 */
15363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 typval_T *argvars;
15366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367{
15368 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015369#ifdef HAVE_READLINK
15370 char_u *buf = NULL;
15371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015373 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374#ifdef FEAT_SHORTCUT
15375 {
15376 char_u *v = NULL;
15377
15378 v = mch_resolve_shortcut(p);
15379 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015380 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 }
15384#else
15385# ifdef HAVE_READLINK
15386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 char_u *cpy;
15388 int len;
15389 char_u *remain = NULL;
15390 char_u *q;
15391 int is_relative_to_current = FALSE;
15392 int has_trailing_pathsep = FALSE;
15393 int limit = 100;
15394
15395 p = vim_strsave(p);
15396
15397 if (p[0] == '.' && (vim_ispathsep(p[1])
15398 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15399 is_relative_to_current = TRUE;
15400
15401 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015402 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015405 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407
15408 q = getnextcomp(p);
15409 if (*q != NUL)
15410 {
15411 /* Separate the first path component in "p", and keep the
15412 * remainder (beginning with the path separator). */
15413 remain = vim_strsave(q - 1);
15414 q[-1] = NUL;
15415 }
15416
Bram Moolenaard9462e32011-04-11 21:35:11 +020015417 buf = alloc(MAXPATHL + 1);
15418 if (buf == NULL)
15419 goto fail;
15420
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 for (;;)
15422 {
15423 for (;;)
15424 {
15425 len = readlink((char *)p, (char *)buf, MAXPATHL);
15426 if (len <= 0)
15427 break;
15428 buf[len] = NUL;
15429
15430 if (limit-- == 0)
15431 {
15432 vim_free(p);
15433 vim_free(remain);
15434 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015435 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 goto fail;
15437 }
15438
15439 /* Ensure that the result will have a trailing path separator
15440 * if the argument has one. */
15441 if (remain == NULL && has_trailing_pathsep)
15442 add_pathsep(buf);
15443
15444 /* Separate the first path component in the link value and
15445 * concatenate the remainders. */
15446 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15447 if (*q != NUL)
15448 {
15449 if (remain == NULL)
15450 remain = vim_strsave(q - 1);
15451 else
15452 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015453 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 if (cpy != NULL)
15455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 vim_free(remain);
15457 remain = cpy;
15458 }
15459 }
15460 q[-1] = NUL;
15461 }
15462
15463 q = gettail(p);
15464 if (q > p && *q == NUL)
15465 {
15466 /* Ignore trailing path separator. */
15467 q[-1] = NUL;
15468 q = gettail(p);
15469 }
15470 if (q > p && !mch_isFullName(buf))
15471 {
15472 /* symlink is relative to directory of argument */
15473 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15474 if (cpy != NULL)
15475 {
15476 STRCPY(cpy, p);
15477 STRCPY(gettail(cpy), buf);
15478 vim_free(p);
15479 p = cpy;
15480 }
15481 }
15482 else
15483 {
15484 vim_free(p);
15485 p = vim_strsave(buf);
15486 }
15487 }
15488
15489 if (remain == NULL)
15490 break;
15491
15492 /* Append the first path component of "remain" to "p". */
15493 q = getnextcomp(remain + 1);
15494 len = q - remain - (*q != NUL);
15495 cpy = vim_strnsave(p, STRLEN(p) + len);
15496 if (cpy != NULL)
15497 {
15498 STRNCAT(cpy, remain, len);
15499 vim_free(p);
15500 p = cpy;
15501 }
15502 /* Shorten "remain". */
15503 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015504 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 else
15506 {
15507 vim_free(remain);
15508 remain = NULL;
15509 }
15510 }
15511
15512 /* If the result is a relative path name, make it explicitly relative to
15513 * the current directory if and only if the argument had this form. */
15514 if (!vim_ispathsep(*p))
15515 {
15516 if (is_relative_to_current
15517 && *p != NUL
15518 && !(p[0] == '.'
15519 && (p[1] == NUL
15520 || vim_ispathsep(p[1])
15521 || (p[1] == '.'
15522 && (p[2] == NUL
15523 || vim_ispathsep(p[2]))))))
15524 {
15525 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015526 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 if (cpy != NULL)
15528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 vim_free(p);
15530 p = cpy;
15531 }
15532 }
15533 else if (!is_relative_to_current)
15534 {
15535 /* Strip leading "./". */
15536 q = p;
15537 while (q[0] == '.' && vim_ispathsep(q[1]))
15538 q += 2;
15539 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015540 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 }
15542 }
15543
15544 /* Ensure that the result will have no trailing path separator
15545 * if the argument had none. But keep "/" or "//". */
15546 if (!has_trailing_pathsep)
15547 {
15548 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015549 if (after_pathsep(p, q))
15550 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 }
15552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 }
15555# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557# endif
15558#endif
15559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
15562#ifdef HAVE_READLINK
15563fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015564 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567}
15568
15569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015570 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 */
15572 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015573f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015574 typval_T *argvars;
15575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576{
Bram Moolenaar33570922005-01-25 22:26:29 +000015577 list_T *l;
15578 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580 if (argvars[0].v_type != VAR_LIST)
15581 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015582 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015583 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015584 {
15585 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015586 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015587 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 while (li != NULL)
15589 {
15590 ni = li->li_prev;
15591 list_append(l, li);
15592 li = ni;
15593 }
15594 rettv->vval.v_list = l;
15595 rettv->v_type = VAR_LIST;
15596 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015597 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599}
15600
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015601#define SP_NOMOVE 0x01 /* don't move cursor */
15602#define SP_REPEAT 0x02 /* repeat to find outer pair */
15603#define SP_RETCOUNT 0x04 /* return matchcount */
15604#define SP_SETPCMARK 0x08 /* set previous context mark */
15605#define SP_START 0x10 /* accept match at start position */
15606#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15607#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015608
Bram Moolenaar33570922005-01-25 22:26:29 +000015609static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610
15611/*
15612 * Get flags for a search function.
15613 * Possibly sets "p_ws".
15614 * Returns BACKWARD, FORWARD or zero (for an error).
15615 */
15616 static int
15617get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619 int *flagsp;
15620{
15621 int dir = FORWARD;
15622 char_u *flags;
15623 char_u nbuf[NUMBUFLEN];
15624 int mask;
15625
15626 if (varp->v_type != VAR_UNKNOWN)
15627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015628 flags = get_tv_string_buf_chk(varp, nbuf);
15629 if (flags == NULL)
15630 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015631 while (*flags != NUL)
15632 {
15633 switch (*flags)
15634 {
15635 case 'b': dir = BACKWARD; break;
15636 case 'w': p_ws = TRUE; break;
15637 case 'W': p_ws = FALSE; break;
15638 default: mask = 0;
15639 if (flagsp != NULL)
15640 switch (*flags)
15641 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015642 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015643 case 'e': mask = SP_END; break;
15644 case 'm': mask = SP_RETCOUNT; break;
15645 case 'n': mask = SP_NOMOVE; break;
15646 case 'p': mask = SP_SUBPAT; break;
15647 case 'r': mask = SP_REPEAT; break;
15648 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649 }
15650 if (mask == 0)
15651 {
15652 EMSG2(_(e_invarg2), flags);
15653 dir = 0;
15654 }
15655 else
15656 *flagsp |= mask;
15657 }
15658 if (dir == 0)
15659 break;
15660 ++flags;
15661 }
15662 }
15663 return dir;
15664}
15665
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015667 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015669 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015670search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015672 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015673 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015675 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 char_u *pat;
15677 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015678 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 int save_p_ws = p_ws;
15680 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015681 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015682 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015683 proftime_T tm;
15684#ifdef FEAT_RELTIME
15685 long time_limit = 0;
15686#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015687 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015688 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015690 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015691 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015692 if (dir == 0)
15693 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015694 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015695 if (flags & SP_START)
15696 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015697 if (flags & SP_END)
15698 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015699
Bram Moolenaar76929292008-01-06 19:07:36 +000015700 /* Optional arguments: line number to stop searching and timeout. */
15701 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015702 {
15703 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15704 if (lnum_stop < 0)
15705 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015706#ifdef FEAT_RELTIME
15707 if (argvars[3].v_type != VAR_UNKNOWN)
15708 {
15709 time_limit = get_tv_number_chk(&argvars[3], NULL);
15710 if (time_limit < 0)
15711 goto theend;
15712 }
15713#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015714 }
15715
Bram Moolenaar76929292008-01-06 19:07:36 +000015716#ifdef FEAT_RELTIME
15717 /* Set the time limit, if there is one. */
15718 profile_setlimit(time_limit, &tm);
15719#endif
15720
Bram Moolenaar231334e2005-07-25 20:46:57 +000015721 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015722 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015723 * Check to make sure only those flags are set.
15724 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15725 * flags cannot be set. Check for that condition also.
15726 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015727 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015728 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015731 goto theend;
15732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015734 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015735 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015736 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015737 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015739 if (flags & SP_SUBPAT)
15740 retval = subpatnum;
15741 else
15742 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015743 if (flags & SP_SETPCMARK)
15744 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015746 if (match_pos != NULL)
15747 {
15748 /* Store the match cursor position */
15749 match_pos->lnum = pos.lnum;
15750 match_pos->col = pos.col + 1;
15751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 /* "/$" will put the cursor after the end of the line, may need to
15753 * correct that here */
15754 check_cursor();
15755 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015756
15757 /* If 'n' flag is used: restore cursor position. */
15758 if (flags & SP_NOMOVE)
15759 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015760 else
15761 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015762theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015764
15765 return retval;
15766}
15767
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015768#ifdef FEAT_FLOAT
15769/*
15770 * "round({float})" function
15771 */
15772 static void
15773f_round(argvars, rettv)
15774 typval_T *argvars;
15775 typval_T *rettv;
15776{
15777 float_T f;
15778
15779 rettv->v_type = VAR_FLOAT;
15780 if (get_float_arg(argvars, &f) == OK)
15781 /* round() is not in C90, use ceil() or floor() instead. */
15782 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15783 else
15784 rettv->vval.v_float = 0.0;
15785}
15786#endif
15787
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015788/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015789 * "screencol()" function
15790 *
15791 * First column is 1 to be consistent with virtcol().
15792 */
15793 static void
15794f_screencol(argvars, rettv)
15795 typval_T *argvars UNUSED;
15796 typval_T *rettv;
15797{
15798 rettv->vval.v_number = screen_screencol() + 1;
15799}
15800
15801/*
15802 * "screenrow()" function
15803 */
15804 static void
15805f_screenrow(argvars, rettv)
15806 typval_T *argvars UNUSED;
15807 typval_T *rettv;
15808{
15809 rettv->vval.v_number = screen_screenrow() + 1;
15810}
15811
15812/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015813 * "search()" function
15814 */
15815 static void
15816f_search(argvars, rettv)
15817 typval_T *argvars;
15818 typval_T *rettv;
15819{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015820 int flags = 0;
15821
15822 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823}
15824
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015826 * "searchdecl()" function
15827 */
15828 static void
15829f_searchdecl(argvars, rettv)
15830 typval_T *argvars;
15831 typval_T *rettv;
15832{
15833 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015834 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015835 int error = FALSE;
15836 char_u *name;
15837
15838 rettv->vval.v_number = 1; /* default: FAIL */
15839
15840 name = get_tv_string_chk(&argvars[0]);
15841 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015842 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015843 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015844 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15845 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15846 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015847 if (!error && name != NULL)
15848 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015849 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015850}
15851
15852/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015853 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015855 static int
15856searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015858 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859{
15860 char_u *spat, *mpat, *epat;
15861 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 int dir;
15864 int flags = 0;
15865 char_u nbuf1[NUMBUFLEN];
15866 char_u nbuf2[NUMBUFLEN];
15867 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015868 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015869 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015870 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015873 spat = get_tv_string_chk(&argvars[0]);
15874 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15875 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15876 if (spat == NULL || mpat == NULL || epat == NULL)
15877 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 /* Handle the optional fourth argument: flags */
15880 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015881 if (dir == 0)
15882 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015883
15884 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015885 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15886 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015887 if ((flags & (SP_END | SP_SUBPAT)) != 0
15888 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015889 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015890 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015891 goto theend;
15892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015894 /* Using 'r' implies 'W', otherwise it doesn't work. */
15895 if (flags & SP_REPEAT)
15896 p_ws = FALSE;
15897
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015898 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015899 if (argvars[3].v_type == VAR_UNKNOWN
15900 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 skip = (char_u *)"";
15902 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015904 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015905 if (argvars[5].v_type != VAR_UNKNOWN)
15906 {
15907 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15908 if (lnum_stop < 0)
15909 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015910#ifdef FEAT_RELTIME
15911 if (argvars[6].v_type != VAR_UNKNOWN)
15912 {
15913 time_limit = get_tv_number_chk(&argvars[6], NULL);
15914 if (time_limit < 0)
15915 goto theend;
15916 }
15917#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015918 }
15919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015920 if (skip == NULL)
15921 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015923 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015924 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015925
15926theend:
15927 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015928
15929 return retval;
15930}
15931
15932/*
15933 * "searchpair()" function
15934 */
15935 static void
15936f_searchpair(argvars, rettv)
15937 typval_T *argvars;
15938 typval_T *rettv;
15939{
15940 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15941}
15942
15943/*
15944 * "searchpairpos()" function
15945 */
15946 static void
15947f_searchpairpos(argvars, rettv)
15948 typval_T *argvars;
15949 typval_T *rettv;
15950{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015951 pos_T match_pos;
15952 int lnum = 0;
15953 int col = 0;
15954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015955 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015956 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015957
15958 if (searchpair_cmn(argvars, &match_pos) > 0)
15959 {
15960 lnum = match_pos.lnum;
15961 col = match_pos.col;
15962 }
15963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015964 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15965 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015966}
15967
15968/*
15969 * Search for a start/middle/end thing.
15970 * Used by searchpair(), see its documentation for the details.
15971 * Returns 0 or -1 for no match,
15972 */
15973 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015974do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15975 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015976 char_u *spat; /* start pattern */
15977 char_u *mpat; /* middle pattern */
15978 char_u *epat; /* end pattern */
15979 int dir; /* BACKWARD or FORWARD */
15980 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015981 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015982 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015983 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015984 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015985{
15986 char_u *save_cpo;
15987 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15988 long retval = 0;
15989 pos_T pos;
15990 pos_T firstpos;
15991 pos_T foundpos;
15992 pos_T save_cursor;
15993 pos_T save_pos;
15994 int n;
15995 int r;
15996 int nest = 1;
15997 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015998 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015999 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016000
16001 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16002 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016003 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016004
Bram Moolenaar76929292008-01-06 19:07:36 +000016005#ifdef FEAT_RELTIME
16006 /* Set the time limit, if there is one. */
16007 profile_setlimit(time_limit, &tm);
16008#endif
16009
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016010 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16011 * start/middle/end (pat3, for the top pair). */
16012 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16013 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16014 if (pat2 == NULL || pat3 == NULL)
16015 goto theend;
16016 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16017 if (*mpat == NUL)
16018 STRCPY(pat3, pat2);
16019 else
16020 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16021 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016022 if (flags & SP_START)
16023 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016024
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 save_cursor = curwin->w_cursor;
16026 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016027 clearpos(&firstpos);
16028 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 pat = pat3;
16030 for (;;)
16031 {
16032 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016033 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16035 /* didn't find it or found the first match again: FAIL */
16036 break;
16037
16038 if (firstpos.lnum == 0)
16039 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016040 if (equalpos(pos, foundpos))
16041 {
16042 /* Found the same position again. Can happen with a pattern that
16043 * has "\zs" at the end and searching backwards. Advance one
16044 * character and try again. */
16045 if (dir == BACKWARD)
16046 decl(&pos);
16047 else
16048 incl(&pos);
16049 }
16050 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016052 /* clear the start flag to avoid getting stuck here */
16053 options &= ~SEARCH_START;
16054
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 /* If the skip pattern matches, ignore this match. */
16056 if (*skip != NUL)
16057 {
16058 save_pos = curwin->w_cursor;
16059 curwin->w_cursor = pos;
16060 r = eval_to_bool(skip, &err, NULL, FALSE);
16061 curwin->w_cursor = save_pos;
16062 if (err)
16063 {
16064 /* Evaluating {skip} caused an error, break here. */
16065 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016066 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 break;
16068 }
16069 if (r)
16070 continue;
16071 }
16072
16073 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16074 {
16075 /* Found end when searching backwards or start when searching
16076 * forward: nested pair. */
16077 ++nest;
16078 pat = pat2; /* nested, don't search for middle */
16079 }
16080 else
16081 {
16082 /* Found end when searching forward or start when searching
16083 * backward: end of (nested) pair; or found middle in outer pair. */
16084 if (--nest == 1)
16085 pat = pat3; /* outer level, search for middle */
16086 }
16087
16088 if (nest == 0)
16089 {
16090 /* Found the match: return matchcount or line number. */
16091 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016092 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016094 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016095 if (flags & SP_SETPCMARK)
16096 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 curwin->w_cursor = pos;
16098 if (!(flags & SP_REPEAT))
16099 break;
16100 nest = 1; /* search for next unmatched */
16101 }
16102 }
16103
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016104 if (match_pos != NULL)
16105 {
16106 /* Store the match cursor position */
16107 match_pos->lnum = curwin->w_cursor.lnum;
16108 match_pos->col = curwin->w_cursor.col + 1;
16109 }
16110
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016112 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 curwin->w_cursor = save_cursor;
16114
16115theend:
16116 vim_free(pat2);
16117 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016118 if (p_cpo == empty_option)
16119 p_cpo = save_cpo;
16120 else
16121 /* Darn, evaluating the {skip} expression changed the value. */
16122 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016123
16124 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127/*
16128 * "searchpos()" function
16129 */
16130 static void
16131f_searchpos(argvars, rettv)
16132 typval_T *argvars;
16133 typval_T *rettv;
16134{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135 pos_T match_pos;
16136 int lnum = 0;
16137 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016138 int n;
16139 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016142 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016143
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016144 n = search_cmn(argvars, &match_pos, &flags);
16145 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016146 {
16147 lnum = match_pos.lnum;
16148 col = match_pos.col;
16149 }
16150
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016151 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16152 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016153 if (flags & SP_SUBPAT)
16154 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016155}
16156
16157
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158 static void
16159f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163#ifdef FEAT_CLIENTSERVER
16164 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016165 char_u *server = get_tv_string_chk(&argvars[0]);
16166 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016169 if (server == NULL || reply == NULL)
16170 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171 if (check_restricted() || check_secure())
16172 return;
16173# ifdef FEAT_X11
16174 if (check_connection() == FAIL)
16175 return;
16176# endif
16177
16178 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180 EMSG(_("E258: Unable to send to client"));
16181 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016183 rettv->vval.v_number = 0;
16184#else
16185 rettv->vval.v_number = -1;
16186#endif
16187}
16188
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 static void
16190f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193{
16194 char_u *r = NULL;
16195
16196#ifdef FEAT_CLIENTSERVER
16197# ifdef WIN32
16198 r = serverGetVimNames();
16199# else
16200 make_connection();
16201 if (X_DISPLAY != NULL)
16202 r = serverGetVimNames(X_DISPLAY);
16203# endif
16204#endif
16205 rettv->v_type = VAR_STRING;
16206 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207}
16208
16209/*
16210 * "setbufvar()" function
16211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016215 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216{
16217 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 char_u nbuf[NUMBUFLEN];
16222
16223 if (check_restricted() || check_secure())
16224 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016225 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16226 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016227 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 varp = &argvars[2];
16229
16230 if (buf != NULL && varname != NULL && varp != NULL)
16231 {
16232 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234
16235 if (*varname == '&')
16236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016237 long numval;
16238 char_u *strval;
16239 int error = FALSE;
16240
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016242 numval = get_tv_number_chk(varp, &error);
16243 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016244 if (!error && strval != NULL)
16245 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 }
16247 else
16248 {
16249 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16250 if (bufvarname != NULL)
16251 {
16252 STRCPY(bufvarname, "b:");
16253 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016254 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 vim_free(bufvarname);
16256 }
16257 }
16258
16259 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262}
16263
16264/*
16265 * "setcmdpos()" function
16266 */
16267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T *argvars;
16270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016272 int pos = (int)get_tv_number(&argvars[0]) - 1;
16273
16274 if (pos >= 0)
16275 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276}
16277
16278/*
16279 * "setline()" function
16280 */
16281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016282f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 typval_T *argvars;
16284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285{
16286 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016287 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016288 list_T *l = NULL;
16289 listitem_T *li = NULL;
16290 long added = 0;
16291 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016293 lnum = get_tv_lnum(&argvars[0]);
16294 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016296 l = argvars[1].vval.v_list;
16297 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016299 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016300 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016301
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016302 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303 for (;;)
16304 {
16305 if (l != NULL)
16306 {
16307 /* list argument, get next string */
16308 if (li == NULL)
16309 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016310 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016311 li = li->li_next;
16312 }
16313
16314 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016315 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016316 break;
16317 if (lnum <= curbuf->b_ml.ml_line_count)
16318 {
16319 /* existing line, replace it */
16320 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16321 {
16322 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016323 if (lnum == curwin->w_cursor.lnum)
16324 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016325 rettv->vval.v_number = 0; /* OK */
16326 }
16327 }
16328 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16329 {
16330 /* lnum is one past the last line, append the line */
16331 ++added;
16332 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16333 rettv->vval.v_number = 0; /* OK */
16334 }
16335
16336 if (l == NULL) /* only one string argument */
16337 break;
16338 ++lnum;
16339 }
16340
16341 if (added > 0)
16342 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343}
16344
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016345static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16346
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016348 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016349 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016350 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016351set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016352 win_T *wp UNUSED;
16353 typval_T *list_arg UNUSED;
16354 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016355 typval_T *rettv;
16356{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016357#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016358 char_u *act;
16359 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016360#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016361
Bram Moolenaar2641f772005-03-25 21:58:17 +000016362 rettv->vval.v_number = -1;
16363
16364#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016365 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016366 EMSG(_(e_listreq));
16367 else
16368 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016369 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016370
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016371 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016372 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016373 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016374 if (act == NULL)
16375 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016376 if (*act == 'a' || *act == 'r')
16377 action = *act;
16378 }
16379
Bram Moolenaar81484f42012-12-05 15:16:47 +010016380 if (l != NULL && set_errorlist(wp, l, action,
16381 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016382 rettv->vval.v_number = 0;
16383 }
16384#endif
16385}
16386
16387/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016388 * "setloclist()" function
16389 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016390 static void
16391f_setloclist(argvars, rettv)
16392 typval_T *argvars;
16393 typval_T *rettv;
16394{
16395 win_T *win;
16396
16397 rettv->vval.v_number = -1;
16398
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016399 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016400 if (win != NULL)
16401 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16402}
16403
16404/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016405 * "setmatches()" function
16406 */
16407 static void
16408f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016409 typval_T *argvars UNUSED;
16410 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016411{
16412#ifdef FEAT_SEARCH_EXTRA
16413 list_T *l;
16414 listitem_T *li;
16415 dict_T *d;
16416
16417 rettv->vval.v_number = -1;
16418 if (argvars[0].v_type != VAR_LIST)
16419 {
16420 EMSG(_(e_listreq));
16421 return;
16422 }
16423 if ((l = argvars[0].vval.v_list) != NULL)
16424 {
16425
16426 /* To some extent make sure that we are dealing with a list from
16427 * "getmatches()". */
16428 li = l->lv_first;
16429 while (li != NULL)
16430 {
16431 if (li->li_tv.v_type != VAR_DICT
16432 || (d = li->li_tv.vval.v_dict) == NULL)
16433 {
16434 EMSG(_(e_invarg));
16435 return;
16436 }
16437 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16438 && dict_find(d, (char_u *)"pattern", -1) != NULL
16439 && dict_find(d, (char_u *)"priority", -1) != NULL
16440 && dict_find(d, (char_u *)"id", -1) != NULL))
16441 {
16442 EMSG(_(e_invarg));
16443 return;
16444 }
16445 li = li->li_next;
16446 }
16447
16448 clear_matches(curwin);
16449 li = l->lv_first;
16450 while (li != NULL)
16451 {
16452 d = li->li_tv.vval.v_dict;
16453 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16454 get_dict_string(d, (char_u *)"pattern", FALSE),
16455 (int)get_dict_number(d, (char_u *)"priority"),
16456 (int)get_dict_number(d, (char_u *)"id"));
16457 li = li->li_next;
16458 }
16459 rettv->vval.v_number = 0;
16460 }
16461#endif
16462}
16463
16464/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016465 * "setpos()" function
16466 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016467 static void
16468f_setpos(argvars, rettv)
16469 typval_T *argvars;
16470 typval_T *rettv;
16471{
16472 pos_T pos;
16473 int fnum;
16474 char_u *name;
16475
Bram Moolenaar08250432008-02-13 11:42:46 +000016476 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016477 name = get_tv_string_chk(argvars);
16478 if (name != NULL)
16479 {
16480 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16481 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016482 if (--pos.col < 0)
16483 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016484 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016485 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016486 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016487 if (fnum == curbuf->b_fnum)
16488 {
16489 curwin->w_cursor = pos;
16490 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016491 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016492 }
16493 else
16494 EMSG(_(e_invarg));
16495 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016496 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16497 {
16498 /* set mark */
16499 if (setmark_pos(name[1], &pos, fnum) == OK)
16500 rettv->vval.v_number = 0;
16501 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016502 else
16503 EMSG(_(e_invarg));
16504 }
16505 }
16506}
16507
16508/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016509 * "setqflist()" function
16510 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016511 static void
16512f_setqflist(argvars, rettv)
16513 typval_T *argvars;
16514 typval_T *rettv;
16515{
16516 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16517}
16518
16519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 * "setreg()" function
16521 */
16522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016523f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *argvars;
16525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526{
16527 int regname;
16528 char_u *strregname;
16529 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 int append;
16532 char_u yank_type;
16533 long block_len;
16534
16535 block_len = -1;
16536 yank_type = MAUTO;
16537 append = FALSE;
16538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016540 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016542 if (strregname == NULL)
16543 return; /* type error; errmsg already given */
16544 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 if (regname == 0 || regname == '@')
16546 regname = '"';
16547 else if (regname == '=')
16548 return;
16549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016550 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 stropt = get_tv_string_chk(&argvars[2]);
16553 if (stropt == NULL)
16554 return; /* type error */
16555 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 switch (*stropt)
16557 {
16558 case 'a': case 'A': /* append */
16559 append = TRUE;
16560 break;
16561 case 'v': case 'c': /* character-wise selection */
16562 yank_type = MCHAR;
16563 break;
16564 case 'V': case 'l': /* line-wise selection */
16565 yank_type = MLINE;
16566 break;
16567#ifdef FEAT_VISUAL
16568 case 'b': case Ctrl_V: /* block-wise selection */
16569 yank_type = MBLOCK;
16570 if (VIM_ISDIGIT(stropt[1]))
16571 {
16572 ++stropt;
16573 block_len = getdigits(&stropt) - 1;
16574 --stropt;
16575 }
16576 break;
16577#endif
16578 }
16579 }
16580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016581 strval = get_tv_string_chk(&argvars[1]);
16582 if (strval != NULL)
16583 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586}
16587
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016588/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016589 * "settabvar()" function
16590 */
16591 static void
16592f_settabvar(argvars, rettv)
16593 typval_T *argvars;
16594 typval_T *rettv;
16595{
16596 tabpage_T *save_curtab;
16597 char_u *varname, *tabvarname;
16598 typval_T *varp;
16599 tabpage_T *tp;
16600
16601 rettv->vval.v_number = 0;
16602
16603 if (check_restricted() || check_secure())
16604 return;
16605
16606 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16607 varname = get_tv_string_chk(&argvars[1]);
16608 varp = &argvars[2];
16609
16610 if (tp != NULL && varname != NULL && varp != NULL)
16611 {
16612 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016613 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016614
16615 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16616 if (tabvarname != NULL)
16617 {
16618 STRCPY(tabvarname, "t:");
16619 STRCPY(tabvarname + 2, varname);
16620 set_var(tabvarname, varp, TRUE);
16621 vim_free(tabvarname);
16622 }
16623
16624 /* Restore current tabpage */
16625 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016626 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016627 }
16628}
16629
16630/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016631 * "settabwinvar()" function
16632 */
16633 static void
16634f_settabwinvar(argvars, rettv)
16635 typval_T *argvars;
16636 typval_T *rettv;
16637{
16638 setwinvar(argvars, rettv, 1);
16639}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640
16641/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016642 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016646 typval_T *argvars;
16647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016649 setwinvar(argvars, rettv, 0);
16650}
16651
16652/*
16653 * "setwinvar()" and "settabwinvar()" functions
16654 */
16655 static void
16656setwinvar(argvars, rettv, off)
16657 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016658 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016659 int off;
16660{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 win_T *win;
16662#ifdef FEAT_WINDOWS
16663 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016664 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665#endif
16666 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016669 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
16671 if (check_restricted() || check_secure())
16672 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016673
16674#ifdef FEAT_WINDOWS
16675 if (off == 1)
16676 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16677 else
16678 tp = curtab;
16679#endif
16680 win = find_win_by_nr(&argvars[off], tp);
16681 varname = get_tv_string_chk(&argvars[off + 1]);
16682 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683
16684 if (win != NULL && varname != NULL && varp != NULL)
16685 {
16686#ifdef FEAT_WINDOWS
16687 /* set curwin to be our win, temporarily */
16688 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016689 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016690 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016691 if (!win_valid(win))
16692 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 curwin = win;
16694 curbuf = curwin->w_buffer;
16695#endif
16696
16697 if (*varname == '&')
16698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 long numval;
16700 char_u *strval;
16701 int error = FALSE;
16702
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016704 numval = get_tv_number_chk(varp, &error);
16705 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 if (!error && strval != NULL)
16707 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 }
16709 else
16710 {
16711 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16712 if (winvarname != NULL)
16713 {
16714 STRCPY(winvarname, "w:");
16715 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016716 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 vim_free(winvarname);
16718 }
16719 }
16720
16721#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016722 /* Restore current tabpage and window, if still valid (autocomands can
16723 * make them invalid). */
16724 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016725 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 if (win_valid(save_curwin))
16727 {
16728 curwin = save_curwin;
16729 curbuf = curwin->w_buffer;
16730 }
16731#endif
16732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733}
16734
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016735#ifdef FEAT_CRYPT
16736/*
16737 * "sha256({string})" function
16738 */
16739 static void
16740f_sha256(argvars, rettv)
16741 typval_T *argvars;
16742 typval_T *rettv;
16743{
16744 char_u *p;
16745
16746 p = get_tv_string(&argvars[0]);
16747 rettv->vval.v_string = vim_strsave(
16748 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16749 rettv->v_type = VAR_STRING;
16750}
16751#endif /* FEAT_CRYPT */
16752
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016754 * "shellescape({string})" function
16755 */
16756 static void
16757f_shellescape(argvars, rettv)
16758 typval_T *argvars;
16759 typval_T *rettv;
16760{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016761 rettv->vval.v_string = vim_strsave_shellescape(
16762 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016763 rettv->v_type = VAR_STRING;
16764}
16765
16766/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016767 * shiftwidth() function
16768 */
16769 static void
16770f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016771 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016772 typval_T *rettv;
16773{
16774 rettv->vval.v_number = get_sw_value();
16775}
16776
16777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016778 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 */
16780 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016782 typval_T *argvars;
16783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016785 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786
Bram Moolenaar0d660222005-01-07 21:51:51 +000016787 p = get_tv_string(&argvars[0]);
16788 rettv->vval.v_string = vim_strsave(p);
16789 simplify_filename(rettv->vval.v_string); /* simplify in place */
16790 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791}
16792
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016793#ifdef FEAT_FLOAT
16794/*
16795 * "sin()" function
16796 */
16797 static void
16798f_sin(argvars, rettv)
16799 typval_T *argvars;
16800 typval_T *rettv;
16801{
16802 float_T f;
16803
16804 rettv->v_type = VAR_FLOAT;
16805 if (get_float_arg(argvars, &f) == OK)
16806 rettv->vval.v_float = sin(f);
16807 else
16808 rettv->vval.v_float = 0.0;
16809}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016810
16811/*
16812 * "sinh()" function
16813 */
16814 static void
16815f_sinh(argvars, rettv)
16816 typval_T *argvars;
16817 typval_T *rettv;
16818{
16819 float_T f;
16820
16821 rettv->v_type = VAR_FLOAT;
16822 if (get_float_arg(argvars, &f) == OK)
16823 rettv->vval.v_float = sinh(f);
16824 else
16825 rettv->vval.v_float = 0.0;
16826}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016827#endif
16828
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829static int
16830#ifdef __BORLANDC__
16831 _RTLENTRYF
16832#endif
16833 item_compare __ARGS((const void *s1, const void *s2));
16834static int
16835#ifdef __BORLANDC__
16836 _RTLENTRYF
16837#endif
16838 item_compare2 __ARGS((const void *s1, const void *s2));
16839
16840static int item_compare_ic;
16841static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016842static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016843static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844#define ITEM_COMPARE_FAIL 999
16845
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849 static int
16850#ifdef __BORLANDC__
16851_RTLENTRYF
16852#endif
16853item_compare(s1, s2)
16854 const void *s1;
16855 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857 char_u *p1, *p2;
16858 char_u *tofree1, *tofree2;
16859 int res;
16860 char_u numbuf1[NUMBUFLEN];
16861 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016863 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16864 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016865 if (p1 == NULL)
16866 p1 = (char_u *)"";
16867 if (p2 == NULL)
16868 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016869 if (item_compare_ic)
16870 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872 res = STRCMP(p1, p2);
16873 vim_free(tofree1);
16874 vim_free(tofree2);
16875 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876}
16877
16878 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879#ifdef __BORLANDC__
16880_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882item_compare2(s1, s2)
16883 const void *s1;
16884 const void *s2;
16885{
16886 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016887 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016888 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016891 /* shortcut after failure in previous call; compare all items equal */
16892 if (item_compare_func_err)
16893 return 0;
16894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016895 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16896 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16898 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899
16900 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016901 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016902 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16903 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016904 clear_tv(&argv[0]);
16905 clear_tv(&argv[1]);
16906
16907 if (res == FAIL)
16908 res = ITEM_COMPARE_FAIL;
16909 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016910 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16911 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016912 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913 clear_tv(&rettv);
16914 return res;
16915}
16916
16917/*
16918 * "sort({list})" function
16919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016921f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016922 typval_T *argvars;
16923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924{
Bram Moolenaar33570922005-01-25 22:26:29 +000016925 list_T *l;
16926 listitem_T *li;
16927 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 long len;
16929 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 if (argvars[0].v_type != VAR_LIST)
16932 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 else
16934 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016936 if (l == NULL || tv_check_lock(l->lv_lock,
16937 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938 return;
16939 rettv->vval.v_list = l;
16940 rettv->v_type = VAR_LIST;
16941 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 len = list_len(l);
16944 if (len <= 1)
16945 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947 item_compare_ic = FALSE;
16948 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016949 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 if (argvars[1].v_type != VAR_UNKNOWN)
16951 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016952 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016953 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 else
16956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 int error = FALSE;
16958
16959 i = get_tv_number_chk(&argvars[1], &error);
16960 if (error)
16961 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 if (i == 1)
16963 item_compare_ic = TRUE;
16964 else
16965 item_compare_func = get_tv_string(&argvars[1]);
16966 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016967
16968 if (argvars[2].v_type != VAR_UNKNOWN)
16969 {
16970 /* optional third argument: {dict} */
16971 if (argvars[2].v_type != VAR_DICT)
16972 {
16973 EMSG(_(e_dictreq));
16974 return;
16975 }
16976 item_compare_selfdict = argvars[2].vval.v_dict;
16977 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 if (ptrs == NULL)
16983 return;
16984 i = 0;
16985 for (li = l->lv_first; li != NULL; li = li->li_next)
16986 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 /* test the compare function */
16990 if (item_compare_func != NULL
16991 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16992 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016993 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 {
16996 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 item_compare_func == NULL ? item_compare : item_compare2);
16999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 if (!item_compare_func_err)
17001 {
17002 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017003 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 l->lv_len = 0;
17005 for (i = 0; i < len; ++i)
17006 list_append(l, ptrs[i]);
17007 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017008 }
17009
17010 vim_free(ptrs);
17011 }
17012}
17013
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017014/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017015 * "soundfold({word})" function
17016 */
17017 static void
17018f_soundfold(argvars, rettv)
17019 typval_T *argvars;
17020 typval_T *rettv;
17021{
17022 char_u *s;
17023
17024 rettv->v_type = VAR_STRING;
17025 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017026#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017027 rettv->vval.v_string = eval_soundfold(s);
17028#else
17029 rettv->vval.v_string = vim_strsave(s);
17030#endif
17031}
17032
17033/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017034 * "spellbadword()" function
17035 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017036 static void
17037f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017038 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017039 typval_T *rettv;
17040{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017041 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017042 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017043 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017044
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017045 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017046 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017047
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017048#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017049 if (argvars[0].v_type == VAR_UNKNOWN)
17050 {
17051 /* Find the start and length of the badly spelled word. */
17052 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17053 if (len != 0)
17054 word = ml_get_cursor();
17055 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017056 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017057 {
17058 char_u *str = get_tv_string_chk(&argvars[0]);
17059 int capcol = -1;
17060
17061 if (str != NULL)
17062 {
17063 /* Check the argument for spelling. */
17064 while (*str != NUL)
17065 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017066 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017067 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017068 {
17069 word = str;
17070 break;
17071 }
17072 str += len;
17073 }
17074 }
17075 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017076#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017078 list_append_string(rettv->vval.v_list, word, len);
17079 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017080 attr == HLF_SPB ? "bad" :
17081 attr == HLF_SPR ? "rare" :
17082 attr == HLF_SPL ? "local" :
17083 attr == HLF_SPC ? "caps" :
17084 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017085}
17086
17087/*
17088 * "spellsuggest()" function
17089 */
17090 static void
17091f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017092 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017093 typval_T *rettv;
17094{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017095#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017096 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017097 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017098 int maxcount;
17099 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017100 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017101 listitem_T *li;
17102 int need_capital = FALSE;
17103#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017104
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017105 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017106 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017107
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017108#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017109 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017110 {
17111 str = get_tv_string(&argvars[0]);
17112 if (argvars[1].v_type != VAR_UNKNOWN)
17113 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017114 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017115 if (maxcount <= 0)
17116 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017117 if (argvars[2].v_type != VAR_UNKNOWN)
17118 {
17119 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17120 if (typeerr)
17121 return;
17122 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017123 }
17124 else
17125 maxcount = 25;
17126
Bram Moolenaar4770d092006-01-12 23:22:24 +000017127 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017128
17129 for (i = 0; i < ga.ga_len; ++i)
17130 {
17131 str = ((char_u **)ga.ga_data)[i];
17132
17133 li = listitem_alloc();
17134 if (li == NULL)
17135 vim_free(str);
17136 else
17137 {
17138 li->li_tv.v_type = VAR_STRING;
17139 li->li_tv.v_lock = 0;
17140 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017141 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017142 }
17143 }
17144 ga_clear(&ga);
17145 }
17146#endif
17147}
17148
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017150f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 typval_T *argvars;
17152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153{
17154 char_u *str;
17155 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017156 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 regmatch_T regmatch;
17158 char_u patbuf[NUMBUFLEN];
17159 char_u *save_cpo;
17160 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017162 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017164
17165 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17166 save_cpo = p_cpo;
17167 p_cpo = (char_u *)"";
17168
17169 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017170 if (argvars[1].v_type != VAR_UNKNOWN)
17171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17173 if (pat == NULL)
17174 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017175 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017176 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017177 }
17178 if (pat == NULL || *pat == NUL)
17179 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017181 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017183 if (typeerr)
17184 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17187 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017190 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017192 if (*str == NUL)
17193 match = FALSE; /* empty item at the end */
17194 else
17195 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196 if (match)
17197 end = regmatch.startp[0];
17198 else
17199 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017200 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17201 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017202 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017203 if (list_append_string(rettv->vval.v_list, str,
17204 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 }
17207 if (!match)
17208 break;
17209 /* Advance to just after the match. */
17210 if (regmatch.endp[0] > str)
17211 col = 0;
17212 else
17213 {
17214 /* Don't get stuck at the same match. */
17215#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017216 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217#else
17218 col = 1;
17219#endif
17220 }
17221 str = regmatch.endp[0];
17222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228}
17229
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017230#ifdef FEAT_FLOAT
17231/*
17232 * "sqrt()" function
17233 */
17234 static void
17235f_sqrt(argvars, rettv)
17236 typval_T *argvars;
17237 typval_T *rettv;
17238{
17239 float_T f;
17240
17241 rettv->v_type = VAR_FLOAT;
17242 if (get_float_arg(argvars, &f) == OK)
17243 rettv->vval.v_float = sqrt(f);
17244 else
17245 rettv->vval.v_float = 0.0;
17246}
17247
17248/*
17249 * "str2float()" function
17250 */
17251 static void
17252f_str2float(argvars, rettv)
17253 typval_T *argvars;
17254 typval_T *rettv;
17255{
17256 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17257
17258 if (*p == '+')
17259 p = skipwhite(p + 1);
17260 (void)string2float(p, &rettv->vval.v_float);
17261 rettv->v_type = VAR_FLOAT;
17262}
17263#endif
17264
Bram Moolenaar2c932302006-03-18 21:42:09 +000017265/*
17266 * "str2nr()" function
17267 */
17268 static void
17269f_str2nr(argvars, rettv)
17270 typval_T *argvars;
17271 typval_T *rettv;
17272{
17273 int base = 10;
17274 char_u *p;
17275 long n;
17276
17277 if (argvars[1].v_type != VAR_UNKNOWN)
17278 {
17279 base = get_tv_number(&argvars[1]);
17280 if (base != 8 && base != 10 && base != 16)
17281 {
17282 EMSG(_(e_invarg));
17283 return;
17284 }
17285 }
17286
17287 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017288 if (*p == '+')
17289 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017290 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17291 rettv->vval.v_number = n;
17292}
17293
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294#ifdef HAVE_STRFTIME
17295/*
17296 * "strftime({format}[, {time}])" function
17297 */
17298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 typval_T *argvars;
17301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302{
17303 char_u result_buf[256];
17304 struct tm *curtime;
17305 time_t seconds;
17306 char_u *p;
17307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017311 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 seconds = time(NULL);
17313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017314 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 curtime = localtime(&seconds);
17316 /* MSVC returns NULL for an invalid value of seconds. */
17317 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 else
17320 {
17321# ifdef FEAT_MBYTE
17322 vimconv_T conv;
17323 char_u *enc;
17324
17325 conv.vc_type = CONV_NONE;
17326 enc = enc_locale();
17327 convert_setup(&conv, p_enc, enc);
17328 if (conv.vc_type != CONV_NONE)
17329 p = string_convert(&conv, p, NULL);
17330# endif
17331 if (p != NULL)
17332 (void)strftime((char *)result_buf, sizeof(result_buf),
17333 (char *)p, curtime);
17334 else
17335 result_buf[0] = NUL;
17336
17337# ifdef FEAT_MBYTE
17338 if (conv.vc_type != CONV_NONE)
17339 vim_free(p);
17340 convert_setup(&conv, enc, p_enc);
17341 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 else
17344# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017345 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346
17347# ifdef FEAT_MBYTE
17348 /* Release conversion descriptors */
17349 convert_setup(&conv, NULL, NULL);
17350 vim_free(enc);
17351# endif
17352 }
17353}
17354#endif
17355
17356/*
17357 * "stridx()" function
17358 */
17359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 typval_T *argvars;
17362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363{
17364 char_u buf[NUMBUFLEN];
17365 char_u *needle;
17366 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 needle = get_tv_string_chk(&argvars[1]);
17372 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017374 if (needle == NULL || haystack == NULL)
17375 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 if (argvars[2].v_type != VAR_UNKNOWN)
17378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 int error = FALSE;
17380
17381 start_idx = get_tv_number_chk(&argvars[2], &error);
17382 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017383 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017384 if (start_idx >= 0)
17385 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017386 }
17387
17388 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17389 if (pos != NULL)
17390 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391}
17392
17393/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017394 * "string()" function
17395 */
17396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017397f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 typval_T *argvars;
17399 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017400{
17401 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017402 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017404 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017405 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017406 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017407 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409}
17410
17411/*
17412 * "strlen()" function
17413 */
17414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017416 typval_T *argvars;
17417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419 rettv->vval.v_number = (varnumber_T)(STRLEN(
17420 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421}
17422
17423/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017424 * "strchars()" function
17425 */
17426 static void
17427f_strchars(argvars, rettv)
17428 typval_T *argvars;
17429 typval_T *rettv;
17430{
17431 char_u *s = get_tv_string(&argvars[0]);
17432#ifdef FEAT_MBYTE
17433 varnumber_T len = 0;
17434
17435 while (*s != NUL)
17436 {
17437 mb_cptr2char_adv(&s);
17438 ++len;
17439 }
17440 rettv->vval.v_number = len;
17441#else
17442 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17443#endif
17444}
17445
17446/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017447 * "strdisplaywidth()" function
17448 */
17449 static void
17450f_strdisplaywidth(argvars, rettv)
17451 typval_T *argvars;
17452 typval_T *rettv;
17453{
17454 char_u *s = get_tv_string(&argvars[0]);
17455 int col = 0;
17456
17457 if (argvars[1].v_type != VAR_UNKNOWN)
17458 col = get_tv_number(&argvars[1]);
17459
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017460 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017461}
17462
17463/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017464 * "strwidth()" function
17465 */
17466 static void
17467f_strwidth(argvars, rettv)
17468 typval_T *argvars;
17469 typval_T *rettv;
17470{
17471 char_u *s = get_tv_string(&argvars[0]);
17472
17473 rettv->vval.v_number = (varnumber_T)(
17474#ifdef FEAT_MBYTE
17475 mb_string2cells(s, -1)
17476#else
17477 STRLEN(s)
17478#endif
17479 );
17480}
17481
17482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 * "strpart()" function
17484 */
17485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017486f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 typval_T *argvars;
17488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489{
17490 char_u *p;
17491 int n;
17492 int len;
17493 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017494 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 slen = (int)STRLEN(p);
17498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017499 n = get_tv_number_chk(&argvars[1], &error);
17500 if (error)
17501 len = 0;
17502 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 else
17505 len = slen - n; /* default len: all bytes that are available. */
17506
17507 /*
17508 * Only return the overlap between the specified part and the actual
17509 * string.
17510 */
17511 if (n < 0)
17512 {
17513 len += n;
17514 n = 0;
17515 }
17516 else if (n > slen)
17517 n = slen;
17518 if (len < 0)
17519 len = 0;
17520 else if (n + len > slen)
17521 len = slen - n;
17522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->v_type = VAR_STRING;
17524 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525}
17526
17527/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017528 * "strridx()" function
17529 */
17530 static void
17531f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017532 typval_T *argvars;
17533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534{
17535 char_u buf[NUMBUFLEN];
17536 char_u *needle;
17537 char_u *haystack;
17538 char_u *rest;
17539 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017540 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017542 needle = get_tv_string_chk(&argvars[1]);
17543 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544
17545 rettv->vval.v_number = -1;
17546 if (needle == NULL || haystack == NULL)
17547 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017548
17549 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017550 if (argvars[2].v_type != VAR_UNKNOWN)
17551 {
17552 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017553 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017554 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017555 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017556 }
17557 else
17558 end_idx = haystack_len;
17559
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017562 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017563 lastmatch = haystack + end_idx;
17564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017565 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 for (rest = haystack; *rest != '\0'; ++rest)
17568 {
17569 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017570 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017571 break;
17572 lastmatch = rest;
17573 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575
17576 if (lastmatch == NULL)
17577 rettv->vval.v_number = -1;
17578 else
17579 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17580}
17581
17582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 * "strtrans()" function
17584 */
17585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017587 typval_T *argvars;
17588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017590 rettv->v_type = VAR_STRING;
17591 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592}
17593
17594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595 * "submatch()" function
17596 */
17597 static void
17598f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 typval_T *argvars;
17600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017601{
17602 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017603 rettv->vval.v_string =
17604 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017605}
17606
17607/*
17608 * "substitute()" function
17609 */
17610 static void
17611f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 typval_T *argvars;
17613 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017614{
17615 char_u patbuf[NUMBUFLEN];
17616 char_u subbuf[NUMBUFLEN];
17617 char_u flagsbuf[NUMBUFLEN];
17618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017619 char_u *str = get_tv_string_chk(&argvars[0]);
17620 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17621 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17622 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17623
Bram Moolenaar0d660222005-01-07 21:51:51 +000017624 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017625 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17626 rettv->vval.v_string = NULL;
17627 else
17628 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017629}
17630
17631/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017632 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017635f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
17639 int id = 0;
17640#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017641 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 long col;
17643 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017644 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646 lnum = get_tv_lnum(argvars); /* -1 on type error */
17647 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17648 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017650 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017651 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017652 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653#endif
17654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656}
17657
17658/*
17659 * "synIDattr(id, what [, mode])" function
17660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017663 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665{
17666 char_u *p = NULL;
17667#ifdef FEAT_SYN_HL
17668 int id;
17669 char_u *what;
17670 char_u *mode;
17671 char_u modebuf[NUMBUFLEN];
17672 int modec;
17673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 id = get_tv_number(&argvars[0]);
17675 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017676 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017680 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 modec = 0; /* replace invalid with current */
17682 }
17683 else
17684 {
17685#ifdef FEAT_GUI
17686 if (gui.in_use)
17687 modec = 'g';
17688 else
17689#endif
17690 if (t_colors > 1)
17691 modec = 'c';
17692 else
17693 modec = 't';
17694 }
17695
17696
17697 switch (TOLOWER_ASC(what[0]))
17698 {
17699 case 'b':
17700 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17701 p = highlight_color(id, what, modec);
17702 else /* bold */
17703 p = highlight_has_attr(id, HL_BOLD, modec);
17704 break;
17705
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017706 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 p = highlight_color(id, what, modec);
17708 break;
17709
17710 case 'i':
17711 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17712 p = highlight_has_attr(id, HL_INVERSE, modec);
17713 else /* italic */
17714 p = highlight_has_attr(id, HL_ITALIC, modec);
17715 break;
17716
17717 case 'n': /* name */
17718 p = get_highlight_name(NULL, id - 1);
17719 break;
17720
17721 case 'r': /* reverse */
17722 p = highlight_has_attr(id, HL_INVERSE, modec);
17723 break;
17724
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017725 case 's':
17726 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17727 p = highlight_color(id, what, modec);
17728 else /* standout */
17729 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 break;
17731
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017732 case 'u':
17733 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17734 /* underline */
17735 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17736 else
17737 /* undercurl */
17738 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 break;
17740 }
17741
17742 if (p != NULL)
17743 p = vim_strsave(p);
17744#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017745 rettv->v_type = VAR_STRING;
17746 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747}
17748
17749/*
17750 * "synIDtrans(id)" function
17751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017754 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756{
17757 int id;
17758
17759#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017760 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761
17762 if (id > 0)
17763 id = syn_get_final_id(id);
17764 else
17765#endif
17766 id = 0;
17767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769}
17770
17771/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017772 * "synconcealed(lnum, col)" function
17773 */
17774 static void
17775f_synconcealed(argvars, rettv)
17776 typval_T *argvars UNUSED;
17777 typval_T *rettv;
17778{
17779#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17780 long lnum;
17781 long col;
17782 int syntax_flags = 0;
17783 int cchar;
17784 int matchid = 0;
17785 char_u str[NUMBUFLEN];
17786#endif
17787
17788 rettv->v_type = VAR_LIST;
17789 rettv->vval.v_list = NULL;
17790
17791#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17792 lnum = get_tv_lnum(argvars); /* -1 on type error */
17793 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17794
17795 vim_memset(str, NUL, sizeof(str));
17796
17797 if (rettv_list_alloc(rettv) != FAIL)
17798 {
17799 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17800 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17801 && curwin->w_p_cole > 0)
17802 {
17803 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17804 syntax_flags = get_syntax_info(&matchid);
17805
17806 /* get the conceal character */
17807 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17808 {
17809 cchar = syn_get_sub_char();
17810 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17811 cchar = lcs_conceal;
17812 if (cchar != NUL)
17813 {
17814# ifdef FEAT_MBYTE
17815 if (has_mbyte)
17816 (*mb_char2bytes)(cchar, str);
17817 else
17818# endif
17819 str[0] = cchar;
17820 }
17821 }
17822 }
17823
17824 list_append_number(rettv->vval.v_list,
17825 (syntax_flags & HL_CONCEAL) != 0);
17826 /* -1 to auto-determine strlen */
17827 list_append_string(rettv->vval.v_list, str, -1);
17828 list_append_number(rettv->vval.v_list, matchid);
17829 }
17830#endif
17831}
17832
17833/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017834 * "synstack(lnum, col)" function
17835 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017836 static void
17837f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017838 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017839 typval_T *rettv;
17840{
17841#ifdef FEAT_SYN_HL
17842 long lnum;
17843 long col;
17844 int i;
17845 int id;
17846#endif
17847
17848 rettv->v_type = VAR_LIST;
17849 rettv->vval.v_list = NULL;
17850
17851#ifdef FEAT_SYN_HL
17852 lnum = get_tv_lnum(argvars); /* -1 on type error */
17853 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17854
17855 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017856 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017857 && rettv_list_alloc(rettv) != FAIL)
17858 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017859 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017860 for (i = 0; ; ++i)
17861 {
17862 id = syn_get_stack_item(i);
17863 if (id < 0)
17864 break;
17865 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17866 break;
17867 }
17868 }
17869#endif
17870}
17871
17872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 * "system()" function
17874 */
17875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017876f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017877 typval_T *argvars;
17878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017880 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017882 char_u *infile = NULL;
17883 char_u buf[NUMBUFLEN];
17884 int err = FALSE;
17885 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017887 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017888 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017889
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017890 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017891 {
17892 /*
17893 * Write the string to a temp file, to be used for input of the shell
17894 * command.
17895 */
17896 if ((infile = vim_tempname('i')) == NULL)
17897 {
17898 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017899 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017900 }
17901
17902 fd = mch_fopen((char *)infile, WRITEBIN);
17903 if (fd == NULL)
17904 {
17905 EMSG2(_(e_notopen), infile);
17906 goto done;
17907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017908 p = get_tv_string_buf_chk(&argvars[1], buf);
17909 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017910 {
17911 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017912 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017913 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017914 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17915 err = TRUE;
17916 if (fclose(fd) != 0)
17917 err = TRUE;
17918 if (err)
17919 {
17920 EMSG(_("E677: Error writing temp file"));
17921 goto done;
17922 }
17923 }
17924
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017925 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17926 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017927
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928#ifdef USE_CR
17929 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017930 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 {
17932 char_u *s;
17933
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017934 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 {
17936 if (*s == CAR)
17937 *s = NL;
17938 }
17939 }
17940#else
17941# ifdef USE_CRNL
17942 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017943 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 {
17945 char_u *s, *d;
17946
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017947 d = res;
17948 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 {
17950 if (s[0] == CAR && s[1] == NL)
17951 ++s;
17952 *d++ = *s;
17953 }
17954 *d = NUL;
17955 }
17956# endif
17957#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017958
17959done:
17960 if (infile != NULL)
17961 {
17962 mch_remove(infile);
17963 vim_free(infile);
17964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017965 rettv->v_type = VAR_STRING;
17966 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967}
17968
17969/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017970 * "tabpagebuflist()" function
17971 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017972 static void
17973f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017974 typval_T *argvars UNUSED;
17975 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017976{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017977#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017978 tabpage_T *tp;
17979 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017980
17981 if (argvars[0].v_type == VAR_UNKNOWN)
17982 wp = firstwin;
17983 else
17984 {
17985 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17986 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017987 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017988 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017989 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017990 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017991 for (; wp != NULL; wp = wp->w_next)
17992 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017993 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017994 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017995 }
17996#endif
17997}
17998
17999
18000/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018001 * "tabpagenr()" function
18002 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018003 static void
18004f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018005 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018006 typval_T *rettv;
18007{
18008 int nr = 1;
18009#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018010 char_u *arg;
18011
18012 if (argvars[0].v_type != VAR_UNKNOWN)
18013 {
18014 arg = get_tv_string_chk(&argvars[0]);
18015 nr = 0;
18016 if (arg != NULL)
18017 {
18018 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018019 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018020 else
18021 EMSG2(_(e_invexpr2), arg);
18022 }
18023 }
18024 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018025 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018026#endif
18027 rettv->vval.v_number = nr;
18028}
18029
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018030
18031#ifdef FEAT_WINDOWS
18032static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18033
18034/*
18035 * Common code for tabpagewinnr() and winnr().
18036 */
18037 static int
18038get_winnr(tp, argvar)
18039 tabpage_T *tp;
18040 typval_T *argvar;
18041{
18042 win_T *twin;
18043 int nr = 1;
18044 win_T *wp;
18045 char_u *arg;
18046
18047 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18048 if (argvar->v_type != VAR_UNKNOWN)
18049 {
18050 arg = get_tv_string_chk(argvar);
18051 if (arg == NULL)
18052 nr = 0; /* type error; errmsg already given */
18053 else if (STRCMP(arg, "$") == 0)
18054 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18055 else if (STRCMP(arg, "#") == 0)
18056 {
18057 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18058 if (twin == NULL)
18059 nr = 0;
18060 }
18061 else
18062 {
18063 EMSG2(_(e_invexpr2), arg);
18064 nr = 0;
18065 }
18066 }
18067
18068 if (nr > 0)
18069 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18070 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018071 {
18072 if (wp == NULL)
18073 {
18074 /* didn't find it in this tabpage */
18075 nr = 0;
18076 break;
18077 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018078 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018079 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018080 return nr;
18081}
18082#endif
18083
18084/*
18085 * "tabpagewinnr()" function
18086 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018087 static void
18088f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018089 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018090 typval_T *rettv;
18091{
18092 int nr = 1;
18093#ifdef FEAT_WINDOWS
18094 tabpage_T *tp;
18095
18096 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18097 if (tp == NULL)
18098 nr = 0;
18099 else
18100 nr = get_winnr(tp, &argvars[1]);
18101#endif
18102 rettv->vval.v_number = nr;
18103}
18104
18105
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018106/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018107 * "tagfiles()" function
18108 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018109 static void
18110f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018111 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018112 typval_T *rettv;
18113{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018114 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018115 tagname_T tn;
18116 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018119 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018120 fname = alloc(MAXPATHL);
18121 if (fname == NULL)
18122 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018124 for (first = TRUE; ; first = FALSE)
18125 if (get_tagfname(&tn, first, fname) == FAIL
18126 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018127 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018128 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018129 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018130}
18131
18132/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018133 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018134 */
18135 static void
18136f_taglist(argvars, rettv)
18137 typval_T *argvars;
18138 typval_T *rettv;
18139{
18140 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018141
18142 tag_pattern = get_tv_string(&argvars[0]);
18143
18144 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018145 if (*tag_pattern == NUL)
18146 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018147
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018148 if (rettv_list_alloc(rettv) == OK)
18149 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018150}
18151
18152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 * "tempname()" function
18154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018156f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018157 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159{
18160 static int x = 'A';
18161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162 rettv->v_type = VAR_STRING;
18163 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
18165 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18166 * names. Skip 'I' and 'O', they are used for shell redirection. */
18167 do
18168 {
18169 if (x == 'Z')
18170 x = '0';
18171 else if (x == '9')
18172 x = 'A';
18173 else
18174 {
18175#ifdef EBCDIC
18176 if (x == 'I')
18177 x = 'J';
18178 else if (x == 'R')
18179 x = 'S';
18180 else
18181#endif
18182 ++x;
18183 }
18184 } while (x == 'I' || x == 'O');
18185}
18186
18187/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018188 * "test(list)" function: Just checking the walls...
18189 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018190 static void
18191f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018192 typval_T *argvars UNUSED;
18193 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018194{
18195 /* Used for unit testing. Change the code below to your liking. */
18196#if 0
18197 listitem_T *li;
18198 list_T *l;
18199 char_u *bad, *good;
18200
18201 if (argvars[0].v_type != VAR_LIST)
18202 return;
18203 l = argvars[0].vval.v_list;
18204 if (l == NULL)
18205 return;
18206 li = l->lv_first;
18207 if (li == NULL)
18208 return;
18209 bad = get_tv_string(&li->li_tv);
18210 li = li->li_next;
18211 if (li == NULL)
18212 return;
18213 good = get_tv_string(&li->li_tv);
18214 rettv->vval.v_number = test_edit_score(bad, good);
18215#endif
18216}
18217
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018218#ifdef FEAT_FLOAT
18219/*
18220 * "tan()" function
18221 */
18222 static void
18223f_tan(argvars, rettv)
18224 typval_T *argvars;
18225 typval_T *rettv;
18226{
18227 float_T f;
18228
18229 rettv->v_type = VAR_FLOAT;
18230 if (get_float_arg(argvars, &f) == OK)
18231 rettv->vval.v_float = tan(f);
18232 else
18233 rettv->vval.v_float = 0.0;
18234}
18235
18236/*
18237 * "tanh()" function
18238 */
18239 static void
18240f_tanh(argvars, rettv)
18241 typval_T *argvars;
18242 typval_T *rettv;
18243{
18244 float_T f;
18245
18246 rettv->v_type = VAR_FLOAT;
18247 if (get_float_arg(argvars, &f) == OK)
18248 rettv->vval.v_float = tanh(f);
18249 else
18250 rettv->vval.v_float = 0.0;
18251}
18252#endif
18253
Bram Moolenaard52d9742005-08-21 22:20:28 +000018254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 * "tolower(string)" function
18256 */
18257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018258f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018259 typval_T *argvars;
18260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261{
18262 char_u *p;
18263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018264 p = vim_strsave(get_tv_string(&argvars[0]));
18265 rettv->v_type = VAR_STRING;
18266 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267
18268 if (p != NULL)
18269 while (*p != NUL)
18270 {
18271#ifdef FEAT_MBYTE
18272 int l;
18273
18274 if (enc_utf8)
18275 {
18276 int c, lc;
18277
18278 c = utf_ptr2char(p);
18279 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018280 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 /* TODO: reallocate string when byte count changes. */
18282 if (utf_char2len(lc) == l)
18283 utf_char2bytes(lc, p);
18284 p += l;
18285 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018286 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 p += l; /* skip multi-byte character */
18288 else
18289#endif
18290 {
18291 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18292 ++p;
18293 }
18294 }
18295}
18296
18297/*
18298 * "toupper(string)" function
18299 */
18300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018302 typval_T *argvars;
18303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018306 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307}
18308
18309/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018310 * "tr(string, fromstr, tostr)" function
18311 */
18312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018314 typval_T *argvars;
18315 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018316{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018317 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018318 char_u *fromstr;
18319 char_u *tostr;
18320 char_u *p;
18321#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018322 int inlen;
18323 int fromlen;
18324 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018325 int idx;
18326 char_u *cpstr;
18327 int cplen;
18328 int first = TRUE;
18329#endif
18330 char_u buf[NUMBUFLEN];
18331 char_u buf2[NUMBUFLEN];
18332 garray_T ga;
18333
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018334 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018335 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18336 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018337
18338 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018339 rettv->v_type = VAR_STRING;
18340 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018341 if (fromstr == NULL || tostr == NULL)
18342 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018343 ga_init2(&ga, (int)sizeof(char), 80);
18344
18345#ifdef FEAT_MBYTE
18346 if (!has_mbyte)
18347#endif
18348 /* not multi-byte: fromstr and tostr must be the same length */
18349 if (STRLEN(fromstr) != STRLEN(tostr))
18350 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018351#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018352error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018353#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018354 EMSG2(_(e_invarg2), fromstr);
18355 ga_clear(&ga);
18356 return;
18357 }
18358
18359 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018360 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018361 {
18362#ifdef FEAT_MBYTE
18363 if (has_mbyte)
18364 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018365 inlen = (*mb_ptr2len)(in_str);
18366 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018367 cplen = inlen;
18368 idx = 0;
18369 for (p = fromstr; *p != NUL; p += fromlen)
18370 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018371 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018372 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018373 {
18374 for (p = tostr; *p != NUL; p += tolen)
18375 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018376 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018377 if (idx-- == 0)
18378 {
18379 cplen = tolen;
18380 cpstr = p;
18381 break;
18382 }
18383 }
18384 if (*p == NUL) /* tostr is shorter than fromstr */
18385 goto error;
18386 break;
18387 }
18388 ++idx;
18389 }
18390
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018391 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018392 {
18393 /* Check that fromstr and tostr have the same number of
18394 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018395 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018396 first = FALSE;
18397 for (p = tostr; *p != NUL; p += tolen)
18398 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018399 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018400 --idx;
18401 }
18402 if (idx != 0)
18403 goto error;
18404 }
18405
18406 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018407 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018408 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018409
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018410 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018411 }
18412 else
18413#endif
18414 {
18415 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018416 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018417 if (p != NULL)
18418 ga_append(&ga, tostr[p - fromstr]);
18419 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018420 ga_append(&ga, *in_str);
18421 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018422 }
18423 }
18424
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018425 /* add a terminating NUL */
18426 ga_grow(&ga, 1);
18427 ga_append(&ga, NUL);
18428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018430}
18431
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018432#ifdef FEAT_FLOAT
18433/*
18434 * "trunc({float})" function
18435 */
18436 static void
18437f_trunc(argvars, rettv)
18438 typval_T *argvars;
18439 typval_T *rettv;
18440{
18441 float_T f;
18442
18443 rettv->v_type = VAR_FLOAT;
18444 if (get_float_arg(argvars, &f) == OK)
18445 /* trunc() is not in C90, use floor() or ceil() instead. */
18446 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18447 else
18448 rettv->vval.v_float = 0.0;
18449}
18450#endif
18451
Bram Moolenaar8299df92004-07-10 09:47:34 +000018452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 * "type(expr)" function
18454 */
18455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018456f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 typval_T *argvars;
18458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018460 int n;
18461
18462 switch (argvars[0].v_type)
18463 {
18464 case VAR_NUMBER: n = 0; break;
18465 case VAR_STRING: n = 1; break;
18466 case VAR_FUNC: n = 2; break;
18467 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018468 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018469#ifdef FEAT_FLOAT
18470 case VAR_FLOAT: n = 5; break;
18471#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018472 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18473 }
18474 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475}
18476
18477/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018478 * "undofile(name)" function
18479 */
18480 static void
18481f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018482 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018483 typval_T *rettv;
18484{
18485 rettv->v_type = VAR_STRING;
18486#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018487 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018488 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018489
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018490 if (*fname == NUL)
18491 {
18492 /* If there is no file name there will be no undo file. */
18493 rettv->vval.v_string = NULL;
18494 }
18495 else
18496 {
18497 char_u *ffname = FullName_save(fname, FALSE);
18498
18499 if (ffname != NULL)
18500 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18501 vim_free(ffname);
18502 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018503 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018504#else
18505 rettv->vval.v_string = NULL;
18506#endif
18507}
18508
18509/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018510 * "undotree()" function
18511 */
18512 static void
18513f_undotree(argvars, rettv)
18514 typval_T *argvars UNUSED;
18515 typval_T *rettv;
18516{
18517 if (rettv_dict_alloc(rettv) == OK)
18518 {
18519 dict_T *dict = rettv->vval.v_dict;
18520 list_T *list;
18521
Bram Moolenaar730cde92010-06-27 05:18:54 +020018522 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018523 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018524 dict_add_nr_str(dict, "save_last",
18525 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018526 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18527 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018528 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018529
18530 list = list_alloc();
18531 if (list != NULL)
18532 {
18533 u_eval_tree(curbuf->b_u_oldhead, list);
18534 dict_add_list(dict, "entries", list);
18535 }
18536 }
18537}
18538
18539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018540 * "values(dict)" function
18541 */
18542 static void
18543f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T *argvars;
18545 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018546{
18547 dict_list(argvars, rettv, 1);
18548}
18549
18550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 * "virtcol(string)" function
18552 */
18553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018555 typval_T *argvars;
18556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557{
18558 colnr_T vcol = 0;
18559 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018560 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018562 fp = var2fpos(&argvars[0], FALSE, &fnum);
18563 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18564 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 {
18566 getvvcol(curwin, fp, NULL, NULL, &vcol);
18567 ++vcol;
18568 }
18569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571}
18572
18573/*
18574 * "visualmode()" function
18575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018578 typval_T *argvars UNUSED;
18579 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
18581#ifdef FEAT_VISUAL
18582 char_u str[2];
18583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 str[0] = curbuf->b_visual_mode_eval;
18586 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588
18589 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018590 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592#endif
18593}
18594
18595/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018596 * "wildmenumode()" function
18597 */
18598 static void
18599f_wildmenumode(argvars, rettv)
18600 typval_T *argvars UNUSED;
18601 typval_T *rettv UNUSED;
18602{
18603#ifdef FEAT_WILDMENU
18604 if (wild_menu_showing)
18605 rettv->vval.v_number = 1;
18606#endif
18607}
18608
18609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 * "winbufnr(nr)" function
18611 */
18612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018614 typval_T *argvars;
18615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616{
18617 win_T *wp;
18618
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018619 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624}
18625
18626/*
18627 * "wincol()" function
18628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633{
18634 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636}
18637
18638/*
18639 * "winheight(nr)" function
18640 */
18641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018642f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018643 typval_T *argvars;
18644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645{
18646 win_T *wp;
18647
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018648 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653}
18654
18655/*
18656 * "winline()" function
18657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662{
18663 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665}
18666
18667/*
18668 * "winnr()" function
18669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018671f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018672 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674{
18675 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018676
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018678 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681}
18682
18683/*
18684 * "winrestcmd()" function
18685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018688 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690{
18691#ifdef FEAT_WINDOWS
18692 win_T *wp;
18693 int winnr = 1;
18694 garray_T ga;
18695 char_u buf[50];
18696
18697 ga_init2(&ga, (int)sizeof(char), 70);
18698 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18699 {
18700 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18701 ga_concat(&ga, buf);
18702# ifdef FEAT_VERTSPLIT
18703 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18704 ga_concat(&ga, buf);
18705# endif
18706 ++winnr;
18707 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018708 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018712 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715}
18716
18717/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018718 * "winrestview()" function
18719 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018720 static void
18721f_winrestview(argvars, rettv)
18722 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018723 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018724{
18725 dict_T *dict;
18726
18727 if (argvars[0].v_type != VAR_DICT
18728 || (dict = argvars[0].vval.v_dict) == NULL)
18729 EMSG(_(e_invarg));
18730 else
18731 {
18732 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18733 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18734#ifdef FEAT_VIRTUALEDIT
18735 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18736#endif
18737 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018738 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018739
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018740 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018741#ifdef FEAT_DIFF
18742 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18743#endif
18744 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18745 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18746
18747 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018748 win_new_height(curwin, curwin->w_height);
18749# ifdef FEAT_VERTSPLIT
18750 win_new_width(curwin, W_WIDTH(curwin));
18751# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018752 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018753
18754 if (curwin->w_topline == 0)
18755 curwin->w_topline = 1;
18756 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18757 curwin->w_topline = curbuf->b_ml.ml_line_count;
18758#ifdef FEAT_DIFF
18759 check_topfill(curwin, TRUE);
18760#endif
18761 }
18762}
18763
18764/*
18765 * "winsaveview()" function
18766 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018767 static void
18768f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018769 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018770 typval_T *rettv;
18771{
18772 dict_T *dict;
18773
Bram Moolenaara800b422010-06-27 01:15:55 +020018774 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018775 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018776 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018777
18778 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18779 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18780#ifdef FEAT_VIRTUALEDIT
18781 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18782#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018783 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018784 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18785
18786 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18787#ifdef FEAT_DIFF
18788 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18789#endif
18790 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18791 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18792}
18793
18794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 * "winwidth(nr)" function
18796 */
18797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 typval_T *argvars;
18800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801{
18802 win_T *wp;
18803
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018804 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 else
18808#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812#endif
18813}
18814
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018816 * "writefile()" function
18817 */
18818 static void
18819f_writefile(argvars, rettv)
18820 typval_T *argvars;
18821 typval_T *rettv;
18822{
18823 int binary = FALSE;
18824 char_u *fname;
18825 FILE *fd;
18826 listitem_T *li;
18827 char_u *s;
18828 int ret = 0;
18829 int c;
18830
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018831 if (check_restricted() || check_secure())
18832 return;
18833
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018834 if (argvars[0].v_type != VAR_LIST)
18835 {
18836 EMSG2(_(e_listarg), "writefile()");
18837 return;
18838 }
18839 if (argvars[0].vval.v_list == NULL)
18840 return;
18841
18842 if (argvars[2].v_type != VAR_UNKNOWN
18843 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18844 binary = TRUE;
18845
18846 /* Always open the file in binary mode, library functions have a mind of
18847 * their own about CR-LF conversion. */
18848 fname = get_tv_string(&argvars[1]);
18849 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18850 {
18851 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18852 ret = -1;
18853 }
18854 else
18855 {
18856 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18857 li = li->li_next)
18858 {
18859 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18860 {
18861 if (*s == '\n')
18862 c = putc(NUL, fd);
18863 else
18864 c = putc(*s, fd);
18865 if (c == EOF)
18866 {
18867 ret = -1;
18868 break;
18869 }
18870 }
18871 if (!binary || li->li_next != NULL)
18872 if (putc('\n', fd) == EOF)
18873 {
18874 ret = -1;
18875 break;
18876 }
18877 if (ret < 0)
18878 {
18879 EMSG(_(e_write));
18880 break;
18881 }
18882 }
18883 fclose(fd);
18884 }
18885
18886 rettv->vval.v_number = ret;
18887}
18888
18889/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018890 * "xor(expr, expr)" function
18891 */
18892 static void
18893f_xor(argvars, rettv)
18894 typval_T *argvars;
18895 typval_T *rettv;
18896{
18897 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18898 ^ get_tv_number_chk(&argvars[1], NULL);
18899}
18900
18901
18902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018904 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 */
18906 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018907var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018908 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018909 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018910 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018912 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018914 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915
Bram Moolenaara5525202006-03-02 22:52:09 +000018916 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018917 if (varp->v_type == VAR_LIST)
18918 {
18919 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018920 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018921 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018922 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018923
18924 l = varp->vval.v_list;
18925 if (l == NULL)
18926 return NULL;
18927
18928 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018929 pos.lnum = list_find_nr(l, 0L, &error);
18930 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018931 return NULL; /* invalid line number */
18932
18933 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018934 pos.col = list_find_nr(l, 1L, &error);
18935 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018936 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018937 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018938
18939 /* We accept "$" for the column number: last column. */
18940 li = list_find(l, 1L);
18941 if (li != NULL && li->li_tv.v_type == VAR_STRING
18942 && li->li_tv.vval.v_string != NULL
18943 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18944 pos.col = len + 1;
18945
Bram Moolenaara5525202006-03-02 22:52:09 +000018946 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018947 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018948 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018949 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018950
Bram Moolenaara5525202006-03-02 22:52:09 +000018951#ifdef FEAT_VIRTUALEDIT
18952 /* Get the virtual offset. Defaults to zero. */
18953 pos.coladd = list_find_nr(l, 2L, &error);
18954 if (error)
18955 pos.coladd = 0;
18956#endif
18957
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018958 return &pos;
18959 }
18960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018961 name = get_tv_string_chk(varp);
18962 if (name == NULL)
18963 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018964 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018966#ifdef FEAT_VISUAL
18967 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18968 {
18969 if (VIsual_active)
18970 return &VIsual;
18971 return &curwin->w_cursor;
18972 }
18973#endif
18974 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018976 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18978 return NULL;
18979 return pp;
18980 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018981
18982#ifdef FEAT_VIRTUALEDIT
18983 pos.coladd = 0;
18984#endif
18985
Bram Moolenaar477933c2007-07-17 14:32:23 +000018986 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018987 {
18988 pos.col = 0;
18989 if (name[1] == '0') /* "w0": first visible line */
18990 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018991 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018992 pos.lnum = curwin->w_topline;
18993 return &pos;
18994 }
18995 else if (name[1] == '$') /* "w$": last visible line */
18996 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018997 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018998 pos.lnum = curwin->w_botline - 1;
18999 return &pos;
19000 }
19001 }
19002 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019004 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 {
19006 pos.lnum = curbuf->b_ml.ml_line_count;
19007 pos.col = 0;
19008 }
19009 else
19010 {
19011 pos.lnum = curwin->w_cursor.lnum;
19012 pos.col = (colnr_T)STRLEN(ml_get_curline());
19013 }
19014 return &pos;
19015 }
19016 return NULL;
19017}
19018
19019/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019020 * Convert list in "arg" into a position and optional file number.
19021 * When "fnump" is NULL there is no file number, only 3 items.
19022 * Note that the column is passed on as-is, the caller may want to decrement
19023 * it to use 1 for the first column.
19024 * Return FAIL when conversion is not possible, doesn't check the position for
19025 * validity.
19026 */
19027 static int
19028list2fpos(arg, posp, fnump)
19029 typval_T *arg;
19030 pos_T *posp;
19031 int *fnump;
19032{
19033 list_T *l = arg->vval.v_list;
19034 long i = 0;
19035 long n;
19036
Bram Moolenaarbde35262006-07-23 20:12:24 +000019037 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19038 * when "fnump" isn't NULL and "coladd" is optional. */
19039 if (arg->v_type != VAR_LIST
19040 || l == NULL
19041 || l->lv_len < (fnump == NULL ? 2 : 3)
19042 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019043 return FAIL;
19044
19045 if (fnump != NULL)
19046 {
19047 n = list_find_nr(l, i++, NULL); /* fnum */
19048 if (n < 0)
19049 return FAIL;
19050 if (n == 0)
19051 n = curbuf->b_fnum; /* current buffer */
19052 *fnump = n;
19053 }
19054
19055 n = list_find_nr(l, i++, NULL); /* lnum */
19056 if (n < 0)
19057 return FAIL;
19058 posp->lnum = n;
19059
19060 n = list_find_nr(l, i++, NULL); /* col */
19061 if (n < 0)
19062 return FAIL;
19063 posp->col = n;
19064
19065#ifdef FEAT_VIRTUALEDIT
19066 n = list_find_nr(l, i, NULL);
19067 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019068 posp->coladd = 0;
19069 else
19070 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019071#endif
19072
19073 return OK;
19074}
19075
19076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 * Get the length of an environment variable name.
19078 * Advance "arg" to the first character after the name.
19079 * Return 0 for error.
19080 */
19081 static int
19082get_env_len(arg)
19083 char_u **arg;
19084{
19085 char_u *p;
19086 int len;
19087
19088 for (p = *arg; vim_isIDc(*p); ++p)
19089 ;
19090 if (p == *arg) /* no name found */
19091 return 0;
19092
19093 len = (int)(p - *arg);
19094 *arg = p;
19095 return len;
19096}
19097
19098/*
19099 * Get the length of the name of a function or internal variable.
19100 * "arg" is advanced to the first non-white character after the name.
19101 * Return 0 if something is wrong.
19102 */
19103 static int
19104get_id_len(arg)
19105 char_u **arg;
19106{
19107 char_u *p;
19108 int len;
19109
19110 /* Find the end of the name. */
19111 for (p = *arg; eval_isnamec(*p); ++p)
19112 ;
19113 if (p == *arg) /* no name found */
19114 return 0;
19115
19116 len = (int)(p - *arg);
19117 *arg = skipwhite(p);
19118
19119 return len;
19120}
19121
19122/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019123 * Get the length of the name of a variable or function.
19124 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019126 * Return -1 if curly braces expansion failed.
19127 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 * If the name contains 'magic' {}'s, expand them and return the
19129 * expanded name in an allocated string via 'alias' - caller must free.
19130 */
19131 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019132get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 char_u **arg;
19134 char_u **alias;
19135 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019136 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137{
19138 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 char_u *p;
19140 char_u *expr_start;
19141 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142
19143 *alias = NULL; /* default to no alias */
19144
19145 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19146 && (*arg)[2] == (int)KE_SNR)
19147 {
19148 /* hard coded <SNR>, already translated */
19149 *arg += 3;
19150 return get_id_len(arg) + 3;
19151 }
19152 len = eval_fname_script(*arg);
19153 if (len > 0)
19154 {
19155 /* literal "<SID>", "s:" or "<SNR>" */
19156 *arg += len;
19157 }
19158
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019162 p = find_name_end(*arg, &expr_start, &expr_end,
19163 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 if (expr_start != NULL)
19165 {
19166 char_u *temp_string;
19167
19168 if (!evaluate)
19169 {
19170 len += (int)(p - *arg);
19171 *arg = skipwhite(p);
19172 return len;
19173 }
19174
19175 /*
19176 * Include any <SID> etc in the expanded string:
19177 * Thus the -len here.
19178 */
19179 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19180 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019181 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 *alias = temp_string;
19183 *arg = skipwhite(p);
19184 return (int)STRLEN(temp_string);
19185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186
19187 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019188 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 EMSG2(_(e_invexpr2), *arg);
19190
19191 return len;
19192}
19193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194/*
19195 * Find the end of a variable or function name, taking care of magic braces.
19196 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19197 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019198 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 * Return a pointer to just after the name. Equal to "arg" if there is no
19200 * valid name.
19201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019203find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 char_u *arg;
19205 char_u **expr_start;
19206 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019207 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019209 int mb_nest = 0;
19210 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 char_u *p;
19212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019213 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 *expr_start = NULL;
19216 *expr_end = NULL;
19217 }
19218
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019219 /* Quick check for valid starting character. */
19220 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19221 return arg;
19222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223 for (p = arg; *p != NUL
19224 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019225 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019226 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019228 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019229 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019230 if (*p == '\'')
19231 {
19232 /* skip over 'string' to avoid counting [ and ] inside it. */
19233 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19234 ;
19235 if (*p == NUL)
19236 break;
19237 }
19238 else if (*p == '"')
19239 {
19240 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19241 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19242 if (*p == '\\' && p[1] != NUL)
19243 ++p;
19244 if (*p == NUL)
19245 break;
19246 }
19247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019248 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250 if (*p == '[')
19251 ++br_nest;
19252 else if (*p == ']')
19253 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019258 if (*p == '{')
19259 {
19260 mb_nest++;
19261 if (expr_start != NULL && *expr_start == NULL)
19262 *expr_start = p;
19263 }
19264 else if (*p == '}')
19265 {
19266 mb_nest--;
19267 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19268 *expr_end = p;
19269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 }
19272
19273 return p;
19274}
19275
19276/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019277 * Expands out the 'magic' {}'s in a variable/function name.
19278 * Note that this can call itself recursively, to deal with
19279 * constructs like foo{bar}{baz}{bam}
19280 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19281 * "in_start" ^
19282 * "expr_start" ^
19283 * "expr_end" ^
19284 * "in_end" ^
19285 *
19286 * Returns a new allocated string, which the caller must free.
19287 * Returns NULL for failure.
19288 */
19289 static char_u *
19290make_expanded_name(in_start, expr_start, expr_end, in_end)
19291 char_u *in_start;
19292 char_u *expr_start;
19293 char_u *expr_end;
19294 char_u *in_end;
19295{
19296 char_u c1;
19297 char_u *retval = NULL;
19298 char_u *temp_result;
19299 char_u *nextcmd = NULL;
19300
19301 if (expr_end == NULL || in_end == NULL)
19302 return NULL;
19303 *expr_start = NUL;
19304 *expr_end = NUL;
19305 c1 = *in_end;
19306 *in_end = NUL;
19307
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019308 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019309 if (temp_result != NULL && nextcmd == NULL)
19310 {
19311 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19312 + (in_end - expr_end) + 1));
19313 if (retval != NULL)
19314 {
19315 STRCPY(retval, in_start);
19316 STRCAT(retval, temp_result);
19317 STRCAT(retval, expr_end + 1);
19318 }
19319 }
19320 vim_free(temp_result);
19321
19322 *in_end = c1; /* put char back for error messages */
19323 *expr_start = '{';
19324 *expr_end = '}';
19325
19326 if (retval != NULL)
19327 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019328 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019329 if (expr_start != NULL)
19330 {
19331 /* Further expansion! */
19332 temp_result = make_expanded_name(retval, expr_start,
19333 expr_end, temp_result);
19334 vim_free(retval);
19335 retval = temp_result;
19336 }
19337 }
19338
19339 return retval;
19340}
19341
19342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019344 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 */
19346 static int
19347eval_isnamec(c)
19348 int c;
19349{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019350 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19351}
19352
19353/*
19354 * Return TRUE if character "c" can be used as the first character in a
19355 * variable or function name (excluding '{' and '}').
19356 */
19357 static int
19358eval_isnamec1(c)
19359 int c;
19360{
19361 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362}
19363
19364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 * Set number v: variable to "val".
19366 */
19367 void
19368set_vim_var_nr(idx, val)
19369 int idx;
19370 long val;
19371{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019372 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373}
19374
19375/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019376 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 */
19378 long
19379get_vim_var_nr(idx)
19380 int idx;
19381{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019382 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383}
19384
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019385/*
19386 * Get string v: variable value. Uses a static buffer, can only be used once.
19387 */
19388 char_u *
19389get_vim_var_str(idx)
19390 int idx;
19391{
19392 return get_tv_string(&vimvars[idx].vv_tv);
19393}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019394
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019396 * Get List v: variable value. Caller must take care of reference count when
19397 * needed.
19398 */
19399 list_T *
19400get_vim_var_list(idx)
19401 int idx;
19402{
19403 return vimvars[idx].vv_list;
19404}
19405
19406/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019407 * Set v:char to character "c".
19408 */
19409 void
19410set_vim_var_char(c)
19411 int c;
19412{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019413 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019414
19415#ifdef FEAT_MBYTE
19416 if (has_mbyte)
19417 buf[(*mb_char2bytes)(c, buf)] = NUL;
19418 else
19419#endif
19420 {
19421 buf[0] = c;
19422 buf[1] = NUL;
19423 }
19424 set_vim_var_string(VV_CHAR, buf, -1);
19425}
19426
19427/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019428 * Set v:count to "count" and v:count1 to "count1".
19429 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 */
19431 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019432set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 long count;
19434 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019435 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019437 if (set_prevcount)
19438 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019439 vimvars[VV_COUNT].vv_nr = count;
19440 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441}
19442
19443/*
19444 * Set string v: variable to a copy of "val".
19445 */
19446 void
19447set_vim_var_string(idx, val, len)
19448 int idx;
19449 char_u *val;
19450 int len; /* length of "val" to use or -1 (whole string) */
19451{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019452 /* Need to do this (at least) once, since we can't initialize a union.
19453 * Will always be invoked when "v:progname" is set. */
19454 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19455
Bram Moolenaare9a41262005-01-15 22:18:47 +000019456 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019458 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019460 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019462 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463}
19464
19465/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019466 * Set List v: variable to "val".
19467 */
19468 void
19469set_vim_var_list(idx, val)
19470 int idx;
19471 list_T *val;
19472{
19473 list_unref(vimvars[idx].vv_list);
19474 vimvars[idx].vv_list = val;
19475 if (val != NULL)
19476 ++val->lv_refcount;
19477}
19478
19479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 * Set v:register if needed.
19481 */
19482 void
19483set_reg_var(c)
19484 int c;
19485{
19486 char_u regname;
19487
19488 if (c == 0 || c == ' ')
19489 regname = '"';
19490 else
19491 regname = c;
19492 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019493 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 set_vim_var_string(VV_REG, &regname, 1);
19495}
19496
19497/*
19498 * Get or set v:exception. If "oldval" == NULL, return the current value.
19499 * Otherwise, restore the value to "oldval" and return NULL.
19500 * Must always be called in pairs to save and restore v:exception! Does not
19501 * take care of memory allocations.
19502 */
19503 char_u *
19504v_exception(oldval)
19505 char_u *oldval;
19506{
19507 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019508 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
Bram Moolenaare9a41262005-01-15 22:18:47 +000019510 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 return NULL;
19512}
19513
19514/*
19515 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19516 * Otherwise, restore the value to "oldval" and return NULL.
19517 * Must always be called in pairs to save and restore v:throwpoint! Does not
19518 * take care of memory allocations.
19519 */
19520 char_u *
19521v_throwpoint(oldval)
19522 char_u *oldval;
19523{
19524 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019525 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526
Bram Moolenaare9a41262005-01-15 22:18:47 +000019527 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 return NULL;
19529}
19530
19531#if defined(FEAT_AUTOCMD) || defined(PROTO)
19532/*
19533 * Set v:cmdarg.
19534 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19535 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19536 * Must always be called in pairs!
19537 */
19538 char_u *
19539set_cmdarg(eap, oldarg)
19540 exarg_T *eap;
19541 char_u *oldarg;
19542{
19543 char_u *oldval;
19544 char_u *newval;
19545 unsigned len;
19546
Bram Moolenaare9a41262005-01-15 22:18:47 +000019547 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019548 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019550 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019551 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019552 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 }
19554
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019555 if (eap->force_bin == FORCE_BIN)
19556 len = 6;
19557 else if (eap->force_bin == FORCE_NOBIN)
19558 len = 8;
19559 else
19560 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019561
19562 if (eap->read_edit)
19563 len += 7;
19564
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019565 if (eap->force_ff != 0)
19566 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19567# ifdef FEAT_MBYTE
19568 if (eap->force_enc != 0)
19569 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019570 if (eap->bad_char != 0)
19571 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019572# endif
19573
19574 newval = alloc(len + 1);
19575 if (newval == NULL)
19576 return NULL;
19577
19578 if (eap->force_bin == FORCE_BIN)
19579 sprintf((char *)newval, " ++bin");
19580 else if (eap->force_bin == FORCE_NOBIN)
19581 sprintf((char *)newval, " ++nobin");
19582 else
19583 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019584
19585 if (eap->read_edit)
19586 STRCAT(newval, " ++edit");
19587
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019588 if (eap->force_ff != 0)
19589 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19590 eap->cmd + eap->force_ff);
19591# ifdef FEAT_MBYTE
19592 if (eap->force_enc != 0)
19593 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19594 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019595 if (eap->bad_char == BAD_KEEP)
19596 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19597 else if (eap->bad_char == BAD_DROP)
19598 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19599 else if (eap->bad_char != 0)
19600 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019601# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019602 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019603 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604}
19605#endif
19606
19607/*
19608 * Get the value of internal variable "name".
19609 * Return OK or FAIL.
19610 */
19611 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019612get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 char_u *name;
19614 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019616 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617{
19618 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 typval_T *tv = NULL;
19620 typval_T atv;
19621 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623
19624 /* truncate the name, so that we can use strcmp() */
19625 cc = name[len];
19626 name[len] = NUL;
19627
19628 /*
19629 * Check for "b:changedtick".
19630 */
19631 if (STRCMP(name, "b:changedtick") == 0)
19632 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019633 atv.v_type = VAR_NUMBER;
19634 atv.vval.v_number = curbuf->b_changedtick;
19635 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 }
19637
19638 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 * Check for user-defined variables.
19640 */
19641 else
19642 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019643 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 }
19647
Bram Moolenaare9a41262005-01-15 22:18:47 +000019648 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019650 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019651 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 ret = FAIL;
19653 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019655 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656
19657 name[len] = cc;
19658
19659 return ret;
19660}
19661
19662/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019663 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19664 * Also handle function call with Funcref variable: func(expr)
19665 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19666 */
19667 static int
19668handle_subscript(arg, rettv, evaluate, verbose)
19669 char_u **arg;
19670 typval_T *rettv;
19671 int evaluate; /* do more than finding the end */
19672 int verbose; /* give error messages */
19673{
19674 int ret = OK;
19675 dict_T *selfdict = NULL;
19676 char_u *s;
19677 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019678 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019679
19680 while (ret == OK
19681 && (**arg == '['
19682 || (**arg == '.' && rettv->v_type == VAR_DICT)
19683 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19684 && !vim_iswhite(*(*arg - 1)))
19685 {
19686 if (**arg == '(')
19687 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019688 /* need to copy the funcref so that we can clear rettv */
19689 functv = *rettv;
19690 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019691
19692 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019693 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019694 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019695 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19696 &len, evaluate, selfdict);
19697
19698 /* Clear the funcref afterwards, so that deleting it while
19699 * evaluating the arguments is possible (see test55). */
19700 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019701
19702 /* Stop the expression evaluation when immediately aborting on
19703 * error, or when an interrupt occurred or an exception was thrown
19704 * but not caught. */
19705 if (aborting())
19706 {
19707 if (ret == OK)
19708 clear_tv(rettv);
19709 ret = FAIL;
19710 }
19711 dict_unref(selfdict);
19712 selfdict = NULL;
19713 }
19714 else /* **arg == '[' || **arg == '.' */
19715 {
19716 dict_unref(selfdict);
19717 if (rettv->v_type == VAR_DICT)
19718 {
19719 selfdict = rettv->vval.v_dict;
19720 if (selfdict != NULL)
19721 ++selfdict->dv_refcount;
19722 }
19723 else
19724 selfdict = NULL;
19725 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19726 {
19727 clear_tv(rettv);
19728 ret = FAIL;
19729 }
19730 }
19731 }
19732 dict_unref(selfdict);
19733 return ret;
19734}
19735
19736/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019737 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019738 * value).
19739 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019740 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019742{
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744}
19745
19746/*
19747 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 * The string "s" must have been allocated, it is consumed.
19749 * Return NULL for out of memory, the variable otherwise.
19750 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019751 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019752alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 char_u *s;
19754{
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 rettv = alloc_tv();
19758 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 rettv->v_type = VAR_STRING;
19761 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 }
19763 else
19764 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766}
19767
19768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019769 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019771 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019772free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019773 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774{
19775 if (varp != NULL)
19776 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019777 switch (varp->v_type)
19778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019779 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019780 func_unref(varp->vval.v_string);
19781 /*FALLTHROUGH*/
19782 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783 vim_free(varp->vval.v_string);
19784 break;
19785 case VAR_LIST:
19786 list_unref(varp->vval.v_list);
19787 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019788 case VAR_DICT:
19789 dict_unref(varp->vval.v_dict);
19790 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019791 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019792#ifdef FEAT_FLOAT
19793 case VAR_FLOAT:
19794#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019795 case VAR_UNKNOWN:
19796 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019797 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019798 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799 break;
19800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 vim_free(varp);
19802 }
19803}
19804
19805/*
19806 * Free the memory for a variable value and set the value to NULL or 0.
19807 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019808 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811{
19812 if (varp != NULL)
19813 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019814 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019816 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019817 func_unref(varp->vval.v_string);
19818 /*FALLTHROUGH*/
19819 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019820 vim_free(varp->vval.v_string);
19821 varp->vval.v_string = NULL;
19822 break;
19823 case VAR_LIST:
19824 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019825 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019826 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019827 case VAR_DICT:
19828 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019829 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019830 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019832 varp->vval.v_number = 0;
19833 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019834#ifdef FEAT_FLOAT
19835 case VAR_FLOAT:
19836 varp->vval.v_float = 0.0;
19837 break;
19838#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839 case VAR_UNKNOWN:
19840 break;
19841 default:
19842 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019844 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 }
19846}
19847
19848/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849 * Set the value of a variable to NULL without freeing items.
19850 */
19851 static void
19852init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019854{
19855 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857}
19858
19859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 * Get the number value of a variable.
19861 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019862 * For incompatible types, return 0.
19863 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19864 * caller of incompatible types: it sets *denote to TRUE if "denote"
19865 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 */
19867 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019869 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019871 int error = FALSE;
19872
19873 return get_tv_number_chk(varp, &error); /* return 0L on error */
19874}
19875
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019876 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019877get_tv_number_chk(varp, denote)
19878 typval_T *varp;
19879 int *denote;
19880{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019881 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019883 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019885 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019886 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019887#ifdef FEAT_FLOAT
19888 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019889 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019890 break;
19891#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019892 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019893 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019894 break;
19895 case VAR_STRING:
19896 if (varp->vval.v_string != NULL)
19897 vim_str2nr(varp->vval.v_string, NULL, NULL,
19898 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019899 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019900 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019901 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019902 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019903 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019904 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019905 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019907 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019910 if (denote == NULL) /* useful for values that must be unsigned */
19911 n = -1;
19912 else
19913 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019914 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915}
19916
19917/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019918 * Get the lnum from the first argument.
19919 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019920 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 */
19922 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925{
Bram Moolenaar33570922005-01-25 22:26:29 +000019926 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 linenr_T lnum;
19928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019929 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 if (lnum == 0) /* no valid number, try using line() */
19931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 rettv.v_type = VAR_NUMBER;
19933 f_line(argvars, &rettv);
19934 lnum = rettv.vval.v_number;
19935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 }
19937 return lnum;
19938}
19939
19940/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019941 * Get the lnum from the first argument.
19942 * Also accepts "$", then "buf" is used.
19943 * Returns 0 on error.
19944 */
19945 static linenr_T
19946get_tv_lnum_buf(argvars, buf)
19947 typval_T *argvars;
19948 buf_T *buf;
19949{
19950 if (argvars[0].v_type == VAR_STRING
19951 && argvars[0].vval.v_string != NULL
19952 && argvars[0].vval.v_string[0] == '$'
19953 && buf != NULL)
19954 return buf->b_ml.ml_line_count;
19955 return get_tv_number_chk(&argvars[0], NULL);
19956}
19957
19958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 * Get the string value of a variable.
19960 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019961 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19962 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 * If the String variable has never been set, return an empty string.
19964 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019965 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19966 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 */
19968 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019970 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971{
19972 static char_u mybuf[NUMBUFLEN];
19973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975}
19976
19977 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019979 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019980 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019982 char_u *res = get_tv_string_buf_chk(varp, buf);
19983
19984 return res != NULL ? res : (char_u *)"";
19985}
19986
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019987 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019988get_tv_string_chk(varp)
19989 typval_T *varp;
19990{
19991 static char_u mybuf[NUMBUFLEN];
19992
19993 return get_tv_string_buf_chk(varp, mybuf);
19994}
19995
19996 static char_u *
19997get_tv_string_buf_chk(varp, buf)
19998 typval_T *varp;
19999 char_u *buf;
20000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 case VAR_NUMBER:
20004 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20005 return buf;
20006 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020007 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 break;
20009 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020010 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020011 break;
20012 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020013 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020014 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020015#ifdef FEAT_FLOAT
20016 case VAR_FLOAT:
20017 EMSG(_("E806: using Float as a String"));
20018 break;
20019#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020020 case VAR_STRING:
20021 if (varp->vval.v_string != NULL)
20022 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020023 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020024 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020026 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020028 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029}
20030
20031/*
20032 * Find variable "name" in the list of variables.
20033 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020034 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020035 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020039find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020044 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
Bram Moolenaara7043832005-01-21 11:56:39 +000020046 ht = find_var_ht(name, &varname);
20047 if (htp != NULL)
20048 *htp = ht;
20049 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020051 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052}
20053
20054/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020055 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020056 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020059find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020061 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020062 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020063 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020064{
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 hashitem_T *hi;
20066
20067 if (*varname == NUL)
20068 {
20069 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020070 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020072 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 case 'g': return &globvars_var;
20074 case 'v': return &vimvars_var;
20075 case 'b': return &curbuf->b_bufvar;
20076 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020077#ifdef FEAT_WINDOWS
20078 case 't': return &curtab->tp_winvar;
20079#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020080 case 'l': return current_funccal == NULL
20081 ? NULL : &current_funccal->l_vars_var;
20082 case 'a': return current_funccal == NULL
20083 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020084 }
20085 return NULL;
20086 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020087
20088 hi = hash_find(ht, varname);
20089 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020090 {
20091 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020092 * worked find the variable again. Don't auto-load a script if it was
20093 * loaded already, otherwise it would be loaded every time when
20094 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020095 if (ht == &globvarht && !writing)
20096 {
20097 /* Note: script_autoload() may make "hi" invalid. It must either
20098 * be obtained again or not used. */
20099 if (!script_autoload(varname, FALSE) || aborting())
20100 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020101 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020102 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020103 if (HASHITEM_EMPTY(hi))
20104 return NULL;
20105 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020107}
20108
20109/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020111 * Set "varname" to the start of name without ':'.
20112 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020113 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020114find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 char_u *name;
20116 char_u **varname;
20117{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020118 hashitem_T *hi;
20119
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 if (name[1] != ':')
20121 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020122 /* The name must not start with a colon or #. */
20123 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 return NULL;
20125 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020126
20127 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020128 hi = hash_find(&compat_hashtab, name);
20129 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020130 return &compat_hashtab;
20131
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 return &globvarht; /* global variable */
20134 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 }
20136 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020137 if (*name == 'g') /* global variable */
20138 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020139 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20140 */
20141 if (vim_strchr(name + 2, ':') != NULL
20142 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020143 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020145 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020147 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020148#ifdef FEAT_WINDOWS
20149 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020150 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020151#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 if (*name == 'v') /* v: variable */
20153 return &vimvarht;
20154 if (*name == 'a' && current_funccal != NULL) /* function argument */
20155 return &current_funccal->l_avars.dv_hashtab;
20156 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20157 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 if (*name == 's' /* script variable */
20159 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20160 return &SCRIPT_VARS(current_SID);
20161 return NULL;
20162}
20163
20164/*
20165 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020166 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 * Returns NULL when it doesn't exist.
20168 */
20169 char_u *
20170get_var_value(name)
20171 char_u *name;
20172{
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174
Bram Moolenaara7043832005-01-21 11:56:39 +000020175 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 if (v == NULL)
20177 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179}
20180
20181/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 * sourcing this script and when executing functions defined in the script.
20184 */
20185 void
20186new_script_vars(id)
20187 scid_T id;
20188{
Bram Moolenaara7043832005-01-21 11:56:39 +000020189 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020190 hashtab_T *ht;
20191 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020192
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20194 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020195 /* Re-allocating ga_data means that an ht_array pointing to
20196 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020197 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020198 for (i = 1; i <= ga_scripts.ga_len; ++i)
20199 {
20200 ht = &SCRIPT_VARS(i);
20201 if (ht->ht_mask == HT_INIT_SIZE - 1)
20202 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020203 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020205 }
20206
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 while (ga_scripts.ga_len < id)
20208 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020209 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020210 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020211 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 }
20214 }
20215}
20216
20217/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020218 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20219 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 */
20221 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020222init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 dict_T *dict;
20224 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020225 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226{
Bram Moolenaar33570922005-01-25 22:26:29 +000020227 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020228 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020229 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020230 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020231 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 dict_var->di_tv.vval.v_dict = dict;
20233 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020234 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20236 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237}
20238
20239/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020240 * Unreference a dictionary initialized by init_var_dict().
20241 */
20242 void
20243unref_var_dict(dict)
20244 dict_T *dict;
20245{
20246 /* Now the dict needs to be freed if no one else is using it, go back to
20247 * normal reference counting. */
20248 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20249 dict_unref(dict);
20250}
20251
20252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 * Frees all allocated variables and the value they contain.
20255 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 */
20257 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020258vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 hashtab_T *ht;
20260{
20261 vars_clear_ext(ht, TRUE);
20262}
20263
20264/*
20265 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20266 */
20267 static void
20268vars_clear_ext(ht, free_val)
20269 hashtab_T *ht;
20270 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271{
Bram Moolenaara7043832005-01-21 11:56:39 +000020272 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020273 hashitem_T *hi;
20274 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275
Bram Moolenaar33570922005-01-25 22:26:29 +000020276 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020277 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020278 for (hi = ht->ht_array; todo > 0; ++hi)
20279 {
20280 if (!HASHITEM_EMPTY(hi))
20281 {
20282 --todo;
20283
Bram Moolenaar33570922005-01-25 22:26:29 +000020284 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020285 * ht_array might change then. hash_clear() takes care of it
20286 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 v = HI2DI(hi);
20288 if (free_val)
20289 clear_tv(&v->di_tv);
20290 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20291 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020292 }
20293 }
20294 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020295 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296}
20297
Bram Moolenaara7043832005-01-21 11:56:39 +000020298/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 * Delete a variable from hashtab "ht" at item "hi".
20300 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020303delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020304 hashtab_T *ht;
20305 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306{
Bram Moolenaar33570922005-01-25 22:26:29 +000020307 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020308
20309 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020310 clear_tv(&di->di_tv);
20311 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312}
20313
20314/*
20315 * List the value of one internal variable.
20316 */
20317 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020318list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020321 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020323 char_u *tofree;
20324 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020325 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020326
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020327 current_copyID += COPYID_INC;
20328 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020329 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020330 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020331 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332}
20333
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020335list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 char_u *prefix;
20337 char_u *name;
20338 int type;
20339 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020340 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341{
Bram Moolenaar31859182007-08-14 20:41:13 +000020342 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20343 msg_start();
20344 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 if (name != NULL) /* "a:" vars don't have a name stored */
20346 msg_puts(name);
20347 msg_putchar(' ');
20348 msg_advance(22);
20349 if (type == VAR_NUMBER)
20350 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020351 else if (type == VAR_FUNC)
20352 msg_putchar('*');
20353 else if (type == VAR_LIST)
20354 {
20355 msg_putchar('[');
20356 if (*string == '[')
20357 ++string;
20358 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020359 else if (type == VAR_DICT)
20360 {
20361 msg_putchar('{');
20362 if (*string == '{')
20363 ++string;
20364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 else
20366 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020367
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020369
20370 if (type == VAR_FUNC)
20371 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020372 if (*first)
20373 {
20374 msg_clr_eos();
20375 *first = FALSE;
20376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377}
20378
20379/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 * If the variable already exists, the value is updated.
20382 * Otherwise the variable is created.
20383 */
20384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020385set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020388 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389{
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020394 ht = find_var_ht(name, &varname);
20395 if (ht == NULL || *varname == NUL)
20396 {
20397 EMSG2(_(e_illvar), name);
20398 return;
20399 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020400 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020401
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020402 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20403 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020404
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020408 if (var_check_ro(v->di_flags, name)
20409 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 return;
20411 if (v->di_tv.v_type != tv->v_type
20412 && !((v->di_tv.v_type == VAR_STRING
20413 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020415 || tv->v_type == VAR_NUMBER))
20416#ifdef FEAT_FLOAT
20417 && !((v->di_tv.v_type == VAR_NUMBER
20418 || v->di_tv.v_type == VAR_FLOAT)
20419 && (tv->v_type == VAR_NUMBER
20420 || tv->v_type == VAR_FLOAT))
20421#endif
20422 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020423 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020424 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020425 return;
20426 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020427
20428 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020429 * Handle setting internal v: variables separately: we don't change
20430 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020431 */
20432 if (ht == &vimvarht)
20433 {
20434 if (v->di_tv.v_type == VAR_STRING)
20435 {
20436 vim_free(v->di_tv.vval.v_string);
20437 if (copy || tv->v_type != VAR_STRING)
20438 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20439 else
20440 {
20441 /* Take over the string to avoid an extra alloc/free. */
20442 v->di_tv.vval.v_string = tv->vval.v_string;
20443 tv->vval.v_string = NULL;
20444 }
20445 }
20446 else if (v->di_tv.v_type != VAR_NUMBER)
20447 EMSG2(_(e_intern2), "set_var()");
20448 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020449 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020451 if (STRCMP(varname, "searchforward") == 0)
20452 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20453 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 return;
20455 }
20456
20457 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 }
20459 else /* add a new variable */
20460 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020461 /* Can't add "v:" variable. */
20462 if (ht == &vimvarht)
20463 {
20464 EMSG2(_(e_illvar), name);
20465 return;
20466 }
20467
Bram Moolenaar92124a32005-06-17 22:03:40 +000020468 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020469 if (!valid_varname(varname))
20470 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020471
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020472 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20473 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020474 if (v == NULL)
20475 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020476 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020479 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 return;
20481 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020482 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020484
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020485 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020486 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020487 else
20488 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020489 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020490 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493}
20494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020495/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020496 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020497 * Also give an error message.
20498 */
20499 static int
20500var_check_ro(flags, name)
20501 int flags;
20502 char_u *name;
20503{
20504 if (flags & DI_FLAGS_RO)
20505 {
20506 EMSG2(_(e_readonlyvar), name);
20507 return TRUE;
20508 }
20509 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20510 {
20511 EMSG2(_(e_readonlysbx), name);
20512 return TRUE;
20513 }
20514 return FALSE;
20515}
20516
20517/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020518 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20519 * Also give an error message.
20520 */
20521 static int
20522var_check_fixed(flags, name)
20523 int flags;
20524 char_u *name;
20525{
20526 if (flags & DI_FLAGS_FIX)
20527 {
20528 EMSG2(_("E795: Cannot delete variable %s"), name);
20529 return TRUE;
20530 }
20531 return FALSE;
20532}
20533
20534/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020535 * Check if a funcref is assigned to a valid variable name.
20536 * Return TRUE and give an error if not.
20537 */
20538 static int
20539var_check_func_name(name, new_var)
20540 char_u *name; /* points to start of variable name */
20541 int new_var; /* TRUE when creating the variable */
20542{
20543 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20544 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20545 ? name[2] : name[0]))
20546 {
20547 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20548 name);
20549 return TRUE;
20550 }
20551 /* Don't allow hiding a function. When "v" is not NULL we might be
20552 * assigning another function to the same var, the type is checked
20553 * below. */
20554 if (new_var && function_exists(name))
20555 {
20556 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20557 name);
20558 return TRUE;
20559 }
20560 return FALSE;
20561}
20562
20563/*
20564 * Check if a variable name is valid.
20565 * Return FALSE and give an error if not.
20566 */
20567 static int
20568valid_varname(varname)
20569 char_u *varname;
20570{
20571 char_u *p;
20572
20573 for (p = varname; *p != NUL; ++p)
20574 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20575 && *p != AUTOLOAD_CHAR)
20576 {
20577 EMSG2(_(e_illvar), varname);
20578 return FALSE;
20579 }
20580 return TRUE;
20581}
20582
20583/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020584 * Return TRUE if typeval "tv" is set to be locked (immutable).
20585 * Also give an error message, using "name".
20586 */
20587 static int
20588tv_check_lock(lock, name)
20589 int lock;
20590 char_u *name;
20591{
20592 if (lock & VAR_LOCKED)
20593 {
20594 EMSG2(_("E741: Value is locked: %s"),
20595 name == NULL ? (char_u *)_("Unknown") : name);
20596 return TRUE;
20597 }
20598 if (lock & VAR_FIXED)
20599 {
20600 EMSG2(_("E742: Cannot change value of %s"),
20601 name == NULL ? (char_u *)_("Unknown") : name);
20602 return TRUE;
20603 }
20604 return FALSE;
20605}
20606
20607/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020608 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020610 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020611 * It is OK for "from" and "to" to point to the same item. This is used to
20612 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020613 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020614 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020615copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020616 typval_T *from;
20617 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020619 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020620 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020621 switch (from->v_type)
20622 {
20623 case VAR_NUMBER:
20624 to->vval.v_number = from->vval.v_number;
20625 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020626#ifdef FEAT_FLOAT
20627 case VAR_FLOAT:
20628 to->vval.v_float = from->vval.v_float;
20629 break;
20630#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020631 case VAR_STRING:
20632 case VAR_FUNC:
20633 if (from->vval.v_string == NULL)
20634 to->vval.v_string = NULL;
20635 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020637 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020638 if (from->v_type == VAR_FUNC)
20639 func_ref(to->vval.v_string);
20640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020641 break;
20642 case VAR_LIST:
20643 if (from->vval.v_list == NULL)
20644 to->vval.v_list = NULL;
20645 else
20646 {
20647 to->vval.v_list = from->vval.v_list;
20648 ++to->vval.v_list->lv_refcount;
20649 }
20650 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020651 case VAR_DICT:
20652 if (from->vval.v_dict == NULL)
20653 to->vval.v_dict = NULL;
20654 else
20655 {
20656 to->vval.v_dict = from->vval.v_dict;
20657 ++to->vval.v_dict->dv_refcount;
20658 }
20659 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020660 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020662 break;
20663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664}
20665
20666/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020667 * Make a copy of an item.
20668 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020669 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20670 * reference to an already copied list/dict can be used.
20671 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020673 static int
20674item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020675 typval_T *from;
20676 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020677 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020678 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020679{
20680 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020681 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020682
Bram Moolenaar33570922005-01-25 22:26:29 +000020683 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684 {
20685 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020686 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687 }
20688 ++recurse;
20689
20690 switch (from->v_type)
20691 {
20692 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020693#ifdef FEAT_FLOAT
20694 case VAR_FLOAT:
20695#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020696 case VAR_STRING:
20697 case VAR_FUNC:
20698 copy_tv(from, to);
20699 break;
20700 case VAR_LIST:
20701 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020702 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020703 if (from->vval.v_list == NULL)
20704 to->vval.v_list = NULL;
20705 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20706 {
20707 /* use the copy made earlier */
20708 to->vval.v_list = from->vval.v_list->lv_copylist;
20709 ++to->vval.v_list->lv_refcount;
20710 }
20711 else
20712 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20713 if (to->vval.v_list == NULL)
20714 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020715 break;
20716 case VAR_DICT:
20717 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020718 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020719 if (from->vval.v_dict == NULL)
20720 to->vval.v_dict = NULL;
20721 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20722 {
20723 /* use the copy made earlier */
20724 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20725 ++to->vval.v_dict->dv_refcount;
20726 }
20727 else
20728 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20729 if (to->vval.v_dict == NULL)
20730 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020731 break;
20732 default:
20733 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020734 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020735 }
20736 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020737 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020738}
20739
20740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 * ":echo expr1 ..." print each argument separated with a space, add a
20742 * newline at the end.
20743 * ":echon expr1 ..." print each argument plain.
20744 */
20745 void
20746ex_echo(eap)
20747 exarg_T *eap;
20748{
20749 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020751 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 char_u *p;
20753 int needclr = TRUE;
20754 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020755 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756
20757 if (eap->skip)
20758 ++emsg_skip;
20759 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20760 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020761 /* If eval1() causes an error message the text from the command may
20762 * still need to be cleared. E.g., "echo 22,44". */
20763 need_clr_eos = needclr;
20764
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020766 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 {
20768 /*
20769 * Report the invalid expression unless the expression evaluation
20770 * has been cancelled due to an aborting error, an interrupt, or an
20771 * exception.
20772 */
20773 if (!aborting())
20774 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020775 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 break;
20777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020778 need_clr_eos = FALSE;
20779
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 if (!eap->skip)
20781 {
20782 if (atstart)
20783 {
20784 atstart = FALSE;
20785 /* Call msg_start() after eval1(), evaluating the expression
20786 * may cause a message to appear. */
20787 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020788 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020789 /* Mark the saved text as finishing the line, so that what
20790 * follows is displayed on a new line when scrolling back
20791 * at the more prompt. */
20792 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 }
20796 else if (eap->cmdidx == CMD_echo)
20797 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020798 current_copyID += COPYID_INC;
20799 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020800 if (p != NULL)
20801 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020803 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020805 if (*p != TAB && needclr)
20806 {
20807 /* remove any text still there from the command */
20808 msg_clr_eos();
20809 needclr = FALSE;
20810 }
20811 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 }
20813 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020814 {
20815#ifdef FEAT_MBYTE
20816 if (has_mbyte)
20817 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020818 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020819
20820 (void)msg_outtrans_len_attr(p, i, echo_attr);
20821 p += i - 1;
20822 }
20823 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020825 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020828 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020830 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 arg = skipwhite(arg);
20832 }
20833 eap->nextcmd = check_nextcmd(arg);
20834
20835 if (eap->skip)
20836 --emsg_skip;
20837 else
20838 {
20839 /* remove text that may still be there from the command */
20840 if (needclr)
20841 msg_clr_eos();
20842 if (eap->cmdidx == CMD_echo)
20843 msg_end();
20844 }
20845}
20846
20847/*
20848 * ":echohl {name}".
20849 */
20850 void
20851ex_echohl(eap)
20852 exarg_T *eap;
20853{
20854 int id;
20855
20856 id = syn_name2id(eap->arg);
20857 if (id == 0)
20858 echo_attr = 0;
20859 else
20860 echo_attr = syn_id2attr(id);
20861}
20862
20863/*
20864 * ":execute expr1 ..." execute the result of an expression.
20865 * ":echomsg expr1 ..." Print a message
20866 * ":echoerr expr1 ..." Print an error
20867 * Each gets spaces around each argument and a newline at the end for
20868 * echo commands
20869 */
20870 void
20871ex_execute(eap)
20872 exarg_T *eap;
20873{
20874 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020875 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 int ret = OK;
20877 char_u *p;
20878 garray_T ga;
20879 int len;
20880 int save_did_emsg;
20881
20882 ga_init2(&ga, 1, 80);
20883
20884 if (eap->skip)
20885 ++emsg_skip;
20886 while (*arg != NUL && *arg != '|' && *arg != '\n')
20887 {
20888 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020889 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 {
20891 /*
20892 * Report the invalid expression unless the expression evaluation
20893 * has been cancelled due to an aborting error, an interrupt, or an
20894 * exception.
20895 */
20896 if (!aborting())
20897 EMSG2(_(e_invexpr2), p);
20898 ret = FAIL;
20899 break;
20900 }
20901
20902 if (!eap->skip)
20903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 len = (int)STRLEN(p);
20906 if (ga_grow(&ga, len + 2) == FAIL)
20907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 ret = FAIL;
20910 break;
20911 }
20912 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 ga.ga_len += len;
20916 }
20917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020918 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 arg = skipwhite(arg);
20920 }
20921
20922 if (ret != FAIL && ga.ga_data != NULL)
20923 {
20924 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020927 out_flush();
20928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 else if (eap->cmdidx == CMD_echoerr)
20930 {
20931 /* We don't want to abort following commands, restore did_emsg. */
20932 save_did_emsg = did_emsg;
20933 EMSG((char_u *)ga.ga_data);
20934 if (!force_abort)
20935 did_emsg = save_did_emsg;
20936 }
20937 else if (eap->cmdidx == CMD_execute)
20938 do_cmdline((char_u *)ga.ga_data,
20939 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20940 }
20941
20942 ga_clear(&ga);
20943
20944 if (eap->skip)
20945 --emsg_skip;
20946
20947 eap->nextcmd = check_nextcmd(arg);
20948}
20949
20950/*
20951 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20952 * "arg" points to the "&" or '+' when called, to "option" when returning.
20953 * Returns NULL when no option name found. Otherwise pointer to the char
20954 * after the option name.
20955 */
20956 static char_u *
20957find_option_end(arg, opt_flags)
20958 char_u **arg;
20959 int *opt_flags;
20960{
20961 char_u *p = *arg;
20962
20963 ++p;
20964 if (*p == 'g' && p[1] == ':')
20965 {
20966 *opt_flags = OPT_GLOBAL;
20967 p += 2;
20968 }
20969 else if (*p == 'l' && p[1] == ':')
20970 {
20971 *opt_flags = OPT_LOCAL;
20972 p += 2;
20973 }
20974 else
20975 *opt_flags = 0;
20976
20977 if (!ASCII_ISALPHA(*p))
20978 return NULL;
20979 *arg = p;
20980
20981 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20982 p += 4; /* termcap option */
20983 else
20984 while (ASCII_ISALPHA(*p))
20985 ++p;
20986 return p;
20987}
20988
20989/*
20990 * ":function"
20991 */
20992 void
20993ex_function(eap)
20994 exarg_T *eap;
20995{
20996 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020997 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 int j;
20999 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 char_u *name = NULL;
21002 char_u *p;
21003 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021004 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 garray_T newargs;
21006 garray_T newlines;
21007 int varargs = FALSE;
21008 int mustend = FALSE;
21009 int flags = 0;
21010 ufunc_T *fp;
21011 int indent;
21012 int nesting;
21013 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 dictitem_T *v;
21015 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016 static int func_nr = 0; /* number for nameless function */
21017 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021018 hashtab_T *ht;
21019 int todo;
21020 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021021 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022
21023 /*
21024 * ":function" without argument: list functions.
21025 */
21026 if (ends_excmd(*eap->arg))
21027 {
21028 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021030 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021031 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021032 {
21033 if (!HASHITEM_EMPTY(hi))
21034 {
21035 --todo;
21036 fp = HI2UF(hi);
21037 if (!isdigit(*fp->uf_name))
21038 list_func_head(fp, FALSE);
21039 }
21040 }
21041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 eap->nextcmd = check_nextcmd(eap->arg);
21043 return;
21044 }
21045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021046 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021047 * ":function /pat": list functions matching pattern.
21048 */
21049 if (*eap->arg == '/')
21050 {
21051 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21052 if (!eap->skip)
21053 {
21054 regmatch_T regmatch;
21055
21056 c = *p;
21057 *p = NUL;
21058 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21059 *p = c;
21060 if (regmatch.regprog != NULL)
21061 {
21062 regmatch.rm_ic = p_ic;
21063
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021064 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021065 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21066 {
21067 if (!HASHITEM_EMPTY(hi))
21068 {
21069 --todo;
21070 fp = HI2UF(hi);
21071 if (!isdigit(*fp->uf_name)
21072 && vim_regexec(&regmatch, fp->uf_name, 0))
21073 list_func_head(fp, FALSE);
21074 }
21075 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021076 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021077 }
21078 }
21079 if (*p == '/')
21080 ++p;
21081 eap->nextcmd = check_nextcmd(p);
21082 return;
21083 }
21084
21085 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021086 * Get the function name. There are these situations:
21087 * func normal function name
21088 * "name" == func, "fudi.fd_dict" == NULL
21089 * dict.func new dictionary entry
21090 * "name" == NULL, "fudi.fd_dict" set,
21091 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21092 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021093 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021094 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21095 * dict.func existing dict entry that's not a Funcref
21096 * "name" == NULL, "fudi.fd_dict" set,
21097 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021100 name = trans_function_name(&p, eap->skip, 0, &fudi);
21101 paren = (vim_strchr(p, '(') != NULL);
21102 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 {
21104 /*
21105 * Return on an invalid expression in braces, unless the expression
21106 * evaluation has been cancelled due to an aborting error, an
21107 * interrupt, or an exception.
21108 */
21109 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021111 if (!eap->skip && fudi.fd_newkey != NULL)
21112 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021113 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 else
21117 eap->skip = TRUE;
21118 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021119
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 /* An error in a function call during evaluation of an expression in magic
21121 * braces should not cause the function not to be defined. */
21122 saved_did_emsg = did_emsg;
21123 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124
21125 /*
21126 * ":function func" with only function name: list function.
21127 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021128 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 {
21130 if (!ends_excmd(*skipwhite(p)))
21131 {
21132 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021133 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 }
21135 eap->nextcmd = check_nextcmd(p);
21136 if (eap->nextcmd != NULL)
21137 *p = NUL;
21138 if (!eap->skip && !got_int)
21139 {
21140 fp = find_func(name);
21141 if (fp != NULL)
21142 {
21143 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021144 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021146 if (FUNCLINE(fp, j) == NULL)
21147 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 msg_putchar('\n');
21149 msg_outnum((long)(j + 1));
21150 if (j < 9)
21151 msg_putchar(' ');
21152 if (j < 99)
21153 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021154 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 out_flush(); /* show a line at a time */
21156 ui_breakcheck();
21157 }
21158 if (!got_int)
21159 {
21160 msg_putchar('\n');
21161 msg_puts((char_u *)" endfunction");
21162 }
21163 }
21164 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021165 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021167 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 }
21169
21170 /*
21171 * ":function name(arg1, arg2)" Define function.
21172 */
21173 p = skipwhite(p);
21174 if (*p != '(')
21175 {
21176 if (!eap->skip)
21177 {
21178 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021179 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 }
21181 /* attempt to continue by skipping some text */
21182 if (vim_strchr(p, '(') != NULL)
21183 p = vim_strchr(p, '(');
21184 }
21185 p = skipwhite(p + 1);
21186
21187 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21188 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21189
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021190 if (!eap->skip)
21191 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021192 /* Check the name of the function. Unless it's a dictionary function
21193 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021194 if (name != NULL)
21195 arg = name;
21196 else
21197 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021198 if (arg != NULL && (fudi.fd_di == NULL
21199 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021200 {
21201 if (*arg == K_SPECIAL)
21202 j = 3;
21203 else
21204 j = 0;
21205 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21206 : eval_isnamec(arg[j])))
21207 ++j;
21208 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021209 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021210 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021211 /* Disallow using the g: dict. */
21212 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21213 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021214 }
21215
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 /*
21217 * Isolate the arguments: "arg1, arg2, ...)"
21218 */
21219 while (*p != ')')
21220 {
21221 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21222 {
21223 varargs = TRUE;
21224 p += 3;
21225 mustend = TRUE;
21226 }
21227 else
21228 {
21229 arg = p;
21230 while (ASCII_ISALNUM(*p) || *p == '_')
21231 ++p;
21232 if (arg == p || isdigit(*arg)
21233 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21234 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21235 {
21236 if (!eap->skip)
21237 EMSG2(_("E125: Illegal argument: %s"), arg);
21238 break;
21239 }
21240 if (ga_grow(&newargs, 1) == FAIL)
21241 goto erret;
21242 c = *p;
21243 *p = NUL;
21244 arg = vim_strsave(arg);
21245 if (arg == NULL)
21246 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021247
21248 /* Check for duplicate argument name. */
21249 for (i = 0; i < newargs.ga_len; ++i)
21250 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21251 {
21252 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21253 goto erret;
21254 }
21255
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21257 *p = c;
21258 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 if (*p == ',')
21260 ++p;
21261 else
21262 mustend = TRUE;
21263 }
21264 p = skipwhite(p);
21265 if (mustend && *p != ')')
21266 {
21267 if (!eap->skip)
21268 EMSG2(_(e_invarg2), eap->arg);
21269 break;
21270 }
21271 }
21272 ++p; /* skip the ')' */
21273
Bram Moolenaare9a41262005-01-15 22:18:47 +000021274 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 for (;;)
21276 {
21277 p = skipwhite(p);
21278 if (STRNCMP(p, "range", 5) == 0)
21279 {
21280 flags |= FC_RANGE;
21281 p += 5;
21282 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021283 else if (STRNCMP(p, "dict", 4) == 0)
21284 {
21285 flags |= FC_DICT;
21286 p += 4;
21287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288 else if (STRNCMP(p, "abort", 5) == 0)
21289 {
21290 flags |= FC_ABORT;
21291 p += 5;
21292 }
21293 else
21294 break;
21295 }
21296
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021297 /* When there is a line break use what follows for the function body.
21298 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21299 if (*p == '\n')
21300 line_arg = p + 1;
21301 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 EMSG(_(e_trailing));
21303
21304 /*
21305 * Read the body of the function, until ":endfunction" is found.
21306 */
21307 if (KeyTyped)
21308 {
21309 /* Check if the function already exists, don't let the user type the
21310 * whole function before telling him it doesn't work! For a script we
21311 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021312 if (!eap->skip && !eap->forceit)
21313 {
21314 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21315 EMSG(_(e_funcdict));
21316 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021317 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021320 if (!eap->skip && did_emsg)
21321 goto erret;
21322
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 msg_putchar('\n'); /* don't overwrite the function name */
21324 cmdline_row = msg_row;
21325 }
21326
21327 indent = 2;
21328 nesting = 0;
21329 for (;;)
21330 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021331 if (KeyTyped)
21332 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021334 sourcing_lnum_off = sourcing_lnum;
21335
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021336 if (line_arg != NULL)
21337 {
21338 /* Use eap->arg, split up in parts by line breaks. */
21339 theline = line_arg;
21340 p = vim_strchr(theline, '\n');
21341 if (p == NULL)
21342 line_arg += STRLEN(line_arg);
21343 else
21344 {
21345 *p = NUL;
21346 line_arg = p + 1;
21347 }
21348 }
21349 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 theline = getcmdline(':', 0L, indent);
21351 else
21352 theline = eap->getline(':', eap->cookie, indent);
21353 if (KeyTyped)
21354 lines_left = Rows - 1;
21355 if (theline == NULL)
21356 {
21357 EMSG(_("E126: Missing :endfunction"));
21358 goto erret;
21359 }
21360
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021361 /* Detect line continuation: sourcing_lnum increased more than one. */
21362 if (sourcing_lnum > sourcing_lnum_off + 1)
21363 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21364 else
21365 sourcing_lnum_off = 0;
21366
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 if (skip_until != NULL)
21368 {
21369 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21370 * don't check for ":endfunc". */
21371 if (STRCMP(theline, skip_until) == 0)
21372 {
21373 vim_free(skip_until);
21374 skip_until = NULL;
21375 }
21376 }
21377 else
21378 {
21379 /* skip ':' and blanks*/
21380 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21381 ;
21382
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021383 /* Check for "endfunction". */
21384 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021386 if (line_arg == NULL)
21387 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 break;
21389 }
21390
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021391 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 * at "end". */
21393 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21394 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021395 else if (STRNCMP(p, "if", 2) == 0
21396 || STRNCMP(p, "wh", 2) == 0
21397 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 || STRNCMP(p, "try", 3) == 0)
21399 indent += 2;
21400
21401 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021402 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021404 if (*p == '!')
21405 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 p += eval_fname_script(p);
21407 if (ASCII_ISALPHA(*p))
21408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021409 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 if (*skipwhite(p) == '(')
21411 {
21412 ++nesting;
21413 indent += 2;
21414 }
21415 }
21416 }
21417
21418 /* Check for ":append" or ":insert". */
21419 p = skip_range(p, NULL);
21420 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21421 || (p[0] == 'i'
21422 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21423 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21424 skip_until = vim_strsave((char_u *)".");
21425
21426 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21427 arg = skipwhite(skiptowhite(p));
21428 if (arg[0] == '<' && arg[1] =='<'
21429 && ((p[0] == 'p' && p[1] == 'y'
21430 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21431 || (p[0] == 'p' && p[1] == 'e'
21432 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21433 || (p[0] == 't' && p[1] == 'c'
21434 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021435 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21436 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21438 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021439 || (p[0] == 'm' && p[1] == 'z'
21440 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 ))
21442 {
21443 /* ":python <<" continues until a dot, like ":append" */
21444 p = skipwhite(arg + 2);
21445 if (*p == NUL)
21446 skip_until = vim_strsave((char_u *)".");
21447 else
21448 skip_until = vim_strsave(p);
21449 }
21450 }
21451
21452 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021453 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021454 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021455 if (line_arg == NULL)
21456 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021458 }
21459
21460 /* Copy the line to newly allocated memory. get_one_sourceline()
21461 * allocates 250 bytes per line, this saves 80% on average. The cost
21462 * is an extra alloc/free. */
21463 p = vim_strsave(theline);
21464 if (p != NULL)
21465 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021466 if (line_arg == NULL)
21467 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021468 theline = p;
21469 }
21470
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021471 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21472
21473 /* Add NULL lines for continuation lines, so that the line count is
21474 * equal to the index in the growarray. */
21475 while (sourcing_lnum_off-- > 0)
21476 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021477
21478 /* Check for end of eap->arg. */
21479 if (line_arg != NULL && *line_arg == NUL)
21480 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 }
21482
21483 /* Don't define the function when skipping commands or when an error was
21484 * detected. */
21485 if (eap->skip || did_emsg)
21486 goto erret;
21487
21488 /*
21489 * If there are no errors, add the function
21490 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021491 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021492 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021493 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021496 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021497 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498 goto erret;
21499 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021500
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021501 fp = find_func(name);
21502 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504 if (!eap->forceit)
21505 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021506 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021507 goto erret;
21508 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021509 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021510 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021511 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 name);
21513 goto erret;
21514 }
21515 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021516 ga_clear_strings(&(fp->uf_args));
21517 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021518 vim_free(name);
21519 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 }
21522 else
21523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021524 char numbuf[20];
21525
21526 fp = NULL;
21527 if (fudi.fd_newkey == NULL && !eap->forceit)
21528 {
21529 EMSG(_(e_funcdict));
21530 goto erret;
21531 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021532 if (fudi.fd_di == NULL)
21533 {
21534 /* Can't add a function to a locked dictionary */
21535 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21536 goto erret;
21537 }
21538 /* Can't change an existing function if it is locked */
21539 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21540 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021541
21542 /* Give the function a sequential number. Can only be used with a
21543 * Funcref! */
21544 vim_free(name);
21545 sprintf(numbuf, "%d", ++func_nr);
21546 name = vim_strsave((char_u *)numbuf);
21547 if (name == NULL)
21548 goto erret;
21549 }
21550
21551 if (fp == NULL)
21552 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021553 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021554 {
21555 int slen, plen;
21556 char_u *scriptname;
21557
21558 /* Check that the autoload name matches the script name. */
21559 j = FAIL;
21560 if (sourcing_name != NULL)
21561 {
21562 scriptname = autoload_name(name);
21563 if (scriptname != NULL)
21564 {
21565 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021566 plen = (int)STRLEN(p);
21567 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021568 if (slen > plen && fnamecmp(p,
21569 sourcing_name + slen - plen) == 0)
21570 j = OK;
21571 vim_free(scriptname);
21572 }
21573 }
21574 if (j == FAIL)
21575 {
21576 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21577 goto erret;
21578 }
21579 }
21580
21581 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 if (fp == NULL)
21583 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584
21585 if (fudi.fd_dict != NULL)
21586 {
21587 if (fudi.fd_di == NULL)
21588 {
21589 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021590 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591 if (fudi.fd_di == NULL)
21592 {
21593 vim_free(fp);
21594 goto erret;
21595 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021596 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21597 {
21598 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021599 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021600 goto erret;
21601 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021602 }
21603 else
21604 /* overwrite existing dict entry */
21605 clear_tv(&fudi.fd_di->di_tv);
21606 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021607 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021609 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021610
21611 /* behave like "dict" was used */
21612 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 }
21614
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021616 STRCPY(fp->uf_name, name);
21617 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021619 fp->uf_args = newargs;
21620 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021621#ifdef FEAT_PROFILE
21622 fp->uf_tml_count = NULL;
21623 fp->uf_tml_total = NULL;
21624 fp->uf_tml_self = NULL;
21625 fp->uf_profiling = FALSE;
21626 if (prof_def_func())
21627 func_do_profile(fp);
21628#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021629 fp->uf_varargs = varargs;
21630 fp->uf_flags = flags;
21631 fp->uf_calls = 0;
21632 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634
21635erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 ga_clear_strings(&newargs);
21637 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021638ret_free:
21639 vim_free(skip_until);
21640 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643}
21644
21645/*
21646 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021647 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649 * flags:
21650 * TFN_INT: internal function name OK
21651 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 * Advances "pp" to just after the function name (if no error).
21653 */
21654 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021655trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 char_u **pp;
21657 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021658 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021661 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 char_u *start;
21663 char_u *end;
21664 int lead;
21665 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021668
21669 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021670 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021672
21673 /* Check for hard coded <SNR>: already translated function ID (from a user
21674 * command). */
21675 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21676 && (*pp)[2] == (int)KE_SNR)
21677 {
21678 *pp += 3;
21679 len = get_id_len(pp) + 3;
21680 return vim_strnsave(start, len);
21681 }
21682
21683 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21684 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021686 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021688
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021689 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21690 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021691 if (end == start)
21692 {
21693 if (!skip)
21694 EMSG(_("E129: Function name required"));
21695 goto theend;
21696 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021697 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021698 {
21699 /*
21700 * Report an invalid expression in braces, unless the expression
21701 * evaluation has been cancelled due to an aborting error, an
21702 * interrupt, or an exception.
21703 */
21704 if (!aborting())
21705 {
21706 if (end != NULL)
21707 EMSG2(_(e_invarg2), start);
21708 }
21709 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021710 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021711 goto theend;
21712 }
21713
21714 if (lv.ll_tv != NULL)
21715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021716 if (fdp != NULL)
21717 {
21718 fdp->fd_dict = lv.ll_dict;
21719 fdp->fd_newkey = lv.ll_newkey;
21720 lv.ll_newkey = NULL;
21721 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021723 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21724 {
21725 name = vim_strsave(lv.ll_tv->vval.v_string);
21726 *pp = end;
21727 }
21728 else
21729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021730 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21731 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021732 EMSG(_(e_funcref));
21733 else
21734 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021735 name = NULL;
21736 }
21737 goto theend;
21738 }
21739
21740 if (lv.ll_name == NULL)
21741 {
21742 /* Error found, but continue after the function name. */
21743 *pp = end;
21744 goto theend;
21745 }
21746
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021747 /* Check if the name is a Funcref. If so, use the value. */
21748 if (lv.ll_exp_name != NULL)
21749 {
21750 len = (int)STRLEN(lv.ll_exp_name);
21751 name = deref_func_name(lv.ll_exp_name, &len);
21752 if (name == lv.ll_exp_name)
21753 name = NULL;
21754 }
21755 else
21756 {
21757 len = (int)(end - *pp);
21758 name = deref_func_name(*pp, &len);
21759 if (name == *pp)
21760 name = NULL;
21761 }
21762 if (name != NULL)
21763 {
21764 name = vim_strsave(name);
21765 *pp = end;
21766 goto theend;
21767 }
21768
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021769 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021770 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021771 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021772 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21773 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21774 {
21775 /* When there was "s:" already or the name expanded to get a
21776 * leading "s:" then remove it. */
21777 lv.ll_name += 2;
21778 len -= 2;
21779 lead = 2;
21780 }
21781 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021782 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021783 {
21784 if (lead == 2) /* skip over "s:" */
21785 lv.ll_name += 2;
21786 len = (int)(end - lv.ll_name);
21787 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021788
21789 /*
21790 * Copy the function name to allocated memory.
21791 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21792 * Accept <SNR>123_name() outside a script.
21793 */
21794 if (skip)
21795 lead = 0; /* do nothing */
21796 else if (lead > 0)
21797 {
21798 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021799 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21800 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021801 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021802 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021803 if (current_SID <= 0)
21804 {
21805 EMSG(_(e_usingsid));
21806 goto theend;
21807 }
21808 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21809 lead += (int)STRLEN(sid_buf);
21810 }
21811 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021812 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021813 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021814 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021815 goto theend;
21816 }
21817 name = alloc((unsigned)(len + lead + 1));
21818 if (name != NULL)
21819 {
21820 if (lead > 0)
21821 {
21822 name[0] = K_SPECIAL;
21823 name[1] = KS_EXTRA;
21824 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021825 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021826 STRCPY(name + 3, sid_buf);
21827 }
21828 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21829 name[len + lead] = NUL;
21830 }
21831 *pp = end;
21832
21833theend:
21834 clear_lval(&lv);
21835 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836}
21837
21838/*
21839 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21840 * Return 2 if "p" starts with "s:".
21841 * Return 0 otherwise.
21842 */
21843 static int
21844eval_fname_script(p)
21845 char_u *p;
21846{
21847 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21848 || STRNICMP(p + 1, "SNR>", 4) == 0))
21849 return 5;
21850 if (p[0] == 's' && p[1] == ':')
21851 return 2;
21852 return 0;
21853}
21854
21855/*
21856 * Return TRUE if "p" starts with "<SID>" or "s:".
21857 * Only works if eval_fname_script() returned non-zero for "p"!
21858 */
21859 static int
21860eval_fname_sid(p)
21861 char_u *p;
21862{
21863 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21864}
21865
21866/*
21867 * List the head of the function: "name(arg1, arg2)".
21868 */
21869 static void
21870list_func_head(fp, indent)
21871 ufunc_T *fp;
21872 int indent;
21873{
21874 int j;
21875
21876 msg_start();
21877 if (indent)
21878 MSG_PUTS(" ");
21879 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021880 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 {
21882 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021883 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 }
21885 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021886 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021888 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 {
21890 if (j)
21891 MSG_PUTS(", ");
21892 msg_puts(FUNCARG(fp, j));
21893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021894 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 {
21896 if (j)
21897 MSG_PUTS(", ");
21898 MSG_PUTS("...");
21899 }
21900 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021901 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021902 if (p_verbose > 0)
21903 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904}
21905
21906/*
21907 * Find a function by name, return pointer to it in ufuncs.
21908 * Return NULL for unknown function.
21909 */
21910 static ufunc_T *
21911find_func(name)
21912 char_u *name;
21913{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021914 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021916 hi = hash_find(&func_hashtab, name);
21917 if (!HASHITEM_EMPTY(hi))
21918 return HI2UF(hi);
21919 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920}
21921
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021922#if defined(EXITFREE) || defined(PROTO)
21923 void
21924free_all_functions()
21925{
21926 hashitem_T *hi;
21927
21928 /* Need to start all over every time, because func_free() may change the
21929 * hash table. */
21930 while (func_hashtab.ht_used > 0)
21931 for (hi = func_hashtab.ht_array; ; ++hi)
21932 if (!HASHITEM_EMPTY(hi))
21933 {
21934 func_free(HI2UF(hi));
21935 break;
21936 }
21937}
21938#endif
21939
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021940/*
21941 * Return TRUE if a function "name" exists.
21942 */
21943 static int
21944function_exists(name)
21945 char_u *name;
21946{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021947 char_u *nm = name;
21948 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021949 int n = FALSE;
21950
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021951 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021952 nm = skipwhite(nm);
21953
21954 /* Only accept "funcname", "funcname ", "funcname (..." and
21955 * "funcname(...", not "funcname!...". */
21956 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021958 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021959 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021960 else
21961 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021962 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021963 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021964 return n;
21965}
21966
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021967/*
21968 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021969 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021970 */
21971 static int
21972builtin_function(name)
21973 char_u *name;
21974{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021975 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21976 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021977}
21978
Bram Moolenaar05159a02005-02-26 23:04:13 +000021979#if defined(FEAT_PROFILE) || defined(PROTO)
21980/*
21981 * Start profiling function "fp".
21982 */
21983 static void
21984func_do_profile(fp)
21985 ufunc_T *fp;
21986{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021987 int len = fp->uf_lines.ga_len;
21988
21989 if (len == 0)
21990 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021991 fp->uf_tm_count = 0;
21992 profile_zero(&fp->uf_tm_self);
21993 profile_zero(&fp->uf_tm_total);
21994 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021995 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996 if (fp->uf_tml_total == NULL)
21997 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021998 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021999 if (fp->uf_tml_self == NULL)
22000 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022001 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022002 fp->uf_tml_idx = -1;
22003 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22004 || fp->uf_tml_self == NULL)
22005 return; /* out of memory */
22006
22007 fp->uf_profiling = TRUE;
22008}
22009
22010/*
22011 * Dump the profiling results for all functions in file "fd".
22012 */
22013 void
22014func_dump_profile(fd)
22015 FILE *fd;
22016{
22017 hashitem_T *hi;
22018 int todo;
22019 ufunc_T *fp;
22020 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022021 ufunc_T **sorttab;
22022 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022023
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022024 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022025 if (todo == 0)
22026 return; /* nothing to dump */
22027
Bram Moolenaar73830342005-02-28 22:48:19 +000022028 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22029
Bram Moolenaar05159a02005-02-26 23:04:13 +000022030 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22031 {
22032 if (!HASHITEM_EMPTY(hi))
22033 {
22034 --todo;
22035 fp = HI2UF(hi);
22036 if (fp->uf_profiling)
22037 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022038 if (sorttab != NULL)
22039 sorttab[st_len++] = fp;
22040
Bram Moolenaar05159a02005-02-26 23:04:13 +000022041 if (fp->uf_name[0] == K_SPECIAL)
22042 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22043 else
22044 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22045 if (fp->uf_tm_count == 1)
22046 fprintf(fd, "Called 1 time\n");
22047 else
22048 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22049 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22050 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22051 fprintf(fd, "\n");
22052 fprintf(fd, "count total (s) self (s)\n");
22053
22054 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22055 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022056 if (FUNCLINE(fp, i) == NULL)
22057 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022058 prof_func_line(fd, fp->uf_tml_count[i],
22059 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022060 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22061 }
22062 fprintf(fd, "\n");
22063 }
22064 }
22065 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022066
22067 if (sorttab != NULL && st_len > 0)
22068 {
22069 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22070 prof_total_cmp);
22071 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22072 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22073 prof_self_cmp);
22074 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22075 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022076
22077 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022078}
Bram Moolenaar73830342005-02-28 22:48:19 +000022079
22080 static void
22081prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22082 FILE *fd;
22083 ufunc_T **sorttab;
22084 int st_len;
22085 char *title;
22086 int prefer_self; /* when equal print only self time */
22087{
22088 int i;
22089 ufunc_T *fp;
22090
22091 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22092 fprintf(fd, "count total (s) self (s) function\n");
22093 for (i = 0; i < 20 && i < st_len; ++i)
22094 {
22095 fp = sorttab[i];
22096 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22097 prefer_self);
22098 if (fp->uf_name[0] == K_SPECIAL)
22099 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22100 else
22101 fprintf(fd, " %s()\n", fp->uf_name);
22102 }
22103 fprintf(fd, "\n");
22104}
22105
22106/*
22107 * Print the count and times for one function or function line.
22108 */
22109 static void
22110prof_func_line(fd, count, total, self, prefer_self)
22111 FILE *fd;
22112 int count;
22113 proftime_T *total;
22114 proftime_T *self;
22115 int prefer_self; /* when equal print only self time */
22116{
22117 if (count > 0)
22118 {
22119 fprintf(fd, "%5d ", count);
22120 if (prefer_self && profile_equal(total, self))
22121 fprintf(fd, " ");
22122 else
22123 fprintf(fd, "%s ", profile_msg(total));
22124 if (!prefer_self && profile_equal(total, self))
22125 fprintf(fd, " ");
22126 else
22127 fprintf(fd, "%s ", profile_msg(self));
22128 }
22129 else
22130 fprintf(fd, " ");
22131}
22132
22133/*
22134 * Compare function for total time sorting.
22135 */
22136 static int
22137#ifdef __BORLANDC__
22138_RTLENTRYF
22139#endif
22140prof_total_cmp(s1, s2)
22141 const void *s1;
22142 const void *s2;
22143{
22144 ufunc_T *p1, *p2;
22145
22146 p1 = *(ufunc_T **)s1;
22147 p2 = *(ufunc_T **)s2;
22148 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22149}
22150
22151/*
22152 * Compare function for self time sorting.
22153 */
22154 static int
22155#ifdef __BORLANDC__
22156_RTLENTRYF
22157#endif
22158prof_self_cmp(s1, s2)
22159 const void *s1;
22160 const void *s2;
22161{
22162 ufunc_T *p1, *p2;
22163
22164 p1 = *(ufunc_T **)s1;
22165 p2 = *(ufunc_T **)s2;
22166 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22167}
22168
Bram Moolenaar05159a02005-02-26 23:04:13 +000022169#endif
22170
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022171/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022172 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022173 * Return TRUE if a package was loaded.
22174 */
22175 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022176script_autoload(name, reload)
22177 char_u *name;
22178 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022179{
22180 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022181 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022182 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022183 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022184
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022185 /* Return quickly when autoload disabled. */
22186 if (no_autoload)
22187 return FALSE;
22188
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022189 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022190 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022191 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022192 return FALSE;
22193
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022194 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022195
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022196 /* Find the name in the list of previously loaded package names. Skip
22197 * "autoload/", it's always the same. */
22198 for (i = 0; i < ga_loaded.ga_len; ++i)
22199 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22200 break;
22201 if (!reload && i < ga_loaded.ga_len)
22202 ret = FALSE; /* was loaded already */
22203 else
22204 {
22205 /* Remember the name if it wasn't loaded already. */
22206 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22207 {
22208 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22209 tofree = NULL;
22210 }
22211
22212 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022213 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022214 ret = TRUE;
22215 }
22216
22217 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022218 return ret;
22219}
22220
22221/*
22222 * Return the autoload script name for a function or variable name.
22223 * Returns NULL when out of memory.
22224 */
22225 static char_u *
22226autoload_name(name)
22227 char_u *name;
22228{
22229 char_u *p;
22230 char_u *scriptname;
22231
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022232 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022233 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22234 if (scriptname == NULL)
22235 return FALSE;
22236 STRCPY(scriptname, "autoload/");
22237 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022238 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022239 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022240 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022241 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022242 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022243}
22244
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22246
22247/*
22248 * Function given to ExpandGeneric() to obtain the list of user defined
22249 * function names.
22250 */
22251 char_u *
22252get_user_func_name(xp, idx)
22253 expand_T *xp;
22254 int idx;
22255{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022256 static long_u done;
22257 static hashitem_T *hi;
22258 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259
22260 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022262 done = 0;
22263 hi = func_hashtab.ht_array;
22264 }
22265 if (done < func_hashtab.ht_used)
22266 {
22267 if (done++ > 0)
22268 ++hi;
22269 while (HASHITEM_EMPTY(hi))
22270 ++hi;
22271 fp = HI2UF(hi);
22272
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022273 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022274 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022275
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022276 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22277 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278
22279 cat_func_name(IObuff, fp);
22280 if (xp->xp_context != EXPAND_USER_FUNC)
22281 {
22282 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022283 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 STRCAT(IObuff, ")");
22285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 return IObuff;
22287 }
22288 return NULL;
22289}
22290
22291#endif /* FEAT_CMDL_COMPL */
22292
22293/*
22294 * Copy the function name of "fp" to buffer "buf".
22295 * "buf" must be able to hold the function name plus three bytes.
22296 * Takes care of script-local function names.
22297 */
22298 static void
22299cat_func_name(buf, fp)
22300 char_u *buf;
22301 ufunc_T *fp;
22302{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022303 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 {
22305 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022306 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 }
22308 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022309 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310}
22311
22312/*
22313 * ":delfunction {name}"
22314 */
22315 void
22316ex_delfunction(eap)
22317 exarg_T *eap;
22318{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022319 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 char_u *p;
22321 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022322 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323
22324 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022325 name = trans_function_name(&p, eap->skip, 0, &fudi);
22326 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022328 {
22329 if (fudi.fd_dict != NULL && !eap->skip)
22330 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 if (!ends_excmd(*skipwhite(p)))
22334 {
22335 vim_free(name);
22336 EMSG(_(e_trailing));
22337 return;
22338 }
22339 eap->nextcmd = check_nextcmd(p);
22340 if (eap->nextcmd != NULL)
22341 *p = NUL;
22342
22343 if (!eap->skip)
22344 fp = find_func(name);
22345 vim_free(name);
22346
22347 if (!eap->skip)
22348 {
22349 if (fp == NULL)
22350 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022351 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 return;
22353 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022354 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 {
22356 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22357 return;
22358 }
22359
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022360 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362 /* Delete the dict item that refers to the function, it will
22363 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022364 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022366 else
22367 func_free(fp);
22368 }
22369}
22370
22371/*
22372 * Free a function and remove it from the list of functions.
22373 */
22374 static void
22375func_free(fp)
22376 ufunc_T *fp;
22377{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022378 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379
22380 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022381 ga_clear_strings(&(fp->uf_args));
22382 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022383#ifdef FEAT_PROFILE
22384 vim_free(fp->uf_tml_count);
22385 vim_free(fp->uf_tml_total);
22386 vim_free(fp->uf_tml_self);
22387#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022388
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022389 /* remove the function from the function hashtable */
22390 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22391 if (HASHITEM_EMPTY(hi))
22392 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022393 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022394 hash_remove(&func_hashtab, hi);
22395
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022396 vim_free(fp);
22397}
22398
22399/*
22400 * Unreference a Function: decrement the reference count and free it when it
22401 * becomes zero. Only for numbered functions.
22402 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022403 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022404func_unref(name)
22405 char_u *name;
22406{
22407 ufunc_T *fp;
22408
22409 if (name != NULL && isdigit(*name))
22410 {
22411 fp = find_func(name);
22412 if (fp == NULL)
22413 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022414 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022415 {
22416 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022417 * when "uf_calls" becomes zero. */
22418 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 func_free(fp);
22420 }
22421 }
22422}
22423
22424/*
22425 * Count a reference to a Function.
22426 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022427 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022428func_ref(name)
22429 char_u *name;
22430{
22431 ufunc_T *fp;
22432
22433 if (name != NULL && isdigit(*name))
22434 {
22435 fp = find_func(name);
22436 if (fp == NULL)
22437 EMSG2(_(e_intern2), "func_ref()");
22438 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022439 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 }
22441}
22442
22443/*
22444 * Call a user function.
22445 */
22446 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022447call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 ufunc_T *fp; /* pointer to function */
22449 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 typval_T *argvars; /* arguments */
22451 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 linenr_T firstline; /* first line of range */
22453 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022454 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455{
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 char_u *save_sourcing_name;
22457 linenr_T save_sourcing_lnum;
22458 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022459 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022460 int save_did_emsg;
22461 static int depth = 0;
22462 dictitem_T *v;
22463 int fixvar_idx = 0; /* index in fixvar[] */
22464 int i;
22465 int ai;
22466 char_u numbuf[NUMBUFLEN];
22467 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022468#ifdef FEAT_PROFILE
22469 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022470 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472
22473 /* If depth of calling is getting too high, don't execute the function */
22474 if (depth >= p_mfd)
22475 {
22476 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022477 rettv->v_type = VAR_NUMBER;
22478 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 return;
22480 }
22481 ++depth;
22482
22483 line_breakcheck(); /* check for CTRL-C hit */
22484
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022485 fc = (funccall_T *)alloc(sizeof(funccall_T));
22486 fc->caller = current_funccal;
22487 current_funccal = fc;
22488 fc->func = fp;
22489 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022490 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022491 fc->linenr = 0;
22492 fc->returned = FALSE;
22493 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022495 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22496 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497
Bram Moolenaar33570922005-01-25 22:26:29 +000022498 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022499 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22501 * each argument variable and saves a lot of time.
22502 */
22503 /*
22504 * Init l: variables.
22505 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022506 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022507 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022508 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022509 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22510 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022511 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022512 name = v->di_key;
22513 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022515 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022516 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022517 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 v->di_tv.vval.v_dict = selfdict;
22519 ++selfdict->dv_refcount;
22520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022521
Bram Moolenaar33570922005-01-25 22:26:29 +000022522 /*
22523 * Init a: variables.
22524 * Set a:0 to "argcount".
22525 * Set a:000 to a list with room for the "..." arguments.
22526 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022527 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022528 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022529 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022530 /* Use "name" to avoid a warning from some compiler that checks the
22531 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022532 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022533 name = v->di_key;
22534 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022536 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022538 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022539 v->di_tv.vval.v_list = &fc->l_varlist;
22540 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22541 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22542 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022543
22544 /*
22545 * Set a:firstline to "firstline" and a:lastline to "lastline".
22546 * Set a:name to named arguments.
22547 * Set a:N to the "..." arguments.
22548 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022549 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022551 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 (varnumber_T)lastline);
22553 for (i = 0; i < argcount; ++i)
22554 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022555 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 if (ai < 0)
22557 /* named argument a:name */
22558 name = FUNCARG(fp, i);
22559 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022560 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022561 /* "..." argument a:1, a:2, etc. */
22562 sprintf((char *)numbuf, "%d", ai + 1);
22563 name = numbuf;
22564 }
22565 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22566 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022567 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022568 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22569 }
22570 else
22571 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022572 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22573 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 if (v == NULL)
22575 break;
22576 v->di_flags = DI_FLAGS_RO;
22577 }
22578 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022579 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022580
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022581 /* Note: the values are copied directly to avoid alloc/free.
22582 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022584 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022585
22586 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22587 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022588 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22589 fc->l_listitems[ai].li_tv = argvars[i];
22590 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022591 }
22592 }
22593
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 /* Don't redraw while executing the function. */
22595 ++RedrawingDisabled;
22596 save_sourcing_name = sourcing_name;
22597 save_sourcing_lnum = sourcing_lnum;
22598 sourcing_lnum = 1;
22599 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 if (sourcing_name != NULL)
22602 {
22603 if (save_sourcing_name != NULL
22604 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22605 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22606 else
22607 STRCPY(sourcing_name, "function ");
22608 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22609
22610 if (p_verbose >= 12)
22611 {
22612 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022613 verbose_enter_scroll();
22614
Bram Moolenaar555b2802005-05-19 21:08:39 +000022615 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 if (p_verbose >= 14)
22617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022619 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022620 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022621 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622
22623 msg_puts((char_u *)"(");
22624 for (i = 0; i < argcount; ++i)
22625 {
22626 if (i > 0)
22627 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022628 if (argvars[i].v_type == VAR_NUMBER)
22629 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 else
22631 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022632 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22633 if (s != NULL)
22634 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022635 if (vim_strsize(s) > MSG_BUF_CLEN)
22636 {
22637 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22638 s = buf;
22639 }
22640 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022641 vim_free(tofree);
22642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 }
22644 }
22645 msg_puts((char_u *)")");
22646 }
22647 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022648
22649 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 --no_wait_return;
22651 }
22652 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022653#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022654 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022655 {
22656 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22657 func_do_profile(fp);
22658 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022659 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022660 {
22661 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022662 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022663 profile_zero(&fp->uf_tm_children);
22664 }
22665 script_prof_save(&wait_start);
22666 }
22667#endif
22668
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022670 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 save_did_emsg = did_emsg;
22672 did_emsg = FALSE;
22673
22674 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022675 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22677
22678 --RedrawingDisabled;
22679
22680 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022681 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022683 clear_tv(rettv);
22684 rettv->v_type = VAR_NUMBER;
22685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 }
22687
Bram Moolenaar05159a02005-02-26 23:04:13 +000022688#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022689 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022690 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022691 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022692 profile_end(&call_start);
22693 profile_sub_wait(&wait_start, &call_start);
22694 profile_add(&fp->uf_tm_total, &call_start);
22695 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022696 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022697 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022698 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22699 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022700 }
22701 }
22702#endif
22703
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 /* when being verbose, mention the return value */
22705 if (p_verbose >= 12)
22706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022708 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022711 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022712 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022713 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022714 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022717 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022718 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022719 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022720 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022721
Bram Moolenaar555b2802005-05-19 21:08:39 +000022722 /* The value may be very long. Skip the middle part, so that we
22723 * have some idea how it starts and ends. smsg() would always
22724 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022725 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022726 if (s != NULL)
22727 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022728 if (vim_strsize(s) > MSG_BUF_CLEN)
22729 {
22730 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22731 s = buf;
22732 }
22733 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022734 vim_free(tofree);
22735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 }
22737 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022738
22739 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 --no_wait_return;
22741 }
22742
22743 vim_free(sourcing_name);
22744 sourcing_name = save_sourcing_name;
22745 sourcing_lnum = save_sourcing_lnum;
22746 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022747#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022748 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022749 script_prof_restore(&wait_start);
22750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751
22752 if (p_verbose >= 12 && sourcing_name != NULL)
22753 {
22754 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022755 verbose_enter_scroll();
22756
Bram Moolenaar555b2802005-05-19 21:08:39 +000022757 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022759
22760 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 --no_wait_return;
22762 }
22763
22764 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022765 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022767
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022768 /* If the a:000 list and the l: and a: dicts are not referenced we can
22769 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022770 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22771 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22772 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22773 {
22774 free_funccal(fc, FALSE);
22775 }
22776 else
22777 {
22778 hashitem_T *hi;
22779 listitem_T *li;
22780 int todo;
22781
22782 /* "fc" is still in use. This can happen when returning "a:000" or
22783 * assigning "l:" to a global variable.
22784 * Link "fc" in the list for garbage collection later. */
22785 fc->caller = previous_funccal;
22786 previous_funccal = fc;
22787
22788 /* Make a copy of the a: variables, since we didn't do that above. */
22789 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22790 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22791 {
22792 if (!HASHITEM_EMPTY(hi))
22793 {
22794 --todo;
22795 v = HI2DI(hi);
22796 copy_tv(&v->di_tv, &v->di_tv);
22797 }
22798 }
22799
22800 /* Make a copy of the a:000 items, since we didn't do that above. */
22801 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22802 copy_tv(&li->li_tv, &li->li_tv);
22803 }
22804}
22805
22806/*
22807 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022808 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022809 */
22810 static int
22811can_free_funccal(fc, copyID)
22812 funccall_T *fc;
22813 int copyID;
22814{
22815 return (fc->l_varlist.lv_copyID != copyID
22816 && fc->l_vars.dv_copyID != copyID
22817 && fc->l_avars.dv_copyID != copyID);
22818}
22819
22820/*
22821 * Free "fc" and what it contains.
22822 */
22823 static void
22824free_funccal(fc, free_val)
22825 funccall_T *fc;
22826 int free_val; /* a: vars were allocated */
22827{
22828 listitem_T *li;
22829
22830 /* The a: variables typevals may not have been allocated, only free the
22831 * allocated variables. */
22832 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22833
22834 /* free all l: variables */
22835 vars_clear(&fc->l_vars.dv_hashtab);
22836
22837 /* Free the a:000 variables if they were allocated. */
22838 if (free_val)
22839 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22840 clear_tv(&li->li_tv);
22841
22842 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843}
22844
22845/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022846 * Add a number variable "name" to dict "dp" with value "nr".
22847 */
22848 static void
22849add_nr_var(dp, v, name, nr)
22850 dict_T *dp;
22851 dictitem_T *v;
22852 char *name;
22853 varnumber_T nr;
22854{
22855 STRCPY(v->di_key, name);
22856 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22857 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22858 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022859 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022860 v->di_tv.vval.v_number = nr;
22861}
22862
22863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 * ":return [expr]"
22865 */
22866 void
22867ex_return(eap)
22868 exarg_T *eap;
22869{
22870 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022871 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 int returning = FALSE;
22873
22874 if (current_funccal == NULL)
22875 {
22876 EMSG(_("E133: :return not inside a function"));
22877 return;
22878 }
22879
22880 if (eap->skip)
22881 ++emsg_skip;
22882
22883 eap->nextcmd = NULL;
22884 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022885 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 {
22887 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022888 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022890 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 }
22892 /* It's safer to return also on error. */
22893 else if (!eap->skip)
22894 {
22895 /*
22896 * Return unless the expression evaluation has been cancelled due to an
22897 * aborting error, an interrupt, or an exception.
22898 */
22899 if (!aborting())
22900 returning = do_return(eap, FALSE, TRUE, NULL);
22901 }
22902
22903 /* When skipping or the return gets pending, advance to the next command
22904 * in this line (!returning). Otherwise, ignore the rest of the line.
22905 * Following lines will be ignored by get_func_line(). */
22906 if (returning)
22907 eap->nextcmd = NULL;
22908 else if (eap->nextcmd == NULL) /* no argument */
22909 eap->nextcmd = check_nextcmd(arg);
22910
22911 if (eap->skip)
22912 --emsg_skip;
22913}
22914
22915/*
22916 * Return from a function. Possibly makes the return pending. Also called
22917 * for a pending return at the ":endtry" or after returning from an extra
22918 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022919 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022920 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 * FALSE when the return gets pending.
22922 */
22923 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022924do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 exarg_T *eap;
22926 int reanimate;
22927 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022928 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929{
22930 int idx;
22931 struct condstack *cstack = eap->cstack;
22932
22933 if (reanimate)
22934 /* Undo the return. */
22935 current_funccal->returned = FALSE;
22936
22937 /*
22938 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22939 * not in its finally clause (which then is to be executed next) is found.
22940 * In this case, make the ":return" pending for execution at the ":endtry".
22941 * Otherwise, return normally.
22942 */
22943 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22944 if (idx >= 0)
22945 {
22946 cstack->cs_pending[idx] = CSTP_RETURN;
22947
22948 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022949 /* A pending return again gets pending. "rettv" points to an
22950 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022952 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 else
22954 {
22955 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022956 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022958 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022960 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 {
22962 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022963 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 else
22966 EMSG(_(e_outofmem));
22967 }
22968 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022969 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970
22971 if (reanimate)
22972 {
22973 /* The pending return value could be overwritten by a ":return"
22974 * without argument in a finally clause; reset the default
22975 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022976 current_funccal->rettv->v_type = VAR_NUMBER;
22977 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 }
22979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022980 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 }
22982 else
22983 {
22984 current_funccal->returned = TRUE;
22985
22986 /* If the return is carried out now, store the return value. For
22987 * a return immediately after reanimation, the value is already
22988 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022989 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022991 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022994 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 }
22996 }
22997
22998 return idx < 0;
22999}
23000
23001/*
23002 * Free the variable with a pending return value.
23003 */
23004 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023005discard_pending_return(rettv)
23006 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007{
Bram Moolenaar33570922005-01-25 22:26:29 +000023008 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009}
23010
23011/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023012 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 * is an allocated string. Used by report_pending() for verbose messages.
23014 */
23015 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023016get_return_cmd(rettv)
23017 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023019 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023020 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023021 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023023 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023024 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023025 if (s == NULL)
23026 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023027
23028 STRCPY(IObuff, ":return ");
23029 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23030 if (STRLEN(s) + 8 >= IOSIZE)
23031 STRCPY(IObuff + IOSIZE - 4, "...");
23032 vim_free(tofree);
23033 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034}
23035
23036/*
23037 * Get next function line.
23038 * Called by do_cmdline() to get the next line.
23039 * Returns allocated string, or NULL for end of function.
23040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 char_u *
23042get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023043 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023045 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046{
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023048 ufunc_T *fp = fcp->func;
23049 char_u *retval;
23050 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051
23052 /* If breakpoints have been added/deleted need to check for it. */
23053 if (fcp->dbg_tick != debug_tick)
23054 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023055 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 sourcing_lnum);
23057 fcp->dbg_tick = debug_tick;
23058 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023059#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023060 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023061 func_line_end(cookie);
23062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063
Bram Moolenaar05159a02005-02-26 23:04:13 +000023064 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023065 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23066 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 retval = NULL;
23068 else
23069 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023070 /* Skip NULL lines (continuation lines). */
23071 while (fcp->linenr < gap->ga_len
23072 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23073 ++fcp->linenr;
23074 if (fcp->linenr >= gap->ga_len)
23075 retval = NULL;
23076 else
23077 {
23078 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23079 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023080#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023081 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023082 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023083#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 }
23086
23087 /* Did we encounter a breakpoint? */
23088 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23089 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023090 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023092 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 sourcing_lnum);
23094 fcp->dbg_tick = debug_tick;
23095 }
23096
23097 return retval;
23098}
23099
Bram Moolenaar05159a02005-02-26 23:04:13 +000023100#if defined(FEAT_PROFILE) || defined(PROTO)
23101/*
23102 * Called when starting to read a function line.
23103 * "sourcing_lnum" must be correct!
23104 * When skipping lines it may not actually be executed, but we won't find out
23105 * until later and we need to store the time now.
23106 */
23107 void
23108func_line_start(cookie)
23109 void *cookie;
23110{
23111 funccall_T *fcp = (funccall_T *)cookie;
23112 ufunc_T *fp = fcp->func;
23113
23114 if (fp->uf_profiling && sourcing_lnum >= 1
23115 && sourcing_lnum <= fp->uf_lines.ga_len)
23116 {
23117 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023118 /* Skip continuation lines. */
23119 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23120 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023121 fp->uf_tml_execed = FALSE;
23122 profile_start(&fp->uf_tml_start);
23123 profile_zero(&fp->uf_tml_children);
23124 profile_get_wait(&fp->uf_tml_wait);
23125 }
23126}
23127
23128/*
23129 * Called when actually executing a function line.
23130 */
23131 void
23132func_line_exec(cookie)
23133 void *cookie;
23134{
23135 funccall_T *fcp = (funccall_T *)cookie;
23136 ufunc_T *fp = fcp->func;
23137
23138 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23139 fp->uf_tml_execed = TRUE;
23140}
23141
23142/*
23143 * Called when done with a function line.
23144 */
23145 void
23146func_line_end(cookie)
23147 void *cookie;
23148{
23149 funccall_T *fcp = (funccall_T *)cookie;
23150 ufunc_T *fp = fcp->func;
23151
23152 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23153 {
23154 if (fp->uf_tml_execed)
23155 {
23156 ++fp->uf_tml_count[fp->uf_tml_idx];
23157 profile_end(&fp->uf_tml_start);
23158 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023159 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023160 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23161 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023162 }
23163 fp->uf_tml_idx = -1;
23164 }
23165}
23166#endif
23167
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168/*
23169 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023170 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 */
23172 int
23173func_has_ended(cookie)
23174 void *cookie;
23175{
Bram Moolenaar33570922005-01-25 22:26:29 +000023176 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177
23178 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23179 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023180 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 || fcp->returned);
23182}
23183
23184/*
23185 * return TRUE if cookie indicates a function which "abort"s on errors.
23186 */
23187 int
23188func_has_abort(cookie)
23189 void *cookie;
23190{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023191 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192}
23193
23194#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23195typedef enum
23196{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023197 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23198 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23199 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200} var_flavour_T;
23201
23202static var_flavour_T var_flavour __ARGS((char_u *varname));
23203
23204 static var_flavour_T
23205var_flavour(varname)
23206 char_u *varname;
23207{
23208 char_u *p = varname;
23209
23210 if (ASCII_ISUPPER(*p))
23211 {
23212 while (*(++p))
23213 if (ASCII_ISLOWER(*p))
23214 return VAR_FLAVOUR_SESSION;
23215 return VAR_FLAVOUR_VIMINFO;
23216 }
23217 else
23218 return VAR_FLAVOUR_DEFAULT;
23219}
23220#endif
23221
23222#if defined(FEAT_VIMINFO) || defined(PROTO)
23223/*
23224 * Restore global vars that start with a capital from the viminfo file
23225 */
23226 int
23227read_viminfo_varlist(virp, writing)
23228 vir_T *virp;
23229 int writing;
23230{
23231 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023232 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234
23235 if (!writing && (find_viminfo_parameter('!') != NULL))
23236 {
23237 tab = vim_strchr(virp->vir_line + 1, '\t');
23238 if (tab != NULL)
23239 {
23240 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023241 switch (*tab)
23242 {
23243 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023244#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023245 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023246#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023247 case 'D': type = VAR_DICT; break;
23248 case 'L': type = VAR_LIST; break;
23249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250
23251 tab = vim_strchr(tab, '\t');
23252 if (tab != NULL)
23253 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023254 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023255 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023256 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023258#ifdef FEAT_FLOAT
23259 else if (type == VAR_FLOAT)
23260 (void)string2float(tab + 1, &tv.vval.v_float);
23261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023263 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023264 if (type == VAR_DICT || type == VAR_LIST)
23265 {
23266 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23267
23268 if (etv == NULL)
23269 /* Failed to parse back the dict or list, use it as a
23270 * string. */
23271 tv.v_type = VAR_STRING;
23272 else
23273 {
23274 vim_free(tv.vval.v_string);
23275 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023276 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023277 }
23278 }
23279
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023280 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023281
23282 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023283 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023284 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23285 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 }
23287 }
23288 }
23289
23290 return viminfo_readline(virp);
23291}
23292
23293/*
23294 * Write global vars that start with a capital to the viminfo file
23295 */
23296 void
23297write_viminfo_varlist(fp)
23298 FILE *fp;
23299{
Bram Moolenaar33570922005-01-25 22:26:29 +000023300 hashitem_T *hi;
23301 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023302 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023303 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023304 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023305 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023306 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307
23308 if (find_viminfo_parameter('!') == NULL)
23309 return;
23310
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023311 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023312
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023313 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023314 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023316 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023318 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023319 this_var = HI2DI(hi);
23320 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023321 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023322 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023323 {
23324 case VAR_STRING: s = "STR"; break;
23325 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023326#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023327 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023328#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023329 case VAR_DICT: s = "DIC"; break;
23330 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023331 default: continue;
23332 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023333 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023334 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023335 if (p != NULL)
23336 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023337 vim_free(tofree);
23338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 }
23340 }
23341}
23342#endif
23343
23344#if defined(FEAT_SESSION) || defined(PROTO)
23345 int
23346store_session_globals(fd)
23347 FILE *fd;
23348{
Bram Moolenaar33570922005-01-25 22:26:29 +000023349 hashitem_T *hi;
23350 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023351 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 char_u *p, *t;
23353
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023354 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023357 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023359 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023360 this_var = HI2DI(hi);
23361 if ((this_var->di_tv.v_type == VAR_NUMBER
23362 || this_var->di_tv.v_type == VAR_STRING)
23363 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023364 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023365 /* Escape special characters with a backslash. Turn a LF and
23366 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023367 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023368 (char_u *)"\\\"\n\r");
23369 if (p == NULL) /* out of memory */
23370 break;
23371 for (t = p; *t != NUL; ++t)
23372 if (*t == '\n')
23373 *t = 'n';
23374 else if (*t == '\r')
23375 *t = 'r';
23376 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023377 this_var->di_key,
23378 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23379 : ' ',
23380 p,
23381 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23382 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023383 || put_eol(fd) == FAIL)
23384 {
23385 vim_free(p);
23386 return FAIL;
23387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 vim_free(p);
23389 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023390#ifdef FEAT_FLOAT
23391 else if (this_var->di_tv.v_type == VAR_FLOAT
23392 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23393 {
23394 float_T f = this_var->di_tv.vval.v_float;
23395 int sign = ' ';
23396
23397 if (f < 0)
23398 {
23399 f = -f;
23400 sign = '-';
23401 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023402 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023403 this_var->di_key, sign, f) < 0)
23404 || put_eol(fd) == FAIL)
23405 return FAIL;
23406 }
23407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 }
23409 }
23410 return OK;
23411}
23412#endif
23413
Bram Moolenaar661b1822005-07-28 22:36:45 +000023414/*
23415 * Display script name where an item was last set.
23416 * Should only be invoked when 'verbose' is non-zero.
23417 */
23418 void
23419last_set_msg(scriptID)
23420 scid_T scriptID;
23421{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023422 char_u *p;
23423
Bram Moolenaar661b1822005-07-28 22:36:45 +000023424 if (scriptID != 0)
23425 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023426 p = home_replace_save(NULL, get_scriptname(scriptID));
23427 if (p != NULL)
23428 {
23429 verbose_enter();
23430 MSG_PUTS(_("\n\tLast set from "));
23431 MSG_PUTS(p);
23432 vim_free(p);
23433 verbose_leave();
23434 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023435 }
23436}
23437
Bram Moolenaard812df62008-11-09 12:46:09 +000023438/*
23439 * List v:oldfiles in a nice way.
23440 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023441 void
23442ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023443 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023444{
23445 list_T *l = vimvars[VV_OLDFILES].vv_list;
23446 listitem_T *li;
23447 int nr = 0;
23448
23449 if (l == NULL)
23450 msg((char_u *)_("No old files"));
23451 else
23452 {
23453 msg_start();
23454 msg_scroll = TRUE;
23455 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23456 {
23457 msg_outnum((long)++nr);
23458 MSG_PUTS(": ");
23459 msg_outtrans(get_tv_string(&li->li_tv));
23460 msg_putchar('\n');
23461 out_flush(); /* output one line at a time */
23462 ui_breakcheck();
23463 }
23464 /* Assume "got_int" was set to truncate the listing. */
23465 got_int = FALSE;
23466
23467#ifdef FEAT_BROWSE_CMD
23468 if (cmdmod.browse)
23469 {
23470 quit_more = FALSE;
23471 nr = prompt_for_number(FALSE);
23472 msg_starthere();
23473 if (nr > 0)
23474 {
23475 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23476 (long)nr);
23477
23478 if (p != NULL)
23479 {
23480 p = expand_env_save(p);
23481 eap->arg = p;
23482 eap->cmdidx = CMD_edit;
23483 cmdmod.browse = FALSE;
23484 do_exedit(eap, NULL);
23485 vim_free(p);
23486 }
23487 }
23488 }
23489#endif
23490 }
23491}
23492
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493#endif /* FEAT_EVAL */
23494
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023496#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497
23498#ifdef WIN3264
23499/*
23500 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23501 */
23502static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23503static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23504static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23505
23506/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023507 * Get the short path (8.3) for the filename in "fnamep".
23508 * Only works for a valid file name.
23509 * When the path gets longer "fnamep" is changed and the allocated buffer
23510 * is put in "bufp".
23511 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23512 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 */
23514 static int
23515get_short_pathname(fnamep, bufp, fnamelen)
23516 char_u **fnamep;
23517 char_u **bufp;
23518 int *fnamelen;
23519{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023520 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 char_u *newbuf;
23522
23523 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 l = GetShortPathName(*fnamep, *fnamep, len);
23525 if (l > len - 1)
23526 {
23527 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023528 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 newbuf = vim_strnsave(*fnamep, l);
23530 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023531 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532
23533 vim_free(*bufp);
23534 *fnamep = *bufp = newbuf;
23535
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023536 /* Really should always succeed, as the buffer is big enough. */
23537 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 }
23539
23540 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023541 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542}
23543
23544/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023545 * Get the short path (8.3) for the filename in "fname". The converted
23546 * path is returned in "bufp".
23547 *
23548 * Some of the directories specified in "fname" may not exist. This function
23549 * will shorten the existing directories at the beginning of the path and then
23550 * append the remaining non-existing path.
23551 *
23552 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023553 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023554 * bufp - Pointer to an allocated buffer for the filename.
23555 * fnamelen - Length of the filename pointed to by fname
23556 *
23557 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 */
23559 static int
23560shortpath_for_invalid_fname(fname, bufp, fnamelen)
23561 char_u **fname;
23562 char_u **bufp;
23563 int *fnamelen;
23564{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023565 char_u *short_fname, *save_fname, *pbuf_unused;
23566 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023568 int old_len, len;
23569 int new_len, sfx_len;
23570 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571
23572 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023573 old_len = *fnamelen;
23574 save_fname = vim_strnsave(*fname, old_len);
23575 pbuf_unused = NULL;
23576 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023578 endp = save_fname + old_len - 1; /* Find the end of the copy */
23579 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023581 /*
23582 * Try shortening the supplied path till it succeeds by removing one
23583 * directory at a time from the tail of the path.
23584 */
23585 len = 0;
23586 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023588 /* go back one path-separator */
23589 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23590 --endp;
23591 if (endp <= save_fname)
23592 break; /* processed the complete path */
23593
23594 /*
23595 * Replace the path separator with a NUL and try to shorten the
23596 * resulting path.
23597 */
23598 ch = *endp;
23599 *endp = 0;
23600 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023601 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023602 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23603 {
23604 retval = FAIL;
23605 goto theend;
23606 }
23607 *endp = ch; /* preserve the string */
23608
23609 if (len > 0)
23610 break; /* successfully shortened the path */
23611
23612 /* failed to shorten the path. Skip the path separator */
23613 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 }
23615
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023616 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023618 /*
23619 * Succeeded in shortening the path. Now concatenate the shortened
23620 * path with the remaining path at the tail.
23621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023623 /* Compute the length of the new path. */
23624 sfx_len = (int)(save_endp - endp) + 1;
23625 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023627 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023629 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023631 /* There is not enough space in the currently allocated string,
23632 * copy it to a buffer big enough. */
23633 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023635 {
23636 retval = FAIL;
23637 goto theend;
23638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 }
23640 else
23641 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023642 /* Transfer short_fname to the main buffer (it's big enough),
23643 * unless get_short_pathname() did its work in-place. */
23644 *fname = *bufp = save_fname;
23645 if (short_fname != save_fname)
23646 vim_strncpy(save_fname, short_fname, len);
23647 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023649
23650 /* concat the not-shortened part of the path */
23651 vim_strncpy(*fname + len, endp, sfx_len);
23652 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023654
23655theend:
23656 vim_free(pbuf_unused);
23657 vim_free(save_fname);
23658
23659 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660}
23661
23662/*
23663 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023664 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 */
23666 static int
23667shortpath_for_partial(fnamep, bufp, fnamelen)
23668 char_u **fnamep;
23669 char_u **bufp;
23670 int *fnamelen;
23671{
23672 int sepcount, len, tflen;
23673 char_u *p;
23674 char_u *pbuf, *tfname;
23675 int hasTilde;
23676
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023677 /* Count up the path separators from the RHS.. so we know which part
23678 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023680 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 if (vim_ispathsep(*p))
23682 ++sepcount;
23683
23684 /* Need full path first (use expand_env() to remove a "~/") */
23685 hasTilde = (**fnamep == '~');
23686 if (hasTilde)
23687 pbuf = tfname = expand_env_save(*fnamep);
23688 else
23689 pbuf = tfname = FullName_save(*fnamep, FALSE);
23690
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023691 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023693 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695
23696 if (len == 0)
23697 {
23698 /* Don't have a valid filename, so shorten the rest of the
23699 * path if we can. This CAN give us invalid 8.3 filenames, but
23700 * there's not a lot of point in guessing what it might be.
23701 */
23702 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023703 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23704 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 }
23706
23707 /* Count the paths backward to find the beginning of the desired string. */
23708 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023709 {
23710#ifdef FEAT_MBYTE
23711 if (has_mbyte)
23712 p -= mb_head_off(tfname, p);
23713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 if (vim_ispathsep(*p))
23715 {
23716 if (sepcount == 0 || (hasTilde && sepcount == 1))
23717 break;
23718 else
23719 sepcount --;
23720 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 if (hasTilde)
23723 {
23724 --p;
23725 if (p >= tfname)
23726 *p = '~';
23727 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023728 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 }
23730 else
23731 ++p;
23732
23733 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23734 vim_free(*bufp);
23735 *fnamelen = (int)STRLEN(p);
23736 *bufp = pbuf;
23737 *fnamep = p;
23738
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023739 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740}
23741#endif /* WIN3264 */
23742
23743/*
23744 * Adjust a filename, according to a string of modifiers.
23745 * *fnamep must be NUL terminated when called. When returning, the length is
23746 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023747 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 * When there is an error, *fnamep is set to NULL.
23749 */
23750 int
23751modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23752 char_u *src; /* string with modifiers */
23753 int *usedlen; /* characters after src that are used */
23754 char_u **fnamep; /* file name so far */
23755 char_u **bufp; /* buffer for allocated file name or NULL */
23756 int *fnamelen; /* length of fnamep */
23757{
23758 int valid = 0;
23759 char_u *tail;
23760 char_u *s, *p, *pbuf;
23761 char_u dirname[MAXPATHL];
23762 int c;
23763 int has_fullname = 0;
23764#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023765 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 int has_shortname = 0;
23767#endif
23768
23769repeat:
23770 /* ":p" - full path/file_name */
23771 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23772 {
23773 has_fullname = 1;
23774
23775 valid |= VALID_PATH;
23776 *usedlen += 2;
23777
23778 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23779 if ((*fnamep)[0] == '~'
23780#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23781 && ((*fnamep)[1] == '/'
23782# ifdef BACKSLASH_IN_FILENAME
23783 || (*fnamep)[1] == '\\'
23784# endif
23785 || (*fnamep)[1] == NUL)
23786
23787#endif
23788 )
23789 {
23790 *fnamep = expand_env_save(*fnamep);
23791 vim_free(*bufp); /* free any allocated file name */
23792 *bufp = *fnamep;
23793 if (*fnamep == NULL)
23794 return -1;
23795 }
23796
23797 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023798 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 {
23800 if (vim_ispathsep(*p)
23801 && p[1] == '.'
23802 && (p[2] == NUL
23803 || vim_ispathsep(p[2])
23804 || (p[2] == '.'
23805 && (p[3] == NUL || vim_ispathsep(p[3])))))
23806 break;
23807 }
23808
23809 /* FullName_save() is slow, don't use it when not needed. */
23810 if (*p != NUL || !vim_isAbsName(*fnamep))
23811 {
23812 *fnamep = FullName_save(*fnamep, *p != NUL);
23813 vim_free(*bufp); /* free any allocated file name */
23814 *bufp = *fnamep;
23815 if (*fnamep == NULL)
23816 return -1;
23817 }
23818
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023819#ifdef WIN3264
23820# if _WIN32_WINNT >= 0x0500
23821 if (vim_strchr(*fnamep, '~') != NULL)
23822 {
23823 /* Expand 8.3 filename to full path. Needed to make sure the same
23824 * file does not have two different names.
23825 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23826 p = alloc(_MAX_PATH + 1);
23827 if (p != NULL)
23828 {
23829 if (GetLongPathName(*fnamep, p, MAXPATHL))
23830 {
23831 vim_free(*bufp);
23832 *bufp = *fnamep = p;
23833 }
23834 else
23835 vim_free(p);
23836 }
23837 }
23838# endif
23839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 /* Append a path separator to a directory. */
23841 if (mch_isdir(*fnamep))
23842 {
23843 /* Make room for one or two extra characters. */
23844 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23845 vim_free(*bufp); /* free any allocated file name */
23846 *bufp = *fnamep;
23847 if (*fnamep == NULL)
23848 return -1;
23849 add_pathsep(*fnamep);
23850 }
23851 }
23852
23853 /* ":." - path relative to the current directory */
23854 /* ":~" - path relative to the home directory */
23855 /* ":8" - shortname path - postponed till after */
23856 while (src[*usedlen] == ':'
23857 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23858 {
23859 *usedlen += 2;
23860 if (c == '8')
23861 {
23862#ifdef WIN3264
23863 has_shortname = 1; /* Postpone this. */
23864#endif
23865 continue;
23866 }
23867 pbuf = NULL;
23868 /* Need full path first (use expand_env() to remove a "~/") */
23869 if (!has_fullname)
23870 {
23871 if (c == '.' && **fnamep == '~')
23872 p = pbuf = expand_env_save(*fnamep);
23873 else
23874 p = pbuf = FullName_save(*fnamep, FALSE);
23875 }
23876 else
23877 p = *fnamep;
23878
23879 has_fullname = 0;
23880
23881 if (p != NULL)
23882 {
23883 if (c == '.')
23884 {
23885 mch_dirname(dirname, MAXPATHL);
23886 s = shorten_fname(p, dirname);
23887 if (s != NULL)
23888 {
23889 *fnamep = s;
23890 if (pbuf != NULL)
23891 {
23892 vim_free(*bufp); /* free any allocated file name */
23893 *bufp = pbuf;
23894 pbuf = NULL;
23895 }
23896 }
23897 }
23898 else
23899 {
23900 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23901 /* Only replace it when it starts with '~' */
23902 if (*dirname == '~')
23903 {
23904 s = vim_strsave(dirname);
23905 if (s != NULL)
23906 {
23907 *fnamep = s;
23908 vim_free(*bufp);
23909 *bufp = s;
23910 }
23911 }
23912 }
23913 vim_free(pbuf);
23914 }
23915 }
23916
23917 tail = gettail(*fnamep);
23918 *fnamelen = (int)STRLEN(*fnamep);
23919
23920 /* ":h" - head, remove "/file_name", can be repeated */
23921 /* Don't remove the first "/" or "c:\" */
23922 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23923 {
23924 valid |= VALID_HEAD;
23925 *usedlen += 2;
23926 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023927 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023928 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 *fnamelen = (int)(tail - *fnamep);
23930#ifdef VMS
23931 if (*fnamelen > 0)
23932 *fnamelen += 1; /* the path separator is part of the path */
23933#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023934 if (*fnamelen == 0)
23935 {
23936 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23937 p = vim_strsave((char_u *)".");
23938 if (p == NULL)
23939 return -1;
23940 vim_free(*bufp);
23941 *bufp = *fnamep = tail = p;
23942 *fnamelen = 1;
23943 }
23944 else
23945 {
23946 while (tail > s && !after_pathsep(s, tail))
23947 mb_ptr_back(*fnamep, tail);
23948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 }
23950
23951 /* ":8" - shortname */
23952 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23953 {
23954 *usedlen += 2;
23955#ifdef WIN3264
23956 has_shortname = 1;
23957#endif
23958 }
23959
23960#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023961 /*
23962 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 */
23964 if (has_shortname)
23965 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023966 /* Copy the string if it is shortened by :h and when it wasn't copied
23967 * yet, because we are going to change it in place. Avoids changing
23968 * the buffer name for "%:8". */
23969 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 {
23971 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023972 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 return -1;
23974 vim_free(*bufp);
23975 *bufp = *fnamep = p;
23976 }
23977
23978 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023979 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 if (!has_fullname && !vim_isAbsName(*fnamep))
23981 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023982 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 return -1;
23984 }
23985 else
23986 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023987 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988
Bram Moolenaardc935552011-08-17 15:23:23 +020023989 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023991 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 return -1;
23993
23994 if (l == 0)
23995 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023996 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023998 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 return -1;
24000 }
24001 *fnamelen = l;
24002 }
24003 }
24004#endif /* WIN3264 */
24005
24006 /* ":t" - tail, just the basename */
24007 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24008 {
24009 *usedlen += 2;
24010 *fnamelen -= (int)(tail - *fnamep);
24011 *fnamep = tail;
24012 }
24013
24014 /* ":e" - extension, can be repeated */
24015 /* ":r" - root, without extension, can be repeated */
24016 while (src[*usedlen] == ':'
24017 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24018 {
24019 /* find a '.' in the tail:
24020 * - for second :e: before the current fname
24021 * - otherwise: The last '.'
24022 */
24023 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24024 s = *fnamep - 2;
24025 else
24026 s = *fnamep + *fnamelen - 1;
24027 for ( ; s > tail; --s)
24028 if (s[0] == '.')
24029 break;
24030 if (src[*usedlen + 1] == 'e') /* :e */
24031 {
24032 if (s > tail)
24033 {
24034 *fnamelen += (int)(*fnamep - (s + 1));
24035 *fnamep = s + 1;
24036#ifdef VMS
24037 /* cut version from the extension */
24038 s = *fnamep + *fnamelen - 1;
24039 for ( ; s > *fnamep; --s)
24040 if (s[0] == ';')
24041 break;
24042 if (s > *fnamep)
24043 *fnamelen = s - *fnamep;
24044#endif
24045 }
24046 else if (*fnamep <= tail)
24047 *fnamelen = 0;
24048 }
24049 else /* :r */
24050 {
24051 if (s > tail) /* remove one extension */
24052 *fnamelen = (int)(s - *fnamep);
24053 }
24054 *usedlen += 2;
24055 }
24056
24057 /* ":s?pat?foo?" - substitute */
24058 /* ":gs?pat?foo?" - global substitute */
24059 if (src[*usedlen] == ':'
24060 && (src[*usedlen + 1] == 's'
24061 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24062 {
24063 char_u *str;
24064 char_u *pat;
24065 char_u *sub;
24066 int sep;
24067 char_u *flags;
24068 int didit = FALSE;
24069
24070 flags = (char_u *)"";
24071 s = src + *usedlen + 2;
24072 if (src[*usedlen + 1] == 'g')
24073 {
24074 flags = (char_u *)"g";
24075 ++s;
24076 }
24077
24078 sep = *s++;
24079 if (sep)
24080 {
24081 /* find end of pattern */
24082 p = vim_strchr(s, sep);
24083 if (p != NULL)
24084 {
24085 pat = vim_strnsave(s, (int)(p - s));
24086 if (pat != NULL)
24087 {
24088 s = p + 1;
24089 /* find end of substitution */
24090 p = vim_strchr(s, sep);
24091 if (p != NULL)
24092 {
24093 sub = vim_strnsave(s, (int)(p - s));
24094 str = vim_strnsave(*fnamep, *fnamelen);
24095 if (sub != NULL && str != NULL)
24096 {
24097 *usedlen = (int)(p + 1 - src);
24098 s = do_string_sub(str, pat, sub, flags);
24099 if (s != NULL)
24100 {
24101 *fnamep = s;
24102 *fnamelen = (int)STRLEN(s);
24103 vim_free(*bufp);
24104 *bufp = s;
24105 didit = TRUE;
24106 }
24107 }
24108 vim_free(sub);
24109 vim_free(str);
24110 }
24111 vim_free(pat);
24112 }
24113 }
24114 /* after using ":s", repeat all the modifiers */
24115 if (didit)
24116 goto repeat;
24117 }
24118 }
24119
24120 return valid;
24121}
24122
24123/*
24124 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24125 * "flags" can be "g" to do a global substitute.
24126 * Returns an allocated string, NULL for error.
24127 */
24128 char_u *
24129do_string_sub(str, pat, sub, flags)
24130 char_u *str;
24131 char_u *pat;
24132 char_u *sub;
24133 char_u *flags;
24134{
24135 int sublen;
24136 regmatch_T regmatch;
24137 int i;
24138 int do_all;
24139 char_u *tail;
24140 garray_T ga;
24141 char_u *ret;
24142 char_u *save_cpo;
24143
24144 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24145 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024146 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147
24148 ga_init2(&ga, 1, 200);
24149
24150 do_all = (flags[0] == 'g');
24151
24152 regmatch.rm_ic = p_ic;
24153 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24154 if (regmatch.regprog != NULL)
24155 {
24156 tail = str;
24157 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24158 {
24159 /*
24160 * Get some space for a temporary buffer to do the substitution
24161 * into. It will contain:
24162 * - The text up to where the match is.
24163 * - The substituted text.
24164 * - The text after the match.
24165 */
24166 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24167 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24168 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24169 {
24170 ga_clear(&ga);
24171 break;
24172 }
24173
24174 /* copy the text up to where the match is */
24175 i = (int)(regmatch.startp[0] - tail);
24176 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24177 /* add the substituted text */
24178 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24179 + ga.ga_len + i, TRUE, TRUE, FALSE);
24180 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 /* avoid getting stuck on a match with an empty string */
24182 if (tail == regmatch.endp[0])
24183 {
24184 if (*tail == NUL)
24185 break;
24186 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24187 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 }
24189 else
24190 {
24191 tail = regmatch.endp[0];
24192 if (*tail == NUL)
24193 break;
24194 }
24195 if (!do_all)
24196 break;
24197 }
24198
24199 if (ga.ga_data != NULL)
24200 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24201
24202 vim_free(regmatch.regprog);
24203 }
24204
24205 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24206 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024207 if (p_cpo == empty_option)
24208 p_cpo = save_cpo;
24209 else
24210 /* Darn, evaluating {sub} expression changed the value. */
24211 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212
24213 return ret;
24214}
24215
24216#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */