blob: 13e520b6dbd0fd1de6e7518185f500e4e4ee4c3f [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 listitem_T *listitem_alloc __ARGS((void));
428static void listitem_free __ARGS((listitem_T *item));
429static void listitem_remove __ARGS((list_T *l, listitem_T *item));
430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000435static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000438static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
440static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
441static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100445static 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 +0000446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000452static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
454static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000455static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
460static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
463static int string2float __ARGS((char_u *text, float_T *value));
464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
466static int find_internal_func __ARGS((char_u *name));
467static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
468static 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 +0200469static 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 +0000470static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000471static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
474static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100478static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100607static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000621#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200622static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000623static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
624#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000629static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000631static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000633static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000637#ifdef vim_mkdir
638static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100641#ifdef FEAT_MZSCHEME
642static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100646static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100758static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000760static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000761static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static int get_env_len __ARGS((char_u **arg));
763static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000765static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
766#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
767#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
768 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000769static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
773static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static typval_T *alloc_tv __ARGS((void));
775static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void init_tv __ARGS((typval_T *varp));
777static long get_tv_number __ARGS((typval_T *varp));
778static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000779static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static char_u *get_tv_string __ARGS((typval_T *varp));
781static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
786static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
787static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000788static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
789static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
791static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000792static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200793static int var_check_func_name __ARGS((char_u *name, int new_var));
794static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
824static void func_unref __ARGS((char_u *name));
825static void func_ref __ARGS((char_u *name));
826static 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 +0000827static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
828static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
831static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000832static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200836
837#ifdef EBCDIC
838static int compare_func_name __ARGS((const void *s1, const void *s2));
839static void sortFunctions __ARGS(());
840#endif
841
842
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000843/* Character used as separated in autoload function/variable names. */
844#define AUTOLOAD_CHAR '#'
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
855 init_var_dict(&globvardict, &globvars_var);
856 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200857 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000858 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
861 for (i = 0; i < VV_LEN; ++i)
862 {
863 p = &vimvars[i];
864 STRCPY(p->vv_di.di_key, p->vv_name);
865 if (p->vv_flags & VV_RO)
866 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
867 else if (p->vv_flags & VV_RO_SBX)
868 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
869 else
870 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000871
872 /* add to v: scope dict, unless the value is not always available */
873 if (p->vv_type != VAR_UNKNOWN)
874 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000876 /* add to compat scope dict */
877 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000879 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200880
881#ifdef EBCDIC
882 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100883 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200884 */
885 sortFunctions();
886#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000887}
888
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000889#if defined(EXITFREE) || defined(PROTO)
890 void
891eval_clear()
892{
893 int i;
894 struct vimvar *p;
895
896 for (i = 0; i < VV_LEN; ++i)
897 {
898 p = &vimvars[i];
899 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000900 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000901 vim_free(p->vv_str);
902 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000903 }
904 else if (p->vv_di.di_tv.v_type == VAR_LIST)
905 {
906 list_unref(p->vv_list);
907 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909 }
910 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000911 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000912 hash_clear(&compat_hashtab);
913
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000916
917 /* global variables */
918 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000920 /* autoloaded script names */
921 ga_clear_strings(&ga_loaded);
922
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200923 /* script-local variables */
924 for (i = 1; i <= ga_scripts.ga_len; ++i)
925 {
926 vars_clear(&SCRIPT_VARS(i));
927 vim_free(SCRIPT_SV(i));
928 }
929 ga_clear(&ga_scripts);
930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000931 /* unreferenced lists and dicts */
932 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000933
934 /* functions */
935 free_all_functions();
936 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937}
938#endif
939
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * Return the name of the executed function.
942 */
943 char_u *
944func_name(cookie)
945 void *cookie;
946{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000947 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
951 * Return the address holding the next breakpoint line for a funccall cookie.
952 */
953 linenr_T *
954func_breakpoint(cookie)
955 void *cookie;
956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Return the address holding the debug tick for a funccall cookie.
962 */
963 int *
964func_dbg_tick(cookie)
965 void *cookie;
966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Return the nesting level for a funccall cookie.
972 */
973 int
974func_level(cookie)
975 void *cookie;
976{
Bram Moolenaar33570922005-01-25 22:26:29 +0000977 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000981funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000983/* pointer to list of previously used funccal, still around because some
984 * item in it is still being used. */
985funccall_T *previous_funccal = NULL;
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987/*
988 * Return TRUE when a function was ended by a ":return" command.
989 */
990 int
991current_func_returned()
992{
993 return current_funccal->returned;
994}
995
996
997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * Set an internal variable to a string value. Creates the variable if it does
999 * not already exist.
1000 */
1001 void
1002set_internal_string_var(name, value)
1003 char_u *name;
1004 char_u *value;
1005{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001007 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 val = vim_strsave(value);
1010 if (val != NULL)
1011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018 }
1019}
1020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001022static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static char_u *redir_endp = NULL;
1024static char_u *redir_varname = NULL;
1025
1026/*
1027 * Start recording command output to a variable
1028 * Returns OK if successfully completed the setup. FAIL otherwise.
1029 */
1030 int
1031var_redir_start(name, append)
1032 char_u *name;
1033 int append; /* append to an existing variable */
1034{
1035 int save_emsg;
1036 int err;
1037 typval_T tv;
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001040 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 {
1042 EMSG(_(e_invarg));
1043 return FAIL;
1044 }
1045
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001046 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 redir_varname = vim_strsave(name);
1048 if (redir_varname == NULL)
1049 return FAIL;
1050
1051 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1052 if (redir_lval == NULL)
1053 {
1054 var_redir_stop();
1055 return FAIL;
1056 }
1057
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001058 /* The output is stored in growarray "redir_ga" until redirection ends. */
1059 ga_init2(&redir_ga, (int)sizeof(char), 500);
1060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001062 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1063 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1065 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001066 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 if (redir_endp != NULL && *redir_endp != NUL)
1068 /* Trailing characters are present after the variable name */
1069 EMSG(_(e_trailing));
1070 else
1071 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001072 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 var_redir_stop();
1074 return FAIL;
1075 }
1076
1077 /* check if we can write to the variable: set it to or append an empty
1078 * string */
1079 save_emsg = did_emsg;
1080 did_emsg = FALSE;
1081 tv.v_type = VAR_STRING;
1082 tv.vval.v_string = (char_u *)"";
1083 if (append)
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1085 else
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001087 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001089 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (err)
1091 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 var_redir_stop();
1094 return FAIL;
1095 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001101 * Append "value[value_len]" to the variable set by var_redir_start().
1102 * The actual appending is postponed until redirection ends, because the value
1103 * appended may in fact be the string we write to, changing it may cause freed
1104 * memory to be used:
1105 * :redir => foo
1106 * :let foo
1107 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 */
1109 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115
1116 if (redir_lval == NULL)
1117 return;
1118
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 {
1126 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 }
1129 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131}
1132
1133/*
1134 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001135 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 */
1137 void
1138var_redir_stop()
1139{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 typval_T tv;
1141
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 if (redir_lval != NULL)
1143 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001144 /* If there was no error: assign the text to the variable. */
1145 if (redir_endp != NULL)
1146 {
1147 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1148 tv.v_type = VAR_STRING;
1149 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001150 /* Call get_lval() again, if it's inside a Dict or List it may
1151 * have changed. */
1152 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1153 FALSE, FALSE, FALSE, FNE_CHECK_START);
1154 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1155 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1156 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* free the collected output */
1160 vim_free(redir_ga.ga_data);
1161 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 vim_free(redir_lval);
1164 redir_lval = NULL;
1165 }
1166 vim_free(redir_varname);
1167 redir_varname = NULL;
1168}
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170# if defined(FEAT_MBYTE) || defined(PROTO)
1171 int
1172eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1173 char_u *enc_from;
1174 char_u *enc_to;
1175 char_u *fname_from;
1176 char_u *fname_to;
1177{
1178 int err = FALSE;
1179
1180 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1181 set_vim_var_string(VV_CC_TO, enc_to, -1);
1182 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1183 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1184 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1185 err = TRUE;
1186 set_vim_var_string(VV_CC_FROM, NULL, -1);
1187 set_vim_var_string(VV_CC_TO, NULL, -1);
1188 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1189 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1190
1191 if (err)
1192 return FAIL;
1193 return OK;
1194}
1195# endif
1196
1197# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1198 int
1199eval_printexpr(fname, args)
1200 char_u *fname;
1201 char_u *args;
1202{
1203 int err = FALSE;
1204
1205 set_vim_var_string(VV_FNAME_IN, fname, -1);
1206 set_vim_var_string(VV_CMDARG, args, -1);
1207 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1210 set_vim_var_string(VV_CMDARG, NULL, -1);
1211
1212 if (err)
1213 {
1214 mch_remove(fname);
1215 return FAIL;
1216 }
1217 return OK;
1218}
1219# endif
1220
1221# if defined(FEAT_DIFF) || defined(PROTO)
1222 void
1223eval_diff(origfile, newfile, outfile)
1224 char_u *origfile;
1225 char_u *newfile;
1226 char_u *outfile;
1227{
1228 int err = FALSE;
1229
1230 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1231 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1232 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1233 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1234 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1235 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1236 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1237}
1238
1239 void
1240eval_patch(origfile, difffile, outfile)
1241 char_u *origfile;
1242 char_u *difffile;
1243 char_u *outfile;
1244{
1245 int err;
1246
1247 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1248 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1249 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1250 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1253 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1254}
1255# endif
1256
1257/*
1258 * Top level evaluation function, returning a boolean.
1259 * Sets "error" to TRUE if there was an error.
1260 * Return TRUE or FALSE.
1261 */
1262 int
1263eval_to_bool(arg, error, nextcmd, skip)
1264 char_u *arg;
1265 int *error;
1266 char_u **nextcmd;
1267 int skip; /* only parse, don't execute */
1268{
Bram Moolenaar33570922005-01-25 22:26:29 +00001269 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 int retval = FALSE;
1271
1272 if (skip)
1273 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 else
1277 {
1278 *error = FALSE;
1279 if (!skip)
1280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001281 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 if (skip)
1286 --emsg_skip;
1287
1288 return retval;
1289}
1290
1291/*
1292 * Top level evaluation function, returning a string. If "skip" is TRUE,
1293 * only parsing to "nextcmd" is done, without reporting errors. Return
1294 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1295 */
1296 char_u *
1297eval_to_string_skip(arg, nextcmd, skip)
1298 char_u *arg;
1299 char_u **nextcmd;
1300 int skip; /* only parse, don't execute */
1301{
Bram Moolenaar33570922005-01-25 22:26:29 +00001302 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 char_u *retval;
1304
1305 if (skip)
1306 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 retval = NULL;
1309 else
1310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 retval = vim_strsave(get_tv_string(&tv));
1312 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 if (skip)
1315 --emsg_skip;
1316
1317 return retval;
1318}
1319
1320/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001321 * Skip over an expression at "*pp".
1322 * Return FAIL for an error, OK otherwise.
1323 */
1324 int
1325skip_expr(pp)
1326 char_u **pp;
1327{
Bram Moolenaar33570922005-01-25 22:26:29 +00001328 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329
1330 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001331 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001332}
1333
1334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336 * When "convert" is TRUE convert a List into a sequence of lines and convert
1337 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * Return pointer to allocated memory, or NULL for failure.
1339 */
1340 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *arg;
1343 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
Bram Moolenaar33570922005-01-25 22:26:29 +00001346 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 retval = NULL;
1355 else
1356 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001358 {
1359 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001360 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001361 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 if (tv.vval.v_list->lv_len > 0)
1364 ga_append(&ga, NL);
1365 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 ga_append(&ga, NUL);
1367 retval = (char_u *)ga.ga_data;
1368 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001369#ifdef FEAT_FLOAT
1370 else if (convert && tv.v_type == VAR_FLOAT)
1371 {
1372 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1373 retval = vim_strsave(numbuf);
1374 }
1375#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 else
1377 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380
1381 return retval;
1382}
1383
1384/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 * Call eval_to_string() without using current local variables and using
1386 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 */
1388 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 char_u *arg;
1391 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 char_u *retval;
1395 void *save_funccalp;
1396
1397 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (use_sandbox)
1399 ++sandbox;
1400 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001401 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001402 if (use_sandbox)
1403 --sandbox;
1404 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 restore_funccal(save_funccalp);
1406 return retval;
1407}
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409/*
1410 * Top level evaluation function, returning a number.
1411 * Evaluates "expr" silently.
1412 * Returns -1 for an error.
1413 */
1414 int
1415eval_to_number(expr)
1416 char_u *expr;
1417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001420 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 ++emsg_off;
1423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = -1;
1426 else
1427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001428 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 --emsg_off;
1432
1433 return retval;
1434}
1435
Bram Moolenaara40058a2005-07-11 22:42:07 +00001436/*
1437 * Prepare v: variable "idx" to be used.
1438 * Save the current typeval in "save_tv".
1439 * When not used yet add the variable to the v: hashtable.
1440 */
1441 static void
1442prepare_vimvar(idx, save_tv)
1443 int idx;
1444 typval_T *save_tv;
1445{
1446 *save_tv = vimvars[idx].vv_tv;
1447 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1448 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1449}
1450
1451/*
1452 * Restore v: variable "idx" to typeval "save_tv".
1453 * When no longer defined, remove the variable from the v: hashtable.
1454 */
1455 static void
1456restore_vimvar(idx, save_tv)
1457 int idx;
1458 typval_T *save_tv;
1459{
1460 hashitem_T *hi;
1461
Bram Moolenaara40058a2005-07-11 22:42:07 +00001462 vimvars[idx].vv_tv = *save_tv;
1463 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1464 {
1465 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1466 if (HASHITEM_EMPTY(hi))
1467 EMSG2(_(e_intern2), "restore_vimvar()");
1468 else
1469 hash_remove(&vimvarht, hi);
1470 }
1471}
1472
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001473#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001474/*
1475 * Evaluate an expression to a list with suggestions.
1476 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001477 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001478 */
1479 list_T *
1480eval_spell_expr(badword, expr)
1481 char_u *badword;
1482 char_u *expr;
1483{
1484 typval_T save_val;
1485 typval_T rettv;
1486 list_T *list = NULL;
1487 char_u *p = skipwhite(expr);
1488
1489 /* Set "v:val" to the bad word. */
1490 prepare_vimvar(VV_VAL, &save_val);
1491 vimvars[VV_VAL].vv_type = VAR_STRING;
1492 vimvars[VV_VAL].vv_str = badword;
1493 if (p_verbose == 0)
1494 ++emsg_off;
1495
1496 if (eval1(&p, &rettv, TRUE) == OK)
1497 {
1498 if (rettv.v_type != VAR_LIST)
1499 clear_tv(&rettv);
1500 else
1501 list = rettv.vval.v_list;
1502 }
1503
1504 if (p_verbose == 0)
1505 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001506 restore_vimvar(VV_VAL, &save_val);
1507
1508 return list;
1509}
1510
1511/*
1512 * "list" is supposed to contain two items: a word and a number. Return the
1513 * word in "pp" and the number as the return value.
1514 * Return -1 if anything isn't right.
1515 * Used to get the good word and score from the eval_spell_expr() result.
1516 */
1517 int
1518get_spellword(list, pp)
1519 list_T *list;
1520 char_u **pp;
1521{
1522 listitem_T *li;
1523
1524 li = list->lv_first;
1525 if (li == NULL)
1526 return -1;
1527 *pp = get_tv_string(&li->li_tv);
1528
1529 li = li->li_next;
1530 if (li == NULL)
1531 return -1;
1532 return get_tv_number(&li->li_tv);
1533}
1534#endif
1535
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001537 * Top level evaluation function.
1538 * Returns an allocated typval_T with the result.
1539 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 */
1541 typval_T *
1542eval_expr(arg, nextcmd)
1543 char_u *arg;
1544 char_u **nextcmd;
1545{
1546 typval_T *tv;
1547
1548 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001549 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550 {
1551 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001553 }
1554
1555 return tv;
1556}
1557
1558
Bram Moolenaar4f688582007-07-24 12:34:30 +00001559#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1560 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 long n;
1577 int len;
1578 int i;
1579 int doesrange;
1580 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001583 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 for (i = 0; i < argc; i++)
1588 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 /* Pass a NULL or empty argument as an empty string */
1590 if (argv[i] == NULL || *argv[i] == NUL)
1591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001592 argvars[i].v_type = VAR_STRING;
1593 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001594 continue;
1595 }
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 /* Recognize a number argument, the others must be strings. */
1598 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1599 if (len != 0 && len == (int)STRLEN(argv[i]))
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_NUMBER;
1602 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 else
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610
1611 if (safe)
1612 {
1613 save_funccalp = save_funccal();
1614 ++sandbox;
1615 }
1616
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1618 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (safe)
1622 {
1623 --sandbox;
1624 restore_funccal(save_funccalp);
1625 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 vim_free(argvars);
1627
1628 if (ret == FAIL)
1629 clear_tv(rettv);
1630
1631 return ret;
1632}
1633
Bram Moolenaar4f688582007-07-24 12:34:30 +00001634# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001636 * Call vimL function "func" and return the result as a string.
1637 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 * Uses argv[argc] for the function arguments.
1639 */
1640 void *
1641call_func_retstr(func, argc, argv, safe)
1642 char_u *func;
1643 int argc;
1644 char_u **argv;
1645 int safe; /* use the sandbox */
1646{
1647 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001648 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649
1650 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1651 return NULL;
1652
1653 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 return retval;
1656}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001657# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658
Bram Moolenaar4f688582007-07-24 12:34:30 +00001659# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001661 * Call vimL function "func" and return the result as a number.
1662 * Returns -1 when calling the function fails.
1663 * Uses argv[argc] for the function arguments.
1664 */
1665 long
1666call_func_retnr(func, argc, argv, safe)
1667 char_u *func;
1668 int argc;
1669 char_u **argv;
1670 int safe; /* use the sandbox */
1671{
1672 typval_T rettv;
1673 long retval;
1674
1675 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1676 return -1;
1677
1678 retval = get_tv_number_chk(&rettv, NULL);
1679 clear_tv(&rettv);
1680 return retval;
1681}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001682# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001683
1684/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001687 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 */
1689 void *
1690call_func_retlist(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697
1698 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1699 return NULL;
1700
1701 if (rettv.v_type != VAR_LIST)
1702 {
1703 clear_tv(&rettv);
1704 return NULL;
1705 }
1706
1707 return rettv.vval.v_list;
1708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
1710
Bram Moolenaar4f688582007-07-24 12:34:30 +00001711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712/*
1713 * Save the current function call pointer, and set it to NULL.
1714 * Used when executing autocommands and for ":source".
1715 */
1716 void *
1717save_funccal()
1718{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 current_funccal = NULL;
1722 return (void *)fc;
1723}
1724
1725 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726restore_funccal(vfc)
1727 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = (funccall_T *)vfc;
1730
1731 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734#if defined(FEAT_PROFILE) || defined(PROTO)
1735/*
1736 * Prepare profiling for entering a child or something else that is not
1737 * counted for the script/function itself.
1738 * Should always be called in pair with prof_child_exit().
1739 */
1740 void
1741prof_child_enter(tm)
1742 proftime_T *tm; /* place to store waittime */
1743{
1744 funccall_T *fc = current_funccal;
1745
1746 if (fc != NULL && fc->func->uf_profiling)
1747 profile_start(&fc->prof_child);
1748 script_prof_save(tm);
1749}
1750
1751/*
1752 * Take care of time spent in a child.
1753 * Should always be called after prof_child_enter().
1754 */
1755 void
1756prof_child_exit(tm)
1757 proftime_T *tm; /* where waittime was stored */
1758{
1759 funccall_T *fc = current_funccal;
1760
1761 if (fc != NULL && fc->func->uf_profiling)
1762 {
1763 profile_end(&fc->prof_child);
1764 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1765 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1766 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1767 }
1768 script_prof_restore(tm);
1769}
1770#endif
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_FOLDING
1774/*
1775 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1776 * it in "*cp". Doesn't give error messages.
1777 */
1778 int
1779eval_foldexpr(arg, cp)
1780 char_u *arg;
1781 int *cp;
1782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 int retval;
1785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001790 if (use_sandbox)
1791 ++sandbox;
1792 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (tv.v_type == VAR_NUMBER)
1800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a string, check if there is a non-digit before
1806 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (!VIM_ISDIGIT(*s) && *s != '-')
1809 *cp = *s++;
1810 retval = atol((char *)s);
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001815 if (use_sandbox)
1816 --sandbox;
1817 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return retval;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let" list all variable values
1825 * ":let var1 var2" list variable values
1826 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 * ":let var += expr" assignment command.
1828 * ":let var -= expr" assignment command.
1829 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 */
1832 void
1833ex_let(eap)
1834 exarg_T *eap;
1835{
1836 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 int var_count = 0;
1841 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 argend = skip_var_list(arg, &var_count, &semicolon);
1847 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001849 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1850 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001854 /*
1855 * ":let" without "=": list variables
1856 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (*arg == '[')
1858 EMSG(_(e_invarg));
1859 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001861 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_glob_vars(&first);
1866 list_buf_vars(&first);
1867 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001870#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_script_vars(&first);
1872 list_func_vars(&first);
1873 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 eap->nextcmd = check_nextcmd(arg);
1876 }
1877 else
1878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op[0] = '=';
1880 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 {
1883 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1884 op[0] = expr[-1]; /* +=, -= or .= */
1885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 {
1893 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --emsg_skip;
1896 }
1897 else if (i != FAIL)
1898 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904}
1905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906/*
1907 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1908 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * When "nextchars" is not NULL it points to a string with characters that
1910 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1911 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Returns OK or FAIL;
1913 */
1914 static int
1915ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1916 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int copy; /* copy values from "tv", don't move */
1919 int semicolon; /* from skip_var_list() */
1920 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
1923 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *item;
1927 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928
1929 if (*arg != '[')
1930 {
1931 /*
1932 * ":let var = expr" or ":for var in list"
1933 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 return FAIL;
1936 return OK;
1937 }
1938
1939 /*
1940 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1941 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001942 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 {
1944 EMSG(_(e_listreq));
1945 return FAIL;
1946 }
1947
1948 i = list_len(l);
1949 if (semicolon == 0 && var_count < i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954 if (var_count - semicolon > i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959
1960 item = l->lv_first;
1961 while (*arg != ']')
1962 {
1963 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 item = item->li_next;
1966 if (arg == NULL)
1967 return FAIL;
1968
1969 arg = skipwhite(arg);
1970 if (*arg == ';')
1971 {
1972 /* Put the rest of the list (may be empty) in the var after ';'.
1973 * Create a new list for this. */
1974 l = list_alloc();
1975 if (l == NULL)
1976 return FAIL;
1977 while (item != NULL)
1978 {
1979 list_append_tv(l, &item->li_tv);
1980 item = item->li_next;
1981 }
1982
1983 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001984 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 ltv.vval.v_list = l;
1986 l->lv_refcount = 1;
1987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1989 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 clear_tv(&ltv);
1991 if (arg == NULL)
1992 return FAIL;
1993 break;
1994 }
1995 else if (*arg != ',' && *arg != ']')
1996 {
1997 EMSG2(_(e_intern2), "ex_let_vars()");
1998 return FAIL;
1999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Skip over assignable variable "var" or list of variables "[var, var]".
2007 * Used for ":let varvar = expr" and ":for varvar in expr".
2008 * For "[var, var]" increment "*var_count" for each variable.
2009 * for "[var, var; var]" set "semicolon".
2010 * Return NULL for an error.
2011 */
2012 static char_u *
2013skip_var_list(arg, var_count, semicolon)
2014 char_u *arg;
2015 int *var_count;
2016 int *semicolon;
2017{
2018 char_u *p, *s;
2019
2020 if (*arg == '[')
2021 {
2022 /* "[var, var]": find the matching ']'. */
2023 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002024 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 {
2026 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2027 s = skip_var_one(p);
2028 if (s == p)
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 ++*var_count;
2034
2035 p = skipwhite(s);
2036 if (*p == ']')
2037 break;
2038 else if (*p == ';')
2039 {
2040 if (*semicolon == 1)
2041 {
2042 EMSG(_("Double ; in list of variables"));
2043 return NULL;
2044 }
2045 *semicolon = 1;
2046 }
2047 else if (*p != ',')
2048 {
2049 EMSG2(_(e_invarg2), p);
2050 return NULL;
2051 }
2052 }
2053 return p + 1;
2054 }
2055 else
2056 return skip_var_one(arg);
2057}
2058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002060 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 static char_u *
2064skip_var_one(arg)
2065 char_u *arg;
2066{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 if (*arg == '@' && arg[1] != NUL)
2068 return arg + 2;
2069 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071}
2072
Bram Moolenaara7043832005-01-21 11:56:39 +00002073/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 * List variables for hashtab "ht" with prefix "prefix".
2075 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashitem_T *hi;
2085 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 int todo;
2087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 di = HI2DI(hi);
2095 if (empty || di->di_tv.v_type != VAR_STRING
2096 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 }
2100}
2101
2102/*
2103 * List global variables.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_glob_vars(first)
2107 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110}
2111
2112/*
2113 * List buffer variables.
2114 */
2115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_buf_vars(first)
2117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 char_u numbuf[NUMBUFLEN];
2120
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2122 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123
2124 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2126 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List window variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_win_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2137 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138}
2139
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140#ifdef FEAT_WINDOWS
2141/*
2142 * List tab page variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_tab_vars(first)
2146 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2149 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150}
2151#endif
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
2154 * List Vim variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_vim_vars(first)
2158 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161}
2162
2163/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164 * List script-local variables, if there is a script.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_script_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2172 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
2176 * List function variables, if there is a function.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_func_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_funccal != NULL)
2183 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 * List variables in "arg".
2189 */
2190 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 exarg_T *eap;
2193 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
2196 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u *name_start;
2200 char_u *arg_subsc;
2201 char_u *tofree;
2202 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
2204 while (!ends_excmd(*arg) && !got_int)
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2210 {
2211 emsg_severe = TRUE;
2212 EMSG(_(e_trailing));
2213 break;
2214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* get_name_len() takes care of expanding curly braces */
2219 name_start = name = arg;
2220 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2221 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* This is mainly to keep test 49 working: when expanding
2224 * curly braces fails overrule the exception error message. */
2225 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 emsg_severe = TRUE;
2228 EMSG2(_(e_invarg2), arg);
2229 break;
2230 }
2231 error = TRUE;
2232 }
2233 else
2234 {
2235 if (tofree != NULL)
2236 name = tofree;
2237 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* handle d.key, l[idx], f(expr) */
2242 arg_subsc = arg;
2243 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'g': list_glob_vars(first); break;
2252 case 'b': list_buf_vars(first); break;
2253 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'v': list_vim_vars(first); break;
2258 case 's': list_script_vars(first); break;
2259 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 default:
2261 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
2264 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 {
2266 char_u numbuf[NUMBUFLEN];
2267 char_u *tf;
2268 int c;
2269 char_u *s;
2270
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002271 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 c = *arg;
2273 *arg = NUL;
2274 list_one_var_a((char_u *)"",
2275 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 tv.v_type,
2277 s == NULL ? (char_u *)"" : s,
2278 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 *arg = c;
2280 vim_free(tf);
2281 }
2282 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
2293 return arg;
2294}
2295
2296/*
2297 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2298 * Returns a pointer to the char just after the var name.
2299 * Returns NULL if there is an error.
2300 */
2301 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308{
2309 int c1;
2310 char_u *name;
2311 char_u *p;
2312 char_u *arg_end = NULL;
2313 int len;
2314 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316
2317 /*
2318 * ":let $VAR = expr": Set environment variable.
2319 */
2320 if (*arg == '$')
2321 {
2322 /* Find the end of the name. */
2323 ++arg;
2324 name = arg;
2325 len = get_env_len(&arg);
2326 if (len == 0)
2327 EMSG2(_(e_invarg2), name - 1);
2328 else
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && (*op == '+' || *op == '-'))
2331 EMSG2(_(e_letwrong), op);
2332 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002335 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
2337 c1 = name[len];
2338 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 p = get_tv_string_chk(tv);
2340 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 {
2342 int mustfree = FALSE;
2343 char_u *s = vim_getenv(name, &mustfree);
2344
2345 if (s != NULL)
2346 {
2347 p = tofree = concat_str(s, p);
2348 if (mustfree)
2349 vim_free(s);
2350 }
2351 }
2352 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (STRICMP(name, "HOME") == 0)
2356 init_homedir();
2357 else if (didset_vim && STRICMP(name, "VIM") == 0)
2358 didset_vim = FALSE;
2359 else if (didset_vimruntime
2360 && STRICMP(name, "VIMRUNTIME") == 0)
2361 didset_vimruntime = FALSE;
2362 arg_end = arg;
2363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 }
2367 }
2368 }
2369
2370 /*
2371 * ":let &option = expr": Set option value.
2372 * ":let &l:option = expr": Set local option value.
2373 * ":let &g:option = expr": Set global option value.
2374 */
2375 else if (*arg == '&')
2376 {
2377 /* Find the end of the name. */
2378 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 if (p == NULL || (endchars != NULL
2380 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 long n;
2385 int opt_type;
2386 long numval;
2387 char_u *stringval = NULL;
2388 char_u *s;
2389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 c1 = *p;
2391 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392
2393 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 s = get_tv_string_chk(tv); /* != NULL if number or string */
2395 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 opt_type = get_option_value(arg, &numval,
2398 &stringval, opt_flags);
2399 if ((opt_type == 1 && *op == '.')
2400 || (opt_type == 0 && *op != '.'))
2401 EMSG2(_(e_letwrong), op);
2402 else
2403 {
2404 if (opt_type == 1) /* number */
2405 {
2406 if (*op == '+')
2407 n = numval + n;
2408 else
2409 n = numval - n;
2410 }
2411 else if (opt_type == 0 && stringval != NULL) /* string */
2412 {
2413 s = concat_str(stringval, s);
2414 vim_free(stringval);
2415 stringval = s;
2416 }
2417 }
2418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 if (s != NULL)
2420 {
2421 set_option_value(arg, n, s, opt_flags);
2422 arg_end = p;
2423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let @r = expr": Set register contents.
2431 */
2432 else if (*arg == '@')
2433 {
2434 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 if (op != NULL && (*op == '+' || *op == '-'))
2436 EMSG2(_(e_letwrong), op);
2437 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 char_u *s;
2444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 p = get_tv_string_chk(tv);
2446 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002448 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (s != NULL)
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(s);
2453 }
2454 }
2455 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 arg_end = arg + 1;
2459 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002470 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
2486 else
2487 EMSG2(_(e_invarg2), arg);
2488
2489 return arg_end;
2490}
2491
2492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2494 */
2495 static int
2496check_changedtick(arg)
2497 char_u *arg;
2498{
2499 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2500 {
2501 EMSG2(_(e_readonlyvar), arg);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Get an lval: variable, Dict item or List item that can be assigned a value
2509 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2510 * "name.key", "name.key[expr]" etc.
2511 * Indexing only works if "name" is an existing List or Dictionary.
2512 * "name" points to the start of the name.
2513 * If "rettv" is not NULL it points to the value to be assigned.
2514 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2515 * wrong; must end in space or cmd separator.
2516 *
2517 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002518 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
2521 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T *rettv;
2525 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int unlet;
2527 int skip;
2528 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 char_u *expr_start, *expr_end;
2533 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 dictitem_T *v;
2535 typval_T var1;
2536 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545
2546 if (skip)
2547 {
2548 /* When skipping just find the end of the name. */
2549 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 }
2552
2553 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (expr_start != NULL)
2556 {
2557 /* Don't expand the name when we already know there is an error. */
2558 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2559 && *p != '[' && *p != '.')
2560 {
2561 EMSG(_(e_trailing));
2562 return NULL;
2563 }
2564
2565 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2566 if (lp->ll_exp_name == NULL)
2567 {
2568 /* Report an invalid expression in braces, unless the
2569 * expression evaluation has been cancelled due to an
2570 * aborting error, an interrupt, or an exception. */
2571 if (!aborting() && !quiet)
2572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002573 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 EMSG2(_(e_invarg2), name);
2575 return NULL;
2576 }
2577 }
2578 lp->ll_name = lp->ll_exp_name;
2579 }
2580 else
2581 lp->ll_name = name;
2582
2583 /* Without [idx] or .key we are done. */
2584 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2585 return p;
2586
2587 cc = *p;
2588 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002589 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (v == NULL && !quiet)
2591 EMSG2(_(e_undefvar), lp->ll_name);
2592 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (v == NULL)
2594 return NULL;
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /*
2597 * Loop until no more [idx] or .key is following.
2598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2603 && !(lp->ll_tv->v_type == VAR_DICT
2604 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E689: Can only index a List or Dictionary"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E708: [:] must come last"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 len = -1;
2618 if (*p == '.')
2619 {
2620 key = p + 1;
2621 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2622 ;
2623 if (len == 0)
2624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_(e_emptykey));
2627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629 p = key + len;
2630 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p == ':')
2636 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 empty1 = FALSE;
2640 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 if (get_tv_string_chk(&var1) == NULL)
2643 {
2644 /* not a number or string */
2645 clear_tv(&var1);
2646 return NULL;
2647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649
2650 /* Optionally get the second index [ :expr]. */
2651 if (*p == ':')
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (rettv != NULL && (rettv->v_type != VAR_LIST
2662 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = skipwhite(p + 1);
2671 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var2) == NULL)
2683 {
2684 /* not a number or string */
2685 if (!empty1)
2686 clear_tv(&var1);
2687 clear_tv(&var2);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Skip to past ']'. */
2708 ++p;
2709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
2713 if (len == -1)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*key == NUL)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 lp->ll_list = NULL;
2726 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002727 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728
2729 /* When assigning to g: check that a function and variable name is
2730 * valid. */
2731 if (rettv != NULL && lp->ll_dict == &globvardict)
2732 {
2733 if (rettv->v_type == VAR_FUNC
2734 && var_check_func_name(key, lp->ll_di == NULL))
2735 return NULL;
2736 if (!valid_varname(key))
2737 return NULL;
2738 }
2739
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002742 /* Can't add "v:" variable. */
2743 if (lp->ll_dict == &vimvardict)
2744 {
2745 EMSG2(_(e_illvar), name);
2746 return NULL;
2747 }
2748
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002749 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (len == -1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 if (len == -1)
2763 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 p = NULL;
2766 break;
2767 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* existing variable, need to check if it can be changed */
2769 else if (var_check_ro(lp->ll_di->di_flags, name))
2770 return NULL;
2771
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (len == -1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 else
2777 {
2778 /*
2779 * Get the number and item for the only or first index of the List.
2780 */
2781 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 else
2784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 clear_tv(&var1);
2787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_list = lp->ll_tv->vval.v_list;
2790 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2791 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002793 if (lp->ll_n1 < 0)
2794 {
2795 lp->ll_n1 = 0;
2796 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2797 }
2798 }
2799 if (lp->ll_li == NULL)
2800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002803 if (!quiet)
2804 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 }
2807
2808 /*
2809 * May need to find the item or absolute index for the second
2810 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 * When no index given: "lp->ll_empty2" is TRUE.
2812 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002816 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002822 {
2823 if (!quiet)
2824 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002826 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 }
2829
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2831 if (lp->ll_n1 < 0)
2832 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2833 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002834 {
2835 if (!quiet)
2836 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 return p;
2846}
2847
2848/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 */
2851 static void
2852clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854{
2855 vim_free(lp->ll_exp_name);
2856 vim_free(lp->ll_newkey);
2857}
2858
2859/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002860 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 */
2864 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002870 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871{
2872 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 listitem_T *ri;
2874 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875
2876 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 cc = *endp;
2881 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882 if (op != NULL && *op != '=')
2883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002885
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002887 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002888 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 {
2890 if (tv_op(&tv, rettv, op) == OK)
2891 set_var(lp->ll_name, &tv, FALSE);
2892 clear_tv(&tv);
2893 }
2894 }
2895 else
2896 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 else if (tv_check_lock(lp->ll_newkey == NULL
2901 ? lp->ll_tv->v_lock
2902 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2903 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 else if (lp->ll_range)
2905 {
2906 /*
2907 * Assign the List values to the list items.
2908 */
2909 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911 if (op != NULL && *op != '=')
2912 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2913 else
2914 {
2915 clear_tv(&lp->ll_li->li_tv);
2916 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2917 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 ri = ri->li_next;
2919 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2920 break;
2921 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002924 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 ri = NULL;
2927 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 lp->ll_li = lp->ll_li->li_next;
2931 ++lp->ll_n1;
2932 }
2933 if (ri != NULL)
2934 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 else if (lp->ll_empty2
2936 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 : lp->ll_n1 != lp->ll_n2)
2938 EMSG(_("E711: List value has not enough items"));
2939 }
2940 else
2941 {
2942 /*
2943 * Assign to a List or Dictionary item.
2944 */
2945 if (lp->ll_newkey != NULL)
2946 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 if (op != NULL && *op != '=')
2948 {
2949 EMSG2(_(e_letwrong), op);
2950 return;
2951 }
2952
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002954 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 if (di == NULL)
2956 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002957 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2958 {
2959 vim_free(di);
2960 return;
2961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 else if (op != NULL && *op != '=')
2965 {
2966 tv_op(lp->ll_tv, rettv, op);
2967 return;
2968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002971
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 /*
2973 * Assign the value to the variable or list item.
2974 */
2975 if (copy)
2976 copy_tv(rettv, lp->ll_tv);
2977 else
2978 {
2979 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002980 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 }
2983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984}
2985
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2988 * Returns OK or FAIL.
2989 */
2990 static int
2991tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002992 typval_T *tv1;
2993 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 char_u *op;
2995{
2996 long n;
2997 char_u numbuf[NUMBUFLEN];
2998 char_u *s;
2999
3000 /* Can't do anything with a Funcref or a Dict on the right. */
3001 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3002 {
3003 switch (tv1->v_type)
3004 {
3005 case VAR_DICT:
3006 case VAR_FUNC:
3007 break;
3008
3009 case VAR_LIST:
3010 if (*op != '+' || tv2->v_type != VAR_LIST)
3011 break;
3012 /* List += List */
3013 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3014 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3015 return OK;
3016
3017 case VAR_NUMBER:
3018 case VAR_STRING:
3019 if (tv2->v_type == VAR_LIST)
3020 break;
3021 if (*op == '+' || *op == '-')
3022 {
3023 /* nr += nr or nr -= nr*/
3024 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025#ifdef FEAT_FLOAT
3026 if (tv2->v_type == VAR_FLOAT)
3027 {
3028 float_T f = n;
3029
3030 if (*op == '+')
3031 f += tv2->vval.v_float;
3032 else
3033 f -= tv2->vval.v_float;
3034 clear_tv(tv1);
3035 tv1->v_type = VAR_FLOAT;
3036 tv1->vval.v_float = f;
3037 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#endif
3040 {
3041 if (*op == '+')
3042 n += get_tv_number(tv2);
3043 else
3044 n -= get_tv_number(tv2);
3045 clear_tv(tv1);
3046 tv1->v_type = VAR_NUMBER;
3047 tv1->vval.v_number = n;
3048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 }
3050 else
3051 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003052 if (tv2->v_type == VAR_FLOAT)
3053 break;
3054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003055 /* str .= str */
3056 s = get_tv_string(tv1);
3057 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3058 clear_tv(tv1);
3059 tv1->v_type = VAR_STRING;
3060 tv1->vval.v_string = s;
3061 }
3062 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063
3064#ifdef FEAT_FLOAT
3065 case VAR_FLOAT:
3066 {
3067 float_T f;
3068
3069 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3070 && tv2->v_type != VAR_NUMBER
3071 && tv2->v_type != VAR_STRING))
3072 break;
3073 if (tv2->v_type == VAR_FLOAT)
3074 f = tv2->vval.v_float;
3075 else
3076 f = get_tv_number(tv2);
3077 if (*op == '+')
3078 tv1->vval.v_float += f;
3079 else
3080 tv1->vval.v_float -= f;
3081 }
3082 return OK;
3083#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 }
3086
3087 EMSG2(_(e_letwrong), op);
3088 return FAIL;
3089}
3090
3091/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092 * Add a watcher to a list.
3093 */
3094 static void
3095list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 list_T *l;
3097 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003098{
3099 lw->lw_next = l->lv_watch;
3100 l->lv_watch = lw;
3101}
3102
3103/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003104 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105 * No warning when it isn't found...
3106 */
3107 static void
3108list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 list_T *l;
3110 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111{
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113
3114 lwp = &l->lv_watch;
3115 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3116 {
3117 if (lw == lwrem)
3118 {
3119 *lwp = lw->lw_next;
3120 break;
3121 }
3122 lwp = &lw->lw_next;
3123 }
3124}
3125
3126/*
3127 * Just before removing an item from a list: advance watchers to the next
3128 * item.
3129 */
3130 static void
3131list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 list_T *l;
3133 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134{
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136
3137 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3138 if (lw->lw_item == item)
3139 lw->lw_item = item->li_next;
3140}
3141
3142/*
3143 * Evaluate the expression used in a ":for var in expr" command.
3144 * "arg" points to "var".
3145 * Set "*errp" to TRUE for an error, FALSE otherwise;
3146 * Return a pointer that holds the info. Null when there is an error.
3147 */
3148 void *
3149eval_for_line(arg, errp, nextcmdp, skip)
3150 char_u *arg;
3151 int *errp;
3152 char_u **nextcmdp;
3153 int skip;
3154{
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 typval_T tv;
3158 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159
3160 *errp = TRUE; /* default: there is an error */
3161
Bram Moolenaar33570922005-01-25 22:26:29 +00003162 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003163 if (fi == NULL)
3164 return NULL;
3165
3166 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3167 if (expr == NULL)
3168 return fi;
3169
3170 expr = skipwhite(expr);
3171 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3172 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003173 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 return fi;
3175 }
3176
3177 if (skip)
3178 ++emsg_skip;
3179 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3180 {
3181 *errp = FALSE;
3182 if (!skip)
3183 {
3184 l = tv.vval.v_list;
3185 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003186 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003188 clear_tv(&tv);
3189 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 else
3191 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003192 /* No need to increment the refcount, it's already set for the
3193 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 fi->fi_list = l;
3195 list_add_watch(l, &fi->fi_lw);
3196 fi->fi_lw.lw_item = l->lv_first;
3197 }
3198 }
3199 }
3200 if (skip)
3201 --emsg_skip;
3202
3203 return fi;
3204}
3205
3206/*
3207 * Use the first item in a ":for" list. Advance to the next.
3208 * Assign the values to the variable (list). "arg" points to the first one.
3209 * Return TRUE when a valid item was found, FALSE when at end of list or
3210 * something wrong.
3211 */
3212 int
3213next_for_item(fi_void, arg)
3214 void *fi_void;
3215 char_u *arg;
3216{
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220
3221 item = fi->fi_lw.lw_item;
3222 if (item == NULL)
3223 result = FALSE;
3224 else
3225 {
3226 fi->fi_lw.lw_item = item->li_next;
3227 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3228 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3229 }
3230 return result;
3231}
3232
3233/*
3234 * Free the structure used to store info used by ":for".
3235 */
3236 void
3237free_for_info(fi_void)
3238 void *fi_void;
3239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003242 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003243 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 list_unref(fi->fi_list);
3246 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 vim_free(fi);
3248}
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3251
3252 void
3253set_context_for_expression(xp, arg, cmdidx)
3254 expand_T *xp;
3255 char_u *arg;
3256 cmdidx_T cmdidx;
3257{
3258 int got_eq = FALSE;
3259 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 if (cmdidx == CMD_let)
3263 {
3264 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003265 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 {
3267 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003268 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 {
3270 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 if (vim_iswhite(*p))
3273 break;
3274 }
3275 return;
3276 }
3277 }
3278 else
3279 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3280 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 while ((xp->xp_pattern = vim_strpbrk(arg,
3282 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3283 {
3284 c = *xp->xp_pattern;
3285 if (c == '&')
3286 {
3287 c = xp->xp_pattern[1];
3288 if (c == '&')
3289 {
3290 ++xp->xp_pattern;
3291 xp->xp_context = cmdidx != CMD_let || got_eq
3292 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3293 }
3294 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003297 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3298 xp->xp_pattern += 2;
3299
3300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 else if (c == '$')
3303 {
3304 /* environment variable */
3305 xp->xp_context = EXPAND_ENV_VARS;
3306 }
3307 else if (c == '=')
3308 {
3309 got_eq = TRUE;
3310 xp->xp_context = EXPAND_EXPRESSION;
3311 }
3312 else if (c == '<'
3313 && xp->xp_context == EXPAND_FUNCTIONS
3314 && vim_strchr(xp->xp_pattern, '(') == NULL)
3315 {
3316 /* Function name can start with "<SNR>" */
3317 break;
3318 }
3319 else if (cmdidx != CMD_let || got_eq)
3320 {
3321 if (c == '"') /* string */
3322 {
3323 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3324 if (c == '\\' && xp->xp_pattern[1] != NUL)
3325 ++xp->xp_pattern;
3326 xp->xp_context = EXPAND_NOTHING;
3327 }
3328 else if (c == '\'') /* literal string */
3329 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003330 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3332 /* skip */ ;
3333 xp->xp_context = EXPAND_NOTHING;
3334 }
3335 else if (c == '|')
3336 {
3337 if (xp->xp_pattern[1] == '|')
3338 {
3339 ++xp->xp_pattern;
3340 xp->xp_context = EXPAND_EXPRESSION;
3341 }
3342 else
3343 xp->xp_context = EXPAND_COMMANDS;
3344 }
3345 else
3346 xp->xp_context = EXPAND_EXPRESSION;
3347 }
3348 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 /* Doesn't look like something valid, expand as an expression
3350 * anyway. */
3351 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 arg = xp->xp_pattern;
3353 if (*arg != NUL)
3354 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3355 /* skip */ ;
3356 }
3357 xp->xp_pattern = arg;
3358}
3359
3360#endif /* FEAT_CMDL_COMPL */
3361
3362/*
3363 * ":1,25call func(arg1, arg2)" function call.
3364 */
3365 void
3366ex_call(eap)
3367 exarg_T *eap;
3368{
3369 char_u *arg = eap->arg;
3370 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003372 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 linenr_T lnum;
3376 int doesrange;
3377 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003380 if (eap->skip)
3381 {
3382 /* trans_function_name() doesn't work well when skipping, use eval0()
3383 * instead to skip to any following command, e.g. for:
3384 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003385 ++emsg_skip;
3386 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3387 clear_tv(&rettv);
3388 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003389 return;
3390 }
3391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003392 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003393 if (fudi.fd_newkey != NULL)
3394 {
3395 /* Still need to give an error message for missing key. */
3396 EMSG2(_(e_dictkey), fudi.fd_newkey);
3397 vim_free(fudi.fd_newkey);
3398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003399 if (tofree == NULL)
3400 return;
3401
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003402 /* Increase refcount on dictionary, it could get deleted when evaluating
3403 * the arguments. */
3404 if (fudi.fd_dict != NULL)
3405 ++fudi.fd_dict->dv_refcount;
3406
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003408 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003409 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar532c7802005-01-27 14:44:31 +00003411 /* Skip white space to allow ":call func ()". Not good, but required for
3412 * backward compatibility. */
3413 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003414 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 if (*startarg != '(')
3417 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003418 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 goto end;
3420 }
3421
3422 /*
3423 * When skipping, evaluate the function once, to find the end of the
3424 * arguments.
3425 * When the function takes a range, this is discovered after the first
3426 * call, and the loop is broken.
3427 */
3428 if (eap->skip)
3429 {
3430 ++emsg_skip;
3431 lnum = eap->line2; /* do it once, also with an invalid range */
3432 }
3433 else
3434 lnum = eap->line1;
3435 for ( ; lnum <= eap->line2; ++lnum)
3436 {
3437 if (!eap->skip && eap->addr_count > 0)
3438 {
3439 curwin->w_cursor.lnum = lnum;
3440 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003441#ifdef FEAT_VIRTUALEDIT
3442 curwin->w_cursor.coladd = 0;
3443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003446 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003447 eap->line1, eap->line2, &doesrange,
3448 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 failed = TRUE;
3451 break;
3452 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003453
3454 /* Handle a function returning a Funcref, Dictionary or List. */
3455 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3456 {
3457 failed = TRUE;
3458 break;
3459 }
3460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (doesrange || eap->skip)
3463 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003466 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003468 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (aborting())
3470 break;
3471 }
3472 if (eap->skip)
3473 --emsg_skip;
3474
3475 if (!failed)
3476 {
3477 /* Check for trailing illegal characters and a following command. */
3478 if (!ends_excmd(*arg))
3479 {
3480 emsg_severe = TRUE;
3481 EMSG(_(e_trailing));
3482 }
3483 else
3484 eap->nextcmd = check_nextcmd(arg);
3485 }
3486
3487end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003488 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490}
3491
3492/*
3493 * ":unlet[!] var1 ... " command.
3494 */
3495 void
3496ex_unlet(eap)
3497 exarg_T *eap;
3498{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003499 ex_unletlock(eap, eap->arg, 0);
3500}
3501
3502/*
3503 * ":lockvar" and ":unlockvar" commands
3504 */
3505 void
3506ex_lockvar(eap)
3507 exarg_T *eap;
3508{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 int deep = 2;
3511
3512 if (eap->forceit)
3513 deep = -1;
3514 else if (vim_isdigit(*arg))
3515 {
3516 deep = getdigits(&arg);
3517 arg = skipwhite(arg);
3518 }
3519
3520 ex_unletlock(eap, arg, deep);
3521}
3522
3523/*
3524 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3525 */
3526 static void
3527ex_unletlock(eap, argstart, deep)
3528 exarg_T *eap;
3529 char_u *argstart;
3530 int deep;
3531{
3532 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 do
3538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003540 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3541 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003542 if (lv.ll_name == NULL)
3543 error = TRUE; /* error but continue parsing */
3544 if (name_end == NULL || (!vim_iswhite(*name_end)
3545 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003547 if (name_end != NULL)
3548 {
3549 emsg_severe = TRUE;
3550 EMSG(_(e_trailing));
3551 }
3552 if (!(eap->skip || error))
3553 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 break;
3555 }
3556
3557 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558 {
3559 if (eap->cmdidx == CMD_unlet)
3560 {
3561 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3562 error = TRUE;
3563 }
3564 else
3565 {
3566 if (do_lock_var(&lv, name_end, deep,
3567 eap->cmdidx == CMD_lockvar) == FAIL)
3568 error = TRUE;
3569 }
3570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 if (!eap->skip)
3573 clear_lval(&lv);
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 arg = skipwhite(name_end);
3576 } while (!ends_excmd(*arg));
3577
3578 eap->nextcmd = check_nextcmd(arg);
3579}
3580
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 int forceit;
3586{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 int ret = OK;
3588 int cc;
3589
3590 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 cc = *name_end;
3593 *name_end = NUL;
3594
3595 /* Normal name or expanded name. */
3596 if (check_changedtick(lp->ll_name))
3597 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003602 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3603 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 else if (lp->ll_range)
3605 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607
3608 /* Delete a range of List items. */
3609 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3610 {
3611 li = lp->ll_li->li_next;
3612 listitem_remove(lp->ll_list, lp->ll_li);
3613 lp->ll_li = li;
3614 ++lp->ll_n1;
3615 }
3616 }
3617 else
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 /* unlet a List item. */
3621 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003624 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 }
3626
3627 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628}
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
3634 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 hashtab_T *ht;
3640 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003641 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003642 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar33570922005-01-25 22:26:29 +00003644 ht = find_var_ht(name, &varname);
3645 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 hi = hash_find(ht, varname);
3648 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003650 di = HI2DI(hi);
3651 if (var_check_fixed(di->di_flags, name)
3652 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 return FAIL;
3654 delete_var(ht, hi);
3655 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 if (forceit)
3659 return OK;
3660 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 return FAIL;
3662}
3663
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664/*
3665 * Lock or unlock variable indicated by "lp".
3666 * "deep" is the levels to go (-1 for unlimited);
3667 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3668 */
3669 static int
3670do_lock_var(lp, name_end, deep, lock)
3671 lval_T *lp;
3672 char_u *name_end;
3673 int deep;
3674 int lock;
3675{
3676 int ret = OK;
3677 int cc;
3678 dictitem_T *di;
3679
3680 if (deep == 0) /* nothing to do */
3681 return OK;
3682
3683 if (lp->ll_tv == NULL)
3684 {
3685 cc = *name_end;
3686 *name_end = NUL;
3687
3688 /* Normal name or expanded name. */
3689 if (check_changedtick(lp->ll_name))
3690 ret = FAIL;
3691 else
3692 {
3693 di = find_var(lp->ll_name, NULL);
3694 if (di == NULL)
3695 ret = FAIL;
3696 else
3697 {
3698 if (lock)
3699 di->di_flags |= DI_FLAGS_LOCK;
3700 else
3701 di->di_flags &= ~DI_FLAGS_LOCK;
3702 item_lock(&di->di_tv, deep, lock);
3703 }
3704 }
3705 *name_end = cc;
3706 }
3707 else if (lp->ll_range)
3708 {
3709 listitem_T *li = lp->ll_li;
3710
3711 /* (un)lock a range of List items. */
3712 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3713 {
3714 item_lock(&li->li_tv, deep, lock);
3715 li = li->li_next;
3716 ++lp->ll_n1;
3717 }
3718 }
3719 else if (lp->ll_list != NULL)
3720 /* (un)lock a List item. */
3721 item_lock(&lp->ll_li->li_tv, deep, lock);
3722 else
3723 /* un(lock) a Dictionary item. */
3724 item_lock(&lp->ll_di->di_tv, deep, lock);
3725
3726 return ret;
3727}
3728
3729/*
3730 * Lock or unlock an item. "deep" is nr of levels to go.
3731 */
3732 static void
3733item_lock(tv, deep, lock)
3734 typval_T *tv;
3735 int deep;
3736 int lock;
3737{
3738 static int recurse = 0;
3739 list_T *l;
3740 listitem_T *li;
3741 dict_T *d;
3742 hashitem_T *hi;
3743 int todo;
3744
3745 if (recurse >= DICT_MAXNEST)
3746 {
3747 EMSG(_("E743: variable nested too deep for (un)lock"));
3748 return;
3749 }
3750 if (deep == 0)
3751 return;
3752 ++recurse;
3753
3754 /* lock/unlock the item itself */
3755 if (lock)
3756 tv->v_lock |= VAR_LOCKED;
3757 else
3758 tv->v_lock &= ~VAR_LOCKED;
3759
3760 switch (tv->v_type)
3761 {
3762 case VAR_LIST:
3763 if ((l = tv->vval.v_list) != NULL)
3764 {
3765 if (lock)
3766 l->lv_lock |= VAR_LOCKED;
3767 else
3768 l->lv_lock &= ~VAR_LOCKED;
3769 if (deep < 0 || deep > 1)
3770 /* recursive: lock/unlock the items the List contains */
3771 for (li = l->lv_first; li != NULL; li = li->li_next)
3772 item_lock(&li->li_tv, deep - 1, lock);
3773 }
3774 break;
3775 case VAR_DICT:
3776 if ((d = tv->vval.v_dict) != NULL)
3777 {
3778 if (lock)
3779 d->dv_lock |= VAR_LOCKED;
3780 else
3781 d->dv_lock &= ~VAR_LOCKED;
3782 if (deep < 0 || deep > 1)
3783 {
3784 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003785 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3787 {
3788 if (!HASHITEM_EMPTY(hi))
3789 {
3790 --todo;
3791 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3792 }
3793 }
3794 }
3795 }
3796 }
3797 --recurse;
3798}
3799
Bram Moolenaara40058a2005-07-11 22:42:07 +00003800/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003801 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3802 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003803 */
3804 static int
3805tv_islocked(tv)
3806 typval_T *tv;
3807{
3808 return (tv->v_lock & VAR_LOCKED)
3809 || (tv->v_type == VAR_LIST
3810 && tv->vval.v_list != NULL
3811 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3812 || (tv->v_type == VAR_DICT
3813 && tv->vval.v_dict != NULL
3814 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3815}
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3818/*
3819 * Delete all "menutrans_" variables.
3820 */
3821 void
3822del_menutrans_vars()
3823{
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003825 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003828 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003830 {
3831 if (!HASHITEM_EMPTY(hi))
3832 {
3833 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3835 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 }
3837 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839}
3840#endif
3841
3842#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3843
3844/*
3845 * Local string buffer for the next two functions to store a variable name
3846 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3847 * get_user_var_name().
3848 */
3849
3850static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3851
3852static char_u *varnamebuf = NULL;
3853static int varnamebuflen = 0;
3854
3855/*
3856 * Function to concatenate a prefix and a variable name.
3857 */
3858 static char_u *
3859cat_prefix_varname(prefix, name)
3860 int prefix;
3861 char_u *name;
3862{
3863 int len;
3864
3865 len = (int)STRLEN(name) + 3;
3866 if (len > varnamebuflen)
3867 {
3868 vim_free(varnamebuf);
3869 len += 10; /* some additional space */
3870 varnamebuf = alloc(len);
3871 if (varnamebuf == NULL)
3872 {
3873 varnamebuflen = 0;
3874 return NULL;
3875 }
3876 varnamebuflen = len;
3877 }
3878 *varnamebuf = prefix;
3879 varnamebuf[1] = ':';
3880 STRCPY(varnamebuf + 2, name);
3881 return varnamebuf;
3882}
3883
3884/*
3885 * Function given to ExpandGeneric() to obtain the list of user defined
3886 * (global/buffer/window/built-in) variable names.
3887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 char_u *
3889get_user_var_name(xp, idx)
3890 expand_T *xp;
3891 int idx;
3892{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003893 static long_u gdone;
3894 static long_u bdone;
3895 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003896#ifdef FEAT_WINDOWS
3897 static long_u tdone;
3898#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003899 static int vidx;
3900 static hashitem_T *hi;
3901 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003904 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003905 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003906#ifdef FEAT_WINDOWS
3907 tdone = 0;
3908#endif
3909 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003910
3911 /* Global variables */
3912 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003914 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003916 else
3917 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 while (HASHITEM_EMPTY(hi))
3919 ++hi;
3920 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3921 return cat_prefix_varname('g', hi->hi_key);
3922 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924
3925 /* b: variables */
3926 ht = &curbuf->b_vars.dv_hashtab;
3927 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 else
3932 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 while (HASHITEM_EMPTY(hi))
3934 ++hi;
3935 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 return (char_u *)"b:changedtick";
3941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942
3943 /* w: variables */
3944 ht = &curwin->w_vars.dv_hashtab;
3945 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003947 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003949 else
3950 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 while (HASHITEM_EMPTY(hi))
3952 ++hi;
3953 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003956#ifdef FEAT_WINDOWS
3957 /* t: variables */
3958 ht = &curtab->tp_vars.dv_hashtab;
3959 if (tdone < ht->ht_used)
3960 {
3961 if (tdone++ == 0)
3962 hi = ht->ht_array;
3963 else
3964 ++hi;
3965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('t', hi->hi_key);
3968 }
3969#endif
3970
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 /* v: variables */
3972 if (vidx < VV_LEN)
3973 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 vim_free(varnamebuf);
3976 varnamebuf = NULL;
3977 varnamebuflen = 0;
3978 return NULL;
3979}
3980
3981#endif /* FEAT_CMDL_COMPL */
3982
3983/*
3984 * types for expressions.
3985 */
3986typedef enum
3987{
3988 TYPE_UNKNOWN = 0
3989 , TYPE_EQUAL /* == */
3990 , TYPE_NEQUAL /* != */
3991 , TYPE_GREATER /* > */
3992 , TYPE_GEQUAL /* >= */
3993 , TYPE_SMALLER /* < */
3994 , TYPE_SEQUAL /* <= */
3995 , TYPE_MATCH /* =~ */
3996 , TYPE_NOMATCH /* !~ */
3997} exptype_T;
3998
3999/*
4000 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4003 */
4004
4005/*
4006 * Handle zero level expression.
4007 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004009 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * Return OK or FAIL.
4011 */
4012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 char_u **nextcmd;
4017 int evaluate;
4018{
4019 int ret;
4020 char_u *p;
4021
4022 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (ret == FAIL || !ends_excmd(*p))
4025 {
4026 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 /*
4029 * Report the invalid expression unless the expression evaluation has
4030 * been cancelled due to an aborting error, an interrupt, or an
4031 * exception.
4032 */
4033 if (!aborting())
4034 EMSG2(_(e_invexpr2), arg);
4035 ret = FAIL;
4036 }
4037 if (nextcmd != NULL)
4038 *nextcmd = check_nextcmd(p);
4039
4040 return ret;
4041}
4042
4043/*
4044 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004045 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 *
4047 * "arg" must point to the first non-white of the expression.
4048 * "arg" is advanced to the next non-white after the recognized expression.
4049 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004050 * Note: "rettv.v_lock" is not set.
4051 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * Return OK or FAIL.
4053 */
4054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int evaluate;
4059{
4060 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 /*
4064 * Get the first variable.
4065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 return FAIL;
4068
4069 if ((*arg)[0] == '?')
4070 {
4071 result = FALSE;
4072 if (evaluate)
4073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074 int error = FALSE;
4075
4076 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079 if (error)
4080 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082
4083 /*
4084 * Get the second variable.
4085 */
4086 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 /*
4091 * Check for the ":".
4092 */
4093 if ((*arg)[0] != ':')
4094 {
4095 EMSG(_("E109: Missing ':' after '?'"));
4096 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099 }
4100
4101 /*
4102 * Get the third variable.
4103 */
4104 *arg = skipwhite(*arg + 1);
4105 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4106 {
4107 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110 }
4111 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 return OK;
4116}
4117
4118/*
4119 * Handle first level expression:
4120 * expr2 || expr2 || expr2 logical OR
4121 *
4122 * "arg" must point to the first non-white of the expression.
4123 * "arg" is advanced to the next non-white after the recognized expression.
4124 *
4125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int evaluate;
4132{
Bram Moolenaar33570922005-01-25 22:26:29 +00004133 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 long result;
4135 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004136 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 /*
4139 * Get the first variable.
4140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143
4144 /*
4145 * Repeat until there is no following "||".
4146 */
4147 first = TRUE;
4148 result = FALSE;
4149 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4150 {
4151 if (evaluate && first)
4152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 if (error)
4157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 first = FALSE;
4159 }
4160
4161 /*
4162 * Get the second variable.
4163 */
4164 *arg = skipwhite(*arg + 2);
4165 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4166 return FAIL;
4167
4168 /*
4169 * Compute the result.
4170 */
4171 if (evaluate && !result)
4172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (error)
4177 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 if (evaluate)
4180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 rettv->v_type = VAR_NUMBER;
4182 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 }
4185
4186 return OK;
4187}
4188
4189/*
4190 * Handle second level expression:
4191 * expr3 && expr3 && expr3 logical AND
4192 *
4193 * "arg" must point to the first non-white of the expression.
4194 * "arg" is advanced to the next non-white after the recognized expression.
4195 *
4196 * Return OK or FAIL.
4197 */
4198 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 int evaluate;
4203{
Bram Moolenaar33570922005-01-25 22:26:29 +00004204 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 long result;
4206 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 /*
4210 * Get the first variable.
4211 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214
4215 /*
4216 * Repeat until there is no following "&&".
4217 */
4218 first = TRUE;
4219 result = TRUE;
4220 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4221 {
4222 if (evaluate && first)
4223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 if (error)
4228 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 first = FALSE;
4230 }
4231
4232 /*
4233 * Get the second variable.
4234 */
4235 *arg = skipwhite(*arg + 2);
4236 if (eval4(arg, &var2, evaluate && result) == FAIL)
4237 return FAIL;
4238
4239 /*
4240 * Compute the result.
4241 */
4242 if (evaluate && result)
4243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 if (evaluate)
4251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 rettv->v_type = VAR_NUMBER;
4253 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256
4257 return OK;
4258}
4259
4260/*
4261 * Handle third level expression:
4262 * var1 == var2
4263 * var1 =~ var2
4264 * var1 != var2
4265 * var1 !~ var2
4266 * var1 > var2
4267 * var1 >= var2
4268 * var1 < var2
4269 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004270 * var1 is var2
4271 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 *
4273 * "arg" must point to the first non-white of the expression.
4274 * "arg" is advanced to the next non-white after the recognized expression.
4275 *
4276 * Return OK or FAIL.
4277 */
4278 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int evaluate;
4283{
Bram Moolenaar33570922005-01-25 22:26:29 +00004284 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 char_u *p;
4286 int i;
4287 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004288 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int len = 2;
4290 long n1, n2;
4291 char_u *s1, *s2;
4292 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4293 regmatch_T regmatch;
4294 int ic;
4295 char_u *save_cpo;
4296
4297 /*
4298 * Get the first variable.
4299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302
4303 p = *arg;
4304 switch (p[0])
4305 {
4306 case '=': if (p[1] == '=')
4307 type = TYPE_EQUAL;
4308 else if (p[1] == '~')
4309 type = TYPE_MATCH;
4310 break;
4311 case '!': if (p[1] == '=')
4312 type = TYPE_NEQUAL;
4313 else if (p[1] == '~')
4314 type = TYPE_NOMATCH;
4315 break;
4316 case '>': if (p[1] != '=')
4317 {
4318 type = TYPE_GREATER;
4319 len = 1;
4320 }
4321 else
4322 type = TYPE_GEQUAL;
4323 break;
4324 case '<': if (p[1] != '=')
4325 {
4326 type = TYPE_SMALLER;
4327 len = 1;
4328 }
4329 else
4330 type = TYPE_SEQUAL;
4331 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004332 case 'i': if (p[1] == 's')
4333 {
4334 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4335 len = 5;
4336 if (!vim_isIDc(p[len]))
4337 {
4338 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4339 type_is = TRUE;
4340 }
4341 }
4342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344
4345 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004346 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 */
4348 if (type != TYPE_UNKNOWN)
4349 {
4350 /* extra question mark appended: ignore case */
4351 if (p[len] == '?')
4352 {
4353 ic = TRUE;
4354 ++len;
4355 }
4356 /* extra '#' appended: match case */
4357 else if (p[len] == '#')
4358 {
4359 ic = FALSE;
4360 ++len;
4361 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004362 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 ic = p_ic;
4365
4366 /*
4367 * Get the second variable.
4368 */
4369 *arg = skipwhite(p + len);
4370 if (eval5(arg, &var2, evaluate) == FAIL)
4371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 return FAIL;
4374 }
4375
4376 if (evaluate)
4377 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 if (type_is && rettv->v_type != var2.v_type)
4379 {
4380 /* For "is" a different type always means FALSE, for "notis"
4381 * it means TRUE. */
4382 n1 = (type == TYPE_NEQUAL);
4383 }
4384 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4385 {
4386 if (type_is)
4387 {
4388 n1 = (rettv->v_type == var2.v_type
4389 && rettv->vval.v_list == var2.vval.v_list);
4390 if (type == TYPE_NEQUAL)
4391 n1 = !n1;
4392 }
4393 else if (rettv->v_type != var2.v_type
4394 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4395 {
4396 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004397 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004399 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
4404 else
4405 {
4406 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004407 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4408 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 }
4413
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004414 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4415 {
4416 if (type_is)
4417 {
4418 n1 = (rettv->v_type == var2.v_type
4419 && rettv->vval.v_dict == var2.vval.v_dict);
4420 if (type == TYPE_NEQUAL)
4421 n1 = !n1;
4422 }
4423 else if (rettv->v_type != var2.v_type
4424 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4425 {
4426 if (rettv->v_type != var2.v_type)
4427 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4428 else
4429 EMSG(_("E736: Invalid operation for Dictionary"));
4430 clear_tv(rettv);
4431 clear_tv(&var2);
4432 return FAIL;
4433 }
4434 else
4435 {
4436 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004437 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4438 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004439 if (type == TYPE_NEQUAL)
4440 n1 = !n1;
4441 }
4442 }
4443
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4445 {
4446 if (rettv->v_type != var2.v_type
4447 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4448 {
4449 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004450 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004452 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 clear_tv(rettv);
4454 clear_tv(&var2);
4455 return FAIL;
4456 }
4457 else
4458 {
4459 /* Compare two Funcrefs for being equal or unequal. */
4460 if (rettv->vval.v_string == NULL
4461 || var2.vval.v_string == NULL)
4462 n1 = FALSE;
4463 else
4464 n1 = STRCMP(rettv->vval.v_string,
4465 var2.vval.v_string) == 0;
4466 if (type == TYPE_NEQUAL)
4467 n1 = !n1;
4468 }
4469 }
4470
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471#ifdef FEAT_FLOAT
4472 /*
4473 * If one of the two variables is a float, compare as a float.
4474 * When using "=~" or "!~", always compare as string.
4475 */
4476 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4477 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4478 {
4479 float_T f1, f2;
4480
4481 if (rettv->v_type == VAR_FLOAT)
4482 f1 = rettv->vval.v_float;
4483 else
4484 f1 = get_tv_number(rettv);
4485 if (var2.v_type == VAR_FLOAT)
4486 f2 = var2.vval.v_float;
4487 else
4488 f2 = get_tv_number(&var2);
4489 n1 = FALSE;
4490 switch (type)
4491 {
4492 case TYPE_EQUAL: n1 = (f1 == f2); break;
4493 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4494 case TYPE_GREATER: n1 = (f1 > f2); break;
4495 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4496 case TYPE_SMALLER: n1 = (f1 < f2); break;
4497 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4498 case TYPE_UNKNOWN:
4499 case TYPE_MATCH:
4500 case TYPE_NOMATCH: break; /* avoid gcc warning */
4501 }
4502 }
4503#endif
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /*
4506 * If one of the two variables is a number, compare as a number.
4507 * When using "=~" or "!~", always compare as string.
4508 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 n1 = get_tv_number(rettv);
4513 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 switch (type)
4515 {
4516 case TYPE_EQUAL: n1 = (n1 == n2); break;
4517 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4518 case TYPE_GREATER: n1 = (n1 > n2); break;
4519 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4520 case TYPE_SMALLER: n1 = (n1 < n2); break;
4521 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4522 case TYPE_UNKNOWN:
4523 case TYPE_MATCH:
4524 case TYPE_NOMATCH: break; /* avoid gcc warning */
4525 }
4526 }
4527 else
4528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 s1 = get_tv_string_buf(rettv, buf1);
4530 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4532 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4533 else
4534 i = 0;
4535 n1 = FALSE;
4536 switch (type)
4537 {
4538 case TYPE_EQUAL: n1 = (i == 0); break;
4539 case TYPE_NEQUAL: n1 = (i != 0); break;
4540 case TYPE_GREATER: n1 = (i > 0); break;
4541 case TYPE_GEQUAL: n1 = (i >= 0); break;
4542 case TYPE_SMALLER: n1 = (i < 0); break;
4543 case TYPE_SEQUAL: n1 = (i <= 0); break;
4544
4545 case TYPE_MATCH:
4546 case TYPE_NOMATCH:
4547 /* avoid 'l' flag in 'cpoptions' */
4548 save_cpo = p_cpo;
4549 p_cpo = (char_u *)"";
4550 regmatch.regprog = vim_regcomp(s2,
4551 RE_MAGIC + RE_STRING);
4552 regmatch.rm_ic = ic;
4553 if (regmatch.regprog != NULL)
4554 {
4555 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4556 vim_free(regmatch.regprog);
4557 if (type == TYPE_NOMATCH)
4558 n1 = !n1;
4559 }
4560 p_cpo = save_cpo;
4561 break;
4562
4563 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4564 }
4565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 clear_tv(rettv);
4567 clear_tv(&var2);
4568 rettv->v_type = VAR_NUMBER;
4569 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571 }
4572
4573 return OK;
4574}
4575
4576/*
4577 * Handle fourth level expression:
4578 * + number addition
4579 * - number subtraction
4580 * . string concatenation
4581 *
4582 * "arg" must point to the first non-white of the expression.
4583 * "arg" is advanced to the next non-white after the recognized expression.
4584 *
4585 * Return OK or FAIL.
4586 */
4587 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 int evaluate;
4592{
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T var2;
4594 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 int op;
4596 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004597#ifdef FEAT_FLOAT
4598 float_T f1 = 0, f2 = 0;
4599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 char_u *s1, *s2;
4601 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4602 char_u *p;
4603
4604 /*
4605 * Get the first variable.
4606 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004607 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 return FAIL;
4609
4610 /*
4611 * Repeat computing, until no '+', '-' or '.' is following.
4612 */
4613 for (;;)
4614 {
4615 op = **arg;
4616 if (op != '+' && op != '-' && op != '.')
4617 break;
4618
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004619 if ((op != '+' || rettv->v_type != VAR_LIST)
4620#ifdef FEAT_FLOAT
4621 && (op == '.' || rettv->v_type != VAR_FLOAT)
4622#endif
4623 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 {
4625 /* For "list + ...", an illegal use of the first operand as
4626 * a number cannot be determined before evaluating the 2nd
4627 * operand: if this is also a list, all is ok.
4628 * For "something . ...", "something - ..." or "non-list + ...",
4629 * we know that the first operand needs to be a string or number
4630 * without evaluating the 2nd operand. So check before to avoid
4631 * side effects after an error. */
4632 if (evaluate && get_tv_string_chk(rettv) == NULL)
4633 {
4634 clear_tv(rettv);
4635 return FAIL;
4636 }
4637 }
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /*
4640 * Get the second variable.
4641 */
4642 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004643 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return FAIL;
4647 }
4648
4649 if (evaluate)
4650 {
4651 /*
4652 * Compute the result.
4653 */
4654 if (op == '.')
4655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4657 s2 = get_tv_string_buf_chk(&var2, buf2);
4658 if (s2 == NULL) /* type error ? */
4659 {
4660 clear_tv(rettv);
4661 clear_tv(&var2);
4662 return FAIL;
4663 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004664 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 rettv->v_type = VAR_STRING;
4667 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004669 else if (op == '+' && rettv->v_type == VAR_LIST
4670 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004671 {
4672 /* concatenate Lists */
4673 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4674 &var3) == FAIL)
4675 {
4676 clear_tv(rettv);
4677 clear_tv(&var2);
4678 return FAIL;
4679 }
4680 clear_tv(rettv);
4681 *rettv = var3;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 else
4684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 int error = FALSE;
4686
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004687#ifdef FEAT_FLOAT
4688 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690 f1 = rettv->vval.v_float;
4691 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693 else
4694#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696 n1 = get_tv_number_chk(rettv, &error);
4697 if (error)
4698 {
4699 /* This can only happen for "list + non-list". For
4700 * "non-list + ..." or "something - ...", we returned
4701 * before evaluating the 2nd operand. */
4702 clear_tv(rettv);
4703 return FAIL;
4704 }
4705#ifdef FEAT_FLOAT
4706 if (var2.v_type == VAR_FLOAT)
4707 f1 = n1;
4708#endif
4709 }
4710#ifdef FEAT_FLOAT
4711 if (var2.v_type == VAR_FLOAT)
4712 {
4713 f2 = var2.vval.v_float;
4714 n2 = 0;
4715 }
4716 else
4717#endif
4718 {
4719 n2 = get_tv_number_chk(&var2, &error);
4720 if (error)
4721 {
4722 clear_tv(rettv);
4723 clear_tv(&var2);
4724 return FAIL;
4725 }
4726#ifdef FEAT_FLOAT
4727 if (rettv->v_type == VAR_FLOAT)
4728 f2 = n2;
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732
4733#ifdef FEAT_FLOAT
4734 /* If there is a float on either side the result is a float. */
4735 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4736 {
4737 if (op == '+')
4738 f1 = f1 + f2;
4739 else
4740 f1 = f1 - f2;
4741 rettv->v_type = VAR_FLOAT;
4742 rettv->vval.v_float = f1;
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745#endif
4746 {
4747 if (op == '+')
4748 n1 = n1 + n2;
4749 else
4750 n1 = n1 - n2;
4751 rettv->v_type = VAR_NUMBER;
4752 rettv->vval.v_number = n1;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 }
4758 return OK;
4759}
4760
4761/*
4762 * Handle fifth level expression:
4763 * * number multiplication
4764 * / number division
4765 * % number modulo
4766 *
4767 * "arg" must point to the first non-white of the expression.
4768 * "arg" is advanced to the next non-white after the recognized expression.
4769 *
4770 * Return OK or FAIL.
4771 */
4772 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004773eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004777 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 int op;
4781 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782#ifdef FEAT_FLOAT
4783 int use_float = FALSE;
4784 float_T f1 = 0, f2;
4785#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 /*
4789 * Get the first variable.
4790 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 return FAIL;
4793
4794 /*
4795 * Repeat computing, until no '*', '/' or '%' is following.
4796 */
4797 for (;;)
4798 {
4799 op = **arg;
4800 if (op != '*' && op != '/' && op != '%')
4801 break;
4802
4803 if (evaluate)
4804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (rettv->v_type == VAR_FLOAT)
4807 {
4808 f1 = rettv->vval.v_float;
4809 use_float = TRUE;
4810 n1 = 0;
4811 }
4812 else
4813#endif
4814 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 if (error)
4817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 else
4820 n1 = 0;
4821
4822 /*
4823 * Get the second variable.
4824 */
4825 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (var2.v_type == VAR_FLOAT)
4833 {
4834 if (!use_float)
4835 {
4836 f1 = n1;
4837 use_float = TRUE;
4838 }
4839 f2 = var2.vval.v_float;
4840 n2 = 0;
4841 }
4842 else
4843#endif
4844 {
4845 n2 = get_tv_number_chk(&var2, &error);
4846 clear_tv(&var2);
4847 if (error)
4848 return FAIL;
4849#ifdef FEAT_FLOAT
4850 if (use_float)
4851 f2 = n2;
4852#endif
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 /*
4856 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004859#ifdef FEAT_FLOAT
4860 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862 if (op == '*')
4863 f1 = f1 * f2;
4864 else if (op == '/')
4865 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004866# ifdef VMS
4867 /* VMS crashes on divide by zero, work around it */
4868 if (f2 == 0.0)
4869 {
4870 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004871 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004872 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004873 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004874 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004875 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004876 }
4877 else
4878 f1 = f1 / f2;
4879# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 /* We rely on the floating point library to handle divide
4881 * by zero to result in "inf" and not a crash. */
4882 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004887 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 return FAIL;
4889 }
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004896 if (op == '*')
4897 n1 = n1 * n2;
4898 else if (op == '/')
4899 {
4900 if (n2 == 0) /* give an error message? */
4901 {
4902 if (n1 == 0)
4903 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4904 else if (n1 < 0)
4905 n1 = -0x7fffffffL;
4906 else
4907 n1 = 0x7fffffffL;
4908 }
4909 else
4910 n1 = n1 / n2;
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 {
4914 if (n2 == 0) /* give an error message? */
4915 n1 = 0;
4916 else
4917 n1 = n1 % n2;
4918 }
4919 rettv->v_type = VAR_NUMBER;
4920 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 }
4924
4925 return OK;
4926}
4927
4928/*
4929 * Handle sixth level expression:
4930 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004931 * "string" string constant
4932 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 * &option-name option value
4934 * @r register contents
4935 * identifier variable value
4936 * function() function call
4937 * $VAR environment variable
4938 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004939 * [expr, expr] List
4940 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 *
4942 * Also handle:
4943 * ! in front logical NOT
4944 * - in front unary minus
4945 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004946 * trailing [] subscript in String or List
4947 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 *
4949 * "arg" must point to the first non-white of the expression.
4950 * "arg" is advanced to the next non-white after the recognized expression.
4951 *
4952 * Return OK or FAIL.
4953 */
4954 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004955eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004959 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 long n;
4962 int len;
4963 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 char_u *start_leader, *end_leader;
4965 int ret = OK;
4966 char_u *alias;
4967
4968 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004969 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004972 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 /*
4975 * Skip '!' and '-' characters. They are handled later.
4976 */
4977 start_leader = *arg;
4978 while (**arg == '!' || **arg == '-' || **arg == '+')
4979 *arg = skipwhite(*arg + 1);
4980 end_leader = *arg;
4981
4982 switch (**arg)
4983 {
4984 /*
4985 * Number constant.
4986 */
4987 case '0':
4988 case '1':
4989 case '2':
4990 case '3':
4991 case '4':
4992 case '5':
4993 case '6':
4994 case '7':
4995 case '8':
4996 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 {
4998#ifdef FEAT_FLOAT
4999 char_u *p = skipdigits(*arg + 1);
5000 int get_float = FALSE;
5001
5002 /* We accept a float when the format matches
5003 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005004 * strict to avoid backwards compatibility problems.
5005 * Don't look for a float after the "." operator, so that
5006 * ":let vers = 1.2.3" doesn't fail. */
5007 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 get_float = TRUE;
5010 p = skipdigits(p + 2);
5011 if (*p == 'e' || *p == 'E')
5012 {
5013 ++p;
5014 if (*p == '-' || *p == '+')
5015 ++p;
5016 if (!vim_isdigit(*p))
5017 get_float = FALSE;
5018 else
5019 p = skipdigits(p + 1);
5020 }
5021 if (ASCII_ISALPHA(*p) || *p == '.')
5022 get_float = FALSE;
5023 }
5024 if (get_float)
5025 {
5026 float_T f;
5027
5028 *arg += string2float(*arg, &f);
5029 if (evaluate)
5030 {
5031 rettv->v_type = VAR_FLOAT;
5032 rettv->vval.v_float = f;
5033 }
5034 }
5035 else
5036#endif
5037 {
5038 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5039 *arg += len;
5040 if (evaluate)
5041 {
5042 rettv->v_type = VAR_NUMBER;
5043 rettv->vval.v_number = n;
5044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 /*
5050 * String constant: "string".
5051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054
5055 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005058 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005059 break;
5060
5061 /*
5062 * List: [expr, expr]
5063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
5066
5067 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 * Dictionary: {key: val, key: val}
5069 */
5070 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5071 break;
5072
5073 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005074 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005076 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078
5079 /*
5080 * Environment variable: $VAR.
5081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084
5085 /*
5086 * Register contents: @r.
5087 */
5088 case '@': ++*arg;
5089 if (evaluate)
5090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005092 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 if (**arg != NUL)
5095 ++*arg;
5096 break;
5097
5098 /*
5099 * nested expression: (expression).
5100 */
5101 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (**arg == ')')
5104 ++*arg;
5105 else if (ret == OK)
5106 {
5107 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 ret = FAIL;
5110 }
5111 break;
5112
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 break;
5115 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116
5117 if (ret == NOTDONE)
5118 {
5119 /*
5120 * Must be a variable or function name.
5121 * Can also be a curly-braces kind of name: {expr}.
5122 */
5123 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005124 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 if (alias != NULL)
5126 s = alias;
5127
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005128 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 ret = FAIL;
5130 else
5131 {
5132 if (**arg == '(') /* recursive! */
5133 {
5134 /* If "s" is the name of a variable of type VAR_FUNC
5135 * use its contents. */
5136 s = deref_func_name(s, &len);
5137
5138 /* Invoke the function. */
5139 ret = get_func_tv(s, len, rettv, arg,
5140 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005141 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 /* Stop the expression evaluation when immediately
5143 * aborting on error, or when an interrupt occurred or
5144 * an exception was thrown but not caught. */
5145 if (aborting())
5146 {
5147 if (ret == OK)
5148 clear_tv(rettv);
5149 ret = FAIL;
5150 }
5151 }
5152 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005154 else
5155 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005157 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 }
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 *arg = skipwhite(*arg);
5161
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005162 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5163 * expr(expr). */
5164 if (ret == OK)
5165 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167 /*
5168 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5169 */
5170 if (ret == OK && evaluate && end_leader > start_leader)
5171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005173 int val = 0;
5174#ifdef FEAT_FLOAT
5175 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005177 if (rettv->v_type == VAR_FLOAT)
5178 f = rettv->vval.v_float;
5179 else
5180#endif
5181 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005182 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005184 clear_tv(rettv);
5185 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 else
5188 {
5189 while (end_leader > start_leader)
5190 {
5191 --end_leader;
5192 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 {
5194#ifdef FEAT_FLOAT
5195 if (rettv->v_type == VAR_FLOAT)
5196 f = !f;
5197 else
5198#endif
5199 val = !val;
5200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 {
5203#ifdef FEAT_FLOAT
5204 if (rettv->v_type == VAR_FLOAT)
5205 f = -f;
5206 else
5207#endif
5208 val = -val;
5209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005211#ifdef FEAT_FLOAT
5212 if (rettv->v_type == VAR_FLOAT)
5213 {
5214 clear_tv(rettv);
5215 rettv->vval.v_float = f;
5216 }
5217 else
5218#endif
5219 {
5220 clear_tv(rettv);
5221 rettv->v_type = VAR_NUMBER;
5222 rettv->vval.v_number = val;
5223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226
5227 return ret;
5228}
5229
5230/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005231 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5232 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5234 */
5235 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241{
5242 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005243 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 long len = -1;
5246 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005250 if (rettv->v_type == VAR_FUNC
5251#ifdef FEAT_FLOAT
5252 || rettv->v_type == VAR_FLOAT
5253#endif
5254 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256 if (verbose)
5257 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 return FAIL;
5259 }
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /*
5264 * dict.name
5265 */
5266 key = *arg + 1;
5267 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5268 ;
5269 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 }
5273 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 /*
5276 * something[idx]
5277 *
5278 * Get the (first) variable from inside the [].
5279 */
5280 *arg = skipwhite(*arg + 1);
5281 if (**arg == ':')
5282 empty1 = TRUE;
5283 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5284 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5286 {
5287 /* not a number or string */
5288 clear_tv(&var1);
5289 return FAIL;
5290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291
5292 /*
5293 * Get the second variable from inside the [:].
5294 */
5295 if (**arg == ':')
5296 {
5297 range = TRUE;
5298 *arg = skipwhite(*arg + 1);
5299 if (**arg == ']')
5300 empty2 = TRUE;
5301 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 if (!empty1)
5304 clear_tv(&var1);
5305 return FAIL;
5306 }
5307 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5308 {
5309 /* not a number or string */
5310 if (!empty1)
5311 clear_tv(&var1);
5312 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 return FAIL;
5314 }
5315 }
5316
5317 /* Check for the ']'. */
5318 if (**arg != ']')
5319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005320 if (verbose)
5321 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 clear_tv(&var1);
5323 if (range)
5324 clear_tv(&var2);
5325 return FAIL;
5326 }
5327 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 }
5329
5330 if (evaluate)
5331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 n1 = 0;
5333 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 n1 = get_tv_number(&var1);
5336 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 }
5338 if (range)
5339 {
5340 if (empty2)
5341 n2 = -1;
5342 else
5343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 n2 = get_tv_number(&var2);
5345 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 }
5347 }
5348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 {
5351 case VAR_NUMBER:
5352 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 len = (long)STRLEN(s);
5355 if (range)
5356 {
5357 /* The resulting variable is a substring. If the indexes
5358 * are out of range the result is empty. */
5359 if (n1 < 0)
5360 {
5361 n1 = len + n1;
5362 if (n1 < 0)
5363 n1 = 0;
5364 }
5365 if (n2 < 0)
5366 n2 = len + n2;
5367 else if (n2 >= len)
5368 n2 = len;
5369 if (n1 >= len || n2 < 0 || n1 > n2)
5370 s = NULL;
5371 else
5372 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5373 }
5374 else
5375 {
5376 /* The resulting variable is a string of a single
5377 * character. If the index is too big or negative the
5378 * result is empty. */
5379 if (n1 >= len || n1 < 0)
5380 s = NULL;
5381 else
5382 s = vim_strnsave(s + n1, 1);
5383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 clear_tv(rettv);
5385 rettv->v_type = VAR_STRING;
5386 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 break;
5388
5389 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 if (n1 < 0)
5392 n1 = len + n1;
5393 if (!empty1 && (n1 < 0 || n1 >= len))
5394 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005395 /* For a range we allow invalid values and return an empty
5396 * list. A list index out of range is an error. */
5397 if (!range)
5398 {
5399 if (verbose)
5400 EMSGN(_(e_listidx), n1);
5401 return FAIL;
5402 }
5403 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 if (range)
5406 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005407 list_T *l;
5408 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409
5410 if (n2 < 0)
5411 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005412 else if (n2 >= len)
5413 n2 = len - 1;
5414 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005415 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 l = list_alloc();
5417 if (l == NULL)
5418 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 n1 <= n2; ++n1)
5421 {
5422 if (list_append_tv(l, &item->li_tv) == FAIL)
5423 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005424 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 return FAIL;
5426 }
5427 item = item->li_next;
5428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 clear_tv(rettv);
5430 rettv->v_type = VAR_LIST;
5431 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005432 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 else
5435 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005436 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 clear_tv(rettv);
5438 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441
5442 case VAR_DICT:
5443 if (range)
5444 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 if (verbose)
5446 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 if (len == -1)
5448 clear_tv(&var1);
5449 return FAIL;
5450 }
5451 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453
5454 if (len == -1)
5455 {
5456 key = get_tv_string(&var1);
5457 if (*key == NUL)
5458 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005459 if (verbose)
5460 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 clear_tv(&var1);
5462 return FAIL;
5463 }
5464 }
5465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005466 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005468 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005469 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 if (len == -1)
5471 clear_tv(&var1);
5472 if (item == NULL)
5473 return FAIL;
5474
5475 copy_tv(&item->di_tv, &var1);
5476 clear_tv(rettv);
5477 *rettv = var1;
5478 }
5479 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 }
5482
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 return OK;
5484}
5485
5486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 * Get an option value.
5488 * "arg" points to the '&' or '+' before the option name.
5489 * "arg" is advanced to character after the option name.
5490 * Return OK or FAIL.
5491 */
5492 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int evaluate;
5497{
5498 char_u *option_end;
5499 long numval;
5500 char_u *stringval;
5501 int opt_type;
5502 int c;
5503 int working = (**arg == '+'); /* has("+option") */
5504 int ret = OK;
5505 int opt_flags;
5506
5507 /*
5508 * Isolate the option name and find its value.
5509 */
5510 option_end = find_option_end(arg, &opt_flags);
5511 if (option_end == NULL)
5512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 EMSG2(_("E112: Option name missing: %s"), *arg);
5515 return FAIL;
5516 }
5517
5518 if (!evaluate)
5519 {
5520 *arg = option_end;
5521 return OK;
5522 }
5523
5524 c = *option_end;
5525 *option_end = NUL;
5526 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529 if (opt_type == -3) /* invalid name */
5530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 EMSG2(_("E113: Unknown option: %s"), *arg);
5533 ret = FAIL;
5534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 if (opt_type == -2) /* hidden string option */
5538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 rettv->v_type = VAR_STRING;
5540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else if (opt_type == -1) /* hidden number option */
5543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 rettv->v_type = VAR_NUMBER;
5545 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547 else if (opt_type == 1) /* number option */
5548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 rettv->v_type = VAR_NUMBER;
5550 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552 else /* string option */
5553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 rettv->v_type = VAR_STRING;
5555 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 }
5558 else if (working && (opt_type == -2 || opt_type == -1))
5559 ret = FAIL;
5560
5561 *option_end = c; /* put back for error messages */
5562 *arg = option_end;
5563
5564 return ret;
5565}
5566
5567/*
5568 * Allocate a variable for a string constant.
5569 * Return OK or FAIL.
5570 */
5571 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 int evaluate;
5576{
5577 char_u *p;
5578 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 int extra = 0;
5580
5581 /*
5582 * Find the end of the string, skipping backslashed characters.
5583 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 if (*p == '\\' && p[1] != NUL)
5587 {
5588 ++p;
5589 /* A "\<x>" form occupies at least 4 characters, and produces up
5590 * to 6 characters: reserve space for 2 extra */
5591 if (*p == '<')
5592 extra += 2;
5593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595
5596 if (*p != '"')
5597 {
5598 EMSG2(_("E114: Missing quote: %s"), *arg);
5599 return FAIL;
5600 }
5601
5602 /* If only parsing, set *arg and return here */
5603 if (!evaluate)
5604 {
5605 *arg = p + 1;
5606 return OK;
5607 }
5608
5609 /*
5610 * Copy the string into allocated memory, handling backslashed
5611 * characters.
5612 */
5613 name = alloc((unsigned)(p - *arg + extra));
5614 if (name == NULL)
5615 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 rettv->v_type = VAR_STRING;
5617 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (*p == '\\')
5622 {
5623 switch (*++p)
5624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 case 'b': *name++ = BS; ++p; break;
5626 case 'e': *name++ = ESC; ++p; break;
5627 case 'f': *name++ = FF; ++p; break;
5628 case 'n': *name++ = NL; ++p; break;
5629 case 'r': *name++ = CAR; ++p; break;
5630 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 case 'X': /* hex: "\x1", "\x12" */
5633 case 'x':
5634 case 'u': /* Unicode: "\u0023" */
5635 case 'U':
5636 if (vim_isxdigit(p[1]))
5637 {
5638 int n, nr;
5639 int c = toupper(*p);
5640
5641 if (c == 'X')
5642 n = 2;
5643 else
5644 n = 4;
5645 nr = 0;
5646 while (--n >= 0 && vim_isxdigit(p[1]))
5647 {
5648 ++p;
5649 nr = (nr << 4) + hex2nr(*p);
5650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652#ifdef FEAT_MBYTE
5653 /* For "\u" store the number according to
5654 * 'encoding'. */
5655 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 else
5658#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 break;
5662
5663 /* octal: "\1", "\12", "\123" */
5664 case '0':
5665 case '1':
5666 case '2':
5667 case '3':
5668 case '4':
5669 case '5':
5670 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 case '7': *name = *p++ - '0';
5672 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 *name = (*name << 3) + *p++ - '0';
5675 if (*p >= '0' && *p <= '7')
5676 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 break;
5680
5681 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (extra != 0)
5684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687 }
5688 /* FALLTHROUGH */
5689
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 break;
5692 }
5693 }
5694 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 *arg = p + 1;
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 return OK;
5702}
5703
5704/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 * Return OK or FAIL.
5707 */
5708 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 int evaluate;
5713{
5714 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 int reduce = 0;
5717
5718 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 */
5721 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 break;
5727 ++reduce;
5728 ++p;
5729 }
5730 }
5731
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 return FAIL;
5736 }
5737
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 if (!evaluate)
5740 {
5741 *arg = p + 1;
5742 return OK;
5743 }
5744
5745 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 */
5748 str = alloc((unsigned)((p - *arg) - reduce));
5749 if (str == NULL)
5750 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 rettv->v_type = VAR_STRING;
5752 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 break;
5760 ++p;
5761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 *arg = p + 1;
5766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 return OK;
5768}
5769
5770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 * Allocate a variable for a List and fill it from "*arg".
5772 * Return OK or FAIL.
5773 */
5774 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 int evaluate;
5779{
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 list_T *l = NULL;
5781 typval_T tv;
5782 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783
5784 if (evaluate)
5785 {
5786 l = list_alloc();
5787 if (l == NULL)
5788 return FAIL;
5789 }
5790
5791 *arg = skipwhite(*arg + 1);
5792 while (**arg != ']' && **arg != NUL)
5793 {
5794 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5795 goto failret;
5796 if (evaluate)
5797 {
5798 item = listitem_alloc();
5799 if (item != NULL)
5800 {
5801 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005802 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 list_append(l, item);
5804 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005805 else
5806 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 }
5808
5809 if (**arg == ']')
5810 break;
5811 if (**arg != ',')
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 goto failret;
5815 }
5816 *arg = skipwhite(*arg + 1);
5817 }
5818
5819 if (**arg != ']')
5820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822failret:
5823 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005824 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 return FAIL;
5826 }
5827
5828 *arg = skipwhite(*arg + 1);
5829 if (evaluate)
5830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831 rettv->v_type = VAR_LIST;
5832 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 ++l->lv_refcount;
5834 }
5835
5836 return OK;
5837}
5838
5839/*
5840 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005841 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005843 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844list_alloc()
5845{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005846 list_T *l;
5847
5848 l = (list_T *)alloc_clear(sizeof(list_T));
5849 if (l != NULL)
5850 {
5851 /* Prepend the list to the list of lists for garbage collection. */
5852 if (first_list != NULL)
5853 first_list->lv_used_prev = l;
5854 l->lv_used_prev = NULL;
5855 l->lv_used_next = first_list;
5856 first_list = l;
5857 }
5858 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859}
5860
5861/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005862 * Allocate an empty list for a return value.
5863 * Returns OK or FAIL.
5864 */
5865 static int
5866rettv_list_alloc(rettv)
5867 typval_T *rettv;
5868{
5869 list_T *l = list_alloc();
5870
5871 if (l == NULL)
5872 return FAIL;
5873
5874 rettv->vval.v_list = l;
5875 rettv->v_type = VAR_LIST;
5876 ++l->lv_refcount;
5877 return OK;
5878}
5879
5880/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 * Unreference a list: decrement the reference count and free it when it
5882 * becomes zero.
5883 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005884 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005888 if (l != NULL && --l->lv_refcount <= 0)
5889 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890}
5891
5892/*
5893 * Free a list, including all items it points to.
5894 * Ignores the reference count.
5895 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005896 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005897list_free(l, recurse)
5898 list_T *l;
5899 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900{
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005903 /* Remove the list from the list of lists for garbage collection. */
5904 if (l->lv_used_prev == NULL)
5905 first_list = l->lv_used_next;
5906 else
5907 l->lv_used_prev->lv_used_next = l->lv_used_next;
5908 if (l->lv_used_next != NULL)
5909 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5910
Bram Moolenaard9fba312005-06-26 22:34:35 +00005911 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005913 /* Remove the item before deleting it. */
5914 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005915 if (recurse || (item->li_tv.v_type != VAR_LIST
5916 && item->li_tv.v_type != VAR_DICT))
5917 clear_tv(&item->li_tv);
5918 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 }
5920 vim_free(l);
5921}
5922
5923/*
5924 * Allocate a list item.
5925 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927listitem_alloc()
5928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005933 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 */
5935 static void
5936listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005939 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 vim_free(item);
5941}
5942
5943/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005944 * Remove a list item from a List and free it. Also clears the value.
5945 */
5946 static void
5947listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 list_T *l;
5949 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005950{
5951 list_remove(l, item, item);
5952 listitem_free(item);
5953}
5954
5955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 * Get the number of items in a list.
5957 */
5958 static long
5959list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 if (l == NULL)
5963 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005964 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965}
5966
5967/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 * Return TRUE when two lists have exactly the same values.
5969 */
5970 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005971list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 list_T *l1;
5973 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005975 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005976{
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005979 if (l1 == NULL || l2 == NULL)
5980 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005981 if (l1 == l2)
5982 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005983 if (list_len(l1) != list_len(l2))
5984 return FALSE;
5985
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 for (item1 = l1->lv_first, item2 = l2->lv_first;
5987 item1 != NULL && item2 != NULL;
5988 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005989 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990 return FALSE;
5991 return item1 == NULL && item2 == NULL;
5992}
5993
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005994#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5995 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996/*
5997 * Return the dictitem that an entry in a hashtable points to.
5998 */
5999 dictitem_T *
6000dict_lookup(hi)
6001 hashitem_T *hi;
6002{
6003 return HI2DI(hi);
6004}
6005#endif
6006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008 * Return TRUE when two dictionaries have exactly the same key/values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 dict_T *d1;
6013 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 hashitem_T *hi;
6018 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006019 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006020
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006021 if (d1 == NULL || d2 == NULL)
6022 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006023 if (d1 == d2)
6024 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006025 if (dict_len(d1) != dict_len(d2))
6026 return FALSE;
6027
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006028 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006031 if (!HASHITEM_EMPTY(hi))
6032 {
6033 item2 = dict_find(d2, hi->hi_key, -1);
6034 if (item2 == NULL)
6035 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006037 return FALSE;
6038 --todo;
6039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040 }
6041 return TRUE;
6042}
6043
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044static int tv_equal_recurse_limit;
6045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006048 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006049 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006050 */
6051 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006052tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 typval_T *tv1;
6054 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int ic; /* ignore case */
6056 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057{
6058 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006059 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006061 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006064 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006066 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067 * recursiveness to a limit. We guess they are equal then.
6068 * A fixed limit has the problem of still taking an awful long time.
6069 * Reduce the limit every time running into it. That should work fine for
6070 * deeply linked structures that are not recursively linked and catch
6071 * recursiveness quickly. */
6072 if (!recursive)
6073 tv_equal_recurse_limit = 1000;
6074 if (recursive_cnt >= tv_equal_recurse_limit)
6075 {
6076 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006077 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006079
6080 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006082 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 ++recursive_cnt;
6084 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6085 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006086 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006087
6088 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 ++recursive_cnt;
6090 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6091 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006092 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006093
6094 case VAR_FUNC:
6095 return (tv1->vval.v_string != NULL
6096 && tv2->vval.v_string != NULL
6097 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6098
6099 case VAR_NUMBER:
6100 return tv1->vval.v_number == tv2->vval.v_number;
6101
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006102#ifdef FEAT_FLOAT
6103 case VAR_FLOAT:
6104 return tv1->vval.v_float == tv2->vval.v_float;
6105#endif
6106
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107 case VAR_STRING:
6108 s1 = get_tv_string_buf(tv1, buf1);
6109 s2 = get_tv_string_buf(tv2, buf2);
6110 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return TRUE;
6115}
6116
6117/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118 * Locate item with index "n" in list "l" and return it.
6119 * A negative index is counted from the end; -1 is the last item.
6120 * Returns NULL when "n" is out of range.
6121 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 long n;
6126{
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128 long idx;
6129
6130 if (l == NULL)
6131 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006132
6133 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006135 n = l->lv_len + n;
6136
6137 /* Check for index out of range. */
6138 if (n < 0 || n >= l->lv_len)
6139 return NULL;
6140
6141 /* When there is a cached index may start search from there. */
6142 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006144 if (n < l->lv_idx / 2)
6145 {
6146 /* closest to the start of the list */
6147 item = l->lv_first;
6148 idx = 0;
6149 }
6150 else if (n > (l->lv_idx + l->lv_len) / 2)
6151 {
6152 /* closest to the end of the list */
6153 item = l->lv_last;
6154 idx = l->lv_len - 1;
6155 }
6156 else
6157 {
6158 /* closest to the cached index */
6159 item = l->lv_idx_item;
6160 idx = l->lv_idx;
6161 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 }
6163 else
6164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165 if (n < l->lv_len / 2)
6166 {
6167 /* closest to the start of the list */
6168 item = l->lv_first;
6169 idx = 0;
6170 }
6171 else
6172 {
6173 /* closest to the end of the list */
6174 item = l->lv_last;
6175 idx = l->lv_len - 1;
6176 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006178
6179 while (n > idx)
6180 {
6181 /* search forward */
6182 item = item->li_next;
6183 ++idx;
6184 }
6185 while (n < idx)
6186 {
6187 /* search backward */
6188 item = item->li_prev;
6189 --idx;
6190 }
6191
6192 /* cache the used index */
6193 l->lv_idx = idx;
6194 l->lv_idx_item = item;
6195
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 return item;
6197}
6198
6199/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006200 * Get list item "l[idx]" as a number.
6201 */
6202 static long
6203list_find_nr(l, idx, errorp)
6204 list_T *l;
6205 long idx;
6206 int *errorp; /* set to TRUE when something wrong */
6207{
6208 listitem_T *li;
6209
6210 li = list_find(l, idx);
6211 if (li == NULL)
6212 {
6213 if (errorp != NULL)
6214 *errorp = TRUE;
6215 return -1L;
6216 }
6217 return get_tv_number_chk(&li->li_tv, errorp);
6218}
6219
6220/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006221 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6222 */
6223 char_u *
6224list_find_str(l, idx)
6225 list_T *l;
6226 long idx;
6227{
6228 listitem_T *li;
6229
6230 li = list_find(l, idx - 1);
6231 if (li == NULL)
6232 {
6233 EMSGN(_(e_listidx), idx);
6234 return NULL;
6235 }
6236 return get_tv_string(&li->li_tv);
6237}
6238
6239/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006240 * Locate "item" list "l" and return its index.
6241 * Returns -1 when "item" is not in the list.
6242 */
6243 static long
6244list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 list_T *l;
6246 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006247{
6248 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006250
6251 if (l == NULL)
6252 return -1;
6253 idx = 0;
6254 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6255 ++idx;
6256 if (li == NULL)
6257 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006258 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006259}
6260
6261/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 * Append item "item" to the end of list "l".
6263 */
6264 static void
6265list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268{
6269 if (l->lv_last == NULL)
6270 {
6271 /* empty list */
6272 l->lv_first = item;
6273 l->lv_last = item;
6274 item->li_prev = NULL;
6275 }
6276 else
6277 {
6278 l->lv_last->li_next = item;
6279 item->li_prev = l->lv_last;
6280 l->lv_last = item;
6281 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006282 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 item->li_next = NULL;
6284}
6285
6286/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006290 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 list_T *l;
6293 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006295 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296
Bram Moolenaar05159a02005-02-26 23:04:13 +00006297 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006299 copy_tv(tv, &li->li_tv);
6300 list_append(l, li);
6301 return OK;
6302}
6303
6304/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006305 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006306 * Return FAIL when out of memory.
6307 */
6308 int
6309list_append_dict(list, dict)
6310 list_T *list;
6311 dict_T *dict;
6312{
6313 listitem_T *li = listitem_alloc();
6314
6315 if (li == NULL)
6316 return FAIL;
6317 li->li_tv.v_type = VAR_DICT;
6318 li->li_tv.v_lock = 0;
6319 li->li_tv.vval.v_dict = dict;
6320 list_append(list, li);
6321 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 return OK;
6323}
6324
6325/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006326 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006327 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006328 * Returns FAIL when out of memory.
6329 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006330 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006331list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006332 list_T *l;
6333 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006334 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006335{
6336 listitem_T *li = listitem_alloc();
6337
6338 if (li == NULL)
6339 return FAIL;
6340 list_append(l, li);
6341 li->li_tv.v_type = VAR_STRING;
6342 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006343 if (str == NULL)
6344 li->li_tv.vval.v_string = NULL;
6345 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006346 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 return FAIL;
6348 return OK;
6349}
6350
6351/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352 * Append "n" to list "l".
6353 * Returns FAIL when out of memory.
6354 */
6355 static int
6356list_append_number(l, n)
6357 list_T *l;
6358 varnumber_T n;
6359{
6360 listitem_T *li;
6361
6362 li = listitem_alloc();
6363 if (li == NULL)
6364 return FAIL;
6365 li->li_tv.v_type = VAR_NUMBER;
6366 li->li_tv.v_lock = 0;
6367 li->li_tv.vval.v_number = n;
6368 list_append(l, li);
6369 return OK;
6370}
6371
6372/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374 * If "item" is NULL append at the end.
6375 * Return FAIL when out of memory.
6376 */
6377 static int
6378list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 list_T *l;
6380 typval_T *tv;
6381 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006382{
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006384
6385 if (ni == NULL)
6386 return FAIL;
6387 copy_tv(tv, &ni->li_tv);
6388 if (item == NULL)
6389 /* Append new item at end of list. */
6390 list_append(l, ni);
6391 else
6392 {
6393 /* Insert new item before existing item. */
6394 ni->li_prev = item->li_prev;
6395 ni->li_next = item;
6396 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006397 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 ++l->lv_idx;
6400 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006404 l->lv_idx_item = NULL;
6405 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006407 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006408 }
6409 return OK;
6410}
6411
6412/*
6413 * Extend "l1" with "l2".
6414 * If "bef" is NULL append at the end, otherwise insert before this item.
6415 * Returns FAIL when out of memory.
6416 */
6417 static int
6418list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l1;
6420 list_T *l2;
6421 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006424 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006426 /* We also quit the loop when we have inserted the original item count of
6427 * the list, avoid a hang when we extend a list with itself. */
6428 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6430 return FAIL;
6431 return OK;
6432}
6433
6434/*
6435 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6436 * Return FAIL when out of memory.
6437 */
6438 static int
6439list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l1;
6441 list_T *l2;
6442 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006446 if (l1 == NULL || l2 == NULL)
6447 return FAIL;
6448
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 if (l == NULL)
6452 return FAIL;
6453 tv->v_type = VAR_LIST;
6454 tv->vval.v_list = l;
6455
6456 /* append all items from the second list */
6457 return list_extend(l, l2, NULL);
6458}
6459
6460/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464 * Returns NULL when out of memory.
6465 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *copy;
6473 listitem_T *item;
6474 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475
6476 if (orig == NULL)
6477 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478
6479 copy = list_alloc();
6480 if (copy != NULL)
6481 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 if (copyID != 0)
6483 {
6484 /* Do this before adding the items, because one of the items may
6485 * refer back to this list. */
6486 orig->lv_copyID = copyID;
6487 orig->lv_copylist = copy;
6488 }
6489 for (item = orig->lv_first; item != NULL && !got_int;
6490 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 {
6492 ni = listitem_alloc();
6493 if (ni == NULL)
6494 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 {
6497 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6498 {
6499 vim_free(ni);
6500 break;
6501 }
6502 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 list_append(copy, ni);
6506 }
6507 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 if (item != NULL)
6509 {
6510 list_unref(copy);
6511 copy = NULL;
6512 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 }
6514
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 return copy;
6516}
6517
6518/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006520 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006523list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 list_T *l;
6525 listitem_T *item;
6526 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527{
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 /* notify watchers */
6531 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 list_fix_watch(l, ip);
6535 if (ip == item2)
6536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538
6539 if (item2->li_next == NULL)
6540 l->lv_last = item->li_prev;
6541 else
6542 item2->li_next->li_prev = item->li_prev;
6543 if (item->li_prev == NULL)
6544 l->lv_first = item2->li_next;
6545 else
6546 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548}
6549
6550/*
6551 * Return an allocated string with the string representation of a list.
6552 * May return NULL.
6553 */
6554 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006555list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006557 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558{
6559 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560
6561 if (tv->vval.v_list == NULL)
6562 return NULL;
6563 ga_init2(&ga, (int)sizeof(char), 80);
6564 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006565 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 {
6567 vim_free(ga.ga_data);
6568 return NULL;
6569 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 ga_append(&ga, ']');
6571 ga_append(&ga, NUL);
6572 return (char_u *)ga.ga_data;
6573}
6574
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006575typedef struct join_S {
6576 char_u *s;
6577 char_u *tofree;
6578} join_T;
6579
6580 static int
6581list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6582 garray_T *gap; /* to store the result in */
6583 list_T *l;
6584 char_u *sep;
6585 int echo_style;
6586 int copyID;
6587 garray_T *join_gap; /* to keep each list item string */
6588{
6589 int i;
6590 join_T *p;
6591 int len;
6592 int sumlen = 0;
6593 int first = TRUE;
6594 char_u *tofree;
6595 char_u numbuf[NUMBUFLEN];
6596 listitem_T *item;
6597 char_u *s;
6598
6599 /* Stringify each item in the list. */
6600 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6601 {
6602 if (echo_style)
6603 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6604 else
6605 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6606 if (s == NULL)
6607 return FAIL;
6608
6609 len = (int)STRLEN(s);
6610 sumlen += len;
6611
6612 ga_grow(join_gap, 1);
6613 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6614 if (tofree != NULL || s != numbuf)
6615 {
6616 p->s = s;
6617 p->tofree = tofree;
6618 }
6619 else
6620 {
6621 p->s = vim_strnsave(s, len);
6622 p->tofree = p->s;
6623 }
6624
6625 line_breakcheck();
6626 }
6627
6628 /* Allocate result buffer with its total size, avoid re-allocation and
6629 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6630 if (join_gap->ga_len >= 2)
6631 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6632 if (ga_grow(gap, sumlen + 2) == FAIL)
6633 return FAIL;
6634
6635 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6636 {
6637 if (first)
6638 first = FALSE;
6639 else
6640 ga_concat(gap, sep);
6641 p = ((join_T *)join_gap->ga_data) + i;
6642
6643 if (p->s != NULL)
6644 ga_concat(gap, p->s);
6645 line_breakcheck();
6646 }
6647
6648 return OK;
6649}
6650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006652 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006653 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006654 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006657list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006660 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006661 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006662 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006663{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006664 garray_T join_ga;
6665 int retval;
6666 join_T *p;
6667 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006668
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006669 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6670 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6671
6672 /* Dispose each item in join_ga. */
6673 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006675 p = (join_T *)join_ga.ga_data;
6676 for (i = 0; i < join_ga.ga_len; ++i)
6677 {
6678 vim_free(p->tofree);
6679 ++p;
6680 }
6681 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683
6684 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685}
6686
6687/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 * Garbage collection for lists and dictionaries.
6689 *
6690 * We use reference counts to be able to free most items right away when they
6691 * are no longer used. But for composite items it's possible that it becomes
6692 * unused while the reference count is > 0: When there is a recursive
6693 * reference. Example:
6694 * :let l = [1, 2, 3]
6695 * :let d = {9: l}
6696 * :let l[1] = d
6697 *
6698 * Since this is quite unusual we handle this with garbage collection: every
6699 * once in a while find out which lists and dicts are not referenced from any
6700 * variable.
6701 *
6702 * Here is a good reference text about garbage collection (refers to Python
6703 * but it applies to all reference-counting mechanisms):
6704 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006705 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006706
6707/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006708 * Do garbage collection for lists and dicts.
6709 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006710 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006711 int
6712garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006713{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006714 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006715 buf_T *buf;
6716 win_T *wp;
6717 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006718 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006719 int did_free;
6720 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006721#ifdef FEAT_WINDOWS
6722 tabpage_T *tp;
6723#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006724
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006725 /* Only do this once. */
6726 want_garbage_collect = FALSE;
6727 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006728 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006729
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006730 /* We advance by two because we add one for items referenced through
6731 * previous_funccal. */
6732 current_copyID += COPYID_INC;
6733 copyID = current_copyID;
6734
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006735 /*
6736 * 1. Go through all accessible variables and mark all lists and dicts
6737 * with copyID.
6738 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006739
6740 /* Don't free variables in the previous_funccal list unless they are only
6741 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006742 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006743 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6744 {
6745 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6746 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6747 }
6748
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 /* script-local variables */
6750 for (i = 1; i <= ga_scripts.ga_len; ++i)
6751 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6752
6753 /* buffer-local variables */
6754 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6755 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6756
6757 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006758 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6760
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006761#ifdef FEAT_WINDOWS
6762 /* tabpage-local variables */
6763 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6764 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6765#endif
6766
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 /* global variables */
6768 set_ref_in_ht(&globvarht, copyID);
6769
6770 /* function-local variables */
6771 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6772 {
6773 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6774 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6775 }
6776
Bram Moolenaard812df62008-11-09 12:46:09 +00006777 /* v: vars */
6778 set_ref_in_ht(&vimvarht, copyID);
6779
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006780 /*
6781 * 2. Free lists and dictionaries that are not referenced.
6782 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006783 did_free = free_unref_items(copyID);
6784
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006785 /*
6786 * 3. Check if any funccal can be freed now.
6787 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788 for (pfc = &previous_funccal; *pfc != NULL; )
6789 {
6790 if (can_free_funccal(*pfc, copyID))
6791 {
6792 fc = *pfc;
6793 *pfc = fc->caller;
6794 free_funccal(fc, TRUE);
6795 did_free = TRUE;
6796 did_free_funccal = TRUE;
6797 }
6798 else
6799 pfc = &(*pfc)->caller;
6800 }
6801 if (did_free_funccal)
6802 /* When a funccal was freed some more items might be garbage
6803 * collected, so run again. */
6804 (void)garbage_collect();
6805
6806 return did_free;
6807}
6808
6809/*
6810 * Free lists and dictionaries that are no longer referenced.
6811 */
6812 static int
6813free_unref_items(copyID)
6814 int copyID;
6815{
6816 dict_T *dd;
6817 list_T *ll;
6818 int did_free = FALSE;
6819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006821 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 */
6823 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006824 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006826 /* Free the Dictionary and ordinary items it contains, but don't
6827 * recurse into Lists and Dictionaries, they will be in the list
6828 * of dicts or list of lists. */
6829 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 did_free = TRUE;
6831
6832 /* restart, next dict may also have been freed */
6833 dd = first_dict;
6834 }
6835 else
6836 dd = dd->dv_used_next;
6837
6838 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839 * Go through the list of lists and free items without the copyID.
6840 * But don't free a list that has a watcher (used in a for loop), these
6841 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 */
6843 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6845 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006847 /* Free the List and ordinary items it contains, but don't recurse
6848 * into Lists and Dictionaries, they will be in the list of dicts
6849 * or list of lists. */
6850 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 did_free = TRUE;
6852
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006853 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 ll = first_list;
6855 }
6856 else
6857 ll = ll->lv_used_next;
6858
6859 return did_free;
6860}
6861
6862/*
6863 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6864 */
6865 static void
6866set_ref_in_ht(ht, copyID)
6867 hashtab_T *ht;
6868 int copyID;
6869{
6870 int todo;
6871 hashitem_T *hi;
6872
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 for (hi = ht->ht_array; todo > 0; ++hi)
6875 if (!HASHITEM_EMPTY(hi))
6876 {
6877 --todo;
6878 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6879 }
6880}
6881
6882/*
6883 * Mark all lists and dicts referenced through list "l" with "copyID".
6884 */
6885 static void
6886set_ref_in_list(l, copyID)
6887 list_T *l;
6888 int copyID;
6889{
6890 listitem_T *li;
6891
6892 for (li = l->lv_first; li != NULL; li = li->li_next)
6893 set_ref_in_item(&li->li_tv, copyID);
6894}
6895
6896/*
6897 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6898 */
6899 static void
6900set_ref_in_item(tv, copyID)
6901 typval_T *tv;
6902 int copyID;
6903{
6904 dict_T *dd;
6905 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006906
6907 switch (tv->v_type)
6908 {
6909 case VAR_DICT:
6910 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006911 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006912 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 /* Didn't see this dict yet. */
6914 dd->dv_copyID = copyID;
6915 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006916 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006918
6919 case VAR_LIST:
6920 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006921 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006922 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 /* Didn't see this list yet. */
6924 ll->lv_copyID = copyID;
6925 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006926 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006930}
6931
6932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 * Allocate an empty header for a dictionary.
6934 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006935 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936dict_alloc()
6937{
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006941 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006942 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006943 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 if (first_dict != NULL)
6945 first_dict->dv_used_prev = d;
6946 d->dv_used_next = first_dict;
6947 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006948 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006951 d->dv_lock = 0;
6952 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006953 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006954 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006956}
6957
6958/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006959 * Allocate an empty dict for a return value.
6960 * Returns OK or FAIL.
6961 */
6962 static int
6963rettv_dict_alloc(rettv)
6964 typval_T *rettv;
6965{
6966 dict_T *d = dict_alloc();
6967
6968 if (d == NULL)
6969 return FAIL;
6970
6971 rettv->vval.v_dict = d;
6972 rettv->v_type = VAR_DICT;
6973 ++d->dv_refcount;
6974 return OK;
6975}
6976
6977
6978/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979 * Unreference a Dictionary: decrement the reference count and free it when it
6980 * becomes zero.
6981 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006982 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006985{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006986 if (d != NULL && --d->dv_refcount <= 0)
6987 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006988}
6989
6990/*
6991 * Free a Dictionary, including all items it contains.
6992 * Ignores the reference count.
6993 */
6994 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006995dict_free(d, recurse)
6996 dict_T *d;
6997 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007002
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 /* Remove the dict from the list of dicts for garbage collection. */
7004 if (d->dv_used_prev == NULL)
7005 first_dict = d->dv_used_next;
7006 else
7007 d->dv_used_prev->dv_used_next = d->dv_used_next;
7008 if (d->dv_used_next != NULL)
7009 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7010
7011 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007013 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (!HASHITEM_EMPTY(hi))
7017 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007018 /* Remove the item before deleting it, just in case there is
7019 * something recursive causing trouble. */
7020 di = HI2DI(hi);
7021 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007022 if (recurse || (di->di_tv.v_type != VAR_LIST
7023 && di->di_tv.v_type != VAR_DICT))
7024 clear_tv(&di->di_tv);
7025 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007026 --todo;
7027 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030 vim_free(d);
7031}
7032
7033/*
7034 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035 * The "key" is copied to the new item.
7036 * Note that the value of the item "di_tv" still needs to be initialized!
7037 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007039 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040dictitem_alloc(key)
7041 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042{
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007045 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007046 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007049 di->di_flags = 0;
7050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052}
7053
7054/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007055 * Make a copy of a Dictionary item.
7056 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007058dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007060{
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007063 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7064 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 if (di != NULL)
7066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007067 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007068 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069 copy_tv(&org->di_tv, &di->di_tv);
7070 }
7071 return di;
7072}
7073
7074/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 * Remove item "item" from Dictionary "dict" and free it.
7076 */
7077 static void
7078dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 dict_T *dict;
7080 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007081{
Bram Moolenaar33570922005-01-25 22:26:29 +00007082 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 if (HASHITEM_EMPTY(hi))
7086 EMSG2(_(e_intern2), "dictitem_remove()");
7087 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 dictitem_free(item);
7090}
7091
7092/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 * Free a dict item. Also clears the value.
7094 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007095 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 clear_tv(&item->di_tv);
7100 vim_free(item);
7101}
7102
7103/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7105 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007106 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 * Returns NULL when out of memory.
7108 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007110dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007113 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114{
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 dict_T *copy;
7116 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119
7120 if (orig == NULL)
7121 return NULL;
7122
7123 copy = dict_alloc();
7124 if (copy != NULL)
7125 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007126 if (copyID != 0)
7127 {
7128 orig->dv_copyID = copyID;
7129 orig->dv_copydict = copy;
7130 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007131 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007132 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 --todo;
7137
7138 di = dictitem_alloc(hi->hi_key);
7139 if (di == NULL)
7140 break;
7141 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007142 {
7143 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7144 copyID) == FAIL)
7145 {
7146 vim_free(di);
7147 break;
7148 }
7149 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007150 else
7151 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7152 if (dict_add(copy, di) == FAIL)
7153 {
7154 dictitem_free(di);
7155 break;
7156 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007161 if (todo > 0)
7162 {
7163 dict_unref(copy);
7164 copy = NULL;
7165 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 }
7167
7168 return copy;
7169}
7170
7171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007173 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007175 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *d;
7178 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179{
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181}
7182
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007184 * Add a number or string entry to dictionary "d".
7185 * When "str" is NULL use number "nr", otherwise use "str".
7186 * Returns FAIL when out of memory and when key already exists.
7187 */
7188 int
7189dict_add_nr_str(d, key, nr, str)
7190 dict_T *d;
7191 char *key;
7192 long nr;
7193 char_u *str;
7194{
7195 dictitem_T *item;
7196
7197 item = dictitem_alloc((char_u *)key);
7198 if (item == NULL)
7199 return FAIL;
7200 item->di_tv.v_lock = 0;
7201 if (str == NULL)
7202 {
7203 item->di_tv.v_type = VAR_NUMBER;
7204 item->di_tv.vval.v_number = nr;
7205 }
7206 else
7207 {
7208 item->di_tv.v_type = VAR_STRING;
7209 item->di_tv.vval.v_string = vim_strsave(str);
7210 }
7211 if (dict_add(d, item) == FAIL)
7212 {
7213 dictitem_free(item);
7214 return FAIL;
7215 }
7216 return OK;
7217}
7218
7219/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007220 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Returns FAIL when out of memory and when key already exists.
7222 */
7223 int
7224dict_add_list(d, key, list)
7225 dict_T *d;
7226 char *key;
7227 list_T *list;
7228{
7229 dictitem_T *item;
7230
7231 item = dictitem_alloc((char_u *)key);
7232 if (item == NULL)
7233 return FAIL;
7234 item->di_tv.v_lock = 0;
7235 item->di_tv.v_type = VAR_LIST;
7236 item->di_tv.vval.v_list = list;
7237 if (dict_add(d, item) == FAIL)
7238 {
7239 dictitem_free(item);
7240 return FAIL;
7241 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007242 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007243 return OK;
7244}
7245
7246/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 * Get the number of items in a Dictionary.
7248 */
7249 static long
7250dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007252{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 if (d == NULL)
7254 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256}
7257
7258/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Find item "key[len]" in Dictionary "d".
7260 * If "len" is negative use strlen(key).
7261 * Returns NULL when not found.
7262 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007263 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 char_u *key;
7267 int len;
7268{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269#define AKEYLEN 200
7270 char_u buf[AKEYLEN];
7271 char_u *akey;
7272 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 if (len < 0)
7276 akey = key;
7277 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 tofree = akey = vim_strnsave(key, len);
7280 if (akey == NULL)
7281 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007282 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 else
7284 {
7285 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007286 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 akey = buf;
7288 }
7289
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 vim_free(tofree);
7292 if (HASHITEM_EMPTY(hi))
7293 return NULL;
7294 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295}
7296
7297/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007298 * Get a string item from a dictionary.
7299 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007300 * Returns NULL if the entry doesn't exist or out of memory.
7301 */
7302 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007303get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007304 dict_T *d;
7305 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007306 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007307{
7308 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007309 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007310
7311 di = dict_find(d, key, -1);
7312 if (di == NULL)
7313 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007314 s = get_tv_string(&di->di_tv);
7315 if (save && s != NULL)
7316 s = vim_strsave(s);
7317 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007318}
7319
7320/*
7321 * Get a number item from a dictionary.
7322 * Returns 0 if the entry doesn't exist or out of memory.
7323 */
7324 long
7325get_dict_number(d, key)
7326 dict_T *d;
7327 char_u *key;
7328{
7329 dictitem_T *di;
7330
7331 di = dict_find(d, key, -1);
7332 if (di == NULL)
7333 return 0;
7334 return get_tv_number(&di->di_tv);
7335}
7336
7337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 * Return an allocated string with the string representation of a Dictionary.
7339 * May return NULL.
7340 */
7341 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007342dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007344 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345{
7346 garray_T ga;
7347 int first = TRUE;
7348 char_u *tofree;
7349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 return NULL;
7357 ga_init2(&ga, (int)sizeof(char), 80);
7358 ga_append(&ga, '{');
7359
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007360 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 --todo;
7366
7367 if (first)
7368 first = FALSE;
7369 else
7370 ga_concat(&ga, (char_u *)", ");
7371
7372 tofree = string_quote(hi->hi_key, FALSE);
7373 if (tofree != NULL)
7374 {
7375 ga_concat(&ga, tofree);
7376 vim_free(tofree);
7377 }
7378 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007379 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 if (s != NULL)
7381 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 if (s == NULL)
7384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007387 if (todo > 0)
7388 {
7389 vim_free(ga.ga_data);
7390 return NULL;
7391 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392
7393 ga_append(&ga, '}');
7394 ga_append(&ga, NUL);
7395 return (char_u *)ga.ga_data;
7396}
7397
7398/*
7399 * Allocate a variable for a Dictionary and fill it from "*arg".
7400 * Return OK or FAIL. Returns NOTDONE for {expr}.
7401 */
7402 static int
7403get_dict_tv(arg, rettv, evaluate)
7404 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 int evaluate;
7407{
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 dict_T *d = NULL;
7409 typval_T tvkey;
7410 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007411 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415
7416 /*
7417 * First check if it's not a curly-braces thing: {expr}.
7418 * Must do this without evaluating, otherwise a function may be called
7419 * twice. Unfortunately this means we need to call eval1() twice for the
7420 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 if (*start != '}')
7424 {
7425 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7426 return FAIL;
7427 if (*start == '}')
7428 return NOTDONE;
7429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
7431 if (evaluate)
7432 {
7433 d = dict_alloc();
7434 if (d == NULL)
7435 return FAIL;
7436 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 tvkey.v_type = VAR_UNKNOWN;
7438 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439
7440 *arg = skipwhite(*arg + 1);
7441 while (**arg != '}' && **arg != NUL)
7442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007443 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 goto failret;
7445 if (**arg != ':')
7446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007447 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 goto failret;
7450 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007451 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007453 key = get_tv_string_buf_chk(&tvkey, buf);
7454 if (key == NULL || *key == NUL)
7455 {
7456 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7457 if (key != NULL)
7458 EMSG(_(e_emptykey));
7459 clear_tv(&tvkey);
7460 goto failret;
7461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 *arg = skipwhite(*arg + 1);
7465 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7466 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007467 if (evaluate)
7468 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469 goto failret;
7470 }
7471 if (evaluate)
7472 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 if (item != NULL)
7475 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007476 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 clear_tv(&tv);
7479 goto failret;
7480 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 item = dictitem_alloc(key);
7482 clear_tv(&tvkey);
7483 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007486 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 if (dict_add(d, item) == FAIL)
7488 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 }
7490 }
7491
7492 if (**arg == '}')
7493 break;
7494 if (**arg != ',')
7495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007496 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 goto failret;
7498 }
7499 *arg = skipwhite(*arg + 1);
7500 }
7501
7502 if (**arg != '}')
7503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007504 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505failret:
7506 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007507 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 return FAIL;
7509 }
7510
7511 *arg = skipwhite(*arg + 1);
7512 if (evaluate)
7513 {
7514 rettv->v_type = VAR_DICT;
7515 rettv->vval.v_dict = d;
7516 ++d->dv_refcount;
7517 }
7518
7519 return OK;
7520}
7521
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007523 * Return a string with the string representation of a variable.
7524 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007525 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007526 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007527 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007528 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007529 */
7530 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007531echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007533 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007534 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007535 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007536{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007537 static int recurse = 0;
7538 char_u *r = NULL;
7539
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543 *tofree = NULL;
7544 return NULL;
7545 }
7546 ++recurse;
7547
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007548 switch (tv->v_type)
7549 {
7550 case VAR_FUNC:
7551 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 r = tv->vval.v_string;
7553 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007556 if (tv->vval.v_list == NULL)
7557 {
7558 *tofree = NULL;
7559 r = NULL;
7560 }
7561 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7562 {
7563 *tofree = NULL;
7564 r = (char_u *)"[...]";
7565 }
7566 else
7567 {
7568 tv->vval.v_list->lv_copyID = copyID;
7569 *tofree = list2string(tv, copyID);
7570 r = *tofree;
7571 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 if (tv->vval.v_dict == NULL)
7576 {
7577 *tofree = NULL;
7578 r = NULL;
7579 }
7580 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7581 {
7582 *tofree = NULL;
7583 r = (char_u *)"{...}";
7584 }
7585 else
7586 {
7587 tv->vval.v_dict->dv_copyID = copyID;
7588 *tofree = dict2string(tv, copyID);
7589 r = *tofree;
7590 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 case VAR_STRING:
7594 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007595 *tofree = NULL;
7596 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007597 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007599#ifdef FEAT_FLOAT
7600 case VAR_FLOAT:
7601 *tofree = NULL;
7602 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7603 r = numbuf;
7604 break;
7605#endif
7606
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007608 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007611
7612 --recurse;
7613 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007614}
7615
7616/*
7617 * Return a string with the string representation of a variable.
7618 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7619 * "numbuf" is used for a number.
7620 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007621 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007622 */
7623 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007624tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007626 char_u **tofree;
7627 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007629{
7630 switch (tv->v_type)
7631 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007632 case VAR_FUNC:
7633 *tofree = string_quote(tv->vval.v_string, TRUE);
7634 return *tofree;
7635 case VAR_STRING:
7636 *tofree = string_quote(tv->vval.v_string, FALSE);
7637 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007638#ifdef FEAT_FLOAT
7639 case VAR_FLOAT:
7640 *tofree = NULL;
7641 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7642 return numbuf;
7643#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007645 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652}
7653
7654/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 * Return string "str" in ' quotes, doubling ' characters.
7656 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 */
7659 static char_u *
7660string_quote(str, function)
7661 char_u *str;
7662 int function;
7663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665 char_u *p, *r, *s;
7666
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 len = (function ? 13 : 3);
7668 if (str != NULL)
7669 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007670 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 for (p = str; *p != NUL; mb_ptr_adv(p))
7672 if (*p == '\'')
7673 ++len;
7674 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675 s = r = alloc(len);
7676 if (r != NULL)
7677 {
7678 if (function)
7679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 r += 10;
7682 }
7683 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 if (str != NULL)
7686 for (p = str; *p != NUL; )
7687 {
7688 if (*p == '\'')
7689 *r++ = '\'';
7690 MB_COPY_CHAR(p, r);
7691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 if (function)
7694 *r++ = ')';
7695 *r++ = NUL;
7696 }
7697 return s;
7698}
7699
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701/*
7702 * Convert the string "text" to a floating point number.
7703 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7704 * this always uses a decimal point.
7705 * Returns the length of the text that was consumed.
7706 */
7707 static int
7708string2float(text, value)
7709 char_u *text;
7710 float_T *value; /* result stored here */
7711{
7712 char *s = (char *)text;
7713 float_T f;
7714
7715 f = strtod(s, &s);
7716 *value = f;
7717 return (int)((char_u *)s - text);
7718}
7719#endif
7720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 * Get the value of an environment variable.
7723 * "arg" is pointing to the '$'. It is advanced to after the name.
7724 * If the environment variable was not set, silently assume it is empty.
7725 * Always return OK.
7726 */
7727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007728get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 int evaluate;
7732{
7733 char_u *string = NULL;
7734 int len;
7735 int cc;
7736 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007737 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
7739 ++*arg;
7740 name = *arg;
7741 len = get_env_len(arg);
7742 if (evaluate)
7743 {
7744 if (len != 0)
7745 {
7746 cc = name[len];
7747 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007748 /* first try vim_getenv(), fast for normal environment vars */
7749 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007751 {
7752 if (!mustfree)
7753 string = vim_strsave(string);
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 else
7756 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007757 if (mustfree)
7758 vim_free(string);
7759
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 /* next try expanding things like $VIM and ${HOME} */
7761 string = expand_env_save(name - 1);
7762 if (string != NULL && *string == '$')
7763 {
7764 vim_free(string);
7765 string = NULL;
7766 }
7767 }
7768 name[len] = cc;
7769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->v_type = VAR_STRING;
7771 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773
7774 return OK;
7775}
7776
7777/*
7778 * Array with names and number of arguments of all internal functions
7779 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7780 */
7781static struct fst
7782{
7783 char *f_name; /* function name */
7784 char f_min_argc; /* minimal number of arguments */
7785 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007787 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788} functions[] =
7789{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007790#ifdef FEAT_FLOAT
7791 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007792 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007793#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007794 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007795 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {"append", 2, 2, f_append},
7797 {"argc", 0, 0, f_argc},
7798 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007799 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007800#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007801 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007803 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007806 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {"bufexists", 1, 1, f_bufexists},
7808 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7809 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7810 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7811 {"buflisted", 1, 1, f_buflisted},
7812 {"bufloaded", 1, 1, f_bufloaded},
7813 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007814 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"bufwinnr", 1, 1, f_bufwinnr},
7816 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007817 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007819#ifdef FEAT_FLOAT
7820 {"ceil", 1, 1, f_ceil},
7821#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007822 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"char2nr", 1, 1, f_char2nr},
7824 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007825 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007827#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007828 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007829 {"complete_add", 1, 1, f_complete_add},
7830 {"complete_check", 0, 0, f_complete_check},
7831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007834#ifdef FEAT_FLOAT
7835 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007836 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007837#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007840 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007841 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"delete", 1, 1, f_delete},
7843 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007844 {"diff_filler", 1, 1, f_diff_filler},
7845 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007846 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007848 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"eventhandler", 0, 0, f_eventhandler},
7850 {"executable", 1, 1, f_executable},
7851 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007852#ifdef FEAT_FLOAT
7853 {"exp", 1, 1, f_exp},
7854#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007855 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007856 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007857 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7859 {"filereadable", 1, 1, f_filereadable},
7860 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007862 {"finddir", 1, 3, f_finddir},
7863 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"float2nr", 1, 1, f_float2nr},
7866 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007869 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"fnamemodify", 2, 2, f_fnamemodify},
7871 {"foldclosed", 1, 1, f_foldclosed},
7872 {"foldclosedend", 1, 1, f_foldclosedend},
7873 {"foldlevel", 1, 1, f_foldlevel},
7874 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007875 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007878 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007879 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007880 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"getbufvar", 2, 2, f_getbufvar},
7882 {"getchar", 0, 1, f_getchar},
7883 {"getcharmod", 0, 0, f_getcharmod},
7884 {"getcmdline", 0, 0, f_getcmdline},
7885 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007886 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007888 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007889 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"getfsize", 1, 1, f_getfsize},
7891 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007892 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007893 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007894 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007895 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007896 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007897 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007898 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007899 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007901 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007902 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"getwinposx", 0, 0, f_getwinposx},
7904 {"getwinposy", 0, 0, f_getwinposy},
7905 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007906 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007907 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007909 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007910 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007911 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7913 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7914 {"histadd", 2, 2, f_histadd},
7915 {"histdel", 1, 2, f_histdel},
7916 {"histget", 1, 2, f_histget},
7917 {"histnr", 1, 1, f_histnr},
7918 {"hlID", 1, 1, f_hlID},
7919 {"hlexists", 1, 1, f_hlexists},
7920 {"hostname", 0, 0, f_hostname},
7921 {"iconv", 3, 3, f_iconv},
7922 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007924 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007926 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"inputrestore", 0, 0, f_inputrestore},
7928 {"inputsave", 0, 0, f_inputsave},
7929 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007930 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007931 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007933 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007936 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"libcall", 3, 3, f_libcall},
7940 {"libcallnr", 3, 3, f_libcallnr},
7941 {"line", 1, 1, f_line},
7942 {"line2byte", 1, 1, f_line2byte},
7943 {"lispindent", 1, 1, f_lispindent},
7944 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007946 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947 {"log10", 1, 1, f_log10},
7948#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007950 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007951 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007952 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007953 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007954 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007955 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007956 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007957 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007958 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007959 {"max", 1, 1, f_max},
7960 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007961#ifdef vim_mkdir
7962 {"mkdir", 1, 3, f_mkdir},
7963#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007965#ifdef FEAT_MZSCHEME
7966 {"mzeval", 1, 1, f_mzeval},
7967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"nextnonblank", 1, 1, f_nextnonblank},
7969 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007970 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007971 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007972#ifdef FEAT_FLOAT
7973 {"pow", 2, 2, f_pow},
7974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007976 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007977 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007979 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007980 {"reltime", 0, 2, f_reltime},
7981 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"remote_expr", 2, 3, f_remote_expr},
7983 {"remote_foreground", 1, 1, f_remote_foreground},
7984 {"remote_peek", 1, 2, f_remote_peek},
7985 {"remote_read", 1, 1, f_remote_read},
7986 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007987 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007989 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007991 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992#ifdef FEAT_FLOAT
7993 {"round", 1, 1, f_round},
7994#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007995 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007996 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007997 {"searchpair", 3, 7, f_searchpair},
7998 {"searchpairpos", 3, 7, f_searchpairpos},
7999 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"server2client", 2, 2, f_server2client},
8001 {"serverlist", 0, 0, f_serverlist},
8002 {"setbufvar", 3, 3, f_setbufvar},
8003 {"setcmdpos", 1, 1, f_setcmdpos},
8004 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008005 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008007 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008008 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008010 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008011 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008013 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015#ifdef FEAT_FLOAT
8016 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008017 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008019 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008020 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008021 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008022 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008023 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"sqrt", 1, 1, f_sqrt},
8026 {"str2float", 1, 1, f_str2float},
8027#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008028 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008029 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008030 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#ifdef HAVE_STRFTIME
8032 {"strftime", 1, 2, f_strftime},
8033#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"strlen", 1, 1, f_strlen},
8037 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008038 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008040 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"submatch", 1, 1, f_submatch},
8042 {"substitute", 4, 4, f_substitute},
8043 {"synID", 3, 3, f_synID},
8044 {"synIDattr", 2, 3, f_synIDattr},
8045 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008046 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008047 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008048 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008049 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008050 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008051 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008052 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008053 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054#ifdef FEAT_FLOAT
8055 {"tan", 1, 1, f_tan},
8056 {"tanh", 1, 1, f_tanh},
8057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008059 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"tolower", 1, 1, f_tolower},
8061 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008062 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#ifdef FEAT_FLOAT
8064 {"trunc", 1, 1, f_trunc},
8065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008067 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008068 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"virtcol", 1, 1, f_virtcol},
8071 {"visualmode", 0, 1, f_visualmode},
8072 {"winbufnr", 1, 1, f_winbufnr},
8073 {"wincol", 0, 0, f_wincol},
8074 {"winheight", 1, 1, f_winheight},
8075 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008076 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008078 {"winrestview", 1, 1, f_winrestview},
8079 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008081 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083};
8084
8085#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8086
8087/*
8088 * Function given to ExpandGeneric() to obtain the list of internal
8089 * or user defined function names.
8090 */
8091 char_u *
8092get_function_name(xp, idx)
8093 expand_T *xp;
8094 int idx;
8095{
8096 static int intidx = -1;
8097 char_u *name;
8098
8099 if (idx == 0)
8100 intidx = -1;
8101 if (intidx < 0)
8102 {
8103 name = get_user_func_name(xp, idx);
8104 if (name != NULL)
8105 return name;
8106 }
8107 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8108 {
8109 STRCPY(IObuff, functions[intidx].f_name);
8110 STRCAT(IObuff, "(");
8111 if (functions[intidx].f_max_argc == 0)
8112 STRCAT(IObuff, ")");
8113 return IObuff;
8114 }
8115
8116 return NULL;
8117}
8118
8119/*
8120 * Function given to ExpandGeneric() to obtain the list of internal or
8121 * user defined variable or function names.
8122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 char_u *
8124get_expr_name(xp, idx)
8125 expand_T *xp;
8126 int idx;
8127{
8128 static int intidx = -1;
8129 char_u *name;
8130
8131 if (idx == 0)
8132 intidx = -1;
8133 if (intidx < 0)
8134 {
8135 name = get_function_name(xp, idx);
8136 if (name != NULL)
8137 return name;
8138 }
8139 return get_user_var_name(xp, ++intidx);
8140}
8141
8142#endif /* FEAT_CMDL_COMPL */
8143
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008144#if defined(EBCDIC) || defined(PROTO)
8145/*
8146 * Compare struct fst by function name.
8147 */
8148 static int
8149compare_func_name(s1, s2)
8150 const void *s1;
8151 const void *s2;
8152{
8153 struct fst *p1 = (struct fst *)s1;
8154 struct fst *p2 = (struct fst *)s2;
8155
8156 return STRCMP(p1->f_name, p2->f_name);
8157}
8158
8159/*
8160 * Sort the function table by function name.
8161 * The sorting of the table above is ASCII dependant.
8162 * On machines using EBCDIC we have to sort it.
8163 */
8164 static void
8165sortFunctions()
8166{
8167 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8168
8169 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8170}
8171#endif
8172
8173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174/*
8175 * Find internal function in table above.
8176 * Return index, or -1 if not found
8177 */
8178 static int
8179find_internal_func(name)
8180 char_u *name; /* name of the function */
8181{
8182 int first = 0;
8183 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8184 int cmp;
8185 int x;
8186
8187 /*
8188 * Find the function name in the table. Binary search.
8189 */
8190 while (first <= last)
8191 {
8192 x = first + ((unsigned)(last - first) >> 1);
8193 cmp = STRCMP(name, functions[x].f_name);
8194 if (cmp < 0)
8195 last = x - 1;
8196 else if (cmp > 0)
8197 first = x + 1;
8198 else
8199 return x;
8200 }
8201 return -1;
8202}
8203
8204/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008205 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8206 * name it contains, otherwise return "name".
8207 */
8208 static char_u *
8209deref_func_name(name, lenp)
8210 char_u *name;
8211 int *lenp;
8212{
Bram Moolenaar33570922005-01-25 22:26:29 +00008213 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 int cc;
8215
8216 cc = name[*lenp];
8217 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008218 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008219 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008221 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008223 {
8224 *lenp = 0;
8225 return (char_u *)""; /* just in case */
8226 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008227 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 }
8230
8231 return name;
8232}
8233
8234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 * Allocate a variable for the result of a function.
8236 * Return OK or FAIL.
8237 */
8238 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8240 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 char_u *name; /* name of the function */
8242 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 char_u **arg; /* argument, pointing to the '(' */
8245 linenr_T firstline; /* first line of range */
8246 linenr_T lastline; /* last line of range */
8247 int *doesrange; /* return: function handled range */
8248 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
8251 char_u *argp;
8252 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008253 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 int argcount = 0; /* number of arguments found */
8255
8256 /*
8257 * Get the arguments.
8258 */
8259 argp = *arg;
8260 while (argcount < MAX_FUNC_ARGS)
8261 {
8262 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8263 if (*argp == ')' || *argp == ',' || *argp == NUL)
8264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8266 {
8267 ret = FAIL;
8268 break;
8269 }
8270 ++argcount;
8271 if (*argp != ',')
8272 break;
8273 }
8274 if (*argp == ')')
8275 ++argp;
8276 else
8277 ret = FAIL;
8278
8279 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 {
8284 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008285 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008287 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
8293 *arg = skipwhite(argp);
8294 return ret;
8295}
8296
8297
8298/*
8299 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008300 * Return OK when the function can't be called, FAIL otherwise.
8301 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 */
8303 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008304call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008306 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008310 typval_T *argvars; /* vars for arguments, must have "argcount"
8311 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 linenr_T firstline; /* first line of range */
8313 linenr_T lastline; /* last line of range */
8314 int *doesrange; /* return: function handled range */
8315 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#define ERROR_UNKNOWN 0
8320#define ERROR_TOOMANY 1
8321#define ERROR_TOOFEW 2
8322#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323#define ERROR_DICT 4
8324#define ERROR_NONE 5
8325#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 int error = ERROR_NONE;
8327 int i;
8328 int llen;
8329 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#define FLEN_FIXED 40
8331 char_u fname_buf[FLEN_FIXED + 1];
8332 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008333 char_u *name;
8334
8335 /* Make a copy of the name, if it comes from a funcref variable it could
8336 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008337 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008338 if (name == NULL)
8339 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
8341 /*
8342 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8343 * Change <SNR>123_name() to K_SNR 123_name().
8344 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 llen = eval_fname_script(name);
8347 if (llen > 0)
8348 {
8349 fname_buf[0] = K_SPECIAL;
8350 fname_buf[1] = KS_EXTRA;
8351 fname_buf[2] = (int)KE_SNR;
8352 i = 3;
8353 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8354 {
8355 if (current_SID <= 0)
8356 error = ERROR_SCRIPT;
8357 else
8358 {
8359 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8360 i = (int)STRLEN(fname_buf);
8361 }
8362 }
8363 if (i + STRLEN(name + llen) < FLEN_FIXED)
8364 {
8365 STRCPY(fname_buf + i, name + llen);
8366 fname = fname_buf;
8367 }
8368 else
8369 {
8370 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8371 if (fname == NULL)
8372 error = ERROR_OTHER;
8373 else
8374 {
8375 mch_memmove(fname, fname_buf, (size_t)i);
8376 STRCPY(fname + i, name + llen);
8377 }
8378 }
8379 }
8380 else
8381 fname = name;
8382
8383 *doesrange = FALSE;
8384
8385
8386 /* execute the function if no errors detected and executing */
8387 if (evaluate && error == ERROR_NONE)
8388 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008389 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8390 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 error = ERROR_UNKNOWN;
8392
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008393 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {
8395 /*
8396 * User defined function.
8397 */
8398 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008401 /* Trigger FuncUndefined event, may load the function. */
8402 if (fp == NULL
8403 && apply_autocmds(EVENT_FUNCUNDEFINED,
8404 fname, fname, TRUE, NULL)
8405 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008407 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 fp = find_func(fname);
8409 }
8410#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008411 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008412 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008413 {
8414 /* loaded a package, search for the function again */
8415 fp = find_func(fname);
8416 }
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (fp != NULL)
8419 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008420 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008422 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008424 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008426 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 else
8429 {
8430 /*
8431 * Call the user function.
8432 * Save and restore search patterns, script variables and
8433 * redo buffer.
8434 */
8435 save_search_patterns();
8436 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008437 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008439 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008440 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8441 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8442 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008443 /* Function was unreferenced while being used, free it
8444 * now. */
8445 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 restoreRedobuff();
8447 restore_search_patterns();
8448 error = ERROR_NONE;
8449 }
8450 }
8451 }
8452 else
8453 {
8454 /*
8455 * Find the function name in the table, call its implementation.
8456 */
8457 i = find_internal_func(fname);
8458 if (i >= 0)
8459 {
8460 if (argcount < functions[i].f_min_argc)
8461 error = ERROR_TOOFEW;
8462 else if (argcount > functions[i].f_max_argc)
8463 error = ERROR_TOOMANY;
8464 else
8465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 error = ERROR_NONE;
8469 }
8470 }
8471 }
8472 /*
8473 * The function call (or "FuncUndefined" autocommand sequence) might
8474 * have been aborted by an error, an interrupt, or an explicitly thrown
8475 * exception that has not been caught so far. This situation can be
8476 * tested for by calling aborting(). For an error in an internal
8477 * function or for the "E132" error in call_user_func(), however, the
8478 * throw point at which the "force_abort" flag (temporarily reset by
8479 * emsg()) is normally updated has not been reached yet. We need to
8480 * update that flag first to make aborting() reliable.
8481 */
8482 update_force_abort();
8483 }
8484 if (error == ERROR_NONE)
8485 ret = OK;
8486
8487 /*
8488 * Report an error unless the argument evaluation or function call has been
8489 * cancelled due to an aborting error, an interrupt, or an exception.
8490 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008491 if (!aborting())
8492 {
8493 switch (error)
8494 {
8495 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008496 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008497 break;
8498 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008499 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008500 break;
8501 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008502 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008503 name);
8504 break;
8505 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008506 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008507 name);
8508 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008509 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008510 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511 name);
8512 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008513 }
8514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 if (fname != name && fname != fname_buf)
8517 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008518 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519
8520 return ret;
8521}
8522
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008523/*
8524 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008525 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008526 */
8527 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008528emsg_funcname(ermsg, name)
8529 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008530 char_u *name;
8531{
8532 char_u *p;
8533
8534 if (*name == K_SPECIAL)
8535 p = concat_str((char_u *)"<SNR>", name + 3);
8536 else
8537 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008538 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008539 if (p != name)
8540 vim_free(p);
8541}
8542
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008543/*
8544 * Return TRUE for a non-zero Number and a non-empty String.
8545 */
8546 static int
8547non_zero_arg(argvars)
8548 typval_T *argvars;
8549{
8550 return ((argvars[0].v_type == VAR_NUMBER
8551 && argvars[0].vval.v_number != 0)
8552 || (argvars[0].v_type == VAR_STRING
8553 && argvars[0].vval.v_string != NULL
8554 && *argvars[0].vval.v_string != NUL));
8555}
8556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557/*********************************************
8558 * Implementation of the built-in functions
8559 */
8560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008561#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008562static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8563
8564/*
8565 * Get the float value of "argvars[0]" into "f".
8566 * Returns FAIL when the argument is not a Number or Float.
8567 */
8568 static int
8569get_float_arg(argvars, f)
8570 typval_T *argvars;
8571 float_T *f;
8572{
8573 if (argvars[0].v_type == VAR_FLOAT)
8574 {
8575 *f = argvars[0].vval.v_float;
8576 return OK;
8577 }
8578 if (argvars[0].v_type == VAR_NUMBER)
8579 {
8580 *f = (float_T)argvars[0].vval.v_number;
8581 return OK;
8582 }
8583 EMSG(_("E808: Number or Float required"));
8584 return FAIL;
8585}
8586
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008587/*
8588 * "abs(expr)" function
8589 */
8590 static void
8591f_abs(argvars, rettv)
8592 typval_T *argvars;
8593 typval_T *rettv;
8594{
8595 if (argvars[0].v_type == VAR_FLOAT)
8596 {
8597 rettv->v_type = VAR_FLOAT;
8598 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8599 }
8600 else
8601 {
8602 varnumber_T n;
8603 int error = FALSE;
8604
8605 n = get_tv_number_chk(&argvars[0], &error);
8606 if (error)
8607 rettv->vval.v_number = -1;
8608 else if (n > 0)
8609 rettv->vval.v_number = n;
8610 else
8611 rettv->vval.v_number = -n;
8612 }
8613}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614
8615/*
8616 * "acos()" function
8617 */
8618 static void
8619f_acos(argvars, rettv)
8620 typval_T *argvars;
8621 typval_T *rettv;
8622{
8623 float_T f;
8624
8625 rettv->v_type = VAR_FLOAT;
8626 if (get_float_arg(argvars, &f) == OK)
8627 rettv->vval.v_float = acos(f);
8628 else
8629 rettv->vval.v_float = 0.0;
8630}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008631#endif
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008634 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 */
8636 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008637f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 typval_T *argvars;
8639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008646 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008647 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008648 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 }
8651 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008652 EMSG(_(e_listreq));
8653}
8654
8655/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008656 * "and(expr, expr)" function
8657 */
8658 static void
8659f_and(argvars, rettv)
8660 typval_T *argvars;
8661 typval_T *rettv;
8662{
8663 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8664 & get_tv_number_chk(&argvars[1], NULL);
8665}
8666
8667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008668 * "append(lnum, string/list)" function
8669 */
8670 static void
8671f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 typval_T *argvars;
8673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008674{
8675 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008676 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 list_T *l = NULL;
8678 listitem_T *li = NULL;
8679 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008680 long added = 0;
8681
Bram Moolenaar0d660222005-01-07 21:51:51 +00008682 lnum = get_tv_lnum(argvars);
8683 if (lnum >= 0
8684 && lnum <= curbuf->b_ml.ml_line_count
8685 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008686 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008687 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008688 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008689 l = argvars[1].vval.v_list;
8690 if (l == NULL)
8691 return;
8692 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008693 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008694 for (;;)
8695 {
8696 if (l == NULL)
8697 tv = &argvars[1]; /* append a string */
8698 else if (li == NULL)
8699 break; /* end of list */
8700 else
8701 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008702 line = get_tv_string_chk(tv);
8703 if (line == NULL) /* type error */
8704 {
8705 rettv->vval.v_number = 1; /* Failed */
8706 break;
8707 }
8708 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008709 ++added;
8710 if (l == NULL)
8711 break;
8712 li = li->li_next;
8713 }
8714
8715 appended_lines_mark(lnum, added);
8716 if (curwin->w_cursor.lnum > lnum)
8717 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 else
8720 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721}
8722
8723/*
8724 * "argc()" function
8725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008731 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732}
8733
8734/*
8735 * "argidx()" function
8736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008739 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744
8745/*
8746 * "argv(nr)" function
8747 */
8748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
8753 int idx;
8754
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008755 if (argvars[0].v_type != VAR_UNKNOWN)
8756 {
8757 idx = get_tv_number_chk(&argvars[0], NULL);
8758 if (idx >= 0 && idx < ARGCOUNT)
8759 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8760 else
8761 rettv->vval.v_string = NULL;
8762 rettv->v_type = VAR_STRING;
8763 }
8764 else if (rettv_list_alloc(rettv) == OK)
8765 for (idx = 0; idx < ARGCOUNT; ++idx)
8766 list_append_string(rettv->vval.v_list,
8767 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768}
8769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008770#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008771/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008772 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008773 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008774 static void
8775f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008776 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008777 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008778{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008779 float_T f;
8780
8781 rettv->v_type = VAR_FLOAT;
8782 if (get_float_arg(argvars, &f) == OK)
8783 rettv->vval.v_float = asin(f);
8784 else
8785 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008786}
8787
8788/*
8789 * "atan()" function
8790 */
8791 static void
8792f_atan(argvars, rettv)
8793 typval_T *argvars;
8794 typval_T *rettv;
8795{
8796 float_T f;
8797
8798 rettv->v_type = VAR_FLOAT;
8799 if (get_float_arg(argvars, &f) == OK)
8800 rettv->vval.v_float = atan(f);
8801 else
8802 rettv->vval.v_float = 0.0;
8803}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008804
8805/*
8806 * "atan2()" function
8807 */
8808 static void
8809f_atan2(argvars, rettv)
8810 typval_T *argvars;
8811 typval_T *rettv;
8812{
8813 float_T fx, fy;
8814
8815 rettv->v_type = VAR_FLOAT;
8816 if (get_float_arg(argvars, &fx) == OK
8817 && get_float_arg(&argvars[1], &fy) == OK)
8818 rettv->vval.v_float = atan2(fx, fy);
8819 else
8820 rettv->vval.v_float = 0.0;
8821}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822#endif
8823
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824/*
8825 * "browse(save, title, initdir, default)" function
8826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008829 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832#ifdef FEAT_BROWSE
8833 int save;
8834 char_u *title;
8835 char_u *initdir;
8836 char_u *defname;
8837 char_u buf[NUMBUFLEN];
8838 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 save = get_tv_number_chk(&argvars[0], &error);
8842 title = get_tv_string_chk(&argvars[1]);
8843 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8844 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 if (error || title == NULL || initdir == NULL || defname == NULL)
8847 rettv->vval.v_string = NULL;
8848 else
8849 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008850 do_browse(save ? BROWSE_SAVE : 0,
8851 title, defname, NULL, initdir, NULL, curbuf);
8852#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008856}
8857
8858/*
8859 * "browsedir(title, initdir)" function
8860 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008865{
8866#ifdef FEAT_BROWSE
8867 char_u *title;
8868 char_u *initdir;
8869 char_u buf[NUMBUFLEN];
8870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008871 title = get_tv_string_chk(&argvars[0]);
8872 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 if (title == NULL || initdir == NULL)
8875 rettv->vval.v_string = NULL;
8876 else
8877 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008878 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883}
8884
Bram Moolenaar33570922005-01-25 22:26:29 +00008885static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * Find a buffer by number or exact name.
8889 */
8890 static buf_T *
8891find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
8894 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008896 if (avar->v_type == VAR_NUMBER)
8897 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008898 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008900 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008901 if (buf == NULL)
8902 {
8903 /* No full path name match, try a match with a URL or a "nofile"
8904 * buffer, these don't use the full path. */
8905 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8906 if (buf->b_fname != NULL
8907 && (path_with_url(buf->b_fname)
8908#ifdef FEAT_QUICKFIX
8909 || bt_nofile(buf)
8910#endif
8911 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008913 break;
8914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 }
8916 return buf;
8917}
8918
8919/*
8920 * "bufexists(expr)" function
8921 */
8922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *argvars;
8925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928}
8929
8930/*
8931 * "buflisted(expr)" function
8932 */
8933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *argvars;
8936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 buf_T *buf;
8939
8940 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
8944/*
8945 * "bufloaded(expr)" function
8946 */
8947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T *argvars;
8950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 buf_T *buf;
8953
8954 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956}
8957
Bram Moolenaar33570922005-01-25 22:26:29 +00008958static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960/*
8961 * Get buffer by number or pattern.
8962 */
8963 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 int save_magic;
8969 char_u *save_cpo;
8970 buf_T *buf;
8971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 if (tv->v_type == VAR_NUMBER)
8973 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008974 if (tv->v_type != VAR_STRING)
8975 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (name == NULL || *name == NUL)
8977 return curbuf;
8978 if (name[0] == '$' && name[1] == NUL)
8979 return lastbuf;
8980
8981 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8982 save_magic = p_magic;
8983 p_magic = TRUE;
8984 save_cpo = p_cpo;
8985 p_cpo = (char_u *)"";
8986
8987 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8988 TRUE, FALSE));
8989
8990 p_magic = save_magic;
8991 p_cpo = save_cpo;
8992
8993 /* If not found, try expanding the name, like done for bufexists(). */
8994 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996
8997 return buf;
8998}
8999
9000/*
9001 * "bufname(expr)" function
9002 */
9003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 buf_T *buf;
9009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 buf = get_buf_tv(&argvars[0]);
9013 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 --emsg_off;
9019}
9020
9021/*
9022 * "bufnr(expr)" function
9023 */
9024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *argvars;
9027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009030 int error = FALSE;
9031 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009036 --emsg_off;
9037
9038 /* If the buffer isn't found and the second argument is not zero create a
9039 * new buffer. */
9040 if (buf == NULL
9041 && argvars[1].v_type != VAR_UNKNOWN
9042 && get_tv_number_chk(&argvars[1], &error) != 0
9043 && !error
9044 && (name = get_tv_string_chk(&argvars[0])) != NULL
9045 && !error)
9046 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052}
9053
9054/*
9055 * "bufwinnr(nr)" function
9056 */
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062#ifdef FEAT_WINDOWS
9063 win_T *wp;
9064 int winnr = 0;
9065#endif
9066 buf_T *buf;
9067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009068 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071#ifdef FEAT_WINDOWS
9072 for (wp = firstwin; wp; wp = wp->w_next)
9073 {
9074 ++winnr;
9075 if (wp->w_buffer == buf)
9076 break;
9077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#endif
9082 --emsg_off;
9083}
9084
9085/*
9086 * "byte2line(byte)" function
9087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095#else
9096 long boff = 0;
9097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 (linenr_T)0, &boff);
9104#endif
9105}
9106
9107/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009108 * "byteidx()" function
9109 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 typval_T *argvars;
9113 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009114{
9115#ifdef FEAT_MBYTE
9116 char_u *t;
9117#endif
9118 char_u *str;
9119 long idx;
9120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 str = get_tv_string_chk(&argvars[0]);
9122 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009124 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009125 return;
9126
9127#ifdef FEAT_MBYTE
9128 t = str;
9129 for ( ; idx > 0; idx--)
9130 {
9131 if (*t == NUL) /* EOL reached */
9132 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009133 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009134 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009135 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009136#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009137 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009139#endif
9140}
9141
9142/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009143 * "call(func, arglist)" function
9144 */
9145 static void
9146f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009147 typval_T *argvars;
9148 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009149{
9150 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009151 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009152 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009154 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009156
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009157 if (argvars[1].v_type != VAR_LIST)
9158 {
9159 EMSG(_(e_listreq));
9160 return;
9161 }
9162 if (argvars[1].vval.v_list == NULL)
9163 return;
9164
9165 if (argvars[0].v_type == VAR_FUNC)
9166 func = argvars[0].vval.v_string;
9167 else
9168 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009169 if (*func == NUL)
9170 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009171
Bram Moolenaare9a41262005-01-15 22:18:47 +00009172 if (argvars[2].v_type != VAR_UNKNOWN)
9173 {
9174 if (argvars[2].v_type != VAR_DICT)
9175 {
9176 EMSG(_(e_dictreq));
9177 return;
9178 }
9179 selfdict = argvars[2].vval.v_dict;
9180 }
9181
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009182 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9183 item = item->li_next)
9184 {
9185 if (argc == MAX_FUNC_ARGS)
9186 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009187 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009188 break;
9189 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009190 /* Make a copy of each argument. This is needed to be able to set
9191 * v_lock to VAR_FIXED in the copy without changing the original list.
9192 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009193 copy_tv(&item->li_tv, &argv[argc++]);
9194 }
9195
9196 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009197 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009198 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9199 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009200
9201 /* Free the arguments. */
9202 while (argc > 0)
9203 clear_tv(&argv[--argc]);
9204}
9205
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009206#ifdef FEAT_FLOAT
9207/*
9208 * "ceil({float})" function
9209 */
9210 static void
9211f_ceil(argvars, rettv)
9212 typval_T *argvars;
9213 typval_T *rettv;
9214{
9215 float_T f;
9216
9217 rettv->v_type = VAR_FLOAT;
9218 if (get_float_arg(argvars, &f) == OK)
9219 rettv->vval.v_float = ceil(f);
9220 else
9221 rettv->vval.v_float = 0.0;
9222}
9223#endif
9224
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009225/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009226 * "changenr()" function
9227 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009228 static void
9229f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009230 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009231 typval_T *rettv;
9232{
9233 rettv->vval.v_number = curbuf->b_u_seq_cur;
9234}
9235
9236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 * "char2nr(string)" function
9238 */
9239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 typval_T *argvars;
9242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244#ifdef FEAT_MBYTE
9245 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 else
9248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250}
9251
9252/*
9253 * "cindent(lnum)" function
9254 */
9255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009257 typval_T *argvars;
9258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259{
9260#ifdef FEAT_CINDENT
9261 pos_T pos;
9262 linenr_T lnum;
9263
9264 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9267 {
9268 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 curwin->w_cursor = pos;
9271 }
9272 else
9273#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275}
9276
9277/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009278 * "clearmatches()" function
9279 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009280 static void
9281f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009282 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009283 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009284{
9285#ifdef FEAT_SEARCH_EXTRA
9286 clear_matches(curwin);
9287#endif
9288}
9289
9290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 * "col(string)" function
9292 */
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 colnr_T col = 0;
9299 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009300 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009302 fp = var2fpos(&argvars[0], FALSE, &fnum);
9303 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 {
9305 if (fp->col == MAXCOL)
9306 {
9307 /* '> can be MAXCOL, get the length of the line then */
9308 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009309 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 else
9311 col = MAXCOL;
9312 }
9313 else
9314 {
9315 col = fp->col + 1;
9316#ifdef FEAT_VIRTUALEDIT
9317 /* col(".") when the cursor is on the NUL at the end of the line
9318 * because of "coladd" can be seen as an extra column. */
9319 if (virtual_active() && fp == &curwin->w_cursor)
9320 {
9321 char_u *p = ml_get_cursor();
9322
9323 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9324 curwin->w_virtcol - curwin->w_cursor.coladd))
9325 {
9326# ifdef FEAT_MBYTE
9327 int l;
9328
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009329 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 col += l;
9331# else
9332 if (*p != NUL && p[1] == NUL)
9333 ++col;
9334# endif
9335 }
9336 }
9337#endif
9338 }
9339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341}
9342
Bram Moolenaar572cb562005-08-05 21:35:02 +00009343#if defined(FEAT_INS_EXPAND)
9344/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009345 * "complete()" function
9346 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009347 static void
9348f_complete(argvars, rettv)
9349 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009350 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009351{
9352 int startcol;
9353
9354 if ((State & INSERT) == 0)
9355 {
9356 EMSG(_("E785: complete() can only be used in Insert mode"));
9357 return;
9358 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009359
9360 /* Check for undo allowed here, because if something was already inserted
9361 * the line was already saved for undo and this check isn't done. */
9362 if (!undo_allowed())
9363 return;
9364
Bram Moolenaarade00832006-03-10 21:46:58 +00009365 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9366 {
9367 EMSG(_(e_invarg));
9368 return;
9369 }
9370
9371 startcol = get_tv_number_chk(&argvars[0], NULL);
9372 if (startcol <= 0)
9373 return;
9374
9375 set_completion(startcol - 1, argvars[1].vval.v_list);
9376}
9377
9378/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009379 * "complete_add()" function
9380 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009381 static void
9382f_complete_add(argvars, rettv)
9383 typval_T *argvars;
9384 typval_T *rettv;
9385{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009386 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009387}
9388
9389/*
9390 * "complete_check()" function
9391 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009392 static void
9393f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009394 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009395 typval_T *rettv;
9396{
9397 int saved = RedrawingDisabled;
9398
9399 RedrawingDisabled = 0;
9400 ins_compl_check_keys(0);
9401 rettv->vval.v_number = compl_interrupted;
9402 RedrawingDisabled = saved;
9403}
9404#endif
9405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406/*
9407 * "confirm(message, buttons[, default [, type]])" function
9408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009411 typval_T *argvars UNUSED;
9412 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9415 char_u *message;
9416 char_u *buttons = NULL;
9417 char_u buf[NUMBUFLEN];
9418 char_u buf2[NUMBUFLEN];
9419 int def = 1;
9420 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 char_u *typestr;
9422 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009424 message = get_tv_string_chk(&argvars[0]);
9425 if (message == NULL)
9426 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9430 if (buttons == NULL)
9431 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009435 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9438 if (typestr == NULL)
9439 error = TRUE;
9440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 switch (TOUPPER_ASC(*typestr))
9443 {
9444 case 'E': type = VIM_ERROR; break;
9445 case 'Q': type = VIM_QUESTION; break;
9446 case 'I': type = VIM_INFO; break;
9447 case 'W': type = VIM_WARNING; break;
9448 case 'G': type = VIM_GENERIC; break;
9449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 }
9451 }
9452 }
9453 }
9454
9455 if (buttons == NULL || *buttons == NUL)
9456 buttons = (char_u *)_("&Ok");
9457
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009458 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009460 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#endif
9462}
9463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009464/*
9465 * "copy()" function
9466 */
9467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009469 typval_T *argvars;
9470 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009471{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009472 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009475#ifdef FEAT_FLOAT
9476/*
9477 * "cos()" function
9478 */
9479 static void
9480f_cos(argvars, rettv)
9481 typval_T *argvars;
9482 typval_T *rettv;
9483{
9484 float_T f;
9485
9486 rettv->v_type = VAR_FLOAT;
9487 if (get_float_arg(argvars, &f) == OK)
9488 rettv->vval.v_float = cos(f);
9489 else
9490 rettv->vval.v_float = 0.0;
9491}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009492
9493/*
9494 * "cosh()" function
9495 */
9496 static void
9497f_cosh(argvars, rettv)
9498 typval_T *argvars;
9499 typval_T *rettv;
9500{
9501 float_T f;
9502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = cosh(f);
9506 else
9507 rettv->vval.v_float = 0.0;
9508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509#endif
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009512 * "count()" function
9513 */
9514 static void
9515f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009518{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009519 long n = 0;
9520 int ic = FALSE;
9521
Bram Moolenaare9a41262005-01-15 22:18:47 +00009522 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009524 listitem_T *li;
9525 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009526 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009527
Bram Moolenaare9a41262005-01-15 22:18:47 +00009528 if ((l = argvars[0].vval.v_list) != NULL)
9529 {
9530 li = l->lv_first;
9531 if (argvars[2].v_type != VAR_UNKNOWN)
9532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 int error = FALSE;
9534
9535 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009536 if (argvars[3].v_type != VAR_UNKNOWN)
9537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 idx = get_tv_number_chk(&argvars[3], &error);
9539 if (!error)
9540 {
9541 li = list_find(l, idx);
9542 if (li == NULL)
9543 EMSGN(_(e_listidx), idx);
9544 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 if (error)
9547 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009548 }
9549
9550 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009551 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009552 ++n;
9553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 else if (argvars[0].v_type == VAR_DICT)
9556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 int todo;
9558 dict_T *d;
9559 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009560
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009561 if ((d = argvars[0].vval.v_dict) != NULL)
9562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 int error = FALSE;
9564
Bram Moolenaare9a41262005-01-15 22:18:47 +00009565 if (argvars[2].v_type != VAR_UNKNOWN)
9566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009568 if (argvars[3].v_type != VAR_UNKNOWN)
9569 EMSG(_(e_invarg));
9570 }
9571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009572 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009574 {
9575 if (!HASHITEM_EMPTY(hi))
9576 {
9577 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009578 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009579 ++n;
9580 }
9581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 }
9583 }
9584 else
9585 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009586 rettv->vval.v_number = n;
9587}
9588
9589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9591 *
9592 * Checks the existence of a cscope connection.
9593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009596 typval_T *argvars UNUSED;
9597 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599#ifdef FEAT_CSCOPE
9600 int num = 0;
9601 char_u *dbpath = NULL;
9602 char_u *prepend = NULL;
9603 char_u buf[NUMBUFLEN];
9604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009605 if (argvars[0].v_type != VAR_UNKNOWN
9606 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 num = (int)get_tv_number(&argvars[0]);
9609 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 }
9613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615#endif
9616}
9617
9618/*
9619 * "cursor(lnum, col)" function
9620 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009621 * Moves the cursor to the specified line and column.
9622 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009630#ifdef FEAT_VIRTUALEDIT
9631 long coladd = 0;
9632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009634 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009635 if (argvars[1].v_type == VAR_UNKNOWN)
9636 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009637 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009638
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009639 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009640 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009641 line = pos.lnum;
9642 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009643#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009644 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009645#endif
9646 }
9647 else
9648 {
9649 line = get_tv_lnum(argvars);
9650 col = get_tv_number_chk(&argvars[1], NULL);
9651#ifdef FEAT_VIRTUALEDIT
9652 if (argvars[2].v_type != VAR_UNKNOWN)
9653 coladd = get_tv_number_chk(&argvars[2], NULL);
9654#endif
9655 }
9656 if (line < 0 || col < 0
9657#ifdef FEAT_VIRTUALEDIT
9658 || coladd < 0
9659#endif
9660 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009661 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 if (line > 0)
9663 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (col > 0)
9665 curwin->w_cursor.col = col - 1;
9666#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009667 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668#endif
9669
9670 /* Make sure the cursor is in a valid position. */
9671 check_cursor();
9672#ifdef FEAT_MBYTE
9673 /* Correct cursor for multi-byte character. */
9674 if (has_mbyte)
9675 mb_adjust_cursor();
9676#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009677
9678 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681
9682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009683 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 */
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009690 int noref = 0;
9691
9692 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009694 if (noref < 0 || noref > 1)
9695 EMSG(_(e_invarg));
9696 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009697 {
9698 current_copyID += COPYID_INC;
9699 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
9703/*
9704 * "delete()" function
9705 */
9706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715}
9716
9717/*
9718 * "did_filetype()" function
9719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009722 typval_T *argvars UNUSED;
9723 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727#endif
9728}
9729
9730/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009731 * "diff_filler()" function
9732 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009735 typval_T *argvars UNUSED;
9736 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009737{
9738#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009740#endif
9741}
9742
9743/*
9744 * "diff_hlID()" function
9745 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009748 typval_T *argvars UNUSED;
9749 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009750{
9751#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009753 static linenr_T prev_lnum = 0;
9754 static int changedtick = 0;
9755 static int fnum = 0;
9756 static int change_start = 0;
9757 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009758 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009759 int filler_lines;
9760 int col;
9761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 if (lnum < 0) /* ignore type error in {lnum} arg */
9763 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009764 if (lnum != prev_lnum
9765 || changedtick != curbuf->b_changedtick
9766 || fnum != curbuf->b_fnum)
9767 {
9768 /* New line, buffer, change: need to get the values. */
9769 filler_lines = diff_check(curwin, lnum);
9770 if (filler_lines < 0)
9771 {
9772 if (filler_lines == -1)
9773 {
9774 change_start = MAXCOL;
9775 change_end = -1;
9776 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9777 hlID = HLF_ADD; /* added line */
9778 else
9779 hlID = HLF_CHD; /* changed line */
9780 }
9781 else
9782 hlID = HLF_ADD; /* added line */
9783 }
9784 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009785 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009786 prev_lnum = lnum;
9787 changedtick = curbuf->b_changedtick;
9788 fnum = curbuf->b_fnum;
9789 }
9790
9791 if (hlID == HLF_CHD || hlID == HLF_TXD)
9792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009794 if (col >= change_start && col <= change_end)
9795 hlID = HLF_TXD; /* changed text */
9796 else
9797 hlID = HLF_CHD; /* changed line */
9798 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009799 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009800#endif
9801}
9802
9803/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009804 * "empty({expr})" function
9805 */
9806 static void
9807f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009808 typval_T *argvars;
9809 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009810{
9811 int n;
9812
9813 switch (argvars[0].v_type)
9814 {
9815 case VAR_STRING:
9816 case VAR_FUNC:
9817 n = argvars[0].vval.v_string == NULL
9818 || *argvars[0].vval.v_string == NUL;
9819 break;
9820 case VAR_NUMBER:
9821 n = argvars[0].vval.v_number == 0;
9822 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009823#ifdef FEAT_FLOAT
9824 case VAR_FLOAT:
9825 n = argvars[0].vval.v_float == 0.0;
9826 break;
9827#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009828 case VAR_LIST:
9829 n = argvars[0].vval.v_list == NULL
9830 || argvars[0].vval.v_list->lv_first == NULL;
9831 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009832 case VAR_DICT:
9833 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009835 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009836 default:
9837 EMSG2(_(e_intern2), "f_empty()");
9838 n = 0;
9839 }
9840
9841 rettv->vval.v_number = n;
9842}
9843
9844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 * "escape({string}, {chars})" function
9846 */
9847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *argvars;
9850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852 char_u buf[NUMBUFLEN];
9853
Bram Moolenaar758711c2005-02-02 23:11:38 +00009854 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9855 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009860 * "eval()" function
9861 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009862 static void
9863f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009866{
9867 char_u *s;
9868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 s = get_tv_string_chk(&argvars[0]);
9870 if (s != NULL)
9871 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009873 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9874 {
9875 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009876 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009878 else if (*s != NUL)
9879 EMSG(_(e_trailing));
9880}
9881
9882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 * "eventhandler()" function
9884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
9893/*
9894 * "executable()" function
9895 */
9896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
9905 * "exists()" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 char_u *p;
9913 char_u *name;
9914 int n = FALSE;
9915 int len = 0;
9916
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009917 no_autoload = TRUE;
9918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 if (*p == '$') /* environment variable */
9921 {
9922 /* first try "normal" environment variables (fast) */
9923 if (mch_getenv(p + 1) != NULL)
9924 n = TRUE;
9925 else
9926 {
9927 /* try expanding things like $VIM and ${HOME} */
9928 p = expand_env_save(p);
9929 if (p != NULL && *p != '$')
9930 n = TRUE;
9931 vim_free(p);
9932 }
9933 }
9934 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009937 if (*skipwhite(p) != NUL)
9938 n = FALSE; /* trailing garbage */
9939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 else if (*p == '*') /* internal or user defined function */
9941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009942 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944 else if (*p == ':')
9945 {
9946 n = cmd_exists(p + 1);
9947 }
9948 else if (*p == '#')
9949 {
9950#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009951 if (p[1] == '#')
9952 n = autocmd_supported(p + 2);
9953 else
9954 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955#endif
9956 }
9957 else /* internal variable */
9958 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009959 char_u *tofree;
9960 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009962 /* get_name_len() takes care of expanding curly braces */
9963 name = p;
9964 len = get_name_len(&p, &tofree, TRUE, FALSE);
9965 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009967 if (tofree != NULL)
9968 name = tofree;
9969 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9970 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009972 /* handle d.key, l[idx], f(expr) */
9973 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9974 if (n)
9975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 }
9977 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009978 if (*p != NUL)
9979 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009981 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 }
9983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009985
9986 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009989#ifdef FEAT_FLOAT
9990/*
9991 * "exp()" function
9992 */
9993 static void
9994f_exp(argvars, rettv)
9995 typval_T *argvars;
9996 typval_T *rettv;
9997{
9998 float_T f;
9999
10000 rettv->v_type = VAR_FLOAT;
10001 if (get_float_arg(argvars, &f) == OK)
10002 rettv->vval.v_float = exp(f);
10003 else
10004 rettv->vval.v_float = 0.0;
10005}
10006#endif
10007
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008/*
10009 * "expand()" function
10010 */
10011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *argvars;
10014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 char_u *s;
10017 int len;
10018 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010019 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010022 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010025 if (argvars[1].v_type != VAR_UNKNOWN
10026 && argvars[2].v_type != VAR_UNKNOWN
10027 && get_tv_number_chk(&argvars[2], &error)
10028 && !error)
10029 {
10030 rettv->v_type = VAR_LIST;
10031 rettv->vval.v_list = NULL;
10032 }
10033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (*s == '%' || *s == '#' || *s == '<')
10036 {
10037 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010038 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010040 if (rettv->v_type == VAR_LIST)
10041 {
10042 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10043 list_append_string(rettv->vval.v_list, result, -1);
10044 else
10045 vim_free(result);
10046 }
10047 else
10048 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 }
10050 else
10051 {
10052 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010053 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010054 if (argvars[1].v_type != VAR_UNKNOWN
10055 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010056 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010057 if (!error)
10058 {
10059 ExpandInit(&xpc);
10060 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010061 if (p_wic)
10062 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010063 if (rettv->v_type == VAR_STRING)
10064 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10065 options, WILD_ALL);
10066 else if (rettv_list_alloc(rettv) != FAIL)
10067 {
10068 int i;
10069
10070 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10071 for (i = 0; i < xpc.xp_numfiles; i++)
10072 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10073 ExpandCleanup(&xpc);
10074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075 }
10076 else
10077 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079}
10080
10081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010082 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010083 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010084 */
10085 static void
10086f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010087 typval_T *argvars;
10088 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010089{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010090 char *arg_errmsg = N_("extend() argument");
10091
Bram Moolenaare9a41262005-01-15 22:18:47 +000010092 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010094 list_T *l1, *l2;
10095 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010096 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010098
Bram Moolenaare9a41262005-01-15 22:18:47 +000010099 l1 = argvars[0].vval.v_list;
10100 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010101 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010102 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010103 {
10104 if (argvars[2].v_type != VAR_UNKNOWN)
10105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 before = get_tv_number_chk(&argvars[2], &error);
10107 if (error)
10108 return; /* type error; errmsg already given */
10109
Bram Moolenaar758711c2005-02-02 23:11:38 +000010110 if (before == l1->lv_len)
10111 item = NULL;
10112 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010114 item = list_find(l1, before);
10115 if (item == NULL)
10116 {
10117 EMSGN(_(e_listidx), before);
10118 return;
10119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010120 }
10121 }
10122 else
10123 item = NULL;
10124 list_extend(l1, l2, item);
10125
Bram Moolenaare9a41262005-01-15 22:18:47 +000010126 copy_tv(&argvars[0], rettv);
10127 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010129 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10130 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010131 dict_T *d1, *d2;
10132 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010133 char_u *action;
10134 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010135 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010136 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010137
10138 d1 = argvars[0].vval.v_dict;
10139 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010140 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010141 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010142 {
10143 /* Check the third argument. */
10144 if (argvars[2].v_type != VAR_UNKNOWN)
10145 {
10146 static char *(av[]) = {"keep", "force", "error"};
10147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010148 action = get_tv_string_chk(&argvars[2]);
10149 if (action == NULL)
10150 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010151 for (i = 0; i < 3; ++i)
10152 if (STRCMP(action, av[i]) == 0)
10153 break;
10154 if (i == 3)
10155 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010156 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010157 return;
10158 }
10159 }
10160 else
10161 action = (char_u *)"force";
10162
10163 /* Go over all entries in the second dict and add them to the
10164 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010165 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010166 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010168 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010170 --todo;
10171 di1 = dict_find(d1, hi2->hi_key, -1);
10172 if (di1 == NULL)
10173 {
10174 di1 = dictitem_copy(HI2DI(hi2));
10175 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10176 dictitem_free(di1);
10177 }
10178 else if (*action == 'e')
10179 {
10180 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10181 break;
10182 }
10183 else if (*action == 'f')
10184 {
10185 clear_tv(&di1->di_tv);
10186 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10187 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 }
10189 }
10190
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 copy_tv(&argvars[0], rettv);
10192 }
10193 }
10194 else
10195 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010196}
10197
10198/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010199 * "feedkeys()" function
10200 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010201 static void
10202f_feedkeys(argvars, rettv)
10203 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010204 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010205{
10206 int remap = TRUE;
10207 char_u *keys, *flags;
10208 char_u nbuf[NUMBUFLEN];
10209 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010210 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010211
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010212 /* This is not allowed in the sandbox. If the commands would still be
10213 * executed in the sandbox it would be OK, but it probably happens later,
10214 * when "sandbox" is no longer set. */
10215 if (check_secure())
10216 return;
10217
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010218 keys = get_tv_string(&argvars[0]);
10219 if (*keys != NUL)
10220 {
10221 if (argvars[1].v_type != VAR_UNKNOWN)
10222 {
10223 flags = get_tv_string_buf(&argvars[1], nbuf);
10224 for ( ; *flags != NUL; ++flags)
10225 {
10226 switch (*flags)
10227 {
10228 case 'n': remap = FALSE; break;
10229 case 'm': remap = TRUE; break;
10230 case 't': typed = TRUE; break;
10231 }
10232 }
10233 }
10234
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010235 /* Need to escape K_SPECIAL and CSI before putting the string in the
10236 * typeahead buffer. */
10237 keys_esc = vim_strsave_escape_csi(keys);
10238 if (keys_esc != NULL)
10239 {
10240 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010241 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010242 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010243 if (vgetc_busy)
10244 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010245 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010246 }
10247}
10248
10249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 * "filereadable()" function
10251 */
10252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 typval_T *argvars;
10255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010257 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 char_u *p;
10259 int n;
10260
Bram Moolenaarc236c162008-07-13 17:41:49 +000010261#ifndef O_NONBLOCK
10262# define O_NONBLOCK 0
10263#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010265 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10266 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 {
10268 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010269 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 }
10271 else
10272 n = FALSE;
10273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275}
10276
10277/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010278 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 * rights to write into.
10280 */
10281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010283 typval_T *argvars;
10284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010286 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287}
10288
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010289static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010290
10291 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010292findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *argvars;
10294 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010295 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010296{
10297#ifdef FEAT_SEARCHPATH
10298 char_u *fname;
10299 char_u *fresult = NULL;
10300 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10301 char_u *p;
10302 char_u pathbuf[NUMBUFLEN];
10303 int count = 1;
10304 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010305 int error = FALSE;
10306#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010307
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010308 rettv->vval.v_string = NULL;
10309 rettv->v_type = VAR_STRING;
10310
10311#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010313
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010314 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10317 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010318 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 else
10320 {
10321 if (*p != NUL)
10322 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010325 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010327 }
10328
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010329 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10330 error = TRUE;
10331
10332 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010334 do
10335 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010336 if (rettv->v_type == VAR_STRING)
10337 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 fresult = find_file_in_path_option(first ? fname : NULL,
10339 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010340 0, first, path,
10341 find_what,
10342 curbuf->b_ffname,
10343 find_what == FINDFILE_DIR
10344 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010346
10347 if (fresult != NULL && rettv->v_type == VAR_LIST)
10348 list_append_string(rettv->vval.v_list, fresult, -1);
10349
10350 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010351 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010352
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010353 if (rettv->v_type == VAR_STRING)
10354 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010355#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010356}
10357
Bram Moolenaar33570922005-01-25 22:26:29 +000010358static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10359static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010360
10361/*
10362 * Implementation of map() and filter().
10363 */
10364 static void
10365filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010366 typval_T *argvars;
10367 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010368 int map;
10369{
10370 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010372 listitem_T *li, *nli;
10373 list_T *l = NULL;
10374 dictitem_T *di;
10375 hashtab_T *ht;
10376 hashitem_T *hi;
10377 dict_T *d = NULL;
10378 typval_T save_val;
10379 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010380 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010381 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010382 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10383 char *arg_errmsg = (map ? N_("map() argument")
10384 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010385 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010386 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010387
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010389 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010390 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010391 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392 return;
10393 }
10394 else if (argvars[0].v_type == VAR_DICT)
10395 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010396 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010397 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010398 return;
10399 }
10400 else
10401 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010402 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010403 return;
10404 }
10405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010406 expr = get_tv_string_buf_chk(&argvars[1], buf);
10407 /* On type errors, the preceding call has already displayed an error
10408 * message. Avoid a misleading error message for an empty string that
10409 * was not passed as argument. */
10410 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 prepare_vimvar(VV_VAL, &save_val);
10413 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010414
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010415 /* We reset "did_emsg" to be able to detect whether an error
10416 * occurred during evaluation of the expression. */
10417 save_did_emsg = did_emsg;
10418 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010419
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010420 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 vimvars[VV_KEY].vv_type = VAR_STRING;
10424
10425 ht = &d->dv_hashtab;
10426 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010427 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 if (!HASHITEM_EMPTY(hi))
10431 {
10432 --todo;
10433 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010434 if (tv_check_lock(di->di_tv.v_lock,
10435 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 break;
10437 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010438 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010439 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 break;
10441 if (!map && rem)
10442 dictitem_remove(d, di);
10443 clear_tv(&vimvars[VV_KEY].vv_tv);
10444 }
10445 }
10446 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 }
10448 else
10449 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010450 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 for (li = l->lv_first; li != NULL; li = nli)
10453 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010454 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010455 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010457 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010458 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010459 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010460 break;
10461 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010463 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010464 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010465 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010466
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010467 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010469
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010470 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010471 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010472
10473 copy_tv(&argvars[0], rettv);
10474}
10475
10476 static int
10477filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479 char_u *expr;
10480 int map;
10481 int *remp;
10482{
Bram Moolenaar33570922005-01-25 22:26:29 +000010483 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010485 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010488 s = expr;
10489 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010490 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010491 if (*s != NUL) /* check for trailing chars after expr */
10492 {
10493 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010494 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 }
10496 if (map)
10497 {
10498 /* map(): replace the list item value */
10499 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010500 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 *tv = rettv;
10502 }
10503 else
10504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 int error = FALSE;
10506
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010509 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 /* On type error, nothing has been removed; return FAIL to stop the
10511 * loop. The error message was given by get_tv_number_chk(). */
10512 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010513 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010514 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010515 retval = OK;
10516theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010518 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010519}
10520
10521/*
10522 * "filter()" function
10523 */
10524 static void
10525f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010526 typval_T *argvars;
10527 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010528{
10529 filter_map(argvars, rettv, FALSE);
10530}
10531
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010533 * "finddir({fname}[, {path}[, {count}]])" function
10534 */
10535 static void
10536f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010539{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010540 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010541}
10542
10543/*
10544 * "findfile({fname}[, {path}[, {count}]])" function
10545 */
10546 static void
10547f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010548 typval_T *argvars;
10549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010551 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010552}
10553
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010554#ifdef FEAT_FLOAT
10555/*
10556 * "float2nr({float})" function
10557 */
10558 static void
10559f_float2nr(argvars, rettv)
10560 typval_T *argvars;
10561 typval_T *rettv;
10562{
10563 float_T f;
10564
10565 if (get_float_arg(argvars, &f) == OK)
10566 {
10567 if (f < -0x7fffffff)
10568 rettv->vval.v_number = -0x7fffffff;
10569 else if (f > 0x7fffffff)
10570 rettv->vval.v_number = 0x7fffffff;
10571 else
10572 rettv->vval.v_number = (varnumber_T)f;
10573 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010574}
10575
10576/*
10577 * "floor({float})" function
10578 */
10579 static void
10580f_floor(argvars, rettv)
10581 typval_T *argvars;
10582 typval_T *rettv;
10583{
10584 float_T f;
10585
10586 rettv->v_type = VAR_FLOAT;
10587 if (get_float_arg(argvars, &f) == OK)
10588 rettv->vval.v_float = floor(f);
10589 else
10590 rettv->vval.v_float = 0.0;
10591}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010592
10593/*
10594 * "fmod()" function
10595 */
10596 static void
10597f_fmod(argvars, rettv)
10598 typval_T *argvars;
10599 typval_T *rettv;
10600{
10601 float_T fx, fy;
10602
10603 rettv->v_type = VAR_FLOAT;
10604 if (get_float_arg(argvars, &fx) == OK
10605 && get_float_arg(&argvars[1], &fy) == OK)
10606 rettv->vval.v_float = fmod(fx, fy);
10607 else
10608 rettv->vval.v_float = 0.0;
10609}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010610#endif
10611
Bram Moolenaar0d660222005-01-07 21:51:51 +000010612/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010613 * "fnameescape({string})" function
10614 */
10615 static void
10616f_fnameescape(argvars, rettv)
10617 typval_T *argvars;
10618 typval_T *rettv;
10619{
10620 rettv->vval.v_string = vim_strsave_fnameescape(
10621 get_tv_string(&argvars[0]), FALSE);
10622 rettv->v_type = VAR_STRING;
10623}
10624
10625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 * "fnamemodify({fname}, {mods})" function
10627 */
10628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633 char_u *fname;
10634 char_u *mods;
10635 int usedlen = 0;
10636 int len;
10637 char_u *fbuf = NULL;
10638 char_u buf[NUMBUFLEN];
10639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 fname = get_tv_string_chk(&argvars[0]);
10641 mods = get_tv_string_buf_chk(&argvars[1], buf);
10642 if (fname == NULL || mods == NULL)
10643 fname = NULL;
10644 else
10645 {
10646 len = (int)STRLEN(fname);
10647 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 vim_free(fbuf);
10656}
10657
Bram Moolenaar33570922005-01-25 22:26:29 +000010658static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659
10660/*
10661 * "foldclosed()" function
10662 */
10663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010665 typval_T *argvars;
10666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 int end;
10668{
10669#ifdef FEAT_FOLDING
10670 linenr_T lnum;
10671 linenr_T first, last;
10672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10675 {
10676 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10677 {
10678 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 return;
10683 }
10684 }
10685#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687}
10688
10689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010690 * "foldclosed()" function
10691 */
10692 static void
10693f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010694 typval_T *argvars;
10695 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010696{
10697 foldclosed_both(argvars, rettv, FALSE);
10698}
10699
10700/*
10701 * "foldclosedend()" function
10702 */
10703 static void
10704f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010705 typval_T *argvars;
10706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707{
10708 foldclosed_both(argvars, rettv, TRUE);
10709}
10710
10711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 * "foldlevel()" function
10713 */
10714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 typval_T *argvars;
10717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718{
10719#ifdef FEAT_FOLDING
10720 linenr_T lnum;
10721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726}
10727
10728/*
10729 * "foldtext()" function
10730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
10736#ifdef FEAT_FOLDING
10737 linenr_T lnum;
10738 char_u *s;
10739 char_u *r;
10740 int len;
10741 char *txt;
10742#endif
10743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 rettv->v_type = VAR_STRING;
10745 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10748 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10749 <= curbuf->b_ml.ml_line_count
10750 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 {
10752 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010753 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10754 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 {
10756 if (!linewhite(lnum))
10757 break;
10758 ++lnum;
10759 }
10760
10761 /* Find interesting text in this line. */
10762 s = skipwhite(ml_get(lnum));
10763 /* skip C comment-start */
10764 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010767 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010768 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010769 {
10770 s = skipwhite(ml_get(lnum + 1));
10771 if (*s == '*')
10772 s = skipwhite(s + 1);
10773 }
10774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 txt = _("+-%s%3ld lines: ");
10776 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 + 20 /* for %3ld */
10779 + STRLEN(s))); /* concatenated */
10780 if (r != NULL)
10781 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10783 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10784 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 len = (int)STRLEN(r);
10786 STRCAT(r, s);
10787 /* remove 'foldmarker' and 'commentstring' */
10788 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 }
10791 }
10792#endif
10793}
10794
10795/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010796 * "foldtextresult(lnum)" function
10797 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010802{
10803#ifdef FEAT_FOLDING
10804 linenr_T lnum;
10805 char_u *text;
10806 char_u buf[51];
10807 foldinfo_T foldinfo;
10808 int fold_count;
10809#endif
10810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 rettv->v_type = VAR_STRING;
10812 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010813#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010815 /* treat illegal types and illegal string values for {lnum} the same */
10816 if (lnum < 0)
10817 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010818 fold_count = foldedCount(curwin, lnum, &foldinfo);
10819 if (fold_count > 0)
10820 {
10821 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10822 &foldinfo, buf);
10823 if (text == buf)
10824 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010826 }
10827#endif
10828}
10829
10830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 * "foreground()" function
10832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010835 typval_T *argvars UNUSED;
10836 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838#ifdef FEAT_GUI
10839 if (gui.in_use)
10840 gui_mch_set_foreground();
10841#else
10842# ifdef WIN32
10843 win32_set_foreground();
10844# endif
10845#endif
10846}
10847
10848/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010849 * "function()" function
10850 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010853 typval_T *argvars;
10854 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010855{
10856 char_u *s;
10857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010859 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010860 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010861 /* Don't check an autoload name for existence here. */
10862 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010863 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010864 else
10865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 rettv->vval.v_string = vim_strsave(s);
10867 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868 }
10869}
10870
10871/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010872 * "garbagecollect()" function
10873 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010874 static void
10875f_garbagecollect(argvars, rettv)
10876 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010877 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010878{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010879 /* This is postponed until we are back at the toplevel, because we may be
10880 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10881 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010882
10883 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10884 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010885}
10886
10887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010888 * "get()" function
10889 */
10890 static void
10891f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T *argvars;
10893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894{
Bram Moolenaar33570922005-01-25 22:26:29 +000010895 listitem_T *li;
10896 list_T *l;
10897 dictitem_T *di;
10898 dict_T *d;
10899 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010900
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010902 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 int error = FALSE;
10906
10907 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10908 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010909 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010910 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010911 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912 else if (argvars[0].v_type == VAR_DICT)
10913 {
10914 if ((d = argvars[0].vval.v_dict) != NULL)
10915 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010916 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010917 if (di != NULL)
10918 tv = &di->di_tv;
10919 }
10920 }
10921 else
10922 EMSG2(_(e_listdictarg), "get()");
10923
10924 if (tv == NULL)
10925 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010926 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 copy_tv(&argvars[2], rettv);
10928 }
10929 else
10930 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010931}
10932
Bram Moolenaar342337a2005-07-21 21:11:17 +000010933static 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 +000010934
10935/*
10936 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010937 * Return a range (from start to end) of lines in rettv from the specified
10938 * buffer.
10939 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010940 */
10941 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010942get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010943 buf_T *buf;
10944 linenr_T start;
10945 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010946 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010947 typval_T *rettv;
10948{
10949 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010950
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010951 if (retlist && rettv_list_alloc(rettv) == FAIL)
10952 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010953
10954 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10955 return;
10956
10957 if (!retlist)
10958 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010959 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10960 p = ml_get_buf(buf, start, FALSE);
10961 else
10962 p = (char_u *)"";
10963
10964 rettv->v_type = VAR_STRING;
10965 rettv->vval.v_string = vim_strsave(p);
10966 }
10967 else
10968 {
10969 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010970 return;
10971
10972 if (start < 1)
10973 start = 1;
10974 if (end > buf->b_ml.ml_line_count)
10975 end = buf->b_ml.ml_line_count;
10976 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010977 if (list_append_string(rettv->vval.v_list,
10978 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010979 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010980 }
10981}
10982
10983/*
10984 * "getbufline()" function
10985 */
10986 static void
10987f_getbufline(argvars, rettv)
10988 typval_T *argvars;
10989 typval_T *rettv;
10990{
10991 linenr_T lnum;
10992 linenr_T end;
10993 buf_T *buf;
10994
10995 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10996 ++emsg_off;
10997 buf = get_buf_tv(&argvars[0]);
10998 --emsg_off;
10999
Bram Moolenaar661b1822005-07-28 22:36:45 +000011000 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011001 if (argvars[2].v_type == VAR_UNKNOWN)
11002 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011003 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011004 end = get_tv_lnum_buf(&argvars[2], buf);
11005
Bram Moolenaar342337a2005-07-21 21:11:17 +000011006 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011007}
11008
Bram Moolenaar0d660222005-01-07 21:51:51 +000011009/*
11010 * "getbufvar()" function
11011 */
11012 static void
11013f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011016{
11017 buf_T *buf;
11018 buf_T *save_curbuf;
11019 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011020 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011022 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11023 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011024 ++emsg_off;
11025 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011026
11027 rettv->v_type = VAR_STRING;
11028 rettv->vval.v_string = NULL;
11029
11030 if (buf != NULL && varname != NULL)
11031 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011032 /* set curbuf to be our buf, temporarily */
11033 save_curbuf = curbuf;
11034 curbuf = buf;
11035
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011037 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011038 else if (STRCMP(varname, "changedtick") == 0)
11039 {
11040 rettv->v_type = VAR_NUMBER;
11041 rettv->vval.v_number = curbuf->b_changedtick;
11042 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011043 else
11044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 if (*varname == NUL)
11046 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11047 * scope prefix before the NUL byte is required by
11048 * find_var_in_ht(). */
11049 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011050 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011051 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011052 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011054 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011055
11056 /* restore previous notion of curbuf */
11057 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011058 }
11059
11060 --emsg_off;
11061}
11062
11063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 * "getchar()" function
11065 */
11066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 typval_T *argvars;
11069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
11071 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011074 /* Position the cursor. Needed after a message that ends in a space. */
11075 windgoto(msg_row, msg_col);
11076
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 ++no_mapping;
11078 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011079 for (;;)
11080 {
11081 if (argvars[0].v_type == VAR_UNKNOWN)
11082 /* getchar(): blocking wait. */
11083 n = safe_vgetc();
11084 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11085 /* getchar(1): only check if char avail */
11086 n = vpeekc();
11087 else if (error || vpeekc() == NUL)
11088 /* illegal argument or getchar(0) and no char avail: return zero */
11089 n = 0;
11090 else
11091 /* getchar(0) and char avail: return char */
11092 n = safe_vgetc();
11093 if (n == K_IGNORE)
11094 continue;
11095 break;
11096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 --no_mapping;
11098 --allow_keys;
11099
Bram Moolenaar219b8702006-11-01 14:32:36 +000011100 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11101 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11102 vimvars[VV_MOUSE_COL].vv_nr = 0;
11103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 if (IS_SPECIAL(n) || mod_mask != 0)
11106 {
11107 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11108 int i = 0;
11109
11110 /* Turn a special key into three bytes, plus modifier. */
11111 if (mod_mask != 0)
11112 {
11113 temp[i++] = K_SPECIAL;
11114 temp[i++] = KS_MODIFIER;
11115 temp[i++] = mod_mask;
11116 }
11117 if (IS_SPECIAL(n))
11118 {
11119 temp[i++] = K_SPECIAL;
11120 temp[i++] = K_SECOND(n);
11121 temp[i++] = K_THIRD(n);
11122 }
11123#ifdef FEAT_MBYTE
11124 else if (has_mbyte)
11125 i += (*mb_char2bytes)(n, temp + i);
11126#endif
11127 else
11128 temp[i++] = n;
11129 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->v_type = VAR_STRING;
11131 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011132
11133#ifdef FEAT_MOUSE
11134 if (n == K_LEFTMOUSE
11135 || n == K_LEFTMOUSE_NM
11136 || n == K_LEFTDRAG
11137 || n == K_LEFTRELEASE
11138 || n == K_LEFTRELEASE_NM
11139 || n == K_MIDDLEMOUSE
11140 || n == K_MIDDLEDRAG
11141 || n == K_MIDDLERELEASE
11142 || n == K_RIGHTMOUSE
11143 || n == K_RIGHTDRAG
11144 || n == K_RIGHTRELEASE
11145 || n == K_X1MOUSE
11146 || n == K_X1DRAG
11147 || n == K_X1RELEASE
11148 || n == K_X2MOUSE
11149 || n == K_X2DRAG
11150 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011151 || n == K_MOUSELEFT
11152 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011153 || n == K_MOUSEDOWN
11154 || n == K_MOUSEUP)
11155 {
11156 int row = mouse_row;
11157 int col = mouse_col;
11158 win_T *win;
11159 linenr_T lnum;
11160# ifdef FEAT_WINDOWS
11161 win_T *wp;
11162# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011163 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011164
11165 if (row >= 0 && col >= 0)
11166 {
11167 /* Find the window at the mouse coordinates and compute the
11168 * text position. */
11169 win = mouse_find_win(&row, &col);
11170 (void)mouse_comp_pos(win, &row, &col, &lnum);
11171# ifdef FEAT_WINDOWS
11172 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011173 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011174# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011175 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011176 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11177 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11178 }
11179 }
11180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 }
11182}
11183
11184/*
11185 * "getcharmod()" function
11186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011189 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193}
11194
11195/*
11196 * "getcmdline()" function
11197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011200 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->v_type = VAR_STRING;
11204 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205}
11206
11207/*
11208 * "getcmdpos()" function
11209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011212 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216}
11217
11218/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011219 * "getcmdtype()" function
11220 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011221 static void
11222f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011223 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011224 typval_T *rettv;
11225{
11226 rettv->v_type = VAR_STRING;
11227 rettv->vval.v_string = alloc(2);
11228 if (rettv->vval.v_string != NULL)
11229 {
11230 rettv->vval.v_string[0] = get_cmdline_type();
11231 rettv->vval.v_string[1] = NUL;
11232 }
11233}
11234
11235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 * "getcwd()" function
11237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011240 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011243 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011246 rettv->vval.v_string = NULL;
11247 cwd = alloc(MAXPATHL);
11248 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011250 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11251 {
11252 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011254 if (rettv->vval.v_string != NULL)
11255 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011257 }
11258 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 }
11260}
11261
11262/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011263 * "getfontname()" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011267 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011268 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011269{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->v_type = VAR_STRING;
11271 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011272#ifdef FEAT_GUI
11273 if (gui.in_use)
11274 {
11275 GuiFont font;
11276 char_u *name = NULL;
11277
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011278 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011279 {
11280 /* Get the "Normal" font. Either the name saved by
11281 * hl_set_font_name() or from the font ID. */
11282 font = gui.norm_font;
11283 name = hl_get_font_name();
11284 }
11285 else
11286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011288 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11289 return;
11290 font = gui_mch_get_font(name, FALSE);
11291 if (font == NOFONT)
11292 return; /* Invalid font name, return empty string. */
11293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011295 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011296 gui_mch_free_font(font);
11297 }
11298#endif
11299}
11300
11301/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011302 * "getfperm({fname})" function
11303 */
11304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011306 typval_T *argvars;
11307 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011308{
11309 char_u *fname;
11310 struct stat st;
11311 char_u *perm = NULL;
11312 char_u flags[] = "rwx";
11313 int i;
11314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011318 if (mch_stat((char *)fname, &st) >= 0)
11319 {
11320 perm = vim_strsave((char_u *)"---------");
11321 if (perm != NULL)
11322 {
11323 for (i = 0; i < 9; i++)
11324 {
11325 if (st.st_mode & (1 << (8 - i)))
11326 perm[i] = flags[i % 3];
11327 }
11328 }
11329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011331}
11332
11333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 * "getfsize({fname})" function
11335 */
11336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 typval_T *argvars;
11339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
11341 char_u *fname;
11342 struct stat st;
11343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347
11348 if (mch_stat((char *)fname, &st) >= 0)
11349 {
11350 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011355
11356 /* non-perfect check for overflow */
11357 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11358 rettv->vval.v_number = -2;
11359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 }
11361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363}
11364
11365/*
11366 * "getftime({fname})" function
11367 */
11368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
11373 char_u *fname;
11374 struct stat st;
11375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377
11378 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382}
11383
11384/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011385 * "getftype({fname})" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391{
11392 char_u *fname;
11393 struct stat st;
11394 char_u *type = NULL;
11395 char *t;
11396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011400 if (mch_lstat((char *)fname, &st) >= 0)
11401 {
11402#ifdef S_ISREG
11403 if (S_ISREG(st.st_mode))
11404 t = "file";
11405 else if (S_ISDIR(st.st_mode))
11406 t = "dir";
11407# ifdef S_ISLNK
11408 else if (S_ISLNK(st.st_mode))
11409 t = "link";
11410# endif
11411# ifdef S_ISBLK
11412 else if (S_ISBLK(st.st_mode))
11413 t = "bdev";
11414# endif
11415# ifdef S_ISCHR
11416 else if (S_ISCHR(st.st_mode))
11417 t = "cdev";
11418# endif
11419# ifdef S_ISFIFO
11420 else if (S_ISFIFO(st.st_mode))
11421 t = "fifo";
11422# endif
11423# ifdef S_ISSOCK
11424 else if (S_ISSOCK(st.st_mode))
11425 t = "fifo";
11426# endif
11427 else
11428 t = "other";
11429#else
11430# ifdef S_IFMT
11431 switch (st.st_mode & S_IFMT)
11432 {
11433 case S_IFREG: t = "file"; break;
11434 case S_IFDIR: t = "dir"; break;
11435# ifdef S_IFLNK
11436 case S_IFLNK: t = "link"; break;
11437# endif
11438# ifdef S_IFBLK
11439 case S_IFBLK: t = "bdev"; break;
11440# endif
11441# ifdef S_IFCHR
11442 case S_IFCHR: t = "cdev"; break;
11443# endif
11444# ifdef S_IFIFO
11445 case S_IFIFO: t = "fifo"; break;
11446# endif
11447# ifdef S_IFSOCK
11448 case S_IFSOCK: t = "socket"; break;
11449# endif
11450 default: t = "other";
11451 }
11452# else
11453 if (mch_isdir(fname))
11454 t = "dir";
11455 else
11456 t = "file";
11457# endif
11458#endif
11459 type = vim_strsave((char_u *)t);
11460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011462}
11463
11464/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011465 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466 */
11467 static void
11468f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *argvars;
11470 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011471{
11472 linenr_T lnum;
11473 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011474 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011475
11476 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011477 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011478 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011479 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011480 retlist = FALSE;
11481 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011482 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011483 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011485 retlist = TRUE;
11486 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011487
Bram Moolenaar342337a2005-07-21 21:11:17 +000011488 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489}
11490
11491/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011492 * "getmatches()" function
11493 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011494 static void
11495f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011496 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011497 typval_T *rettv;
11498{
11499#ifdef FEAT_SEARCH_EXTRA
11500 dict_T *dict;
11501 matchitem_T *cur = curwin->w_match_head;
11502
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011503 if (rettv_list_alloc(rettv) == OK)
11504 {
11505 while (cur != NULL)
11506 {
11507 dict = dict_alloc();
11508 if (dict == NULL)
11509 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011510 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11511 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11512 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11513 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11514 list_append_dict(rettv->vval.v_list, dict);
11515 cur = cur->next;
11516 }
11517 }
11518#endif
11519}
11520
11521/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011522 * "getpid()" function
11523 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011524 static void
11525f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011526 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011527 typval_T *rettv;
11528{
11529 rettv->vval.v_number = mch_get_pid();
11530}
11531
11532/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011533 * "getpos(string)" function
11534 */
11535 static void
11536f_getpos(argvars, rettv)
11537 typval_T *argvars;
11538 typval_T *rettv;
11539{
11540 pos_T *fp;
11541 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011542 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011543
11544 if (rettv_list_alloc(rettv) == OK)
11545 {
11546 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011547 fp = var2fpos(&argvars[0], TRUE, &fnum);
11548 if (fnum != -1)
11549 list_append_number(l, (varnumber_T)fnum);
11550 else
11551 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011552 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11553 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011554 list_append_number(l, (fp != NULL)
11555 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011556 : (varnumber_T)0);
11557 list_append_number(l,
11558#ifdef FEAT_VIRTUALEDIT
11559 (fp != NULL) ? (varnumber_T)fp->coladd :
11560#endif
11561 (varnumber_T)0);
11562 }
11563 else
11564 rettv->vval.v_number = FALSE;
11565}
11566
11567/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011568 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011569 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011570 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011571f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011572 typval_T *argvars UNUSED;
11573 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011574{
11575#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011576 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011577#endif
11578
Bram Moolenaar2641f772005-03-25 21:58:17 +000011579#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011580 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011581 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011582 wp = NULL;
11583 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11584 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011585 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011586 if (wp == NULL)
11587 return;
11588 }
11589
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011590 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011591 }
11592#endif
11593}
11594
11595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 * "getreg()" function
11597 */
11598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 typval_T *argvars;
11601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602{
11603 char_u *strregname;
11604 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011605 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011606 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 strregname = get_tv_string_chk(&argvars[0]);
11611 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011612 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011613 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011616 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 regname = (strregname == NULL ? '"' : *strregname);
11618 if (regname == 0)
11619 regname = '"';
11620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 rettv->vval.v_string = error ? NULL :
11623 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624}
11625
11626/*
11627 * "getregtype()" function
11628 */
11629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011631 typval_T *argvars;
11632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633{
11634 char_u *strregname;
11635 int regname;
11636 char_u buf[NUMBUFLEN + 2];
11637 long reglen = 0;
11638
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011639 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011640 {
11641 strregname = get_tv_string_chk(&argvars[0]);
11642 if (strregname == NULL) /* type error; errmsg already given */
11643 {
11644 rettv->v_type = VAR_STRING;
11645 rettv->vval.v_string = NULL;
11646 return;
11647 }
11648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 else
11650 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011651 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652
11653 regname = (strregname == NULL ? '"' : *strregname);
11654 if (regname == 0)
11655 regname = '"';
11656
11657 buf[0] = NUL;
11658 buf[1] = NUL;
11659 switch (get_reg_type(regname, &reglen))
11660 {
11661 case MLINE: buf[0] = 'V'; break;
11662 case MCHAR: buf[0] = 'v'; break;
11663#ifdef FEAT_VISUAL
11664 case MBLOCK:
11665 buf[0] = Ctrl_V;
11666 sprintf((char *)buf + 1, "%ld", reglen + 1);
11667 break;
11668#endif
11669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 rettv->v_type = VAR_STRING;
11671 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672}
11673
11674/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011675 * "gettabvar()" function
11676 */
11677 static void
11678f_gettabvar(argvars, rettv)
11679 typval_T *argvars;
11680 typval_T *rettv;
11681{
11682 tabpage_T *tp;
11683 dictitem_T *v;
11684 char_u *varname;
11685
11686 rettv->v_type = VAR_STRING;
11687 rettv->vval.v_string = NULL;
11688
11689 varname = get_tv_string_chk(&argvars[1]);
11690 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11691 if (tp != NULL && varname != NULL)
11692 {
11693 /* look up the variable */
11694 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11695 if (v != NULL)
11696 copy_tv(&v->di_tv, rettv);
11697 }
11698}
11699
11700/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011701 * "gettabwinvar()" function
11702 */
11703 static void
11704f_gettabwinvar(argvars, rettv)
11705 typval_T *argvars;
11706 typval_T *rettv;
11707{
11708 getwinvar(argvars, rettv, 1);
11709}
11710
11711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 * "getwinposx()" function
11713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011716 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720#ifdef FEAT_GUI
11721 if (gui.in_use)
11722 {
11723 int x, y;
11724
11725 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 }
11728#endif
11729}
11730
11731/*
11732 * "getwinposy()" function
11733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740#ifdef FEAT_GUI
11741 if (gui.in_use)
11742 {
11743 int x, y;
11744
11745 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011746 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 }
11748#endif
11749}
11750
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011751/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011752 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011753 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011754 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011755find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011756 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011757 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011758{
11759#ifdef FEAT_WINDOWS
11760 win_T *wp;
11761#endif
11762 int nr;
11763
11764 nr = get_tv_number_chk(vp, NULL);
11765
11766#ifdef FEAT_WINDOWS
11767 if (nr < 0)
11768 return NULL;
11769 if (nr == 0)
11770 return curwin;
11771
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011772 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11773 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011774 if (--nr <= 0)
11775 break;
11776 return wp;
11777#else
11778 if (nr == 0 || nr == 1)
11779 return curwin;
11780 return NULL;
11781#endif
11782}
11783
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784/*
11785 * "getwinvar()" function
11786 */
11787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011792 getwinvar(argvars, rettv, 0);
11793}
11794
11795/*
11796 * getwinvar() and gettabwinvar()
11797 */
11798 static void
11799getwinvar(argvars, rettv, off)
11800 typval_T *argvars;
11801 typval_T *rettv;
11802 int off; /* 1 for gettabwinvar() */
11803{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 win_T *win, *oldcurwin;
11805 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011807 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011809#ifdef FEAT_WINDOWS
11810 if (off == 1)
11811 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11812 else
11813 tp = curtab;
11814#endif
11815 win = find_win_by_nr(&argvars[off], tp);
11816 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821
11822 if (win != NULL && varname != NULL)
11823 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011824 /* Set curwin to be our win, temporarily. Also set curbuf, so
11825 * that we can get buffer-local options. */
11826 oldcurwin = curwin;
11827 curwin = win;
11828 curbuf = win->w_buffer;
11829
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 else
11833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 if (*varname == NUL)
11835 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11836 * scope prefix before the NUL byte is required by
11837 * find_var_in_ht(). */
11838 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011840 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011842 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011844
11845 /* restore previous notion of curwin */
11846 curwin = oldcurwin;
11847 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 }
11849
11850 --emsg_off;
11851}
11852
11853/*
11854 * "glob()" function
11855 */
11856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T *argvars;
11859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011861 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011865 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011866 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011868 if (argvars[1].v_type != VAR_UNKNOWN)
11869 {
11870 if (get_tv_number_chk(&argvars[1], &error))
11871 options |= WILD_KEEP_ALL;
11872 if (argvars[2].v_type != VAR_UNKNOWN
11873 && get_tv_number_chk(&argvars[2], &error))
11874 {
11875 rettv->v_type = VAR_LIST;
11876 rettv->vval.v_list = NULL;
11877 }
11878 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011879 if (!error)
11880 {
11881 ExpandInit(&xpc);
11882 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011883 if (p_wic)
11884 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011885 if (rettv->v_type == VAR_STRING)
11886 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011887 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011888 else if (rettv_list_alloc(rettv) != FAIL)
11889 {
11890 int i;
11891
11892 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11893 NULL, options, WILD_ALL_KEEP);
11894 for (i = 0; i < xpc.xp_numfiles; i++)
11895 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11896
11897 ExpandCleanup(&xpc);
11898 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011899 }
11900 else
11901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902}
11903
11904/*
11905 * "globpath()" function
11906 */
11907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011908f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011909 typval_T *argvars;
11910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011912 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011915 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011917 /* When the optional second argument is non-zero, don't remove matches
11918 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11919 if (argvars[2].v_type != VAR_UNKNOWN
11920 && get_tv_number_chk(&argvars[2], &error))
11921 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011922 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011923 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 rettv->vval.v_string = NULL;
11925 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011926 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11927 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
11931 * "has()" function
11932 */
11933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *argvars;
11936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937{
11938 int i;
11939 char_u *name;
11940 int n = FALSE;
11941 static char *(has_list[]) =
11942 {
11943#ifdef AMIGA
11944 "amiga",
11945# ifdef FEAT_ARP
11946 "arp",
11947# endif
11948#endif
11949#ifdef __BEOS__
11950 "beos",
11951#endif
11952#ifdef MSDOS
11953# ifdef DJGPP
11954 "dos32",
11955# else
11956 "dos16",
11957# endif
11958#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011959#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 "mac",
11961#endif
11962#if defined(MACOS_X_UNIX)
11963 "macunix",
11964#endif
11965#ifdef OS2
11966 "os2",
11967#endif
11968#ifdef __QNX__
11969 "qnx",
11970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971#ifdef UNIX
11972 "unix",
11973#endif
11974#ifdef VMS
11975 "vms",
11976#endif
11977#ifdef WIN16
11978 "win16",
11979#endif
11980#ifdef WIN32
11981 "win32",
11982#endif
11983#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11984 "win32unix",
11985#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011986#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 "win64",
11988#endif
11989#ifdef EBCDIC
11990 "ebcdic",
11991#endif
11992#ifndef CASE_INSENSITIVE_FILENAME
11993 "fname_case",
11994#endif
11995#ifdef FEAT_ARABIC
11996 "arabic",
11997#endif
11998#ifdef FEAT_AUTOCMD
11999 "autocmd",
12000#endif
12001#ifdef FEAT_BEVAL
12002 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012003# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12004 "balloon_multiline",
12005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006#endif
12007#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12008 "builtin_terms",
12009# ifdef ALL_BUILTIN_TCAPS
12010 "all_builtin_terms",
12011# endif
12012#endif
12013#ifdef FEAT_BYTEOFF
12014 "byte_offset",
12015#endif
12016#ifdef FEAT_CINDENT
12017 "cindent",
12018#endif
12019#ifdef FEAT_CLIENTSERVER
12020 "clientserver",
12021#endif
12022#ifdef FEAT_CLIPBOARD
12023 "clipboard",
12024#endif
12025#ifdef FEAT_CMDL_COMPL
12026 "cmdline_compl",
12027#endif
12028#ifdef FEAT_CMDHIST
12029 "cmdline_hist",
12030#endif
12031#ifdef FEAT_COMMENTS
12032 "comments",
12033#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012034#ifdef FEAT_CONCEAL
12035 "conceal",
12036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037#ifdef FEAT_CRYPT
12038 "cryptv",
12039#endif
12040#ifdef FEAT_CSCOPE
12041 "cscope",
12042#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012043#ifdef FEAT_CURSORBIND
12044 "cursorbind",
12045#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012046#ifdef CURSOR_SHAPE
12047 "cursorshape",
12048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049#ifdef DEBUG
12050 "debug",
12051#endif
12052#ifdef FEAT_CON_DIALOG
12053 "dialog_con",
12054#endif
12055#ifdef FEAT_GUI_DIALOG
12056 "dialog_gui",
12057#endif
12058#ifdef FEAT_DIFF
12059 "diff",
12060#endif
12061#ifdef FEAT_DIGRAPHS
12062 "digraphs",
12063#endif
12064#ifdef FEAT_DND
12065 "dnd",
12066#endif
12067#ifdef FEAT_EMACS_TAGS
12068 "emacs_tags",
12069#endif
12070 "eval", /* always present, of course! */
12071#ifdef FEAT_EX_EXTRA
12072 "ex_extra",
12073#endif
12074#ifdef FEAT_SEARCH_EXTRA
12075 "extra_search",
12076#endif
12077#ifdef FEAT_FKMAP
12078 "farsi",
12079#endif
12080#ifdef FEAT_SEARCHPATH
12081 "file_in_path",
12082#endif
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020012083#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012084 "filterpipe",
12085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086#ifdef FEAT_FIND_ID
12087 "find_in_path",
12088#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012089#ifdef FEAT_FLOAT
12090 "float",
12091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092#ifdef FEAT_FOLDING
12093 "folding",
12094#endif
12095#ifdef FEAT_FOOTER
12096 "footer",
12097#endif
12098#if !defined(USE_SYSTEM) && defined(UNIX)
12099 "fork",
12100#endif
12101#ifdef FEAT_GETTEXT
12102 "gettext",
12103#endif
12104#ifdef FEAT_GUI
12105 "gui",
12106#endif
12107#ifdef FEAT_GUI_ATHENA
12108# ifdef FEAT_GUI_NEXTAW
12109 "gui_neXtaw",
12110# else
12111 "gui_athena",
12112# endif
12113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114#ifdef FEAT_GUI_GTK
12115 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012118#ifdef FEAT_GUI_GNOME
12119 "gui_gnome",
12120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121#ifdef FEAT_GUI_MAC
12122 "gui_mac",
12123#endif
12124#ifdef FEAT_GUI_MOTIF
12125 "gui_motif",
12126#endif
12127#ifdef FEAT_GUI_PHOTON
12128 "gui_photon",
12129#endif
12130#ifdef FEAT_GUI_W16
12131 "gui_win16",
12132#endif
12133#ifdef FEAT_GUI_W32
12134 "gui_win32",
12135#endif
12136#ifdef FEAT_HANGULIN
12137 "hangul_input",
12138#endif
12139#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12140 "iconv",
12141#endif
12142#ifdef FEAT_INS_EXPAND
12143 "insert_expand",
12144#endif
12145#ifdef FEAT_JUMPLIST
12146 "jumplist",
12147#endif
12148#ifdef FEAT_KEYMAP
12149 "keymap",
12150#endif
12151#ifdef FEAT_LANGMAP
12152 "langmap",
12153#endif
12154#ifdef FEAT_LIBCALL
12155 "libcall",
12156#endif
12157#ifdef FEAT_LINEBREAK
12158 "linebreak",
12159#endif
12160#ifdef FEAT_LISP
12161 "lispindent",
12162#endif
12163#ifdef FEAT_LISTCMDS
12164 "listcmds",
12165#endif
12166#ifdef FEAT_LOCALMAP
12167 "localmap",
12168#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012169#ifdef FEAT_LUA
12170# ifndef DYNAMIC_LUA
12171 "lua",
12172# endif
12173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174#ifdef FEAT_MENU
12175 "menu",
12176#endif
12177#ifdef FEAT_SESSION
12178 "mksession",
12179#endif
12180#ifdef FEAT_MODIFY_FNAME
12181 "modify_fname",
12182#endif
12183#ifdef FEAT_MOUSE
12184 "mouse",
12185#endif
12186#ifdef FEAT_MOUSESHAPE
12187 "mouseshape",
12188#endif
12189#if defined(UNIX) || defined(VMS)
12190# ifdef FEAT_MOUSE_DEC
12191 "mouse_dec",
12192# endif
12193# ifdef FEAT_MOUSE_GPM
12194 "mouse_gpm",
12195# endif
12196# ifdef FEAT_MOUSE_JSB
12197 "mouse_jsbterm",
12198# endif
12199# ifdef FEAT_MOUSE_NET
12200 "mouse_netterm",
12201# endif
12202# ifdef FEAT_MOUSE_PTERM
12203 "mouse_pterm",
12204# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012205# ifdef FEAT_SYSMOUSE
12206 "mouse_sysmouse",
12207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208# ifdef FEAT_MOUSE_XTERM
12209 "mouse_xterm",
12210# endif
12211#endif
12212#ifdef FEAT_MBYTE
12213 "multi_byte",
12214#endif
12215#ifdef FEAT_MBYTE_IME
12216 "multi_byte_ime",
12217#endif
12218#ifdef FEAT_MULTI_LANG
12219 "multi_lang",
12220#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012221#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012222#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012223 "mzscheme",
12224#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226#ifdef FEAT_OLE
12227 "ole",
12228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229#ifdef FEAT_PATH_EXTRA
12230 "path_extra",
12231#endif
12232#ifdef FEAT_PERL
12233#ifndef DYNAMIC_PERL
12234 "perl",
12235#endif
12236#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012237#ifdef FEAT_PERSISTENT_UNDO
12238 "persistent_undo",
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef FEAT_PYTHON
12241#ifndef DYNAMIC_PYTHON
12242 "python",
12243#endif
12244#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012245#ifdef FEAT_PYTHON3
12246#ifndef DYNAMIC_PYTHON3
12247 "python3",
12248#endif
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250#ifdef FEAT_POSTSCRIPT
12251 "postscript",
12252#endif
12253#ifdef FEAT_PRINTER
12254 "printer",
12255#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012256#ifdef FEAT_PROFILE
12257 "profile",
12258#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012259#ifdef FEAT_RELTIME
12260 "reltime",
12261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262#ifdef FEAT_QUICKFIX
12263 "quickfix",
12264#endif
12265#ifdef FEAT_RIGHTLEFT
12266 "rightleft",
12267#endif
12268#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12269 "ruby",
12270#endif
12271#ifdef FEAT_SCROLLBIND
12272 "scrollbind",
12273#endif
12274#ifdef FEAT_CMDL_INFO
12275 "showcmd",
12276 "cmdline_info",
12277#endif
12278#ifdef FEAT_SIGNS
12279 "signs",
12280#endif
12281#ifdef FEAT_SMARTINDENT
12282 "smartindent",
12283#endif
12284#ifdef FEAT_SNIFF
12285 "sniff",
12286#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012287#ifdef STARTUPTIME
12288 "startuptime",
12289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290#ifdef FEAT_STL_OPT
12291 "statusline",
12292#endif
12293#ifdef FEAT_SUN_WORKSHOP
12294 "sun_workshop",
12295#endif
12296#ifdef FEAT_NETBEANS_INTG
12297 "netbeans_intg",
12298#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012299#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012300 "spell",
12301#endif
12302#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 "syntax",
12304#endif
12305#if defined(USE_SYSTEM) || !defined(UNIX)
12306 "system",
12307#endif
12308#ifdef FEAT_TAG_BINS
12309 "tag_binary",
12310#endif
12311#ifdef FEAT_TAG_OLDSTATIC
12312 "tag_old_static",
12313#endif
12314#ifdef FEAT_TAG_ANYWHITE
12315 "tag_any_white",
12316#endif
12317#ifdef FEAT_TCL
12318# ifndef DYNAMIC_TCL
12319 "tcl",
12320# endif
12321#endif
12322#ifdef TERMINFO
12323 "terminfo",
12324#endif
12325#ifdef FEAT_TERMRESPONSE
12326 "termresponse",
12327#endif
12328#ifdef FEAT_TEXTOBJ
12329 "textobjects",
12330#endif
12331#ifdef HAVE_TGETENT
12332 "tgetent",
12333#endif
12334#ifdef FEAT_TITLE
12335 "title",
12336#endif
12337#ifdef FEAT_TOOLBAR
12338 "toolbar",
12339#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012340#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12341 "unnamedplus",
12342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343#ifdef FEAT_USR_CMDS
12344 "user-commands", /* was accidentally included in 5.4 */
12345 "user_commands",
12346#endif
12347#ifdef FEAT_VIMINFO
12348 "viminfo",
12349#endif
12350#ifdef FEAT_VERTSPLIT
12351 "vertsplit",
12352#endif
12353#ifdef FEAT_VIRTUALEDIT
12354 "virtualedit",
12355#endif
12356#ifdef FEAT_VISUAL
12357 "visual",
12358#endif
12359#ifdef FEAT_VISUALEXTRA
12360 "visualextra",
12361#endif
12362#ifdef FEAT_VREPLACE
12363 "vreplace",
12364#endif
12365#ifdef FEAT_WILDIGN
12366 "wildignore",
12367#endif
12368#ifdef FEAT_WILDMENU
12369 "wildmenu",
12370#endif
12371#ifdef FEAT_WINDOWS
12372 "windows",
12373#endif
12374#ifdef FEAT_WAK
12375 "winaltkeys",
12376#endif
12377#ifdef FEAT_WRITEBACKUP
12378 "writebackup",
12379#endif
12380#ifdef FEAT_XIM
12381 "xim",
12382#endif
12383#ifdef FEAT_XFONTSET
12384 "xfontset",
12385#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012386#ifdef FEAT_XPM_W32
12387 "xpm_w32",
12388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389#ifdef USE_XSMP
12390 "xsmp",
12391#endif
12392#ifdef USE_XSMP_INTERACT
12393 "xsmp_interact",
12394#endif
12395#ifdef FEAT_XCLIPBOARD
12396 "xterm_clipboard",
12397#endif
12398#ifdef FEAT_XTERM_SAVE
12399 "xterm_save",
12400#endif
12401#if defined(UNIX) && defined(FEAT_X11)
12402 "X11",
12403#endif
12404 NULL
12405 };
12406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 for (i = 0; has_list[i] != NULL; ++i)
12409 if (STRICMP(name, has_list[i]) == 0)
12410 {
12411 n = TRUE;
12412 break;
12413 }
12414
12415 if (n == FALSE)
12416 {
12417 if (STRNICMP(name, "patch", 5) == 0)
12418 n = has_patch(atoi((char *)name + 5));
12419 else if (STRICMP(name, "vim_starting") == 0)
12420 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012421#ifdef FEAT_MBYTE
12422 else if (STRICMP(name, "multi_byte_encoding") == 0)
12423 n = has_mbyte;
12424#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012425#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12426 else if (STRICMP(name, "balloon_multiline") == 0)
12427 n = multiline_balloon_available();
12428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef DYNAMIC_TCL
12430 else if (STRICMP(name, "tcl") == 0)
12431 n = tcl_enabled(FALSE);
12432#endif
12433#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12434 else if (STRICMP(name, "iconv") == 0)
12435 n = iconv_enabled(FALSE);
12436#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012437#ifdef DYNAMIC_LUA
12438 else if (STRICMP(name, "lua") == 0)
12439 n = lua_enabled(FALSE);
12440#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012441#ifdef DYNAMIC_MZSCHEME
12442 else if (STRICMP(name, "mzscheme") == 0)
12443 n = mzscheme_enabled(FALSE);
12444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445#ifdef DYNAMIC_RUBY
12446 else if (STRICMP(name, "ruby") == 0)
12447 n = ruby_enabled(FALSE);
12448#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012449#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450#ifdef DYNAMIC_PYTHON
12451 else if (STRICMP(name, "python") == 0)
12452 n = python_enabled(FALSE);
12453#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012454#endif
12455#ifdef FEAT_PYTHON3
12456#ifdef DYNAMIC_PYTHON3
12457 else if (STRICMP(name, "python3") == 0)
12458 n = python3_enabled(FALSE);
12459#endif
12460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461#ifdef DYNAMIC_PERL
12462 else if (STRICMP(name, "perl") == 0)
12463 n = perl_enabled(FALSE);
12464#endif
12465#ifdef FEAT_GUI
12466 else if (STRICMP(name, "gui_running") == 0)
12467 n = (gui.in_use || gui.starting);
12468# ifdef FEAT_GUI_W32
12469 else if (STRICMP(name, "gui_win32s") == 0)
12470 n = gui_is_win32s();
12471# endif
12472# ifdef FEAT_BROWSE
12473 else if (STRICMP(name, "browse") == 0)
12474 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12475# endif
12476#endif
12477#ifdef FEAT_SYN_HL
12478 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012479 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480#endif
12481#if defined(WIN3264)
12482 else if (STRICMP(name, "win95") == 0)
12483 n = mch_windows95();
12484#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012485#ifdef FEAT_NETBEANS_INTG
12486 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012487 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 }
12490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492}
12493
12494/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012495 * "has_key()" function
12496 */
12497 static void
12498f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 typval_T *argvars;
12500 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012501{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012502 if (argvars[0].v_type != VAR_DICT)
12503 {
12504 EMSG(_(e_dictreq));
12505 return;
12506 }
12507 if (argvars[0].vval.v_dict == NULL)
12508 return;
12509
12510 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012511 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012512}
12513
12514/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012515 * "haslocaldir()" function
12516 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012517 static void
12518f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012519 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012520 typval_T *rettv;
12521{
12522 rettv->vval.v_number = (curwin->w_localdir != NULL);
12523}
12524
12525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 * "hasmapto()" function
12527 */
12528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 typval_T *argvars;
12531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
12533 char_u *name;
12534 char_u *mode;
12535 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012536 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012538 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012539 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 mode = (char_u *)"nvo";
12541 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012544 if (argvars[2].v_type != VAR_UNKNOWN)
12545 abbr = get_tv_number(&argvars[2]);
12546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547
Bram Moolenaar2c932302006-03-18 21:42:09 +000012548 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552}
12553
12554/*
12555 * "histadd()" function
12556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561{
12562#ifdef FEAT_CMDHIST
12563 int histype;
12564 char_u *str;
12565 char_u buf[NUMBUFLEN];
12566#endif
12567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 if (check_restricted() || check_secure())
12570 return;
12571#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012572 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12573 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 if (histype >= 0)
12575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 if (*str != NUL)
12578 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012579 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012581 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 return;
12583 }
12584 }
12585#endif
12586}
12587
12588/*
12589 * "histdel()" function
12590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012593 typval_T *argvars UNUSED;
12594 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595{
12596#ifdef FEAT_CMDHIST
12597 int n;
12598 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012599 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12602 if (str == NULL)
12603 n = 0;
12604 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012607 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 else
12612 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012613 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 get_tv_string_buf(&argvars[1], buf));
12615 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616#endif
12617}
12618
12619/*
12620 * "histget()" function
12621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626{
12627#ifdef FEAT_CMDHIST
12628 int type;
12629 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012630 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12633 if (str == NULL)
12634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 {
12637 type = get_histtype(str);
12638 if (argvars[1].v_type == VAR_UNKNOWN)
12639 idx = get_history_idx(type);
12640 else
12641 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12642 /* -1 on type error */
12643 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649}
12650
12651/*
12652 * "histnr()" function
12653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012655f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012656 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
12659 int i;
12660
12661#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012662 char_u *history = get_tv_string_chk(&argvars[0]);
12663
12664 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 if (i >= HIST_CMD && i < HIST_COUNT)
12666 i = get_history_idx(i);
12667 else
12668#endif
12669 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671}
12672
12673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 * "highlightID(name)" function
12675 */
12676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012678 typval_T *argvars;
12679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682}
12683
12684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012685 * "highlight_exists()" function
12686 */
12687 static void
12688f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012689 typval_T *argvars;
12690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012691{
12692 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12693}
12694
12695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 * "hostname()" function
12697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702{
12703 char_u hostname[256];
12704
12705 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 rettv->v_type = VAR_STRING;
12707 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708}
12709
12710/*
12711 * iconv() function
12712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012715 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
12718#ifdef FEAT_MBYTE
12719 char_u buf1[NUMBUFLEN];
12720 char_u buf2[NUMBUFLEN];
12721 char_u *from, *to, *str;
12722 vimconv_T vimconv;
12723#endif
12724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725 rettv->v_type = VAR_STRING;
12726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727
12728#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 str = get_tv_string(&argvars[0]);
12730 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12731 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 vimconv.vc_type = CONV_NONE;
12733 convert_setup(&vimconv, from, to);
12734
12735 /* If the encodings are equal, no conversion needed. */
12736 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740
12741 convert_setup(&vimconv, NULL, NULL);
12742 vim_free(from);
12743 vim_free(to);
12744#endif
12745}
12746
12747/*
12748 * "indent()" function
12749 */
12750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012752 typval_T *argvars;
12753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754{
12755 linenr_T lnum;
12756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762}
12763
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012764/*
12765 * "index()" function
12766 */
12767 static void
12768f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012769 typval_T *argvars;
12770 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012771{
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 list_T *l;
12773 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012774 long idx = 0;
12775 int ic = FALSE;
12776
12777 rettv->vval.v_number = -1;
12778 if (argvars[0].v_type != VAR_LIST)
12779 {
12780 EMSG(_(e_listreq));
12781 return;
12782 }
12783 l = argvars[0].vval.v_list;
12784 if (l != NULL)
12785 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012786 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012789 int error = FALSE;
12790
Bram Moolenaar758711c2005-02-02 23:11:38 +000012791 /* Start at specified item. Use the cached index that list_find()
12792 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012794 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012796 ic = get_tv_number_chk(&argvars[3], &error);
12797 if (error)
12798 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012799 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012800
Bram Moolenaar758711c2005-02-02 23:11:38 +000012801 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012802 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012803 {
12804 rettv->vval.v_number = idx;
12805 break;
12806 }
12807 }
12808}
12809
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810static int inputsecret_flag = 0;
12811
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012812static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12813
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012815 * This function is used by f_input() and f_inputdialog() functions. The third
12816 * argument to f_input() specifies the type of completion to use at the
12817 * prompt. The third argument to f_inputdialog() specifies the value to return
12818 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 */
12820 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012821get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012822 typval_T *argvars;
12823 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012824 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 char_u *p = NULL;
12828 int c;
12829 char_u buf[NUMBUFLEN];
12830 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012831 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012832 int xp_type = EXPAND_NOTHING;
12833 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837
12838#ifdef NO_CONSOLE_INPUT
12839 /* While starting up, there is no place to enter text. */
12840 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842#endif
12843
12844 cmd_silent = FALSE; /* Want to see the prompt. */
12845 if (prompt != NULL)
12846 {
12847 /* Only the part of the message after the last NL is considered as
12848 * prompt for the command line */
12849 p = vim_strrchr(prompt, '\n');
12850 if (p == NULL)
12851 p = prompt;
12852 else
12853 {
12854 ++p;
12855 c = *p;
12856 *p = NUL;
12857 msg_start();
12858 msg_clr_eos();
12859 msg_puts_attr(prompt, echo_attr);
12860 msg_didout = FALSE;
12861 msg_starthere();
12862 *p = c;
12863 }
12864 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 if (argvars[1].v_type != VAR_UNKNOWN)
12867 {
12868 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12869 if (defstr != NULL)
12870 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012872 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012873 {
12874 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012875 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012876 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012877
Bram Moolenaar4463f292005-09-25 22:20:24 +000012878 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012879
Bram Moolenaar4463f292005-09-25 22:20:24 +000012880 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12881 if (xp_name == NULL)
12882 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012884 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012885
Bram Moolenaar4463f292005-09-25 22:20:24 +000012886 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12887 &xp_arg) == FAIL)
12888 return;
12889 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012890 }
12891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012892 if (defstr != NULL)
12893 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012894 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12895 xp_type, xp_arg);
12896
12897 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012899 /* since the user typed this, no need to wait for return */
12900 need_wait_return = FALSE;
12901 msg_didout = FALSE;
12902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 cmd_silent = cmd_silent_save;
12904}
12905
12906/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012907 * "input()" function
12908 * Also handles inputsecret() when inputsecret is set.
12909 */
12910 static void
12911f_input(argvars, rettv)
12912 typval_T *argvars;
12913 typval_T *rettv;
12914{
12915 get_user_input(argvars, rettv, FALSE);
12916}
12917
12918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 * "inputdialog()" function
12920 */
12921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012923 typval_T *argvars;
12924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925{
12926#if defined(FEAT_GUI_TEXTDIALOG)
12927 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12928 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12929 {
12930 char_u *message;
12931 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012932 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 message = get_tv_string_chk(&argvars[0]);
12935 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012936 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012937 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 else
12939 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012940 if (message != NULL && defstr != NULL
12941 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012942 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 else
12945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012946 if (message != NULL && defstr != NULL
12947 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012948 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949 rettv->vval.v_string = vim_strsave(
12950 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 }
12956 else
12957#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012958 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959}
12960
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012961/*
12962 * "inputlist()" function
12963 */
12964 static void
12965f_inputlist(argvars, rettv)
12966 typval_T *argvars;
12967 typval_T *rettv;
12968{
12969 listitem_T *li;
12970 int selected;
12971 int mouse_used;
12972
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012973#ifdef NO_CONSOLE_INPUT
12974 /* While starting up, there is no place to enter text. */
12975 if (no_console_input())
12976 return;
12977#endif
12978 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12979 {
12980 EMSG2(_(e_listarg), "inputlist()");
12981 return;
12982 }
12983
12984 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012985 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012986 lines_left = Rows; /* avoid more prompt */
12987 msg_scroll = TRUE;
12988 msg_clr_eos();
12989
12990 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12991 {
12992 msg_puts(get_tv_string(&li->li_tv));
12993 msg_putchar('\n');
12994 }
12995
12996 /* Ask for choice. */
12997 selected = prompt_for_number(&mouse_used);
12998 if (mouse_used)
12999 selected -= lines_left;
13000
13001 rettv->vval.v_number = selected;
13002}
13003
13004
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13006
13007/*
13008 * "inputrestore()" function
13009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013011f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013012 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014{
13015 if (ga_userinput.ga_len > 0)
13016 {
13017 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13019 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013020 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 }
13022 else if (p_verbose > 1)
13023 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013024 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 }
13027}
13028
13029/*
13030 * "inputsave()" function
13031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013037 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 if (ga_grow(&ga_userinput, 1) == OK)
13039 {
13040 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13041 + ga_userinput.ga_len);
13042 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013043 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 }
13045 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047}
13048
13049/*
13050 * "inputsecret()" function
13051 */
13052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 typval_T *argvars;
13055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056{
13057 ++cmdline_star;
13058 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 --cmdline_star;
13061 --inputsecret_flag;
13062}
13063
13064/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013065 * "insert()" function
13066 */
13067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013069 typval_T *argvars;
13070 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013071{
13072 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013073 listitem_T *item;
13074 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013075 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013076
13077 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013078 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013079 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013080 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013081 {
13082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013083 before = get_tv_number_chk(&argvars[2], &error);
13084 if (error)
13085 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013086
Bram Moolenaar758711c2005-02-02 23:11:38 +000013087 if (before == l->lv_len)
13088 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013089 else
13090 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013091 item = list_find(l, before);
13092 if (item == NULL)
13093 {
13094 EMSGN(_(e_listidx), before);
13095 l = NULL;
13096 }
13097 }
13098 if (l != NULL)
13099 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013100 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013101 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013102 }
13103 }
13104}
13105
13106/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013107 * "invert(expr)" function
13108 */
13109 static void
13110f_invert(argvars, rettv)
13111 typval_T *argvars;
13112 typval_T *rettv;
13113{
13114 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13115}
13116
13117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 * "isdirectory()" function
13119 */
13120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013122 typval_T *argvars;
13123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126}
13127
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013128/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013129 * "islocked()" function
13130 */
13131 static void
13132f_islocked(argvars, rettv)
13133 typval_T *argvars;
13134 typval_T *rettv;
13135{
13136 lval_T lv;
13137 char_u *end;
13138 dictitem_T *di;
13139
13140 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013141 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13142 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013143 if (end != NULL && lv.ll_name != NULL)
13144 {
13145 if (*end != NUL)
13146 EMSG(_(e_trailing));
13147 else
13148 {
13149 if (lv.ll_tv == NULL)
13150 {
13151 if (check_changedtick(lv.ll_name))
13152 rettv->vval.v_number = 1; /* always locked */
13153 else
13154 {
13155 di = find_var(lv.ll_name, NULL);
13156 if (di != NULL)
13157 {
13158 /* Consider a variable locked when:
13159 * 1. the variable itself is locked
13160 * 2. the value of the variable is locked.
13161 * 3. the List or Dict value is locked.
13162 */
13163 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13164 || tv_islocked(&di->di_tv));
13165 }
13166 }
13167 }
13168 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013169 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013170 else if (lv.ll_newkey != NULL)
13171 EMSG2(_(e_dictkey), lv.ll_newkey);
13172 else if (lv.ll_list != NULL)
13173 /* List item. */
13174 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13175 else
13176 /* Dictionary item. */
13177 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13178 }
13179 }
13180
13181 clear_lval(&lv);
13182}
13183
Bram Moolenaar33570922005-01-25 22:26:29 +000013184static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013185
13186/*
13187 * Turn a dict into a list:
13188 * "what" == 0: list of keys
13189 * "what" == 1: list of values
13190 * "what" == 2: list of items
13191 */
13192 static void
13193dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013194 typval_T *argvars;
13195 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013196 int what;
13197{
Bram Moolenaar33570922005-01-25 22:26:29 +000013198 list_T *l2;
13199 dictitem_T *di;
13200 hashitem_T *hi;
13201 listitem_T *li;
13202 listitem_T *li2;
13203 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013204 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013205
Bram Moolenaar8c711452005-01-14 21:53:12 +000013206 if (argvars[0].v_type != VAR_DICT)
13207 {
13208 EMSG(_(e_dictreq));
13209 return;
13210 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013211 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013212 return;
13213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013214 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013215 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013216
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013217 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013218 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013220 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013222 --todo;
13223 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013224
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013225 li = listitem_alloc();
13226 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013227 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013228 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013229
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013230 if (what == 0)
13231 {
13232 /* keys() */
13233 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013234 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013235 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13236 }
13237 else if (what == 1)
13238 {
13239 /* values() */
13240 copy_tv(&di->di_tv, &li->li_tv);
13241 }
13242 else
13243 {
13244 /* items() */
13245 l2 = list_alloc();
13246 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013247 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013248 li->li_tv.vval.v_list = l2;
13249 if (l2 == NULL)
13250 break;
13251 ++l2->lv_refcount;
13252
13253 li2 = listitem_alloc();
13254 if (li2 == NULL)
13255 break;
13256 list_append(l2, li2);
13257 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013258 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013259 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13260
13261 li2 = listitem_alloc();
13262 if (li2 == NULL)
13263 break;
13264 list_append(l2, li2);
13265 copy_tv(&di->di_tv, &li2->li_tv);
13266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013267 }
13268 }
13269}
13270
13271/*
13272 * "items(dict)" function
13273 */
13274 static void
13275f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 typval_T *argvars;
13277 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013278{
13279 dict_list(argvars, rettv, 2);
13280}
13281
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013283 * "join()" function
13284 */
13285 static void
13286f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013287 typval_T *argvars;
13288 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013289{
13290 garray_T ga;
13291 char_u *sep;
13292
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013293 if (argvars[0].v_type != VAR_LIST)
13294 {
13295 EMSG(_(e_listreq));
13296 return;
13297 }
13298 if (argvars[0].vval.v_list == NULL)
13299 return;
13300 if (argvars[1].v_type == VAR_UNKNOWN)
13301 sep = (char_u *)" ";
13302 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013304
13305 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306
13307 if (sep != NULL)
13308 {
13309 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013310 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 ga_append(&ga, NUL);
13312 rettv->vval.v_string = (char_u *)ga.ga_data;
13313 }
13314 else
13315 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013316}
13317
13318/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319 * "keys()" function
13320 */
13321 static void
13322f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013323 typval_T *argvars;
13324 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013325{
13326 dict_list(argvars, rettv, 0);
13327}
13328
13329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 * "last_buffer_nr()" function.
13331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336{
13337 int n = 0;
13338 buf_T *buf;
13339
13340 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13341 if (n < buf->b_fnum)
13342 n = buf->b_fnum;
13343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013345}
13346
13347/*
13348 * "len()" function
13349 */
13350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013351f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 typval_T *argvars;
13353 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013354{
13355 switch (argvars[0].v_type)
13356 {
13357 case VAR_STRING:
13358 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_number = (varnumber_T)STRLEN(
13360 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361 break;
13362 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013364 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013365 case VAR_DICT:
13366 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13367 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013368 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013369 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013370 break;
13371 }
13372}
13373
Bram Moolenaar33570922005-01-25 22:26:29 +000013374static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013375
13376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 typval_T *argvars;
13379 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013380 int type;
13381{
13382#ifdef FEAT_LIBCALL
13383 char_u *string_in;
13384 char_u **string_result;
13385 int nr_result;
13386#endif
13387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013389 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013391
13392 if (check_restricted() || check_secure())
13393 return;
13394
13395#ifdef FEAT_LIBCALL
13396 /* The first two args must be strings, otherwise its meaningless */
13397 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013399 string_in = NULL;
13400 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013401 string_in = argvars[2].vval.v_string;
13402 if (type == VAR_NUMBER)
13403 string_result = NULL;
13404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013406 if (mch_libcall(argvars[0].vval.v_string,
13407 argvars[1].vval.v_string,
13408 string_in,
13409 argvars[2].vval.v_number,
13410 string_result,
13411 &nr_result) == OK
13412 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013413 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013414 }
13415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416}
13417
13418/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013419 * "libcall()" function
13420 */
13421 static void
13422f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013425{
13426 libcall_common(argvars, rettv, VAR_STRING);
13427}
13428
13429/*
13430 * "libcallnr()" function
13431 */
13432 static void
13433f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 typval_T *argvars;
13435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436{
13437 libcall_common(argvars, rettv, VAR_NUMBER);
13438}
13439
13440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 * "line(string)" function
13442 */
13443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 typval_T *argvars;
13446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447{
13448 linenr_T lnum = 0;
13449 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013450 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013452 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 if (fp != NULL)
13454 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456}
13457
13458/*
13459 * "line2byte(lnum)" function
13460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013463 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465{
13466#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468#else
13469 linenr_T lnum;
13470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13476 if (rettv->vval.v_number >= 0)
13477 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478#endif
13479}
13480
13481/*
13482 * "lispindent(lnum)" function
13483 */
13484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
13487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488{
13489#ifdef FEAT_LISP
13490 pos_T pos;
13491 linenr_T lnum;
13492
13493 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13496 {
13497 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 curwin->w_cursor = pos;
13500 }
13501 else
13502#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504}
13505
13506/*
13507 * "localtime()" function
13508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013511 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
Bram Moolenaar33570922005-01-25 22:26:29 +000013517static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518
13519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013521 typval_T *argvars;
13522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 int exact;
13524{
13525 char_u *keys;
13526 char_u *which;
13527 char_u buf[NUMBUFLEN];
13528 char_u *keys_buf = NULL;
13529 char_u *rhs;
13530 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013531 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013532 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013533 mapblock_T *mp;
13534 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535
13536 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 rettv->v_type = VAR_STRING;
13538 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 if (*keys == NUL)
13542 return;
13543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013547 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013548 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013549 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013550 if (argvars[3].v_type != VAR_UNKNOWN)
13551 get_dict = get_tv_number(&argvars[3]);
13552 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 else
13555 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 if (which == NULL)
13557 return;
13558
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 mode = get_map_mode(&which, 0);
13560
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013561 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013562 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013564
13565 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013567 /* Return a string. */
13568 if (rhs != NULL)
13569 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570
Bram Moolenaarbd743252010-10-20 21:23:33 +020013571 }
13572 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13573 {
13574 /* Return a dictionary. */
13575 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13576 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13577 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578
Bram Moolenaarbd743252010-10-20 21:23:33 +020013579 dict_add_nr_str(dict, "lhs", 0L, lhs);
13580 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13581 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13582 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13583 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13584 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13585 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13586 dict_add_nr_str(dict, "mode", 0L, mapmode);
13587
13588 vim_free(lhs);
13589 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 }
13591}
13592
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013593#ifdef FEAT_FLOAT
13594/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013595 * "log()" function
13596 */
13597 static void
13598f_log(argvars, rettv)
13599 typval_T *argvars;
13600 typval_T *rettv;
13601{
13602 float_T f;
13603
13604 rettv->v_type = VAR_FLOAT;
13605 if (get_float_arg(argvars, &f) == OK)
13606 rettv->vval.v_float = log(f);
13607 else
13608 rettv->vval.v_float = 0.0;
13609}
13610
13611/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013612 * "log10()" function
13613 */
13614 static void
13615f_log10(argvars, rettv)
13616 typval_T *argvars;
13617 typval_T *rettv;
13618{
13619 float_T f;
13620
13621 rettv->v_type = VAR_FLOAT;
13622 if (get_float_arg(argvars, &f) == OK)
13623 rettv->vval.v_float = log10(f);
13624 else
13625 rettv->vval.v_float = 0.0;
13626}
13627#endif
13628
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013630 * "map()" function
13631 */
13632 static void
13633f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013634 typval_T *argvars;
13635 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013636{
13637 filter_map(argvars, rettv, TRUE);
13638}
13639
13640/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 */
13643 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013645 typval_T *argvars;
13646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013648 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649}
13650
13651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013652 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 */
13654 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *argvars;
13657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013659 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
Bram Moolenaar33570922005-01-25 22:26:29 +000013662static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
13664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 int type;
13669{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013670 char_u *str = NULL;
13671 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 char_u *pat;
13673 regmatch_T regmatch;
13674 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013675 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 char_u *save_cpo;
13677 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013678 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013679 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013680 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 list_T *l = NULL;
13682 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013683 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013684 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685
13686 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13687 save_cpo = p_cpo;
13688 p_cpo = (char_u *)"";
13689
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013690 rettv->vval.v_number = -1;
13691 if (type == 3)
13692 {
13693 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013694 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013695 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013696 }
13697 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699 rettv->v_type = VAR_STRING;
13700 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013703 if (argvars[0].v_type == VAR_LIST)
13704 {
13705 if ((l = argvars[0].vval.v_list) == NULL)
13706 goto theend;
13707 li = l->lv_first;
13708 }
13709 else
13710 expr = str = get_tv_string(&argvars[0]);
13711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13713 if (pat == NULL)
13714 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013715
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013716 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013718 int error = FALSE;
13719
13720 start = get_tv_number_chk(&argvars[2], &error);
13721 if (error)
13722 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013723 if (l != NULL)
13724 {
13725 li = list_find(l, start);
13726 if (li == NULL)
13727 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013728 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013729 }
13730 else
13731 {
13732 if (start < 0)
13733 start = 0;
13734 if (start > (long)STRLEN(str))
13735 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013736 /* When "count" argument is there ignore matches before "start",
13737 * otherwise skip part of the string. Differs when pattern is "^"
13738 * or "\<". */
13739 if (argvars[3].v_type != VAR_UNKNOWN)
13740 startcol = start;
13741 else
13742 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013743 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013744
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013745 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 nth = get_tv_number_chk(&argvars[3], &error);
13747 if (error)
13748 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 }
13750
13751 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13752 if (regmatch.regprog != NULL)
13753 {
13754 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013755
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013756 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013757 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013758 if (l != NULL)
13759 {
13760 if (li == NULL)
13761 {
13762 match = FALSE;
13763 break;
13764 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013765 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013766 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013767 if (str == NULL)
13768 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013769 }
13770
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013771 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013772
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013773 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013774 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013775 if (l == NULL && !match)
13776 break;
13777
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013778 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013779 if (l != NULL)
13780 {
13781 li = li->li_next;
13782 ++idx;
13783 }
13784 else
13785 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013786#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013787 startcol = (colnr_T)(regmatch.startp[0]
13788 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013789#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013790 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013791#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013792 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013793 }
13794
13795 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013797 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013798 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013799 int i;
13800
13801 /* return list with matched string and submatches */
13802 for (i = 0; i < NSUBEXP; ++i)
13803 {
13804 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013805 {
13806 if (list_append_string(rettv->vval.v_list,
13807 (char_u *)"", 0) == FAIL)
13808 break;
13809 }
13810 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013811 regmatch.startp[i],
13812 (int)(regmatch.endp[i] - regmatch.startp[i]))
13813 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013814 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013815 }
13816 }
13817 else if (type == 2)
13818 {
13819 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 if (l != NULL)
13821 copy_tv(&li->li_tv, rettv);
13822 else
13823 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013825 }
13826 else if (l != NULL)
13827 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 else
13829 {
13830 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 (varnumber_T)(regmatch.startp[0] - str);
13833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013836 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 }
13838 }
13839 vim_free(regmatch.regprog);
13840 }
13841
13842theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013843 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 p_cpo = save_cpo;
13845}
13846
13847/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848 * "match()" function
13849 */
13850 static void
13851f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 typval_T *argvars;
13853 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013854{
13855 find_some_match(argvars, rettv, 1);
13856}
13857
13858/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013859 * "matchadd()" function
13860 */
13861 static void
13862f_matchadd(argvars, rettv)
13863 typval_T *argvars;
13864 typval_T *rettv;
13865{
13866#ifdef FEAT_SEARCH_EXTRA
13867 char_u buf[NUMBUFLEN];
13868 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13869 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13870 int prio = 10; /* default priority */
13871 int id = -1;
13872 int error = FALSE;
13873
13874 rettv->vval.v_number = -1;
13875
13876 if (grp == NULL || pat == NULL)
13877 return;
13878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013879 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013880 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013881 if (argvars[3].v_type != VAR_UNKNOWN)
13882 id = get_tv_number_chk(&argvars[3], &error);
13883 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013884 if (error == TRUE)
13885 return;
13886 if (id >= 1 && id <= 3)
13887 {
13888 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13889 return;
13890 }
13891
13892 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13893#endif
13894}
13895
13896/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013897 * "matcharg()" function
13898 */
13899 static void
13900f_matcharg(argvars, rettv)
13901 typval_T *argvars;
13902 typval_T *rettv;
13903{
13904 if (rettv_list_alloc(rettv) == OK)
13905 {
13906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013907 int id = get_tv_number(&argvars[0]);
13908 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013909
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013910 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013911 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013912 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13913 {
13914 list_append_string(rettv->vval.v_list,
13915 syn_id2name(m->hlg_id), -1);
13916 list_append_string(rettv->vval.v_list, m->pattern, -1);
13917 }
13918 else
13919 {
13920 list_append_string(rettv->vval.v_list, NUL, -1);
13921 list_append_string(rettv->vval.v_list, NUL, -1);
13922 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013923 }
13924#endif
13925 }
13926}
13927
13928/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013929 * "matchdelete()" function
13930 */
13931 static void
13932f_matchdelete(argvars, rettv)
13933 typval_T *argvars;
13934 typval_T *rettv;
13935{
13936#ifdef FEAT_SEARCH_EXTRA
13937 rettv->vval.v_number = match_delete(curwin,
13938 (int)get_tv_number(&argvars[0]), TRUE);
13939#endif
13940}
13941
13942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943 * "matchend()" function
13944 */
13945 static void
13946f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013947 typval_T *argvars;
13948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949{
13950 find_some_match(argvars, rettv, 0);
13951}
13952
13953/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013954 * "matchlist()" function
13955 */
13956 static void
13957f_matchlist(argvars, rettv)
13958 typval_T *argvars;
13959 typval_T *rettv;
13960{
13961 find_some_match(argvars, rettv, 3);
13962}
13963
13964/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965 * "matchstr()" function
13966 */
13967 static void
13968f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971{
13972 find_some_match(argvars, rettv, 2);
13973}
13974
Bram Moolenaar33570922005-01-25 22:26:29 +000013975static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013976
13977 static void
13978max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013979 typval_T *argvars;
13980 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013981 int domax;
13982{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013983 long n = 0;
13984 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013986
13987 if (argvars[0].v_type == VAR_LIST)
13988 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013989 list_T *l;
13990 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013991
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013992 l = argvars[0].vval.v_list;
13993 if (l != NULL)
13994 {
13995 li = l->lv_first;
13996 if (li != NULL)
13997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013998 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013999 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014000 {
14001 li = li->li_next;
14002 if (li == NULL)
14003 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014005 if (domax ? i > n : i < n)
14006 n = i;
14007 }
14008 }
14009 }
14010 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014011 else if (argvars[0].v_type == VAR_DICT)
14012 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014013 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014014 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014015 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014016 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014017
14018 d = argvars[0].vval.v_dict;
14019 if (d != NULL)
14020 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014021 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014023 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014024 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014025 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014026 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014028 if (first)
14029 {
14030 n = i;
14031 first = FALSE;
14032 }
14033 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014034 n = i;
14035 }
14036 }
14037 }
14038 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014039 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014040 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014042}
14043
14044/*
14045 * "max()" function
14046 */
14047 static void
14048f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014049 typval_T *argvars;
14050 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014051{
14052 max_min(argvars, rettv, TRUE);
14053}
14054
14055/*
14056 * "min()" function
14057 */
14058 static void
14059f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 typval_T *argvars;
14061 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014062{
14063 max_min(argvars, rettv, FALSE);
14064}
14065
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014066static int mkdir_recurse __ARGS((char_u *dir, int prot));
14067
14068/*
14069 * Create the directory in which "dir" is located, and higher levels when
14070 * needed.
14071 */
14072 static int
14073mkdir_recurse(dir, prot)
14074 char_u *dir;
14075 int prot;
14076{
14077 char_u *p;
14078 char_u *updir;
14079 int r = FAIL;
14080
14081 /* Get end of directory name in "dir".
14082 * We're done when it's "/" or "c:/". */
14083 p = gettail_sep(dir);
14084 if (p <= get_past_head(dir))
14085 return OK;
14086
14087 /* If the directory exists we're done. Otherwise: create it.*/
14088 updir = vim_strnsave(dir, (int)(p - dir));
14089 if (updir == NULL)
14090 return FAIL;
14091 if (mch_isdir(updir))
14092 r = OK;
14093 else if (mkdir_recurse(updir, prot) == OK)
14094 r = vim_mkdir_emsg(updir, prot);
14095 vim_free(updir);
14096 return r;
14097}
14098
14099#ifdef vim_mkdir
14100/*
14101 * "mkdir()" function
14102 */
14103 static void
14104f_mkdir(argvars, rettv)
14105 typval_T *argvars;
14106 typval_T *rettv;
14107{
14108 char_u *dir;
14109 char_u buf[NUMBUFLEN];
14110 int prot = 0755;
14111
14112 rettv->vval.v_number = FAIL;
14113 if (check_restricted() || check_secure())
14114 return;
14115
14116 dir = get_tv_string_buf(&argvars[0], buf);
14117 if (argvars[1].v_type != VAR_UNKNOWN)
14118 {
14119 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 prot = get_tv_number_chk(&argvars[2], NULL);
14121 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014122 mkdir_recurse(dir, prot);
14123 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014125}
14126#endif
14127
Bram Moolenaar0d660222005-01-07 21:51:51 +000014128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 * "mode()" function
14130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *argvars;
14134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014136 char_u buf[3];
14137
14138 buf[1] = NUL;
14139 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140
14141#ifdef FEAT_VISUAL
14142 if (VIsual_active)
14143 {
14144 if (VIsual_select)
14145 buf[0] = VIsual_mode + 's' - 'v';
14146 else
14147 buf[0] = VIsual_mode;
14148 }
14149 else
14150#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014151 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14152 || State == CONFIRM)
14153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014155 if (State == ASKMORE)
14156 buf[1] = 'm';
14157 else if (State == CONFIRM)
14158 buf[1] = '?';
14159 }
14160 else if (State == EXTERNCMD)
14161 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 else if (State & INSERT)
14163 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014164#ifdef FEAT_VREPLACE
14165 if (State & VREPLACE_FLAG)
14166 {
14167 buf[0] = 'R';
14168 buf[1] = 'v';
14169 }
14170 else
14171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 if (State & REPLACE_FLAG)
14173 buf[0] = 'R';
14174 else
14175 buf[0] = 'i';
14176 }
14177 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014180 if (exmode_active)
14181 buf[1] = 'v';
14182 }
14183 else if (exmode_active)
14184 {
14185 buf[0] = 'c';
14186 buf[1] = 'e';
14187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014191 if (finish_op)
14192 buf[1] = 'o';
14193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014195 /* Clear out the minor mode when the argument is not a non-zero number or
14196 * non-empty string. */
14197 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014198 buf[1] = NUL;
14199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 rettv->vval.v_string = vim_strsave(buf);
14201 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202}
14203
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014204#ifdef FEAT_MZSCHEME
14205/*
14206 * "mzeval()" function
14207 */
14208 static void
14209f_mzeval(argvars, rettv)
14210 typval_T *argvars;
14211 typval_T *rettv;
14212{
14213 char_u *str;
14214 char_u buf[NUMBUFLEN];
14215
14216 str = get_tv_string_buf(&argvars[0], buf);
14217 do_mzeval(str, rettv);
14218}
14219#endif
14220
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222 * "nextnonblank()" function
14223 */
14224 static void
14225f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *argvars;
14227 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014228{
14229 linenr_T lnum;
14230
14231 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234 {
14235 lnum = 0;
14236 break;
14237 }
14238 if (*skipwhite(ml_get(lnum)) != NUL)
14239 break;
14240 }
14241 rettv->vval.v_number = lnum;
14242}
14243
14244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 * "nr2char()" function
14246 */
14247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014249 typval_T *argvars;
14250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251{
14252 char_u buf[NUMBUFLEN];
14253
14254#ifdef FEAT_MBYTE
14255 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 else
14258#endif
14259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 buf[1] = NUL;
14262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263 rettv->v_type = VAR_STRING;
14264 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014265}
14266
14267/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014268 * "or(expr, expr)" function
14269 */
14270 static void
14271f_or(argvars, rettv)
14272 typval_T *argvars;
14273 typval_T *rettv;
14274{
14275 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14276 | get_tv_number_chk(&argvars[1], NULL);
14277}
14278
14279/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014280 * "pathshorten()" function
14281 */
14282 static void
14283f_pathshorten(argvars, rettv)
14284 typval_T *argvars;
14285 typval_T *rettv;
14286{
14287 char_u *p;
14288
14289 rettv->v_type = VAR_STRING;
14290 p = get_tv_string_chk(&argvars[0]);
14291 if (p == NULL)
14292 rettv->vval.v_string = NULL;
14293 else
14294 {
14295 p = vim_strsave(p);
14296 rettv->vval.v_string = p;
14297 if (p != NULL)
14298 shorten_dir(p);
14299 }
14300}
14301
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014302#ifdef FEAT_FLOAT
14303/*
14304 * "pow()" function
14305 */
14306 static void
14307f_pow(argvars, rettv)
14308 typval_T *argvars;
14309 typval_T *rettv;
14310{
14311 float_T fx, fy;
14312
14313 rettv->v_type = VAR_FLOAT;
14314 if (get_float_arg(argvars, &fx) == OK
14315 && get_float_arg(&argvars[1], &fy) == OK)
14316 rettv->vval.v_float = pow(fx, fy);
14317 else
14318 rettv->vval.v_float = 0.0;
14319}
14320#endif
14321
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323 * "prevnonblank()" function
14324 */
14325 static void
14326f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014327 typval_T *argvars;
14328 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329{
14330 linenr_T lnum;
14331
14332 lnum = get_tv_lnum(argvars);
14333 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14334 lnum = 0;
14335 else
14336 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14337 --lnum;
14338 rettv->vval.v_number = lnum;
14339}
14340
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014341#ifdef HAVE_STDARG_H
14342/* This dummy va_list is here because:
14343 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14344 * - locally in the function results in a "used before set" warning
14345 * - using va_start() to initialize it gives "function with fixed args" error */
14346static va_list ap;
14347#endif
14348
Bram Moolenaar8c711452005-01-14 21:53:12 +000014349/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014350 * "printf()" function
14351 */
14352 static void
14353f_printf(argvars, rettv)
14354 typval_T *argvars;
14355 typval_T *rettv;
14356{
14357 rettv->v_type = VAR_STRING;
14358 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014359#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014360 {
14361 char_u buf[NUMBUFLEN];
14362 int len;
14363 char_u *s;
14364 int saved_did_emsg = did_emsg;
14365 char *fmt;
14366
14367 /* Get the required length, allocate the buffer and do it for real. */
14368 did_emsg = FALSE;
14369 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014370 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014371 if (!did_emsg)
14372 {
14373 s = alloc(len + 1);
14374 if (s != NULL)
14375 {
14376 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014377 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014378 }
14379 }
14380 did_emsg |= saved_did_emsg;
14381 }
14382#endif
14383}
14384
14385/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014386 * "pumvisible()" function
14387 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014388 static void
14389f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014390 typval_T *argvars UNUSED;
14391 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014392{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014393#ifdef FEAT_INS_EXPAND
14394 if (pum_visible())
14395 rettv->vval.v_number = 1;
14396#endif
14397}
14398
14399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014400 * "range()" function
14401 */
14402 static void
14403f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 typval_T *argvars;
14405 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014406{
14407 long start;
14408 long end;
14409 long stride = 1;
14410 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014414 if (argvars[1].v_type == VAR_UNKNOWN)
14415 {
14416 end = start - 1;
14417 start = 0;
14418 }
14419 else
14420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014423 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014424 }
14425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 if (error)
14427 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014428 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014429 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014430 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014431 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014432 else
14433 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014434 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014435 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014436 if (list_append_number(rettv->vval.v_list,
14437 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014438 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014439 }
14440}
14441
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014442/*
14443 * "readfile()" function
14444 */
14445 static void
14446f_readfile(argvars, rettv)
14447 typval_T *argvars;
14448 typval_T *rettv;
14449{
14450 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014451 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014452 char_u *fname;
14453 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014454 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14455 int io_size = sizeof(buf);
14456 int readlen; /* size of last fread() */
14457 char_u *prev = NULL; /* previously read bytes, if any */
14458 long prevlen = 0; /* length of data in prev */
14459 long prevsize = 0; /* size of prev buffer */
14460 long maxline = MAXLNUM;
14461 long cnt = 0;
14462 char_u *p; /* position in buf */
14463 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014464
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014465 if (argvars[1].v_type != VAR_UNKNOWN)
14466 {
14467 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14468 binary = TRUE;
14469 if (argvars[2].v_type != VAR_UNKNOWN)
14470 maxline = get_tv_number(&argvars[2]);
14471 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014472
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014473 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014475
14476 /* Always open the file in binary mode, library functions have a mind of
14477 * their own about CR-LF conversion. */
14478 fname = get_tv_string(&argvars[0]);
14479 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14480 {
14481 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14482 return;
14483 }
14484
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014485 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014486 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014487 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014488
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014489 /* This for loop processes what was read, but is also entered at end
14490 * of file so that either:
14491 * - an incomplete line gets written
14492 * - a "binary" file gets an empty line at the end if it ends in a
14493 * newline. */
14494 for (p = buf, start = buf;
14495 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14496 ++p)
14497 {
14498 if (*p == '\n' || readlen <= 0)
14499 {
14500 listitem_T *li;
14501 char_u *s = NULL;
14502 long_u len = p - start;
14503
14504 /* Finished a line. Remove CRs before NL. */
14505 if (readlen > 0 && !binary)
14506 {
14507 while (len > 0 && start[len - 1] == '\r')
14508 --len;
14509 /* removal may cross back to the "prev" string */
14510 if (len == 0)
14511 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14512 --prevlen;
14513 }
14514 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014515 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014516 else
14517 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014518 /* Change "prev" buffer to be the right size. This way
14519 * the bytes are only copied once, and very long lines are
14520 * allocated only once. */
14521 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014522 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014523 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014524 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014525 prev = NULL; /* the list will own the string */
14526 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014527 }
14528 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014529 if (s == NULL)
14530 {
14531 do_outofmem_msg((long_u) prevlen + len + 1);
14532 failed = TRUE;
14533 break;
14534 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014535
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014536 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014537 {
14538 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014539 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014540 break;
14541 }
14542 li->li_tv.v_type = VAR_STRING;
14543 li->li_tv.v_lock = 0;
14544 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014545 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014546
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014547 start = p + 1; /* step over newline */
14548 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 break;
14550 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014551 else if (*p == NUL)
14552 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014553#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014554 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14555 * when finding the BF and check the previous two bytes. */
14556 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014557 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014558 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14559 * + 1, these may be in the "prev" string. */
14560 char_u back1 = p >= buf + 1 ? p[-1]
14561 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14562 char_u back2 = p >= buf + 2 ? p[-2]
14563 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14564 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014566 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014567 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014568 char_u *dest = p - 2;
14569
14570 /* Usually a BOM is at the beginning of a file, and so at
14571 * the beginning of a line; then we can just step over it.
14572 */
14573 if (start == dest)
14574 start = p + 1;
14575 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014576 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014577 /* have to shuffle buf to close gap */
14578 int adjust_prevlen = 0;
14579
14580 if (dest < buf)
14581 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014582 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014583 dest = buf;
14584 }
14585 if (readlen > p - buf + 1)
14586 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14587 readlen -= 3 - adjust_prevlen;
14588 prevlen -= adjust_prevlen;
14589 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014590 }
14591 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014592 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014593#endif
14594 } /* for */
14595
14596 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14597 break;
14598 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014599 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014600 /* There's part of a line in buf, store it in "prev". */
14601 if (p - start + prevlen >= prevsize)
14602 {
14603 /* need bigger "prev" buffer */
14604 char_u *newprev;
14605
14606 /* A common use case is ordinary text files and "prev" gets a
14607 * fragment of a line, so the first allocation is made
14608 * small, to avoid repeatedly 'allocing' large and
14609 * 'reallocing' small. */
14610 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014611 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014612 else
14613 {
14614 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014615 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014616 prevsize = grow50pc > growmin ? grow50pc : growmin;
14617 }
14618 if ((newprev = vim_realloc(prev, prevsize)) == NULL)
14619 {
14620 do_outofmem_msg((long_u)prevsize);
14621 failed = TRUE;
14622 break;
14623 }
14624 prev = newprev;
14625 }
14626 /* Add the line part to end of "prev". */
14627 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014628 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014629 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014630 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014631
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014632 /*
14633 * For a negative line count use only the lines at the end of the file,
14634 * free the rest.
14635 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014636 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014637 while (cnt > -maxline)
14638 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014639 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014640 --cnt;
14641 }
14642
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014643 if (failed)
14644 {
14645 list_free(rettv->vval.v_list, TRUE);
14646 /* readfile doc says an empty list is returned on error */
14647 rettv->vval.v_list = list_alloc();
14648 }
14649
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014650 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014651 fclose(fd);
14652}
14653
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014654#if defined(FEAT_RELTIME)
14655static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14656
14657/*
14658 * Convert a List to proftime_T.
14659 * Return FAIL when there is something wrong.
14660 */
14661 static int
14662list2proftime(arg, tm)
14663 typval_T *arg;
14664 proftime_T *tm;
14665{
14666 long n1, n2;
14667 int error = FALSE;
14668
14669 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14670 || arg->vval.v_list->lv_len != 2)
14671 return FAIL;
14672 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14673 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14674# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014675 tm->HighPart = n1;
14676 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014677# else
14678 tm->tv_sec = n1;
14679 tm->tv_usec = n2;
14680# endif
14681 return error ? FAIL : OK;
14682}
14683#endif /* FEAT_RELTIME */
14684
14685/*
14686 * "reltime()" function
14687 */
14688 static void
14689f_reltime(argvars, rettv)
14690 typval_T *argvars;
14691 typval_T *rettv;
14692{
14693#ifdef FEAT_RELTIME
14694 proftime_T res;
14695 proftime_T start;
14696
14697 if (argvars[0].v_type == VAR_UNKNOWN)
14698 {
14699 /* No arguments: get current time. */
14700 profile_start(&res);
14701 }
14702 else if (argvars[1].v_type == VAR_UNKNOWN)
14703 {
14704 if (list2proftime(&argvars[0], &res) == FAIL)
14705 return;
14706 profile_end(&res);
14707 }
14708 else
14709 {
14710 /* Two arguments: compute the difference. */
14711 if (list2proftime(&argvars[0], &start) == FAIL
14712 || list2proftime(&argvars[1], &res) == FAIL)
14713 return;
14714 profile_sub(&res, &start);
14715 }
14716
14717 if (rettv_list_alloc(rettv) == OK)
14718 {
14719 long n1, n2;
14720
14721# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014722 n1 = res.HighPart;
14723 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014724# else
14725 n1 = res.tv_sec;
14726 n2 = res.tv_usec;
14727# endif
14728 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14729 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14730 }
14731#endif
14732}
14733
14734/*
14735 * "reltimestr()" function
14736 */
14737 static void
14738f_reltimestr(argvars, rettv)
14739 typval_T *argvars;
14740 typval_T *rettv;
14741{
14742#ifdef FEAT_RELTIME
14743 proftime_T tm;
14744#endif
14745
14746 rettv->v_type = VAR_STRING;
14747 rettv->vval.v_string = NULL;
14748#ifdef FEAT_RELTIME
14749 if (list2proftime(&argvars[0], &tm) == OK)
14750 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14751#endif
14752}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14755static void make_connection __ARGS((void));
14756static int check_connection __ARGS((void));
14757
14758 static void
14759make_connection()
14760{
14761 if (X_DISPLAY == NULL
14762# ifdef FEAT_GUI
14763 && !gui.in_use
14764# endif
14765 )
14766 {
14767 x_force_connect = TRUE;
14768 setup_term_clip();
14769 x_force_connect = FALSE;
14770 }
14771}
14772
14773 static int
14774check_connection()
14775{
14776 make_connection();
14777 if (X_DISPLAY == NULL)
14778 {
14779 EMSG(_("E240: No connection to Vim server"));
14780 return FAIL;
14781 }
14782 return OK;
14783}
14784#endif
14785
14786#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014787static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788
14789 static void
14790remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 typval_T *argvars;
14792 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014793 int expr;
14794{
14795 char_u *server_name;
14796 char_u *keys;
14797 char_u *r = NULL;
14798 char_u buf[NUMBUFLEN];
14799# ifdef WIN32
14800 HWND w;
14801# else
14802 Window w;
14803# endif
14804
14805 if (check_restricted() || check_secure())
14806 return;
14807
14808# ifdef FEAT_X11
14809 if (check_connection() == FAIL)
14810 return;
14811# endif
14812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014813 server_name = get_tv_string_chk(&argvars[0]);
14814 if (server_name == NULL)
14815 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816 keys = get_tv_string_buf(&argvars[1], buf);
14817# ifdef WIN32
14818 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14819# else
14820 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14821 < 0)
14822# endif
14823 {
14824 if (r != NULL)
14825 EMSG(r); /* sending worked but evaluation failed */
14826 else
14827 EMSG2(_("E241: Unable to send to %s"), server_name);
14828 return;
14829 }
14830
14831 rettv->vval.v_string = r;
14832
14833 if (argvars[2].v_type != VAR_UNKNOWN)
14834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014836 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014837 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014838
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014839 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014840 v.di_tv.v_type = VAR_STRING;
14841 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014842 idvar = get_tv_string_chk(&argvars[2]);
14843 if (idvar != NULL)
14844 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014845 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014846 }
14847}
14848#endif
14849
14850/*
14851 * "remote_expr()" function
14852 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014853 static void
14854f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014857{
14858 rettv->v_type = VAR_STRING;
14859 rettv->vval.v_string = NULL;
14860#ifdef FEAT_CLIENTSERVER
14861 remote_common(argvars, rettv, TRUE);
14862#endif
14863}
14864
14865/*
14866 * "remote_foreground()" function
14867 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014868 static void
14869f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014870 typval_T *argvars UNUSED;
14871 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014872{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014873#ifdef FEAT_CLIENTSERVER
14874# ifdef WIN32
14875 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014876 {
14877 char_u *server_name = get_tv_string_chk(&argvars[0]);
14878
14879 if (server_name != NULL)
14880 serverForeground(server_name);
14881 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882# else
14883 /* Send a foreground() expression to the server. */
14884 argvars[1].v_type = VAR_STRING;
14885 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14886 argvars[2].v_type = VAR_UNKNOWN;
14887 remote_common(argvars, rettv, TRUE);
14888 vim_free(argvars[1].vval.v_string);
14889# endif
14890#endif
14891}
14892
Bram Moolenaar0d660222005-01-07 21:51:51 +000014893 static void
14894f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897{
14898#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014899 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900 char_u *s = NULL;
14901# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014902 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014905
14906 if (check_restricted() || check_secure())
14907 {
14908 rettv->vval.v_number = -1;
14909 return;
14910 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014911 serverid = get_tv_string_chk(&argvars[0]);
14912 if (serverid == NULL)
14913 {
14914 rettv->vval.v_number = -1;
14915 return; /* type error; errmsg already given */
14916 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014917# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014918 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919 if (n == 0)
14920 rettv->vval.v_number = -1;
14921 else
14922 {
14923 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14924 rettv->vval.v_number = (s != NULL);
14925 }
14926# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927 if (check_connection() == FAIL)
14928 return;
14929
14930 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014932# endif
14933
14934 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014936 char_u *retvar;
14937
Bram Moolenaar33570922005-01-25 22:26:29 +000014938 v.di_tv.v_type = VAR_STRING;
14939 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014940 retvar = get_tv_string_chk(&argvars[1]);
14941 if (retvar != NULL)
14942 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014943 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944 }
14945#else
14946 rettv->vval.v_number = -1;
14947#endif
14948}
14949
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 static void
14951f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014952 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954{
14955 char_u *r = NULL;
14956
14957#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958 char_u *serverid = get_tv_string_chk(&argvars[0]);
14959
14960 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 {
14962# ifdef WIN32
14963 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014964 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014966 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014967 if (n != 0)
14968 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14969 if (r == NULL)
14970# else
14971 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014972 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014973# endif
14974 EMSG(_("E277: Unable to read a server reply"));
14975 }
14976#endif
14977 rettv->v_type = VAR_STRING;
14978 rettv->vval.v_string = r;
14979}
14980
14981/*
14982 * "remote_send()" function
14983 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 static void
14985f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988{
14989 rettv->v_type = VAR_STRING;
14990 rettv->vval.v_string = NULL;
14991#ifdef FEAT_CLIENTSERVER
14992 remote_common(argvars, rettv, FALSE);
14993#endif
14994}
14995
14996/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014997 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014998 */
14999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015001 typval_T *argvars;
15002 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015003{
Bram Moolenaar33570922005-01-25 22:26:29 +000015004 list_T *l;
15005 listitem_T *item, *item2;
15006 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015007 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015008 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015009 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 dict_T *d;
15011 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015012 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015013
Bram Moolenaar8c711452005-01-14 21:53:12 +000015014 if (argvars[0].v_type == VAR_DICT)
15015 {
15016 if (argvars[2].v_type != VAR_UNKNOWN)
15017 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015018 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015019 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 key = get_tv_string_chk(&argvars[1]);
15022 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015024 di = dict_find(d, key, -1);
15025 if (di == NULL)
15026 EMSG2(_(e_dictkey), key);
15027 else
15028 {
15029 *rettv = di->di_tv;
15030 init_tv(&di->di_tv);
15031 dictitem_remove(d, di);
15032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015034 }
15035 }
15036 else if (argvars[0].v_type != VAR_LIST)
15037 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015038 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015039 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041 int error = FALSE;
15042
15043 idx = get_tv_number_chk(&argvars[1], &error);
15044 if (error)
15045 ; /* type error: do nothing, errmsg already given */
15046 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015047 EMSGN(_(e_listidx), idx);
15048 else
15049 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015050 if (argvars[2].v_type == VAR_UNKNOWN)
15051 {
15052 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015053 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015054 *rettv = item->li_tv;
15055 vim_free(item);
15056 }
15057 else
15058 {
15059 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 end = get_tv_number_chk(&argvars[2], &error);
15061 if (error)
15062 ; /* type error: do nothing */
15063 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015064 EMSGN(_(e_listidx), end);
15065 else
15066 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015067 int cnt = 0;
15068
15069 for (li = item; li != NULL; li = li->li_next)
15070 {
15071 ++cnt;
15072 if (li == item2)
15073 break;
15074 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015075 if (li == NULL) /* didn't find "item2" after "item" */
15076 EMSG(_(e_invrange));
15077 else
15078 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015079 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015080 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015081 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015082 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015083 l->lv_first = item;
15084 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015085 item->li_prev = NULL;
15086 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015087 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015088 }
15089 }
15090 }
15091 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015092 }
15093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094}
15095
15096/*
15097 * "rename({from}, {to})" function
15098 */
15099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 typval_T *argvars;
15102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103{
15104 char_u buf[NUMBUFLEN];
15105
15106 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15110 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111}
15112
15113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015114 * "repeat()" function
15115 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015116 static void
15117f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 typval_T *argvars;
15119 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015120{
15121 char_u *p;
15122 int n;
15123 int slen;
15124 int len;
15125 char_u *r;
15126 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015127
15128 n = get_tv_number(&argvars[1]);
15129 if (argvars[0].v_type == VAR_LIST)
15130 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015131 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015132 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015133 if (list_extend(rettv->vval.v_list,
15134 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015135 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015136 }
15137 else
15138 {
15139 p = get_tv_string(&argvars[0]);
15140 rettv->v_type = VAR_STRING;
15141 rettv->vval.v_string = NULL;
15142
15143 slen = (int)STRLEN(p);
15144 len = slen * n;
15145 if (len <= 0)
15146 return;
15147
15148 r = alloc(len + 1);
15149 if (r != NULL)
15150 {
15151 for (i = 0; i < n; i++)
15152 mch_memmove(r + i * slen, p, (size_t)slen);
15153 r[len] = NUL;
15154 }
15155
15156 rettv->vval.v_string = r;
15157 }
15158}
15159
15160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 * "resolve()" function
15162 */
15163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015164f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 typval_T *argvars;
15166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167{
15168 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015169#ifdef HAVE_READLINK
15170 char_u *buf = NULL;
15171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174#ifdef FEAT_SHORTCUT
15175 {
15176 char_u *v = NULL;
15177
15178 v = mch_resolve_shortcut(p);
15179 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015182 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 }
15184#else
15185# ifdef HAVE_READLINK
15186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 char_u *cpy;
15188 int len;
15189 char_u *remain = NULL;
15190 char_u *q;
15191 int is_relative_to_current = FALSE;
15192 int has_trailing_pathsep = FALSE;
15193 int limit = 100;
15194
15195 p = vim_strsave(p);
15196
15197 if (p[0] == '.' && (vim_ispathsep(p[1])
15198 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15199 is_relative_to_current = TRUE;
15200
15201 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015202 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015205 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207
15208 q = getnextcomp(p);
15209 if (*q != NUL)
15210 {
15211 /* Separate the first path component in "p", and keep the
15212 * remainder (beginning with the path separator). */
15213 remain = vim_strsave(q - 1);
15214 q[-1] = NUL;
15215 }
15216
Bram Moolenaard9462e32011-04-11 21:35:11 +020015217 buf = alloc(MAXPATHL + 1);
15218 if (buf == NULL)
15219 goto fail;
15220
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 for (;;)
15222 {
15223 for (;;)
15224 {
15225 len = readlink((char *)p, (char *)buf, MAXPATHL);
15226 if (len <= 0)
15227 break;
15228 buf[len] = NUL;
15229
15230 if (limit-- == 0)
15231 {
15232 vim_free(p);
15233 vim_free(remain);
15234 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 goto fail;
15237 }
15238
15239 /* Ensure that the result will have a trailing path separator
15240 * if the argument has one. */
15241 if (remain == NULL && has_trailing_pathsep)
15242 add_pathsep(buf);
15243
15244 /* Separate the first path component in the link value and
15245 * concatenate the remainders. */
15246 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15247 if (*q != NUL)
15248 {
15249 if (remain == NULL)
15250 remain = vim_strsave(q - 1);
15251 else
15252 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015253 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 if (cpy != NULL)
15255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 vim_free(remain);
15257 remain = cpy;
15258 }
15259 }
15260 q[-1] = NUL;
15261 }
15262
15263 q = gettail(p);
15264 if (q > p && *q == NUL)
15265 {
15266 /* Ignore trailing path separator. */
15267 q[-1] = NUL;
15268 q = gettail(p);
15269 }
15270 if (q > p && !mch_isFullName(buf))
15271 {
15272 /* symlink is relative to directory of argument */
15273 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15274 if (cpy != NULL)
15275 {
15276 STRCPY(cpy, p);
15277 STRCPY(gettail(cpy), buf);
15278 vim_free(p);
15279 p = cpy;
15280 }
15281 }
15282 else
15283 {
15284 vim_free(p);
15285 p = vim_strsave(buf);
15286 }
15287 }
15288
15289 if (remain == NULL)
15290 break;
15291
15292 /* Append the first path component of "remain" to "p". */
15293 q = getnextcomp(remain + 1);
15294 len = q - remain - (*q != NUL);
15295 cpy = vim_strnsave(p, STRLEN(p) + len);
15296 if (cpy != NULL)
15297 {
15298 STRNCAT(cpy, remain, len);
15299 vim_free(p);
15300 p = cpy;
15301 }
15302 /* Shorten "remain". */
15303 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015304 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 else
15306 {
15307 vim_free(remain);
15308 remain = NULL;
15309 }
15310 }
15311
15312 /* If the result is a relative path name, make it explicitly relative to
15313 * the current directory if and only if the argument had this form. */
15314 if (!vim_ispathsep(*p))
15315 {
15316 if (is_relative_to_current
15317 && *p != NUL
15318 && !(p[0] == '.'
15319 && (p[1] == NUL
15320 || vim_ispathsep(p[1])
15321 || (p[1] == '.'
15322 && (p[2] == NUL
15323 || vim_ispathsep(p[2]))))))
15324 {
15325 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015326 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 if (cpy != NULL)
15328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329 vim_free(p);
15330 p = cpy;
15331 }
15332 }
15333 else if (!is_relative_to_current)
15334 {
15335 /* Strip leading "./". */
15336 q = p;
15337 while (q[0] == '.' && vim_ispathsep(q[1]))
15338 q += 2;
15339 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015340 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 }
15342 }
15343
15344 /* Ensure that the result will have no trailing path separator
15345 * if the argument had none. But keep "/" or "//". */
15346 if (!has_trailing_pathsep)
15347 {
15348 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015349 if (after_pathsep(p, q))
15350 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 }
15352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 }
15355# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357# endif
15358#endif
15359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361
15362#ifdef HAVE_READLINK
15363fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015364 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367}
15368
15369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015370 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 */
15372 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015374 typval_T *argvars;
15375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376{
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 list_T *l;
15378 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379
Bram Moolenaar0d660222005-01-07 21:51:51 +000015380 if (argvars[0].v_type != VAR_LIST)
15381 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015382 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015383 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384 {
15385 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015386 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015387 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388 while (li != NULL)
15389 {
15390 ni = li->li_prev;
15391 list_append(l, li);
15392 li = ni;
15393 }
15394 rettv->vval.v_list = l;
15395 rettv->v_type = VAR_LIST;
15396 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015397 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399}
15400
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015401#define SP_NOMOVE 0x01 /* don't move cursor */
15402#define SP_REPEAT 0x02 /* repeat to find outer pair */
15403#define SP_RETCOUNT 0x04 /* return matchcount */
15404#define SP_SETPCMARK 0x08 /* set previous context mark */
15405#define SP_START 0x10 /* accept match at start position */
15406#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15407#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015408
Bram Moolenaar33570922005-01-25 22:26:29 +000015409static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410
15411/*
15412 * Get flags for a search function.
15413 * Possibly sets "p_ws".
15414 * Returns BACKWARD, FORWARD or zero (for an error).
15415 */
15416 static int
15417get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015418 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419 int *flagsp;
15420{
15421 int dir = FORWARD;
15422 char_u *flags;
15423 char_u nbuf[NUMBUFLEN];
15424 int mask;
15425
15426 if (varp->v_type != VAR_UNKNOWN)
15427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 flags = get_tv_string_buf_chk(varp, nbuf);
15429 if (flags == NULL)
15430 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015431 while (*flags != NUL)
15432 {
15433 switch (*flags)
15434 {
15435 case 'b': dir = BACKWARD; break;
15436 case 'w': p_ws = TRUE; break;
15437 case 'W': p_ws = FALSE; break;
15438 default: mask = 0;
15439 if (flagsp != NULL)
15440 switch (*flags)
15441 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015442 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015443 case 'e': mask = SP_END; break;
15444 case 'm': mask = SP_RETCOUNT; break;
15445 case 'n': mask = SP_NOMOVE; break;
15446 case 'p': mask = SP_SUBPAT; break;
15447 case 'r': mask = SP_REPEAT; break;
15448 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015449 }
15450 if (mask == 0)
15451 {
15452 EMSG2(_(e_invarg2), flags);
15453 dir = 0;
15454 }
15455 else
15456 *flagsp |= mask;
15457 }
15458 if (dir == 0)
15459 break;
15460 ++flags;
15461 }
15462 }
15463 return dir;
15464}
15465
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015467 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015469 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015470search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015471 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015472 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015473 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015475 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 char_u *pat;
15477 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015478 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 int save_p_ws = p_ws;
15480 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015481 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015482 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015483 proftime_T tm;
15484#ifdef FEAT_RELTIME
15485 long time_limit = 0;
15486#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015487 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015488 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015491 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015492 if (dir == 0)
15493 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015494 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015495 if (flags & SP_START)
15496 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015497 if (flags & SP_END)
15498 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015499
Bram Moolenaar76929292008-01-06 19:07:36 +000015500 /* Optional arguments: line number to stop searching and timeout. */
15501 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015502 {
15503 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15504 if (lnum_stop < 0)
15505 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015506#ifdef FEAT_RELTIME
15507 if (argvars[3].v_type != VAR_UNKNOWN)
15508 {
15509 time_limit = get_tv_number_chk(&argvars[3], NULL);
15510 if (time_limit < 0)
15511 goto theend;
15512 }
15513#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015514 }
15515
Bram Moolenaar76929292008-01-06 19:07:36 +000015516#ifdef FEAT_RELTIME
15517 /* Set the time limit, if there is one. */
15518 profile_setlimit(time_limit, &tm);
15519#endif
15520
Bram Moolenaar231334e2005-07-25 20:46:57 +000015521 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015522 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015523 * Check to make sure only those flags are set.
15524 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15525 * flags cannot be set. Check for that condition also.
15526 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015527 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015528 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015530 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015531 goto theend;
15532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015534 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015535 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015536 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015537 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015539 if (flags & SP_SUBPAT)
15540 retval = subpatnum;
15541 else
15542 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015543 if (flags & SP_SETPCMARK)
15544 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015546 if (match_pos != NULL)
15547 {
15548 /* Store the match cursor position */
15549 match_pos->lnum = pos.lnum;
15550 match_pos->col = pos.col + 1;
15551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552 /* "/$" will put the cursor after the end of the line, may need to
15553 * correct that here */
15554 check_cursor();
15555 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015556
15557 /* If 'n' flag is used: restore cursor position. */
15558 if (flags & SP_NOMOVE)
15559 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015560 else
15561 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015562theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015564
15565 return retval;
15566}
15567
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015568#ifdef FEAT_FLOAT
15569/*
15570 * "round({float})" function
15571 */
15572 static void
15573f_round(argvars, rettv)
15574 typval_T *argvars;
15575 typval_T *rettv;
15576{
15577 float_T f;
15578
15579 rettv->v_type = VAR_FLOAT;
15580 if (get_float_arg(argvars, &f) == OK)
15581 /* round() is not in C90, use ceil() or floor() instead. */
15582 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15583 else
15584 rettv->vval.v_float = 0.0;
15585}
15586#endif
15587
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015588/*
15589 * "search()" function
15590 */
15591 static void
15592f_search(argvars, rettv)
15593 typval_T *argvars;
15594 typval_T *rettv;
15595{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015596 int flags = 0;
15597
15598 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599}
15600
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015602 * "searchdecl()" function
15603 */
15604 static void
15605f_searchdecl(argvars, rettv)
15606 typval_T *argvars;
15607 typval_T *rettv;
15608{
15609 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015610 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015611 int error = FALSE;
15612 char_u *name;
15613
15614 rettv->vval.v_number = 1; /* default: FAIL */
15615
15616 name = get_tv_string_chk(&argvars[0]);
15617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015618 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015619 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015620 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15621 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15622 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015623 if (!error && name != NULL)
15624 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015625 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015626}
15627
15628/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015629 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015631 static int
15632searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015633 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015634 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635{
15636 char_u *spat, *mpat, *epat;
15637 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 int dir;
15640 int flags = 0;
15641 char_u nbuf1[NUMBUFLEN];
15642 char_u nbuf2[NUMBUFLEN];
15643 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015644 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015645 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015646 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015649 spat = get_tv_string_chk(&argvars[0]);
15650 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15651 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15652 if (spat == NULL || mpat == NULL || epat == NULL)
15653 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 /* Handle the optional fourth argument: flags */
15656 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015657 if (dir == 0)
15658 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015659
15660 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015661 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15662 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015663 if ((flags & (SP_END | SP_SUBPAT)) != 0
15664 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015665 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015666 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015667 goto theend;
15668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015670 /* Using 'r' implies 'W', otherwise it doesn't work. */
15671 if (flags & SP_REPEAT)
15672 p_ws = FALSE;
15673
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015674 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015675 if (argvars[3].v_type == VAR_UNKNOWN
15676 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 skip = (char_u *)"";
15678 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015680 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015681 if (argvars[5].v_type != VAR_UNKNOWN)
15682 {
15683 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15684 if (lnum_stop < 0)
15685 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015686#ifdef FEAT_RELTIME
15687 if (argvars[6].v_type != VAR_UNKNOWN)
15688 {
15689 time_limit = get_tv_number_chk(&argvars[6], NULL);
15690 if (time_limit < 0)
15691 goto theend;
15692 }
15693#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015694 }
15695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 if (skip == NULL)
15697 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015699 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015700 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015701
15702theend:
15703 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015704
15705 return retval;
15706}
15707
15708/*
15709 * "searchpair()" function
15710 */
15711 static void
15712f_searchpair(argvars, rettv)
15713 typval_T *argvars;
15714 typval_T *rettv;
15715{
15716 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15717}
15718
15719/*
15720 * "searchpairpos()" function
15721 */
15722 static void
15723f_searchpairpos(argvars, rettv)
15724 typval_T *argvars;
15725 typval_T *rettv;
15726{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015727 pos_T match_pos;
15728 int lnum = 0;
15729 int col = 0;
15730
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015731 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015732 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015733
15734 if (searchpair_cmn(argvars, &match_pos) > 0)
15735 {
15736 lnum = match_pos.lnum;
15737 col = match_pos.col;
15738 }
15739
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015740 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15741 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015742}
15743
15744/*
15745 * Search for a start/middle/end thing.
15746 * Used by searchpair(), see its documentation for the details.
15747 * Returns 0 or -1 for no match,
15748 */
15749 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015750do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15751 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015752 char_u *spat; /* start pattern */
15753 char_u *mpat; /* middle pattern */
15754 char_u *epat; /* end pattern */
15755 int dir; /* BACKWARD or FORWARD */
15756 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015757 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015758 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015759 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015760 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015761{
15762 char_u *save_cpo;
15763 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15764 long retval = 0;
15765 pos_T pos;
15766 pos_T firstpos;
15767 pos_T foundpos;
15768 pos_T save_cursor;
15769 pos_T save_pos;
15770 int n;
15771 int r;
15772 int nest = 1;
15773 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015774 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015775 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015776
15777 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15778 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015779 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015780
Bram Moolenaar76929292008-01-06 19:07:36 +000015781#ifdef FEAT_RELTIME
15782 /* Set the time limit, if there is one. */
15783 profile_setlimit(time_limit, &tm);
15784#endif
15785
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015786 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15787 * start/middle/end (pat3, for the top pair). */
15788 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15789 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15790 if (pat2 == NULL || pat3 == NULL)
15791 goto theend;
15792 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15793 if (*mpat == NUL)
15794 STRCPY(pat3, pat2);
15795 else
15796 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15797 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015798 if (flags & SP_START)
15799 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015800
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 save_cursor = curwin->w_cursor;
15802 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015803 clearpos(&firstpos);
15804 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 pat = pat3;
15806 for (;;)
15807 {
15808 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015809 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15811 /* didn't find it or found the first match again: FAIL */
15812 break;
15813
15814 if (firstpos.lnum == 0)
15815 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015816 if (equalpos(pos, foundpos))
15817 {
15818 /* Found the same position again. Can happen with a pattern that
15819 * has "\zs" at the end and searching backwards. Advance one
15820 * character and try again. */
15821 if (dir == BACKWARD)
15822 decl(&pos);
15823 else
15824 incl(&pos);
15825 }
15826 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015828 /* clear the start flag to avoid getting stuck here */
15829 options &= ~SEARCH_START;
15830
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 /* If the skip pattern matches, ignore this match. */
15832 if (*skip != NUL)
15833 {
15834 save_pos = curwin->w_cursor;
15835 curwin->w_cursor = pos;
15836 r = eval_to_bool(skip, &err, NULL, FALSE);
15837 curwin->w_cursor = save_pos;
15838 if (err)
15839 {
15840 /* Evaluating {skip} caused an error, break here. */
15841 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015842 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 break;
15844 }
15845 if (r)
15846 continue;
15847 }
15848
15849 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15850 {
15851 /* Found end when searching backwards or start when searching
15852 * forward: nested pair. */
15853 ++nest;
15854 pat = pat2; /* nested, don't search for middle */
15855 }
15856 else
15857 {
15858 /* Found end when searching forward or start when searching
15859 * backward: end of (nested) pair; or found middle in outer pair. */
15860 if (--nest == 1)
15861 pat = pat3; /* outer level, search for middle */
15862 }
15863
15864 if (nest == 0)
15865 {
15866 /* Found the match: return matchcount or line number. */
15867 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015868 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015870 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015871 if (flags & SP_SETPCMARK)
15872 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 curwin->w_cursor = pos;
15874 if (!(flags & SP_REPEAT))
15875 break;
15876 nest = 1; /* search for next unmatched */
15877 }
15878 }
15879
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015880 if (match_pos != NULL)
15881 {
15882 /* Store the match cursor position */
15883 match_pos->lnum = curwin->w_cursor.lnum;
15884 match_pos->col = curwin->w_cursor.col + 1;
15885 }
15886
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015888 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 curwin->w_cursor = save_cursor;
15890
15891theend:
15892 vim_free(pat2);
15893 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015894 if (p_cpo == empty_option)
15895 p_cpo = save_cpo;
15896 else
15897 /* Darn, evaluating the {skip} expression changed the value. */
15898 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015899
15900 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901}
15902
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015903/*
15904 * "searchpos()" function
15905 */
15906 static void
15907f_searchpos(argvars, rettv)
15908 typval_T *argvars;
15909 typval_T *rettv;
15910{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015911 pos_T match_pos;
15912 int lnum = 0;
15913 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015914 int n;
15915 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015917 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015918 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015919
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015920 n = search_cmn(argvars, &match_pos, &flags);
15921 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015922 {
15923 lnum = match_pos.lnum;
15924 col = match_pos.col;
15925 }
15926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015927 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15928 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015929 if (flags & SP_SUBPAT)
15930 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015931}
15932
15933
Bram Moolenaar0d660222005-01-07 21:51:51 +000015934 static void
15935f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939#ifdef FEAT_CLIENTSERVER
15940 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015941 char_u *server = get_tv_string_chk(&argvars[0]);
15942 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 if (server == NULL || reply == NULL)
15946 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947 if (check_restricted() || check_secure())
15948 return;
15949# ifdef FEAT_X11
15950 if (check_connection() == FAIL)
15951 return;
15952# endif
15953
15954 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015956 EMSG(_("E258: Unable to send to client"));
15957 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015959 rettv->vval.v_number = 0;
15960#else
15961 rettv->vval.v_number = -1;
15962#endif
15963}
15964
Bram Moolenaar0d660222005-01-07 21:51:51 +000015965 static void
15966f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015967 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015969{
15970 char_u *r = NULL;
15971
15972#ifdef FEAT_CLIENTSERVER
15973# ifdef WIN32
15974 r = serverGetVimNames();
15975# else
15976 make_connection();
15977 if (X_DISPLAY != NULL)
15978 r = serverGetVimNames(X_DISPLAY);
15979# endif
15980#endif
15981 rettv->v_type = VAR_STRING;
15982 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983}
15984
15985/*
15986 * "setbufvar()" function
15987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015989f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015990 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015991 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992{
15993 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 char_u nbuf[NUMBUFLEN];
15998
15999 if (check_restricted() || check_secure())
16000 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16002 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 varp = &argvars[2];
16005
16006 if (buf != NULL && varname != NULL && varp != NULL)
16007 {
16008 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010
16011 if (*varname == '&')
16012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016013 long numval;
16014 char_u *strval;
16015 int error = FALSE;
16016
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016018 numval = get_tv_number_chk(varp, &error);
16019 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 if (!error && strval != NULL)
16021 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 }
16023 else
16024 {
16025 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16026 if (bufvarname != NULL)
16027 {
16028 STRCPY(bufvarname, "b:");
16029 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016030 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 vim_free(bufvarname);
16032 }
16033 }
16034
16035 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038}
16039
16040/*
16041 * "setcmdpos()" function
16042 */
16043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 typval_T *argvars;
16046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 int pos = (int)get_tv_number(&argvars[0]) - 1;
16049
16050 if (pos >= 0)
16051 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052}
16053
16054/*
16055 * "setline()" function
16056 */
16057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016058f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 typval_T *argvars;
16060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061{
16062 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016063 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016064 list_T *l = NULL;
16065 listitem_T *li = NULL;
16066 long added = 0;
16067 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016069 lnum = get_tv_lnum(&argvars[0]);
16070 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016072 l = argvars[1].vval.v_list;
16073 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016075 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016076 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016077
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016078 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016079 for (;;)
16080 {
16081 if (l != NULL)
16082 {
16083 /* list argument, get next string */
16084 if (li == NULL)
16085 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016087 li = li->li_next;
16088 }
16089
16090 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016092 break;
16093 if (lnum <= curbuf->b_ml.ml_line_count)
16094 {
16095 /* existing line, replace it */
16096 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16097 {
16098 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016099 if (lnum == curwin->w_cursor.lnum)
16100 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016101 rettv->vval.v_number = 0; /* OK */
16102 }
16103 }
16104 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16105 {
16106 /* lnum is one past the last line, append the line */
16107 ++added;
16108 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16109 rettv->vval.v_number = 0; /* OK */
16110 }
16111
16112 if (l == NULL) /* only one string argument */
16113 break;
16114 ++lnum;
16115 }
16116
16117 if (added > 0)
16118 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119}
16120
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016121static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16122
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016124 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016125 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016126 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016127set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016128 win_T *wp UNUSED;
16129 typval_T *list_arg UNUSED;
16130 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016131 typval_T *rettv;
16132{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016133#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016134 char_u *act;
16135 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016136#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016137
Bram Moolenaar2641f772005-03-25 21:58:17 +000016138 rettv->vval.v_number = -1;
16139
16140#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016141 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016142 EMSG(_(e_listreq));
16143 else
16144 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016145 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016146
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016147 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016148 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016149 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016150 if (act == NULL)
16151 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016152 if (*act == 'a' || *act == 'r')
16153 action = *act;
16154 }
16155
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016156 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016157 rettv->vval.v_number = 0;
16158 }
16159#endif
16160}
16161
16162/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016163 * "setloclist()" function
16164 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016165 static void
16166f_setloclist(argvars, rettv)
16167 typval_T *argvars;
16168 typval_T *rettv;
16169{
16170 win_T *win;
16171
16172 rettv->vval.v_number = -1;
16173
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016174 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016175 if (win != NULL)
16176 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16177}
16178
16179/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016180 * "setmatches()" function
16181 */
16182 static void
16183f_setmatches(argvars, rettv)
16184 typval_T *argvars;
16185 typval_T *rettv;
16186{
16187#ifdef FEAT_SEARCH_EXTRA
16188 list_T *l;
16189 listitem_T *li;
16190 dict_T *d;
16191
16192 rettv->vval.v_number = -1;
16193 if (argvars[0].v_type != VAR_LIST)
16194 {
16195 EMSG(_(e_listreq));
16196 return;
16197 }
16198 if ((l = argvars[0].vval.v_list) != NULL)
16199 {
16200
16201 /* To some extent make sure that we are dealing with a list from
16202 * "getmatches()". */
16203 li = l->lv_first;
16204 while (li != NULL)
16205 {
16206 if (li->li_tv.v_type != VAR_DICT
16207 || (d = li->li_tv.vval.v_dict) == NULL)
16208 {
16209 EMSG(_(e_invarg));
16210 return;
16211 }
16212 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16213 && dict_find(d, (char_u *)"pattern", -1) != NULL
16214 && dict_find(d, (char_u *)"priority", -1) != NULL
16215 && dict_find(d, (char_u *)"id", -1) != NULL))
16216 {
16217 EMSG(_(e_invarg));
16218 return;
16219 }
16220 li = li->li_next;
16221 }
16222
16223 clear_matches(curwin);
16224 li = l->lv_first;
16225 while (li != NULL)
16226 {
16227 d = li->li_tv.vval.v_dict;
16228 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16229 get_dict_string(d, (char_u *)"pattern", FALSE),
16230 (int)get_dict_number(d, (char_u *)"priority"),
16231 (int)get_dict_number(d, (char_u *)"id"));
16232 li = li->li_next;
16233 }
16234 rettv->vval.v_number = 0;
16235 }
16236#endif
16237}
16238
16239/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016240 * "setpos()" function
16241 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016242 static void
16243f_setpos(argvars, rettv)
16244 typval_T *argvars;
16245 typval_T *rettv;
16246{
16247 pos_T pos;
16248 int fnum;
16249 char_u *name;
16250
Bram Moolenaar08250432008-02-13 11:42:46 +000016251 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016252 name = get_tv_string_chk(argvars);
16253 if (name != NULL)
16254 {
16255 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16256 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016257 if (--pos.col < 0)
16258 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016259 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016260 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016261 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016262 if (fnum == curbuf->b_fnum)
16263 {
16264 curwin->w_cursor = pos;
16265 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016266 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016267 }
16268 else
16269 EMSG(_(e_invarg));
16270 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016271 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16272 {
16273 /* set mark */
16274 if (setmark_pos(name[1], &pos, fnum) == OK)
16275 rettv->vval.v_number = 0;
16276 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016277 else
16278 EMSG(_(e_invarg));
16279 }
16280 }
16281}
16282
16283/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016284 * "setqflist()" function
16285 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016286 static void
16287f_setqflist(argvars, rettv)
16288 typval_T *argvars;
16289 typval_T *rettv;
16290{
16291 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16292}
16293
16294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 * "setreg()" function
16296 */
16297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 typval_T *argvars;
16300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301{
16302 int regname;
16303 char_u *strregname;
16304 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 int append;
16307 char_u yank_type;
16308 long block_len;
16309
16310 block_len = -1;
16311 yank_type = MAUTO;
16312 append = FALSE;
16313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016315 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016317 if (strregname == NULL)
16318 return; /* type error; errmsg already given */
16319 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 if (regname == 0 || regname == '@')
16321 regname = '"';
16322 else if (regname == '=')
16323 return;
16324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016325 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016327 stropt = get_tv_string_chk(&argvars[2]);
16328 if (stropt == NULL)
16329 return; /* type error */
16330 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 switch (*stropt)
16332 {
16333 case 'a': case 'A': /* append */
16334 append = TRUE;
16335 break;
16336 case 'v': case 'c': /* character-wise selection */
16337 yank_type = MCHAR;
16338 break;
16339 case 'V': case 'l': /* line-wise selection */
16340 yank_type = MLINE;
16341 break;
16342#ifdef FEAT_VISUAL
16343 case 'b': case Ctrl_V: /* block-wise selection */
16344 yank_type = MBLOCK;
16345 if (VIM_ISDIGIT(stropt[1]))
16346 {
16347 ++stropt;
16348 block_len = getdigits(&stropt) - 1;
16349 --stropt;
16350 }
16351 break;
16352#endif
16353 }
16354 }
16355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016356 strval = get_tv_string_chk(&argvars[1]);
16357 if (strval != NULL)
16358 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016360 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361}
16362
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016363/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016364 * "settabvar()" function
16365 */
16366 static void
16367f_settabvar(argvars, rettv)
16368 typval_T *argvars;
16369 typval_T *rettv;
16370{
16371 tabpage_T *save_curtab;
16372 char_u *varname, *tabvarname;
16373 typval_T *varp;
16374 tabpage_T *tp;
16375
16376 rettv->vval.v_number = 0;
16377
16378 if (check_restricted() || check_secure())
16379 return;
16380
16381 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16382 varname = get_tv_string_chk(&argvars[1]);
16383 varp = &argvars[2];
16384
16385 if (tp != NULL && varname != NULL && varp != NULL)
16386 {
16387 save_curtab = curtab;
16388 goto_tabpage_tp(tp);
16389
16390 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16391 if (tabvarname != NULL)
16392 {
16393 STRCPY(tabvarname, "t:");
16394 STRCPY(tabvarname + 2, varname);
16395 set_var(tabvarname, varp, TRUE);
16396 vim_free(tabvarname);
16397 }
16398
16399 /* Restore current tabpage */
16400 if (valid_tabpage(save_curtab))
16401 goto_tabpage_tp(save_curtab);
16402 }
16403}
16404
16405/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016406 * "settabwinvar()" function
16407 */
16408 static void
16409f_settabwinvar(argvars, rettv)
16410 typval_T *argvars;
16411 typval_T *rettv;
16412{
16413 setwinvar(argvars, rettv, 1);
16414}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415
16416/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016417 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016420f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016421 typval_T *argvars;
16422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016424 setwinvar(argvars, rettv, 0);
16425}
16426
16427/*
16428 * "setwinvar()" and "settabwinvar()" functions
16429 */
16430 static void
16431setwinvar(argvars, rettv, off)
16432 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016433 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016434 int off;
16435{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 win_T *win;
16437#ifdef FEAT_WINDOWS
16438 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016439 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440#endif
16441 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016442 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016444 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445
16446 if (check_restricted() || check_secure())
16447 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016448
16449#ifdef FEAT_WINDOWS
16450 if (off == 1)
16451 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16452 else
16453 tp = curtab;
16454#endif
16455 win = find_win_by_nr(&argvars[off], tp);
16456 varname = get_tv_string_chk(&argvars[off + 1]);
16457 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458
16459 if (win != NULL && varname != NULL && varp != NULL)
16460 {
16461#ifdef FEAT_WINDOWS
16462 /* set curwin to be our win, temporarily */
16463 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016464 save_curtab = curtab;
16465 goto_tabpage_tp(tp);
16466 if (!win_valid(win))
16467 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 curwin = win;
16469 curbuf = curwin->w_buffer;
16470#endif
16471
16472 if (*varname == '&')
16473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016474 long numval;
16475 char_u *strval;
16476 int error = FALSE;
16477
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 numval = get_tv_number_chk(varp, &error);
16480 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 if (!error && strval != NULL)
16482 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 }
16484 else
16485 {
16486 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16487 if (winvarname != NULL)
16488 {
16489 STRCPY(winvarname, "w:");
16490 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016491 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 vim_free(winvarname);
16493 }
16494 }
16495
16496#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016497 /* Restore current tabpage and window, if still valid (autocomands can
16498 * make them invalid). */
16499 if (valid_tabpage(save_curtab))
16500 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 if (win_valid(save_curwin))
16502 {
16503 curwin = save_curwin;
16504 curbuf = curwin->w_buffer;
16505 }
16506#endif
16507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508}
16509
16510/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016511 * "shellescape({string})" function
16512 */
16513 static void
16514f_shellescape(argvars, rettv)
16515 typval_T *argvars;
16516 typval_T *rettv;
16517{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016518 rettv->vval.v_string = vim_strsave_shellescape(
16519 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016520 rettv->v_type = VAR_STRING;
16521}
16522
16523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 */
16526 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 typval_T *argvars;
16529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533 p = get_tv_string(&argvars[0]);
16534 rettv->vval.v_string = vim_strsave(p);
16535 simplify_filename(rettv->vval.v_string); /* simplify in place */
16536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537}
16538
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016539#ifdef FEAT_FLOAT
16540/*
16541 * "sin()" function
16542 */
16543 static void
16544f_sin(argvars, rettv)
16545 typval_T *argvars;
16546 typval_T *rettv;
16547{
16548 float_T f;
16549
16550 rettv->v_type = VAR_FLOAT;
16551 if (get_float_arg(argvars, &f) == OK)
16552 rettv->vval.v_float = sin(f);
16553 else
16554 rettv->vval.v_float = 0.0;
16555}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016556
16557/*
16558 * "sinh()" function
16559 */
16560 static void
16561f_sinh(argvars, rettv)
16562 typval_T *argvars;
16563 typval_T *rettv;
16564{
16565 float_T f;
16566
16567 rettv->v_type = VAR_FLOAT;
16568 if (get_float_arg(argvars, &f) == OK)
16569 rettv->vval.v_float = sinh(f);
16570 else
16571 rettv->vval.v_float = 0.0;
16572}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016573#endif
16574
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575static int
16576#ifdef __BORLANDC__
16577 _RTLENTRYF
16578#endif
16579 item_compare __ARGS((const void *s1, const void *s2));
16580static int
16581#ifdef __BORLANDC__
16582 _RTLENTRYF
16583#endif
16584 item_compare2 __ARGS((const void *s1, const void *s2));
16585
16586static int item_compare_ic;
16587static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016588static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016589static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590#define ITEM_COMPARE_FAIL 999
16591
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016595 static int
16596#ifdef __BORLANDC__
16597_RTLENTRYF
16598#endif
16599item_compare(s1, s2)
16600 const void *s1;
16601 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016603 char_u *p1, *p2;
16604 char_u *tofree1, *tofree2;
16605 int res;
16606 char_u numbuf1[NUMBUFLEN];
16607 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016609 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16610 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016611 if (p1 == NULL)
16612 p1 = (char_u *)"";
16613 if (p2 == NULL)
16614 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016615 if (item_compare_ic)
16616 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618 res = STRCMP(p1, p2);
16619 vim_free(tofree1);
16620 vim_free(tofree2);
16621 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622}
16623
16624 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625#ifdef __BORLANDC__
16626_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016628item_compare2(s1, s2)
16629 const void *s1;
16630 const void *s2;
16631{
16632 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016633 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016634 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016635 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016637 /* shortcut after failure in previous call; compare all items equal */
16638 if (item_compare_func_err)
16639 return 0;
16640
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016641 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16642 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16644 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645
16646 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016647 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016648 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16649 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 clear_tv(&argv[0]);
16651 clear_tv(&argv[1]);
16652
16653 if (res == FAIL)
16654 res = ITEM_COMPARE_FAIL;
16655 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016656 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16657 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016658 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 clear_tv(&rettv);
16660 return res;
16661}
16662
16663/*
16664 * "sort({list})" function
16665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016667f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016668 typval_T *argvars;
16669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670{
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 list_T *l;
16672 listitem_T *li;
16673 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016674 long len;
16675 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676
Bram Moolenaar0d660222005-01-07 21:51:51 +000016677 if (argvars[0].v_type != VAR_LIST)
16678 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 else
16680 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016681 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016682 if (l == NULL || tv_check_lock(l->lv_lock,
16683 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 return;
16685 rettv->vval.v_list = l;
16686 rettv->v_type = VAR_LIST;
16687 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688
Bram Moolenaar0d660222005-01-07 21:51:51 +000016689 len = list_len(l);
16690 if (len <= 1)
16691 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692
Bram Moolenaar0d660222005-01-07 21:51:51 +000016693 item_compare_ic = FALSE;
16694 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016695 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016696 if (argvars[1].v_type != VAR_UNKNOWN)
16697 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016698 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016701 else
16702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 int error = FALSE;
16704
16705 i = get_tv_number_chk(&argvars[1], &error);
16706 if (error)
16707 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708 if (i == 1)
16709 item_compare_ic = TRUE;
16710 else
16711 item_compare_func = get_tv_string(&argvars[1]);
16712 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016713
16714 if (argvars[2].v_type != VAR_UNKNOWN)
16715 {
16716 /* optional third argument: {dict} */
16717 if (argvars[2].v_type != VAR_DICT)
16718 {
16719 EMSG(_(e_dictreq));
16720 return;
16721 }
16722 item_compare_selfdict = argvars[2].vval.v_dict;
16723 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016727 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728 if (ptrs == NULL)
16729 return;
16730 i = 0;
16731 for (li = l->lv_first; li != NULL; li = li->li_next)
16732 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016734 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 /* test the compare function */
16736 if (item_compare_func != NULL
16737 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16738 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016739 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016741 {
16742 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016744 item_compare_func == NULL ? item_compare : item_compare2);
16745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016746 if (!item_compare_func_err)
16747 {
16748 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016749 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 l->lv_len = 0;
16751 for (i = 0; i < len; ++i)
16752 list_append(l, ptrs[i]);
16753 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 }
16755
16756 vim_free(ptrs);
16757 }
16758}
16759
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016760/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016761 * "soundfold({word})" function
16762 */
16763 static void
16764f_soundfold(argvars, rettv)
16765 typval_T *argvars;
16766 typval_T *rettv;
16767{
16768 char_u *s;
16769
16770 rettv->v_type = VAR_STRING;
16771 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016772#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016773 rettv->vval.v_string = eval_soundfold(s);
16774#else
16775 rettv->vval.v_string = vim_strsave(s);
16776#endif
16777}
16778
16779/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016780 * "spellbadword()" function
16781 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016782 static void
16783f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016784 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016785 typval_T *rettv;
16786{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016787 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016788 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016789 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016792 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016793
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016794#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016795 if (argvars[0].v_type == VAR_UNKNOWN)
16796 {
16797 /* Find the start and length of the badly spelled word. */
16798 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16799 if (len != 0)
16800 word = ml_get_cursor();
16801 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016802 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016803 {
16804 char_u *str = get_tv_string_chk(&argvars[0]);
16805 int capcol = -1;
16806
16807 if (str != NULL)
16808 {
16809 /* Check the argument for spelling. */
16810 while (*str != NUL)
16811 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016812 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016813 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016814 {
16815 word = str;
16816 break;
16817 }
16818 str += len;
16819 }
16820 }
16821 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016822#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016823
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016824 list_append_string(rettv->vval.v_list, word, len);
16825 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016826 attr == HLF_SPB ? "bad" :
16827 attr == HLF_SPR ? "rare" :
16828 attr == HLF_SPL ? "local" :
16829 attr == HLF_SPC ? "caps" :
16830 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016831}
16832
16833/*
16834 * "spellsuggest()" function
16835 */
16836 static void
16837f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016838 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016839 typval_T *rettv;
16840{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016841#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016842 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016843 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016844 int maxcount;
16845 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016846 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016847 listitem_T *li;
16848 int need_capital = FALSE;
16849#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016850
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016851 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016852 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016853
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016854#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016855 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016856 {
16857 str = get_tv_string(&argvars[0]);
16858 if (argvars[1].v_type != VAR_UNKNOWN)
16859 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016860 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016861 if (maxcount <= 0)
16862 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016863 if (argvars[2].v_type != VAR_UNKNOWN)
16864 {
16865 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16866 if (typeerr)
16867 return;
16868 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016869 }
16870 else
16871 maxcount = 25;
16872
Bram Moolenaar4770d092006-01-12 23:22:24 +000016873 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016874
16875 for (i = 0; i < ga.ga_len; ++i)
16876 {
16877 str = ((char_u **)ga.ga_data)[i];
16878
16879 li = listitem_alloc();
16880 if (li == NULL)
16881 vim_free(str);
16882 else
16883 {
16884 li->li_tv.v_type = VAR_STRING;
16885 li->li_tv.v_lock = 0;
16886 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016887 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016888 }
16889 }
16890 ga_clear(&ga);
16891 }
16892#endif
16893}
16894
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016896f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 typval_T *argvars;
16898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899{
16900 char_u *str;
16901 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016902 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903 regmatch_T regmatch;
16904 char_u patbuf[NUMBUFLEN];
16905 char_u *save_cpo;
16906 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016908 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016909 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016910
16911 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16912 save_cpo = p_cpo;
16913 p_cpo = (char_u *)"";
16914
16915 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016916 if (argvars[1].v_type != VAR_UNKNOWN)
16917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016918 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16919 if (pat == NULL)
16920 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016923 }
16924 if (pat == NULL || *pat == NUL)
16925 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016929 if (typeerr)
16930 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16933 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016936 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016938 if (*str == NUL)
16939 match = FALSE; /* empty item at the end */
16940 else
16941 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 if (match)
16943 end = regmatch.startp[0];
16944 else
16945 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016946 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16947 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016949 if (list_append_string(rettv->vval.v_list, str,
16950 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 }
16953 if (!match)
16954 break;
16955 /* Advance to just after the match. */
16956 if (regmatch.endp[0] > str)
16957 col = 0;
16958 else
16959 {
16960 /* Don't get stuck at the same match. */
16961#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016962 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963#else
16964 col = 1;
16965#endif
16966 }
16967 str = regmatch.endp[0];
16968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974}
16975
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016976#ifdef FEAT_FLOAT
16977/*
16978 * "sqrt()" function
16979 */
16980 static void
16981f_sqrt(argvars, rettv)
16982 typval_T *argvars;
16983 typval_T *rettv;
16984{
16985 float_T f;
16986
16987 rettv->v_type = VAR_FLOAT;
16988 if (get_float_arg(argvars, &f) == OK)
16989 rettv->vval.v_float = sqrt(f);
16990 else
16991 rettv->vval.v_float = 0.0;
16992}
16993
16994/*
16995 * "str2float()" function
16996 */
16997 static void
16998f_str2float(argvars, rettv)
16999 typval_T *argvars;
17000 typval_T *rettv;
17001{
17002 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17003
17004 if (*p == '+')
17005 p = skipwhite(p + 1);
17006 (void)string2float(p, &rettv->vval.v_float);
17007 rettv->v_type = VAR_FLOAT;
17008}
17009#endif
17010
Bram Moolenaar2c932302006-03-18 21:42:09 +000017011/*
17012 * "str2nr()" function
17013 */
17014 static void
17015f_str2nr(argvars, rettv)
17016 typval_T *argvars;
17017 typval_T *rettv;
17018{
17019 int base = 10;
17020 char_u *p;
17021 long n;
17022
17023 if (argvars[1].v_type != VAR_UNKNOWN)
17024 {
17025 base = get_tv_number(&argvars[1]);
17026 if (base != 8 && base != 10 && base != 16)
17027 {
17028 EMSG(_(e_invarg));
17029 return;
17030 }
17031 }
17032
17033 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017034 if (*p == '+')
17035 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017036 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17037 rettv->vval.v_number = n;
17038}
17039
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040#ifdef HAVE_STRFTIME
17041/*
17042 * "strftime({format}[, {time}])" function
17043 */
17044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 typval_T *argvars;
17047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048{
17049 char_u result_buf[256];
17050 struct tm *curtime;
17051 time_t seconds;
17052 char_u *p;
17053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017057 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 seconds = time(NULL);
17059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 curtime = localtime(&seconds);
17062 /* MSVC returns NULL for an invalid value of seconds. */
17063 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 else
17066 {
17067# ifdef FEAT_MBYTE
17068 vimconv_T conv;
17069 char_u *enc;
17070
17071 conv.vc_type = CONV_NONE;
17072 enc = enc_locale();
17073 convert_setup(&conv, p_enc, enc);
17074 if (conv.vc_type != CONV_NONE)
17075 p = string_convert(&conv, p, NULL);
17076# endif
17077 if (p != NULL)
17078 (void)strftime((char *)result_buf, sizeof(result_buf),
17079 (char *)p, curtime);
17080 else
17081 result_buf[0] = NUL;
17082
17083# ifdef FEAT_MBYTE
17084 if (conv.vc_type != CONV_NONE)
17085 vim_free(p);
17086 convert_setup(&conv, enc, p_enc);
17087 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017088 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 else
17090# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017091 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092
17093# ifdef FEAT_MBYTE
17094 /* Release conversion descriptors */
17095 convert_setup(&conv, NULL, NULL);
17096 vim_free(enc);
17097# endif
17098 }
17099}
17100#endif
17101
17102/*
17103 * "stridx()" function
17104 */
17105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017106f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 typval_T *argvars;
17108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109{
17110 char_u buf[NUMBUFLEN];
17111 char_u *needle;
17112 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017117 needle = get_tv_string_chk(&argvars[1]);
17118 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017119 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 if (needle == NULL || haystack == NULL)
17121 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 if (argvars[2].v_type != VAR_UNKNOWN)
17124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017125 int error = FALSE;
17126
17127 start_idx = get_tv_number_chk(&argvars[2], &error);
17128 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017130 if (start_idx >= 0)
17131 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 }
17133
17134 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17135 if (pos != NULL)
17136 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137}
17138
17139/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017140 * "string()" function
17141 */
17142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 typval_T *argvars;
17145 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017146{
17147 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017151 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017152 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017153 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155}
17156
17157/*
17158 * "strlen()" function
17159 */
17160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017161f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017162 typval_T *argvars;
17163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 rettv->vval.v_number = (varnumber_T)(STRLEN(
17166 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167}
17168
17169/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017170 * "strchars()" function
17171 */
17172 static void
17173f_strchars(argvars, rettv)
17174 typval_T *argvars;
17175 typval_T *rettv;
17176{
17177 char_u *s = get_tv_string(&argvars[0]);
17178#ifdef FEAT_MBYTE
17179 varnumber_T len = 0;
17180
17181 while (*s != NUL)
17182 {
17183 mb_cptr2char_adv(&s);
17184 ++len;
17185 }
17186 rettv->vval.v_number = len;
17187#else
17188 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17189#endif
17190}
17191
17192/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017193 * "strdisplaywidth()" function
17194 */
17195 static void
17196f_strdisplaywidth(argvars, rettv)
17197 typval_T *argvars;
17198 typval_T *rettv;
17199{
17200 char_u *s = get_tv_string(&argvars[0]);
17201 int col = 0;
17202
17203 if (argvars[1].v_type != VAR_UNKNOWN)
17204 col = get_tv_number(&argvars[1]);
17205
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017206 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017207}
17208
17209/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017210 * "strwidth()" function
17211 */
17212 static void
17213f_strwidth(argvars, rettv)
17214 typval_T *argvars;
17215 typval_T *rettv;
17216{
17217 char_u *s = get_tv_string(&argvars[0]);
17218
17219 rettv->vval.v_number = (varnumber_T)(
17220#ifdef FEAT_MBYTE
17221 mb_string2cells(s, -1)
17222#else
17223 STRLEN(s)
17224#endif
17225 );
17226}
17227
17228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 * "strpart()" function
17230 */
17231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017232f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017233 typval_T *argvars;
17234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235{
17236 char_u *p;
17237 int n;
17238 int len;
17239 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017242 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 slen = (int)STRLEN(p);
17244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 n = get_tv_number_chk(&argvars[1], &error);
17246 if (error)
17247 len = 0;
17248 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017249 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 else
17251 len = slen - n; /* default len: all bytes that are available. */
17252
17253 /*
17254 * Only return the overlap between the specified part and the actual
17255 * string.
17256 */
17257 if (n < 0)
17258 {
17259 len += n;
17260 n = 0;
17261 }
17262 else if (n > slen)
17263 n = slen;
17264 if (len < 0)
17265 len = 0;
17266 else if (n + len > slen)
17267 len = slen - n;
17268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269 rettv->v_type = VAR_STRING;
17270 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271}
17272
17273/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274 * "strridx()" function
17275 */
17276 static void
17277f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017278 typval_T *argvars;
17279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280{
17281 char_u buf[NUMBUFLEN];
17282 char_u *needle;
17283 char_u *haystack;
17284 char_u *rest;
17285 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017286 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017288 needle = get_tv_string_chk(&argvars[1]);
17289 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290
17291 rettv->vval.v_number = -1;
17292 if (needle == NULL || haystack == NULL)
17293 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017294
17295 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017296 if (argvars[2].v_type != VAR_UNKNOWN)
17297 {
17298 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017300 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017301 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017302 }
17303 else
17304 end_idx = haystack_len;
17305
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017307 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017309 lastmatch = haystack + end_idx;
17310 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017311 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017312 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 for (rest = haystack; *rest != '\0'; ++rest)
17314 {
17315 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017316 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 break;
17318 lastmatch = rest;
17319 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017320 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321
17322 if (lastmatch == NULL)
17323 rettv->vval.v_number = -1;
17324 else
17325 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17326}
17327
17328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 * "strtrans()" function
17330 */
17331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 typval_T *argvars;
17334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336 rettv->v_type = VAR_STRING;
17337 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338}
17339
17340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017341 * "submatch()" function
17342 */
17343 static void
17344f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017345 typval_T *argvars;
17346 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017347{
17348 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 rettv->vval.v_string =
17350 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351}
17352
17353/*
17354 * "substitute()" function
17355 */
17356 static void
17357f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 typval_T *argvars;
17359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017360{
17361 char_u patbuf[NUMBUFLEN];
17362 char_u subbuf[NUMBUFLEN];
17363 char_u flagsbuf[NUMBUFLEN];
17364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 char_u *str = get_tv_string_chk(&argvars[0]);
17366 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17367 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17368 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17369
Bram Moolenaar0d660222005-01-07 21:51:51 +000017370 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17372 rettv->vval.v_string = NULL;
17373 else
17374 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375}
17376
17377/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017378 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384{
17385 int id = 0;
17386#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017387 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 long col;
17389 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017390 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017392 lnum = get_tv_lnum(argvars); /* -1 on type error */
17393 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17394 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017396 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017397 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017398 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399#endif
17400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402}
17403
17404/*
17405 * "synIDattr(id, what [, mode])" function
17406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
17412 char_u *p = NULL;
17413#ifdef FEAT_SYN_HL
17414 int id;
17415 char_u *what;
17416 char_u *mode;
17417 char_u modebuf[NUMBUFLEN];
17418 int modec;
17419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420 id = get_tv_number(&argvars[0]);
17421 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017426 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 modec = 0; /* replace invalid with current */
17428 }
17429 else
17430 {
17431#ifdef FEAT_GUI
17432 if (gui.in_use)
17433 modec = 'g';
17434 else
17435#endif
17436 if (t_colors > 1)
17437 modec = 'c';
17438 else
17439 modec = 't';
17440 }
17441
17442
17443 switch (TOLOWER_ASC(what[0]))
17444 {
17445 case 'b':
17446 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17447 p = highlight_color(id, what, modec);
17448 else /* bold */
17449 p = highlight_has_attr(id, HL_BOLD, modec);
17450 break;
17451
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017452 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 p = highlight_color(id, what, modec);
17454 break;
17455
17456 case 'i':
17457 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17458 p = highlight_has_attr(id, HL_INVERSE, modec);
17459 else /* italic */
17460 p = highlight_has_attr(id, HL_ITALIC, modec);
17461 break;
17462
17463 case 'n': /* name */
17464 p = get_highlight_name(NULL, id - 1);
17465 break;
17466
17467 case 'r': /* reverse */
17468 p = highlight_has_attr(id, HL_INVERSE, modec);
17469 break;
17470
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017471 case 's':
17472 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17473 p = highlight_color(id, what, modec);
17474 else /* standout */
17475 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 break;
17477
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017478 case 'u':
17479 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17480 /* underline */
17481 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17482 else
17483 /* undercurl */
17484 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 break;
17486 }
17487
17488 if (p != NULL)
17489 p = vim_strsave(p);
17490#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491 rettv->v_type = VAR_STRING;
17492 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493}
17494
17495/*
17496 * "synIDtrans(id)" function
17497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017500 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502{
17503 int id;
17504
17505#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017506 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
17508 if (id > 0)
17509 id = syn_get_final_id(id);
17510 else
17511#endif
17512 id = 0;
17513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515}
17516
17517/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017518 * "synconcealed(lnum, col)" function
17519 */
17520 static void
17521f_synconcealed(argvars, rettv)
17522 typval_T *argvars UNUSED;
17523 typval_T *rettv;
17524{
17525#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17526 long lnum;
17527 long col;
17528 int syntax_flags = 0;
17529 int cchar;
17530 int matchid = 0;
17531 char_u str[NUMBUFLEN];
17532#endif
17533
17534 rettv->v_type = VAR_LIST;
17535 rettv->vval.v_list = NULL;
17536
17537#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17538 lnum = get_tv_lnum(argvars); /* -1 on type error */
17539 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17540
17541 vim_memset(str, NUL, sizeof(str));
17542
17543 if (rettv_list_alloc(rettv) != FAIL)
17544 {
17545 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17546 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17547 && curwin->w_p_cole > 0)
17548 {
17549 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17550 syntax_flags = get_syntax_info(&matchid);
17551
17552 /* get the conceal character */
17553 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17554 {
17555 cchar = syn_get_sub_char();
17556 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17557 cchar = lcs_conceal;
17558 if (cchar != NUL)
17559 {
17560# ifdef FEAT_MBYTE
17561 if (has_mbyte)
17562 (*mb_char2bytes)(cchar, str);
17563 else
17564# endif
17565 str[0] = cchar;
17566 }
17567 }
17568 }
17569
17570 list_append_number(rettv->vval.v_list,
17571 (syntax_flags & HL_CONCEAL) != 0);
17572 /* -1 to auto-determine strlen */
17573 list_append_string(rettv->vval.v_list, str, -1);
17574 list_append_number(rettv->vval.v_list, matchid);
17575 }
17576#endif
17577}
17578
17579/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017580 * "synstack(lnum, col)" function
17581 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017582 static void
17583f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017584 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017585 typval_T *rettv;
17586{
17587#ifdef FEAT_SYN_HL
17588 long lnum;
17589 long col;
17590 int i;
17591 int id;
17592#endif
17593
17594 rettv->v_type = VAR_LIST;
17595 rettv->vval.v_list = NULL;
17596
17597#ifdef FEAT_SYN_HL
17598 lnum = get_tv_lnum(argvars); /* -1 on type error */
17599 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17600
17601 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017602 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017603 && rettv_list_alloc(rettv) != FAIL)
17604 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017605 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017606 for (i = 0; ; ++i)
17607 {
17608 id = syn_get_stack_item(i);
17609 if (id < 0)
17610 break;
17611 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17612 break;
17613 }
17614 }
17615#endif
17616}
17617
17618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 * "system()" function
17620 */
17621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 typval_T *argvars;
17624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017626 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017628 char_u *infile = NULL;
17629 char_u buf[NUMBUFLEN];
17630 int err = FALSE;
17631 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017633 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017634 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017635
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017637 {
17638 /*
17639 * Write the string to a temp file, to be used for input of the shell
17640 * command.
17641 */
17642 if ((infile = vim_tempname('i')) == NULL)
17643 {
17644 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017645 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017646 }
17647
17648 fd = mch_fopen((char *)infile, WRITEBIN);
17649 if (fd == NULL)
17650 {
17651 EMSG2(_(e_notopen), infile);
17652 goto done;
17653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017654 p = get_tv_string_buf_chk(&argvars[1], buf);
17655 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017656 {
17657 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017658 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017659 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017660 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17661 err = TRUE;
17662 if (fclose(fd) != 0)
17663 err = TRUE;
17664 if (err)
17665 {
17666 EMSG(_("E677: Error writing temp file"));
17667 goto done;
17668 }
17669 }
17670
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017671 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17672 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017673
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674#ifdef USE_CR
17675 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017676 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 {
17678 char_u *s;
17679
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017680 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 {
17682 if (*s == CAR)
17683 *s = NL;
17684 }
17685 }
17686#else
17687# ifdef USE_CRNL
17688 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017689 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 {
17691 char_u *s, *d;
17692
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017693 d = res;
17694 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 {
17696 if (s[0] == CAR && s[1] == NL)
17697 ++s;
17698 *d++ = *s;
17699 }
17700 *d = NUL;
17701 }
17702# endif
17703#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017704
17705done:
17706 if (infile != NULL)
17707 {
17708 mch_remove(infile);
17709 vim_free(infile);
17710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711 rettv->v_type = VAR_STRING;
17712 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713}
17714
17715/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017716 * "tabpagebuflist()" function
17717 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017718 static void
17719f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017720 typval_T *argvars UNUSED;
17721 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017722{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017723#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017724 tabpage_T *tp;
17725 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017726
17727 if (argvars[0].v_type == VAR_UNKNOWN)
17728 wp = firstwin;
17729 else
17730 {
17731 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17732 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017733 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017734 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017735 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017736 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017737 for (; wp != NULL; wp = wp->w_next)
17738 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017739 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017740 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017741 }
17742#endif
17743}
17744
17745
17746/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017747 * "tabpagenr()" function
17748 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017749 static void
17750f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017751 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017752 typval_T *rettv;
17753{
17754 int nr = 1;
17755#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017756 char_u *arg;
17757
17758 if (argvars[0].v_type != VAR_UNKNOWN)
17759 {
17760 arg = get_tv_string_chk(&argvars[0]);
17761 nr = 0;
17762 if (arg != NULL)
17763 {
17764 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017765 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017766 else
17767 EMSG2(_(e_invexpr2), arg);
17768 }
17769 }
17770 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017771 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017772#endif
17773 rettv->vval.v_number = nr;
17774}
17775
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017776
17777#ifdef FEAT_WINDOWS
17778static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17779
17780/*
17781 * Common code for tabpagewinnr() and winnr().
17782 */
17783 static int
17784get_winnr(tp, argvar)
17785 tabpage_T *tp;
17786 typval_T *argvar;
17787{
17788 win_T *twin;
17789 int nr = 1;
17790 win_T *wp;
17791 char_u *arg;
17792
17793 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17794 if (argvar->v_type != VAR_UNKNOWN)
17795 {
17796 arg = get_tv_string_chk(argvar);
17797 if (arg == NULL)
17798 nr = 0; /* type error; errmsg already given */
17799 else if (STRCMP(arg, "$") == 0)
17800 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17801 else if (STRCMP(arg, "#") == 0)
17802 {
17803 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17804 if (twin == NULL)
17805 nr = 0;
17806 }
17807 else
17808 {
17809 EMSG2(_(e_invexpr2), arg);
17810 nr = 0;
17811 }
17812 }
17813
17814 if (nr > 0)
17815 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17816 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017817 {
17818 if (wp == NULL)
17819 {
17820 /* didn't find it in this tabpage */
17821 nr = 0;
17822 break;
17823 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017824 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017825 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017826 return nr;
17827}
17828#endif
17829
17830/*
17831 * "tabpagewinnr()" function
17832 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017833 static void
17834f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017835 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017836 typval_T *rettv;
17837{
17838 int nr = 1;
17839#ifdef FEAT_WINDOWS
17840 tabpage_T *tp;
17841
17842 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17843 if (tp == NULL)
17844 nr = 0;
17845 else
17846 nr = get_winnr(tp, &argvars[1]);
17847#endif
17848 rettv->vval.v_number = nr;
17849}
17850
17851
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017852/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017853 * "tagfiles()" function
17854 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017855 static void
17856f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017857 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017858 typval_T *rettv;
17859{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017860 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017861 tagname_T tn;
17862 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017863
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017864 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017865 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017866 fname = alloc(MAXPATHL);
17867 if (fname == NULL)
17868 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017869
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017870 for (first = TRUE; ; first = FALSE)
17871 if (get_tagfname(&tn, first, fname) == FAIL
17872 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017873 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017874 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017875 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017876}
17877
17878/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017879 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017880 */
17881 static void
17882f_taglist(argvars, rettv)
17883 typval_T *argvars;
17884 typval_T *rettv;
17885{
17886 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017887
17888 tag_pattern = get_tv_string(&argvars[0]);
17889
17890 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017891 if (*tag_pattern == NUL)
17892 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017894 if (rettv_list_alloc(rettv) == OK)
17895 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017896}
17897
17898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 * "tempname()" function
17900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017902f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905{
17906 static int x = 'A';
17907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017908 rettv->v_type = VAR_STRING;
17909 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910
17911 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17912 * names. Skip 'I' and 'O', they are used for shell redirection. */
17913 do
17914 {
17915 if (x == 'Z')
17916 x = '0';
17917 else if (x == '9')
17918 x = 'A';
17919 else
17920 {
17921#ifdef EBCDIC
17922 if (x == 'I')
17923 x = 'J';
17924 else if (x == 'R')
17925 x = 'S';
17926 else
17927#endif
17928 ++x;
17929 }
17930 } while (x == 'I' || x == 'O');
17931}
17932
17933/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017934 * "test(list)" function: Just checking the walls...
17935 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017936 static void
17937f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017938 typval_T *argvars UNUSED;
17939 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017940{
17941 /* Used for unit testing. Change the code below to your liking. */
17942#if 0
17943 listitem_T *li;
17944 list_T *l;
17945 char_u *bad, *good;
17946
17947 if (argvars[0].v_type != VAR_LIST)
17948 return;
17949 l = argvars[0].vval.v_list;
17950 if (l == NULL)
17951 return;
17952 li = l->lv_first;
17953 if (li == NULL)
17954 return;
17955 bad = get_tv_string(&li->li_tv);
17956 li = li->li_next;
17957 if (li == NULL)
17958 return;
17959 good = get_tv_string(&li->li_tv);
17960 rettv->vval.v_number = test_edit_score(bad, good);
17961#endif
17962}
17963
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017964#ifdef FEAT_FLOAT
17965/*
17966 * "tan()" function
17967 */
17968 static void
17969f_tan(argvars, rettv)
17970 typval_T *argvars;
17971 typval_T *rettv;
17972{
17973 float_T f;
17974
17975 rettv->v_type = VAR_FLOAT;
17976 if (get_float_arg(argvars, &f) == OK)
17977 rettv->vval.v_float = tan(f);
17978 else
17979 rettv->vval.v_float = 0.0;
17980}
17981
17982/*
17983 * "tanh()" function
17984 */
17985 static void
17986f_tanh(argvars, rettv)
17987 typval_T *argvars;
17988 typval_T *rettv;
17989{
17990 float_T f;
17991
17992 rettv->v_type = VAR_FLOAT;
17993 if (get_float_arg(argvars, &f) == OK)
17994 rettv->vval.v_float = tanh(f);
17995 else
17996 rettv->vval.v_float = 0.0;
17997}
17998#endif
17999
Bram Moolenaard52d9742005-08-21 22:20:28 +000018000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 * "tolower(string)" function
18002 */
18003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 typval_T *argvars;
18006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007{
18008 char_u *p;
18009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018010 p = vim_strsave(get_tv_string(&argvars[0]));
18011 rettv->v_type = VAR_STRING;
18012 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013
18014 if (p != NULL)
18015 while (*p != NUL)
18016 {
18017#ifdef FEAT_MBYTE
18018 int l;
18019
18020 if (enc_utf8)
18021 {
18022 int c, lc;
18023
18024 c = utf_ptr2char(p);
18025 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018026 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 /* TODO: reallocate string when byte count changes. */
18028 if (utf_char2len(lc) == l)
18029 utf_char2bytes(lc, p);
18030 p += l;
18031 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018032 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 p += l; /* skip multi-byte character */
18034 else
18035#endif
18036 {
18037 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18038 ++p;
18039 }
18040 }
18041}
18042
18043/*
18044 * "toupper(string)" function
18045 */
18046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018047f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 typval_T *argvars;
18049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018052 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053}
18054
18055/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018056 * "tr(string, fromstr, tostr)" function
18057 */
18058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018060 typval_T *argvars;
18061 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018062{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018063 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018064 char_u *fromstr;
18065 char_u *tostr;
18066 char_u *p;
18067#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018068 int inlen;
18069 int fromlen;
18070 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018071 int idx;
18072 char_u *cpstr;
18073 int cplen;
18074 int first = TRUE;
18075#endif
18076 char_u buf[NUMBUFLEN];
18077 char_u buf2[NUMBUFLEN];
18078 garray_T ga;
18079
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018080 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018081 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18082 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018083
18084 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 rettv->v_type = VAR_STRING;
18086 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018087 if (fromstr == NULL || tostr == NULL)
18088 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018089 ga_init2(&ga, (int)sizeof(char), 80);
18090
18091#ifdef FEAT_MBYTE
18092 if (!has_mbyte)
18093#endif
18094 /* not multi-byte: fromstr and tostr must be the same length */
18095 if (STRLEN(fromstr) != STRLEN(tostr))
18096 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018097#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018098error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018099#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018100 EMSG2(_(e_invarg2), fromstr);
18101 ga_clear(&ga);
18102 return;
18103 }
18104
18105 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018106 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018107 {
18108#ifdef FEAT_MBYTE
18109 if (has_mbyte)
18110 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018111 inlen = (*mb_ptr2len)(in_str);
18112 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018113 cplen = inlen;
18114 idx = 0;
18115 for (p = fromstr; *p != NUL; p += fromlen)
18116 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018117 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018118 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018119 {
18120 for (p = tostr; *p != NUL; p += tolen)
18121 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018122 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018123 if (idx-- == 0)
18124 {
18125 cplen = tolen;
18126 cpstr = p;
18127 break;
18128 }
18129 }
18130 if (*p == NUL) /* tostr is shorter than fromstr */
18131 goto error;
18132 break;
18133 }
18134 ++idx;
18135 }
18136
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018137 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018138 {
18139 /* Check that fromstr and tostr have the same number of
18140 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018141 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018142 first = FALSE;
18143 for (p = tostr; *p != NUL; p += tolen)
18144 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018145 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018146 --idx;
18147 }
18148 if (idx != 0)
18149 goto error;
18150 }
18151
18152 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018153 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018154 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018155
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018156 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018157 }
18158 else
18159#endif
18160 {
18161 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018162 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018163 if (p != NULL)
18164 ga_append(&ga, tostr[p - fromstr]);
18165 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018166 ga_append(&ga, *in_str);
18167 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018168 }
18169 }
18170
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018171 /* add a terminating NUL */
18172 ga_grow(&ga, 1);
18173 ga_append(&ga, NUL);
18174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018175 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018176}
18177
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018178#ifdef FEAT_FLOAT
18179/*
18180 * "trunc({float})" function
18181 */
18182 static void
18183f_trunc(argvars, rettv)
18184 typval_T *argvars;
18185 typval_T *rettv;
18186{
18187 float_T f;
18188
18189 rettv->v_type = VAR_FLOAT;
18190 if (get_float_arg(argvars, &f) == OK)
18191 /* trunc() is not in C90, use floor() or ceil() instead. */
18192 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18193 else
18194 rettv->vval.v_float = 0.0;
18195}
18196#endif
18197
Bram Moolenaar8299df92004-07-10 09:47:34 +000018198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 * "type(expr)" function
18200 */
18201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018203 typval_T *argvars;
18204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018206 int n;
18207
18208 switch (argvars[0].v_type)
18209 {
18210 case VAR_NUMBER: n = 0; break;
18211 case VAR_STRING: n = 1; break;
18212 case VAR_FUNC: n = 2; break;
18213 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018214 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018215#ifdef FEAT_FLOAT
18216 case VAR_FLOAT: n = 5; break;
18217#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018218 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18219 }
18220 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221}
18222
18223/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018224 * "undofile(name)" function
18225 */
18226 static void
18227f_undofile(argvars, rettv)
18228 typval_T *argvars;
18229 typval_T *rettv;
18230{
18231 rettv->v_type = VAR_STRING;
18232#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018233 {
18234 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18235
18236 if (ffname != NULL)
18237 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18238 vim_free(ffname);
18239 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018240#else
18241 rettv->vval.v_string = NULL;
18242#endif
18243}
18244
18245/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018246 * "undotree()" function
18247 */
18248 static void
18249f_undotree(argvars, rettv)
18250 typval_T *argvars UNUSED;
18251 typval_T *rettv;
18252{
18253 if (rettv_dict_alloc(rettv) == OK)
18254 {
18255 dict_T *dict = rettv->vval.v_dict;
18256 list_T *list;
18257
Bram Moolenaar730cde92010-06-27 05:18:54 +020018258 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018259 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018260 dict_add_nr_str(dict, "save_last",
18261 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018262 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18263 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018264 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018265
18266 list = list_alloc();
18267 if (list != NULL)
18268 {
18269 u_eval_tree(curbuf->b_u_oldhead, list);
18270 dict_add_list(dict, "entries", list);
18271 }
18272 }
18273}
18274
18275/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018276 * "values(dict)" function
18277 */
18278 static void
18279f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018280 typval_T *argvars;
18281 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018282{
18283 dict_list(argvars, rettv, 1);
18284}
18285
18286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 * "virtcol(string)" function
18288 */
18289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018290f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018291 typval_T *argvars;
18292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293{
18294 colnr_T vcol = 0;
18295 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018296 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018298 fp = var2fpos(&argvars[0], FALSE, &fnum);
18299 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18300 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 {
18302 getvvcol(curwin, fp, NULL, NULL, &vcol);
18303 ++vcol;
18304 }
18305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307}
18308
18309/*
18310 * "visualmode()" function
18311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018314 typval_T *argvars UNUSED;
18315 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316{
18317#ifdef FEAT_VISUAL
18318 char_u str[2];
18319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 str[0] = curbuf->b_visual_mode_eval;
18322 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018323 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324
18325 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018326 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328#endif
18329}
18330
18331/*
18332 * "winbufnr(nr)" function
18333 */
18334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018335f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018336 typval_T *argvars;
18337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338{
18339 win_T *wp;
18340
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018341 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018343 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018345 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346}
18347
18348/*
18349 * "wincol()" function
18350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018352f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018353 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355{
18356 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018357 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358}
18359
18360/*
18361 * "winheight(nr)" function
18362 */
18363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018365 typval_T *argvars;
18366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367{
18368 win_T *wp;
18369
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018370 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018374 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375}
18376
18377/*
18378 * "winline()" function
18379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384{
18385 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387}
18388
18389/*
18390 * "winnr()" function
18391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018393f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018394 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396{
18397 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018398
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018400 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403}
18404
18405/*
18406 * "winrestcmd()" function
18407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412{
18413#ifdef FEAT_WINDOWS
18414 win_T *wp;
18415 int winnr = 1;
18416 garray_T ga;
18417 char_u buf[50];
18418
18419 ga_init2(&ga, (int)sizeof(char), 70);
18420 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18421 {
18422 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18423 ga_concat(&ga, buf);
18424# ifdef FEAT_VERTSPLIT
18425 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18426 ga_concat(&ga, buf);
18427# endif
18428 ++winnr;
18429 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018430 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437}
18438
18439/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018440 * "winrestview()" function
18441 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018442 static void
18443f_winrestview(argvars, rettv)
18444 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018445 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018446{
18447 dict_T *dict;
18448
18449 if (argvars[0].v_type != VAR_DICT
18450 || (dict = argvars[0].vval.v_dict) == NULL)
18451 EMSG(_(e_invarg));
18452 else
18453 {
18454 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18455 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18456#ifdef FEAT_VIRTUALEDIT
18457 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18458#endif
18459 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018460 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018461
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018462 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018463#ifdef FEAT_DIFF
18464 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18465#endif
18466 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18467 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18468
18469 check_cursor();
18470 changed_cline_bef_curs();
18471 invalidate_botline();
18472 redraw_later(VALID);
18473
18474 if (curwin->w_topline == 0)
18475 curwin->w_topline = 1;
18476 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18477 curwin->w_topline = curbuf->b_ml.ml_line_count;
18478#ifdef FEAT_DIFF
18479 check_topfill(curwin, TRUE);
18480#endif
18481 }
18482}
18483
18484/*
18485 * "winsaveview()" function
18486 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018487 static void
18488f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018489 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018490 typval_T *rettv;
18491{
18492 dict_T *dict;
18493
Bram Moolenaara800b422010-06-27 01:15:55 +020018494 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018495 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018496 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018497
18498 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18499 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18500#ifdef FEAT_VIRTUALEDIT
18501 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18502#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018503 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018504 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18505
18506 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18507#ifdef FEAT_DIFF
18508 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18509#endif
18510 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18511 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18512}
18513
18514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 * "winwidth(nr)" function
18516 */
18517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018518f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018519 typval_T *argvars;
18520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521{
18522 win_T *wp;
18523
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018524 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018526 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 else
18528#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018531 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532#endif
18533}
18534
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018536 * "writefile()" function
18537 */
18538 static void
18539f_writefile(argvars, rettv)
18540 typval_T *argvars;
18541 typval_T *rettv;
18542{
18543 int binary = FALSE;
18544 char_u *fname;
18545 FILE *fd;
18546 listitem_T *li;
18547 char_u *s;
18548 int ret = 0;
18549 int c;
18550
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018551 if (check_restricted() || check_secure())
18552 return;
18553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018554 if (argvars[0].v_type != VAR_LIST)
18555 {
18556 EMSG2(_(e_listarg), "writefile()");
18557 return;
18558 }
18559 if (argvars[0].vval.v_list == NULL)
18560 return;
18561
18562 if (argvars[2].v_type != VAR_UNKNOWN
18563 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18564 binary = TRUE;
18565
18566 /* Always open the file in binary mode, library functions have a mind of
18567 * their own about CR-LF conversion. */
18568 fname = get_tv_string(&argvars[1]);
18569 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18570 {
18571 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18572 ret = -1;
18573 }
18574 else
18575 {
18576 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18577 li = li->li_next)
18578 {
18579 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18580 {
18581 if (*s == '\n')
18582 c = putc(NUL, fd);
18583 else
18584 c = putc(*s, fd);
18585 if (c == EOF)
18586 {
18587 ret = -1;
18588 break;
18589 }
18590 }
18591 if (!binary || li->li_next != NULL)
18592 if (putc('\n', fd) == EOF)
18593 {
18594 ret = -1;
18595 break;
18596 }
18597 if (ret < 0)
18598 {
18599 EMSG(_(e_write));
18600 break;
18601 }
18602 }
18603 fclose(fd);
18604 }
18605
18606 rettv->vval.v_number = ret;
18607}
18608
18609/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018610 * "xor(expr, expr)" function
18611 */
18612 static void
18613f_xor(argvars, rettv)
18614 typval_T *argvars;
18615 typval_T *rettv;
18616{
18617 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18618 ^ get_tv_number_chk(&argvars[1], NULL);
18619}
18620
18621
18622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018624 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 */
18626 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018627var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018629 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018630 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018632 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018634 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635
Bram Moolenaara5525202006-03-02 22:52:09 +000018636 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018637 if (varp->v_type == VAR_LIST)
18638 {
18639 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018640 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018641 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018642 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018643
18644 l = varp->vval.v_list;
18645 if (l == NULL)
18646 return NULL;
18647
18648 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018649 pos.lnum = list_find_nr(l, 0L, &error);
18650 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018651 return NULL; /* invalid line number */
18652
18653 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018654 pos.col = list_find_nr(l, 1L, &error);
18655 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018656 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018657 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018658
18659 /* We accept "$" for the column number: last column. */
18660 li = list_find(l, 1L);
18661 if (li != NULL && li->li_tv.v_type == VAR_STRING
18662 && li->li_tv.vval.v_string != NULL
18663 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18664 pos.col = len + 1;
18665
Bram Moolenaara5525202006-03-02 22:52:09 +000018666 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018667 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018668 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018669 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018670
Bram Moolenaara5525202006-03-02 22:52:09 +000018671#ifdef FEAT_VIRTUALEDIT
18672 /* Get the virtual offset. Defaults to zero. */
18673 pos.coladd = list_find_nr(l, 2L, &error);
18674 if (error)
18675 pos.coladd = 0;
18676#endif
18677
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018678 return &pos;
18679 }
18680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018681 name = get_tv_string_chk(varp);
18682 if (name == NULL)
18683 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018684 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018686#ifdef FEAT_VISUAL
18687 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18688 {
18689 if (VIsual_active)
18690 return &VIsual;
18691 return &curwin->w_cursor;
18692 }
18693#endif
18694 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018696 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18698 return NULL;
18699 return pp;
18700 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018701
18702#ifdef FEAT_VIRTUALEDIT
18703 pos.coladd = 0;
18704#endif
18705
Bram Moolenaar477933c2007-07-17 14:32:23 +000018706 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018707 {
18708 pos.col = 0;
18709 if (name[1] == '0') /* "w0": first visible line */
18710 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018711 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018712 pos.lnum = curwin->w_topline;
18713 return &pos;
18714 }
18715 else if (name[1] == '$') /* "w$": last visible line */
18716 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018717 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018718 pos.lnum = curwin->w_botline - 1;
18719 return &pos;
18720 }
18721 }
18722 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018724 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 {
18726 pos.lnum = curbuf->b_ml.ml_line_count;
18727 pos.col = 0;
18728 }
18729 else
18730 {
18731 pos.lnum = curwin->w_cursor.lnum;
18732 pos.col = (colnr_T)STRLEN(ml_get_curline());
18733 }
18734 return &pos;
18735 }
18736 return NULL;
18737}
18738
18739/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018740 * Convert list in "arg" into a position and optional file number.
18741 * When "fnump" is NULL there is no file number, only 3 items.
18742 * Note that the column is passed on as-is, the caller may want to decrement
18743 * it to use 1 for the first column.
18744 * Return FAIL when conversion is not possible, doesn't check the position for
18745 * validity.
18746 */
18747 static int
18748list2fpos(arg, posp, fnump)
18749 typval_T *arg;
18750 pos_T *posp;
18751 int *fnump;
18752{
18753 list_T *l = arg->vval.v_list;
18754 long i = 0;
18755 long n;
18756
Bram Moolenaarbde35262006-07-23 20:12:24 +000018757 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18758 * when "fnump" isn't NULL and "coladd" is optional. */
18759 if (arg->v_type != VAR_LIST
18760 || l == NULL
18761 || l->lv_len < (fnump == NULL ? 2 : 3)
18762 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018763 return FAIL;
18764
18765 if (fnump != NULL)
18766 {
18767 n = list_find_nr(l, i++, NULL); /* fnum */
18768 if (n < 0)
18769 return FAIL;
18770 if (n == 0)
18771 n = curbuf->b_fnum; /* current buffer */
18772 *fnump = n;
18773 }
18774
18775 n = list_find_nr(l, i++, NULL); /* lnum */
18776 if (n < 0)
18777 return FAIL;
18778 posp->lnum = n;
18779
18780 n = list_find_nr(l, i++, NULL); /* col */
18781 if (n < 0)
18782 return FAIL;
18783 posp->col = n;
18784
18785#ifdef FEAT_VIRTUALEDIT
18786 n = list_find_nr(l, i, NULL);
18787 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018788 posp->coladd = 0;
18789 else
18790 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018791#endif
18792
18793 return OK;
18794}
18795
18796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 * Get the length of an environment variable name.
18798 * Advance "arg" to the first character after the name.
18799 * Return 0 for error.
18800 */
18801 static int
18802get_env_len(arg)
18803 char_u **arg;
18804{
18805 char_u *p;
18806 int len;
18807
18808 for (p = *arg; vim_isIDc(*p); ++p)
18809 ;
18810 if (p == *arg) /* no name found */
18811 return 0;
18812
18813 len = (int)(p - *arg);
18814 *arg = p;
18815 return len;
18816}
18817
18818/*
18819 * Get the length of the name of a function or internal variable.
18820 * "arg" is advanced to the first non-white character after the name.
18821 * Return 0 if something is wrong.
18822 */
18823 static int
18824get_id_len(arg)
18825 char_u **arg;
18826{
18827 char_u *p;
18828 int len;
18829
18830 /* Find the end of the name. */
18831 for (p = *arg; eval_isnamec(*p); ++p)
18832 ;
18833 if (p == *arg) /* no name found */
18834 return 0;
18835
18836 len = (int)(p - *arg);
18837 *arg = skipwhite(p);
18838
18839 return len;
18840}
18841
18842/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018843 * Get the length of the name of a variable or function.
18844 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018846 * Return -1 if curly braces expansion failed.
18847 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 * If the name contains 'magic' {}'s, expand them and return the
18849 * expanded name in an allocated string via 'alias' - caller must free.
18850 */
18851 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018852get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 char_u **arg;
18854 char_u **alias;
18855 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018856 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857{
18858 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 char_u *p;
18860 char_u *expr_start;
18861 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862
18863 *alias = NULL; /* default to no alias */
18864
18865 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18866 && (*arg)[2] == (int)KE_SNR)
18867 {
18868 /* hard coded <SNR>, already translated */
18869 *arg += 3;
18870 return get_id_len(arg) + 3;
18871 }
18872 len = eval_fname_script(*arg);
18873 if (len > 0)
18874 {
18875 /* literal "<SID>", "s:" or "<SNR>" */
18876 *arg += len;
18877 }
18878
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018880 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018882 p = find_name_end(*arg, &expr_start, &expr_end,
18883 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 if (expr_start != NULL)
18885 {
18886 char_u *temp_string;
18887
18888 if (!evaluate)
18889 {
18890 len += (int)(p - *arg);
18891 *arg = skipwhite(p);
18892 return len;
18893 }
18894
18895 /*
18896 * Include any <SID> etc in the expanded string:
18897 * Thus the -len here.
18898 */
18899 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18900 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018901 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 *alias = temp_string;
18903 *arg = skipwhite(p);
18904 return (int)STRLEN(temp_string);
18905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906
18907 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018908 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 EMSG2(_(e_invexpr2), *arg);
18910
18911 return len;
18912}
18913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018914/*
18915 * Find the end of a variable or function name, taking care of magic braces.
18916 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18917 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018918 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919 * Return a pointer to just after the name. Equal to "arg" if there is no
18920 * valid name.
18921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018923find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 char_u *arg;
18925 char_u **expr_start;
18926 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018927 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018929 int mb_nest = 0;
18930 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 char_u *p;
18932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018935 *expr_start = NULL;
18936 *expr_end = NULL;
18937 }
18938
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018939 /* Quick check for valid starting character. */
18940 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18941 return arg;
18942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018943 for (p = arg; *p != NUL
18944 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018945 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018946 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018947 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018948 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018949 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018950 if (*p == '\'')
18951 {
18952 /* skip over 'string' to avoid counting [ and ] inside it. */
18953 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18954 ;
18955 if (*p == NUL)
18956 break;
18957 }
18958 else if (*p == '"')
18959 {
18960 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18961 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18962 if (*p == '\\' && p[1] != NUL)
18963 ++p;
18964 if (*p == NUL)
18965 break;
18966 }
18967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018968 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018970 if (*p == '[')
18971 ++br_nest;
18972 else if (*p == ']')
18973 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018978 if (*p == '{')
18979 {
18980 mb_nest++;
18981 if (expr_start != NULL && *expr_start == NULL)
18982 *expr_start = p;
18983 }
18984 else if (*p == '}')
18985 {
18986 mb_nest--;
18987 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18988 *expr_end = p;
18989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 }
18992
18993 return p;
18994}
18995
18996/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018997 * Expands out the 'magic' {}'s in a variable/function name.
18998 * Note that this can call itself recursively, to deal with
18999 * constructs like foo{bar}{baz}{bam}
19000 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19001 * "in_start" ^
19002 * "expr_start" ^
19003 * "expr_end" ^
19004 * "in_end" ^
19005 *
19006 * Returns a new allocated string, which the caller must free.
19007 * Returns NULL for failure.
19008 */
19009 static char_u *
19010make_expanded_name(in_start, expr_start, expr_end, in_end)
19011 char_u *in_start;
19012 char_u *expr_start;
19013 char_u *expr_end;
19014 char_u *in_end;
19015{
19016 char_u c1;
19017 char_u *retval = NULL;
19018 char_u *temp_result;
19019 char_u *nextcmd = NULL;
19020
19021 if (expr_end == NULL || in_end == NULL)
19022 return NULL;
19023 *expr_start = NUL;
19024 *expr_end = NUL;
19025 c1 = *in_end;
19026 *in_end = NUL;
19027
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019028 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019029 if (temp_result != NULL && nextcmd == NULL)
19030 {
19031 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19032 + (in_end - expr_end) + 1));
19033 if (retval != NULL)
19034 {
19035 STRCPY(retval, in_start);
19036 STRCAT(retval, temp_result);
19037 STRCAT(retval, expr_end + 1);
19038 }
19039 }
19040 vim_free(temp_result);
19041
19042 *in_end = c1; /* put char back for error messages */
19043 *expr_start = '{';
19044 *expr_end = '}';
19045
19046 if (retval != NULL)
19047 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019048 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019049 if (expr_start != NULL)
19050 {
19051 /* Further expansion! */
19052 temp_result = make_expanded_name(retval, expr_start,
19053 expr_end, temp_result);
19054 vim_free(retval);
19055 retval = temp_result;
19056 }
19057 }
19058
19059 return retval;
19060}
19061
19062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019064 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 */
19066 static int
19067eval_isnamec(c)
19068 int c;
19069{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019070 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19071}
19072
19073/*
19074 * Return TRUE if character "c" can be used as the first character in a
19075 * variable or function name (excluding '{' and '}').
19076 */
19077 static int
19078eval_isnamec1(c)
19079 int c;
19080{
19081 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082}
19083
19084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 * Set number v: variable to "val".
19086 */
19087 void
19088set_vim_var_nr(idx, val)
19089 int idx;
19090 long val;
19091{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019092 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093}
19094
19095/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019096 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 */
19098 long
19099get_vim_var_nr(idx)
19100 int idx;
19101{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019102 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103}
19104
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019105/*
19106 * Get string v: variable value. Uses a static buffer, can only be used once.
19107 */
19108 char_u *
19109get_vim_var_str(idx)
19110 int idx;
19111{
19112 return get_tv_string(&vimvars[idx].vv_tv);
19113}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019114
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019116 * Get List v: variable value. Caller must take care of reference count when
19117 * needed.
19118 */
19119 list_T *
19120get_vim_var_list(idx)
19121 int idx;
19122{
19123 return vimvars[idx].vv_list;
19124}
19125
19126/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019127 * Set v:char to character "c".
19128 */
19129 void
19130set_vim_var_char(c)
19131 int c;
19132{
19133#ifdef FEAT_MBYTE
19134 char_u buf[MB_MAXBYTES];
19135#else
19136 char_u buf[2];
19137#endif
19138
19139#ifdef FEAT_MBYTE
19140 if (has_mbyte)
19141 buf[(*mb_char2bytes)(c, buf)] = NUL;
19142 else
19143#endif
19144 {
19145 buf[0] = c;
19146 buf[1] = NUL;
19147 }
19148 set_vim_var_string(VV_CHAR, buf, -1);
19149}
19150
19151/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019152 * Set v:count to "count" and v:count1 to "count1".
19153 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 */
19155 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019156set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 long count;
19158 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019159 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019161 if (set_prevcount)
19162 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019163 vimvars[VV_COUNT].vv_nr = count;
19164 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165}
19166
19167/*
19168 * Set string v: variable to a copy of "val".
19169 */
19170 void
19171set_vim_var_string(idx, val, len)
19172 int idx;
19173 char_u *val;
19174 int len; /* length of "val" to use or -1 (whole string) */
19175{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019176 /* Need to do this (at least) once, since we can't initialize a union.
19177 * Will always be invoked when "v:progname" is set. */
19178 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19179
Bram Moolenaare9a41262005-01-15 22:18:47 +000019180 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019182 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019184 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019186 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187}
19188
19189/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019190 * Set List v: variable to "val".
19191 */
19192 void
19193set_vim_var_list(idx, val)
19194 int idx;
19195 list_T *val;
19196{
19197 list_unref(vimvars[idx].vv_list);
19198 vimvars[idx].vv_list = val;
19199 if (val != NULL)
19200 ++val->lv_refcount;
19201}
19202
19203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 * Set v:register if needed.
19205 */
19206 void
19207set_reg_var(c)
19208 int c;
19209{
19210 char_u regname;
19211
19212 if (c == 0 || c == ' ')
19213 regname = '"';
19214 else
19215 regname = c;
19216 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019217 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 set_vim_var_string(VV_REG, &regname, 1);
19219}
19220
19221/*
19222 * Get or set v:exception. If "oldval" == NULL, return the current value.
19223 * Otherwise, restore the value to "oldval" and return NULL.
19224 * Must always be called in pairs to save and restore v:exception! Does not
19225 * take care of memory allocations.
19226 */
19227 char_u *
19228v_exception(oldval)
19229 char_u *oldval;
19230{
19231 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019232 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233
Bram Moolenaare9a41262005-01-15 22:18:47 +000019234 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 return NULL;
19236}
19237
19238/*
19239 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19240 * Otherwise, restore the value to "oldval" and return NULL.
19241 * Must always be called in pairs to save and restore v:throwpoint! Does not
19242 * take care of memory allocations.
19243 */
19244 char_u *
19245v_throwpoint(oldval)
19246 char_u *oldval;
19247{
19248 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019249 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250
Bram Moolenaare9a41262005-01-15 22:18:47 +000019251 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 return NULL;
19253}
19254
19255#if defined(FEAT_AUTOCMD) || defined(PROTO)
19256/*
19257 * Set v:cmdarg.
19258 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19259 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19260 * Must always be called in pairs!
19261 */
19262 char_u *
19263set_cmdarg(eap, oldarg)
19264 exarg_T *eap;
19265 char_u *oldarg;
19266{
19267 char_u *oldval;
19268 char_u *newval;
19269 unsigned len;
19270
Bram Moolenaare9a41262005-01-15 22:18:47 +000019271 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019272 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019274 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019275 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019276 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 }
19278
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019279 if (eap->force_bin == FORCE_BIN)
19280 len = 6;
19281 else if (eap->force_bin == FORCE_NOBIN)
19282 len = 8;
19283 else
19284 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019285
19286 if (eap->read_edit)
19287 len += 7;
19288
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019289 if (eap->force_ff != 0)
19290 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19291# ifdef FEAT_MBYTE
19292 if (eap->force_enc != 0)
19293 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019294 if (eap->bad_char != 0)
19295 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019296# endif
19297
19298 newval = alloc(len + 1);
19299 if (newval == NULL)
19300 return NULL;
19301
19302 if (eap->force_bin == FORCE_BIN)
19303 sprintf((char *)newval, " ++bin");
19304 else if (eap->force_bin == FORCE_NOBIN)
19305 sprintf((char *)newval, " ++nobin");
19306 else
19307 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019308
19309 if (eap->read_edit)
19310 STRCAT(newval, " ++edit");
19311
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019312 if (eap->force_ff != 0)
19313 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19314 eap->cmd + eap->force_ff);
19315# ifdef FEAT_MBYTE
19316 if (eap->force_enc != 0)
19317 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19318 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019319 if (eap->bad_char == BAD_KEEP)
19320 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19321 else if (eap->bad_char == BAD_DROP)
19322 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19323 else if (eap->bad_char != 0)
19324 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019325# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019326 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019327 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328}
19329#endif
19330
19331/*
19332 * Get the value of internal variable "name".
19333 * Return OK or FAIL.
19334 */
19335 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019336get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 char_u *name;
19338 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019339 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019340 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341{
19342 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019343 typval_T *tv = NULL;
19344 typval_T atv;
19345 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347
19348 /* truncate the name, so that we can use strcmp() */
19349 cc = name[len];
19350 name[len] = NUL;
19351
19352 /*
19353 * Check for "b:changedtick".
19354 */
19355 if (STRCMP(name, "b:changedtick") == 0)
19356 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019357 atv.v_type = VAR_NUMBER;
19358 atv.vval.v_number = curbuf->b_changedtick;
19359 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 }
19361
19362 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 * Check for user-defined variables.
19364 */
19365 else
19366 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019367 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 }
19371
Bram Moolenaare9a41262005-01-15 22:18:47 +000019372 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019374 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019375 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 ret = FAIL;
19377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019379 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
19381 name[len] = cc;
19382
19383 return ret;
19384}
19385
19386/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019387 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19388 * Also handle function call with Funcref variable: func(expr)
19389 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19390 */
19391 static int
19392handle_subscript(arg, rettv, evaluate, verbose)
19393 char_u **arg;
19394 typval_T *rettv;
19395 int evaluate; /* do more than finding the end */
19396 int verbose; /* give error messages */
19397{
19398 int ret = OK;
19399 dict_T *selfdict = NULL;
19400 char_u *s;
19401 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019402 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019403
19404 while (ret == OK
19405 && (**arg == '['
19406 || (**arg == '.' && rettv->v_type == VAR_DICT)
19407 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19408 && !vim_iswhite(*(*arg - 1)))
19409 {
19410 if (**arg == '(')
19411 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019412 /* need to copy the funcref so that we can clear rettv */
19413 functv = *rettv;
19414 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019415
19416 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019417 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019418 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019419 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19420 &len, evaluate, selfdict);
19421
19422 /* Clear the funcref afterwards, so that deleting it while
19423 * evaluating the arguments is possible (see test55). */
19424 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019425
19426 /* Stop the expression evaluation when immediately aborting on
19427 * error, or when an interrupt occurred or an exception was thrown
19428 * but not caught. */
19429 if (aborting())
19430 {
19431 if (ret == OK)
19432 clear_tv(rettv);
19433 ret = FAIL;
19434 }
19435 dict_unref(selfdict);
19436 selfdict = NULL;
19437 }
19438 else /* **arg == '[' || **arg == '.' */
19439 {
19440 dict_unref(selfdict);
19441 if (rettv->v_type == VAR_DICT)
19442 {
19443 selfdict = rettv->vval.v_dict;
19444 if (selfdict != NULL)
19445 ++selfdict->dv_refcount;
19446 }
19447 else
19448 selfdict = NULL;
19449 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19450 {
19451 clear_tv(rettv);
19452 ret = FAIL;
19453 }
19454 }
19455 }
19456 dict_unref(selfdict);
19457 return ret;
19458}
19459
19460/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019461 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019462 * value).
19463 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019464 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019465alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019466{
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019468}
19469
19470/*
19471 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 * The string "s" must have been allocated, it is consumed.
19473 * Return NULL for out of memory, the variable otherwise.
19474 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019475 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019476alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 char_u *s;
19478{
Bram Moolenaar33570922005-01-25 22:26:29 +000019479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 rettv = alloc_tv();
19482 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019484 rettv->v_type = VAR_STRING;
19485 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 }
19487 else
19488 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019489 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490}
19491
19492/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019493 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019495 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019496free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019497 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498{
19499 if (varp != NULL)
19500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019501 switch (varp->v_type)
19502 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019503 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019504 func_unref(varp->vval.v_string);
19505 /*FALLTHROUGH*/
19506 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019507 vim_free(varp->vval.v_string);
19508 break;
19509 case VAR_LIST:
19510 list_unref(varp->vval.v_list);
19511 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019512 case VAR_DICT:
19513 dict_unref(varp->vval.v_dict);
19514 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019515 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019516#ifdef FEAT_FLOAT
19517 case VAR_FLOAT:
19518#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019519 case VAR_UNKNOWN:
19520 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019521 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019522 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019523 break;
19524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 vim_free(varp);
19526 }
19527}
19528
19529/*
19530 * Free the memory for a variable value and set the value to NULL or 0.
19531 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019532 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019533clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535{
19536 if (varp != NULL)
19537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019538 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019540 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019541 func_unref(varp->vval.v_string);
19542 /*FALLTHROUGH*/
19543 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019544 vim_free(varp->vval.v_string);
19545 varp->vval.v_string = NULL;
19546 break;
19547 case VAR_LIST:
19548 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019549 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019550 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019551 case VAR_DICT:
19552 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019553 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019554 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019555 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019556 varp->vval.v_number = 0;
19557 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019558#ifdef FEAT_FLOAT
19559 case VAR_FLOAT:
19560 varp->vval.v_float = 0.0;
19561 break;
19562#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 case VAR_UNKNOWN:
19564 break;
19565 default:
19566 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019568 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 }
19570}
19571
19572/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573 * Set the value of a variable to NULL without freeing items.
19574 */
19575 static void
19576init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019578{
19579 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581}
19582
19583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 * Get the number value of a variable.
19585 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019586 * For incompatible types, return 0.
19587 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19588 * caller of incompatible types: it sets *denote to TRUE if "denote"
19589 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 */
19591 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019595 int error = FALSE;
19596
19597 return get_tv_number_chk(varp, &error); /* return 0L on error */
19598}
19599
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019600 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019601get_tv_number_chk(varp, denote)
19602 typval_T *varp;
19603 int *denote;
19604{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019605 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019607 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019609 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019610 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019611#ifdef FEAT_FLOAT
19612 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019613 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019614 break;
19615#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019616 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019617 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019618 break;
19619 case VAR_STRING:
19620 if (varp->vval.v_string != NULL)
19621 vim_str2nr(varp->vval.v_string, NULL, NULL,
19622 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019623 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019624 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019625 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019626 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019627 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019628 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019629 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019630 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019631 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019634 if (denote == NULL) /* useful for values that must be unsigned */
19635 n = -1;
19636 else
19637 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019638 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639}
19640
19641/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019642 * Get the lnum from the first argument.
19643 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019644 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 */
19646 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649{
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 linenr_T lnum;
19652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019653 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 if (lnum == 0) /* no valid number, try using line() */
19655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 rettv.v_type = VAR_NUMBER;
19657 f_line(argvars, &rettv);
19658 lnum = rettv.vval.v_number;
19659 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 }
19661 return lnum;
19662}
19663
19664/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019665 * Get the lnum from the first argument.
19666 * Also accepts "$", then "buf" is used.
19667 * Returns 0 on error.
19668 */
19669 static linenr_T
19670get_tv_lnum_buf(argvars, buf)
19671 typval_T *argvars;
19672 buf_T *buf;
19673{
19674 if (argvars[0].v_type == VAR_STRING
19675 && argvars[0].vval.v_string != NULL
19676 && argvars[0].vval.v_string[0] == '$'
19677 && buf != NULL)
19678 return buf->b_ml.ml_line_count;
19679 return get_tv_number_chk(&argvars[0], NULL);
19680}
19681
19682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 * Get the string value of a variable.
19684 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019685 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19686 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 * If the String variable has never been set, return an empty string.
19688 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019689 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19690 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 */
19692 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019693get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695{
19696 static char_u mybuf[NUMBUFLEN];
19697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019698 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699}
19700
19701 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019704 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019706 char_u *res = get_tv_string_buf_chk(varp, buf);
19707
19708 return res != NULL ? res : (char_u *)"";
19709}
19710
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019711 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019712get_tv_string_chk(varp)
19713 typval_T *varp;
19714{
19715 static char_u mybuf[NUMBUFLEN];
19716
19717 return get_tv_string_buf_chk(varp, mybuf);
19718}
19719
19720 static char_u *
19721get_tv_string_buf_chk(varp, buf)
19722 typval_T *varp;
19723 char_u *buf;
19724{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019725 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019727 case VAR_NUMBER:
19728 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19729 return buf;
19730 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019731 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019732 break;
19733 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019734 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019735 break;
19736 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019737 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019738 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019739#ifdef FEAT_FLOAT
19740 case VAR_FLOAT:
19741 EMSG(_("E806: using Float as a String"));
19742 break;
19743#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744 case VAR_STRING:
19745 if (varp->vval.v_string != NULL)
19746 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019747 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019748 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019749 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019752 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753}
19754
19755/*
19756 * Find variable "name" in the list of variables.
19757 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019759 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019760 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019762 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019763find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769
Bram Moolenaara7043832005-01-21 11:56:39 +000019770 ht = find_var_ht(name, &varname);
19771 if (htp != NULL)
19772 *htp = ht;
19773 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019775 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776}
19777
19778/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019779 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019780 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019783find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019784 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019785 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019786 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019787{
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 hashitem_T *hi;
19789
19790 if (*varname == NUL)
19791 {
19792 /* Must be something like "s:", otherwise "ht" would be NULL. */
19793 switch (varname[-2])
19794 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019795 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 case 'g': return &globvars_var;
19797 case 'v': return &vimvars_var;
19798 case 'b': return &curbuf->b_bufvar;
19799 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019800#ifdef FEAT_WINDOWS
19801 case 't': return &curtab->tp_winvar;
19802#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019803 case 'l': return current_funccal == NULL
19804 ? NULL : &current_funccal->l_vars_var;
19805 case 'a': return current_funccal == NULL
19806 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019807 }
19808 return NULL;
19809 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019810
19811 hi = hash_find(ht, varname);
19812 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019813 {
19814 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019815 * worked find the variable again. Don't auto-load a script if it was
19816 * loaded already, otherwise it would be loaded every time when
19817 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019818 if (ht == &globvarht && !writing)
19819 {
19820 /* Note: script_autoload() may make "hi" invalid. It must either
19821 * be obtained again or not used. */
19822 if (!script_autoload(varname, FALSE) || aborting())
19823 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019824 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019825 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019826 if (HASHITEM_EMPTY(hi))
19827 return NULL;
19828 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019830}
19831
19832/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019833 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019834 * Set "varname" to the start of name without ':'.
19835 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019836 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019837find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 char_u *name;
19839 char_u **varname;
19840{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019841 hashitem_T *hi;
19842
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 if (name[1] != ':')
19844 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019845 /* The name must not start with a colon or #. */
19846 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 return NULL;
19848 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019849
19850 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019851 hi = hash_find(&compat_hashtab, name);
19852 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019853 return &compat_hashtab;
19854
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 return &globvarht; /* global variable */
19857 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 }
19859 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019860 if (*name == 'g') /* global variable */
19861 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019862 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19863 */
19864 if (vim_strchr(name + 2, ':') != NULL
19865 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019866 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019868 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019871#ifdef FEAT_WINDOWS
19872 if (*name == 't') /* tab page variable */
19873 return &curtab->tp_vars.dv_hashtab;
19874#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 if (*name == 'v') /* v: variable */
19876 return &vimvarht;
19877 if (*name == 'a' && current_funccal != NULL) /* function argument */
19878 return &current_funccal->l_avars.dv_hashtab;
19879 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19880 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 if (*name == 's' /* script variable */
19882 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19883 return &SCRIPT_VARS(current_SID);
19884 return NULL;
19885}
19886
19887/*
19888 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019889 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 * Returns NULL when it doesn't exist.
19891 */
19892 char_u *
19893get_var_value(name)
19894 char_u *name;
19895{
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897
Bram Moolenaara7043832005-01-21 11:56:39 +000019898 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 if (v == NULL)
19900 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019901 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902}
19903
19904/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019905 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 * sourcing this script and when executing functions defined in the script.
19907 */
19908 void
19909new_script_vars(id)
19910 scid_T id;
19911{
Bram Moolenaara7043832005-01-21 11:56:39 +000019912 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019913 hashtab_T *ht;
19914 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019915
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19917 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019918 /* Re-allocating ga_data means that an ht_array pointing to
19919 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019921 for (i = 1; i <= ga_scripts.ga_len; ++i)
19922 {
19923 ht = &SCRIPT_VARS(i);
19924 if (ht->ht_mask == HT_INIT_SIZE - 1)
19925 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019926 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019928 }
19929
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 while (ga_scripts.ga_len < id)
19931 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019932 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019933 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019934 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 }
19937 }
19938}
19939
19940/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19942 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 */
19944 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019945init_var_dict(dict, dict_var)
19946 dict_T *dict;
19947 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948{
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019950 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019951 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019952 dict_var->di_tv.vval.v_dict = dict;
19953 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019954 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19956 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957}
19958
19959/*
19960 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 * Frees all allocated variables and the value they contain.
19962 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 */
19964 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019965vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 hashtab_T *ht;
19967{
19968 vars_clear_ext(ht, TRUE);
19969}
19970
19971/*
19972 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19973 */
19974 static void
19975vars_clear_ext(ht, free_val)
19976 hashtab_T *ht;
19977 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978{
Bram Moolenaara7043832005-01-21 11:56:39 +000019979 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019980 hashitem_T *hi;
19981 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019984 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019985 for (hi = ht->ht_array; todo > 0; ++hi)
19986 {
19987 if (!HASHITEM_EMPTY(hi))
19988 {
19989 --todo;
19990
Bram Moolenaar33570922005-01-25 22:26:29 +000019991 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019992 * ht_array might change then. hash_clear() takes care of it
19993 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 v = HI2DI(hi);
19995 if (free_val)
19996 clear_tv(&v->di_tv);
19997 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19998 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019999 }
20000 }
20001 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020002 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003}
20004
Bram Moolenaara7043832005-01-21 11:56:39 +000020005/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020006 * Delete a variable from hashtab "ht" at item "hi".
20007 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020010delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020011 hashtab_T *ht;
20012 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013{
Bram Moolenaar33570922005-01-25 22:26:29 +000020014 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020015
20016 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020017 clear_tv(&di->di_tv);
20018 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019}
20020
20021/*
20022 * List the value of one internal variable.
20023 */
20024 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020025list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020026 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020028 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020030 char_u *tofree;
20031 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020032 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020033
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020034 current_copyID += COPYID_INC;
20035 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020037 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020038 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039}
20040
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020042list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 char_u *prefix;
20044 char_u *name;
20045 int type;
20046 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020047 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048{
Bram Moolenaar31859182007-08-14 20:41:13 +000020049 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20050 msg_start();
20051 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 if (name != NULL) /* "a:" vars don't have a name stored */
20053 msg_puts(name);
20054 msg_putchar(' ');
20055 msg_advance(22);
20056 if (type == VAR_NUMBER)
20057 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020058 else if (type == VAR_FUNC)
20059 msg_putchar('*');
20060 else if (type == VAR_LIST)
20061 {
20062 msg_putchar('[');
20063 if (*string == '[')
20064 ++string;
20065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020066 else if (type == VAR_DICT)
20067 {
20068 msg_putchar('{');
20069 if (*string == '{')
20070 ++string;
20071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 else
20073 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020074
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020076
20077 if (type == VAR_FUNC)
20078 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020079 if (*first)
20080 {
20081 msg_clr_eos();
20082 *first = FALSE;
20083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084}
20085
20086/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 * If the variable already exists, the value is updated.
20089 * Otherwise the variable is created.
20090 */
20091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020094 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096{
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020101 ht = find_var_ht(name, &varname);
20102 if (ht == NULL || *varname == NUL)
20103 {
20104 EMSG2(_(e_illvar), name);
20105 return;
20106 }
20107 v = find_var_in_ht(ht, varname, TRUE);
20108
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020109 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20110 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020111
Bram Moolenaar33570922005-01-25 22:26:29 +000020112 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020115 if (var_check_ro(v->di_flags, name)
20116 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 return;
20118 if (v->di_tv.v_type != tv->v_type
20119 && !((v->di_tv.v_type == VAR_STRING
20120 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020121 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020122 || tv->v_type == VAR_NUMBER))
20123#ifdef FEAT_FLOAT
20124 && !((v->di_tv.v_type == VAR_NUMBER
20125 || v->di_tv.v_type == VAR_FLOAT)
20126 && (tv->v_type == VAR_NUMBER
20127 || tv->v_type == VAR_FLOAT))
20128#endif
20129 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020131 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020132 return;
20133 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020134
20135 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020136 * Handle setting internal v: variables separately: we don't change
20137 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020138 */
20139 if (ht == &vimvarht)
20140 {
20141 if (v->di_tv.v_type == VAR_STRING)
20142 {
20143 vim_free(v->di_tv.vval.v_string);
20144 if (copy || tv->v_type != VAR_STRING)
20145 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20146 else
20147 {
20148 /* Take over the string to avoid an extra alloc/free. */
20149 v->di_tv.vval.v_string = tv->vval.v_string;
20150 tv->vval.v_string = NULL;
20151 }
20152 }
20153 else if (v->di_tv.v_type != VAR_NUMBER)
20154 EMSG2(_(e_intern2), "set_var()");
20155 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020156 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020157 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020158 if (STRCMP(varname, "searchforward") == 0)
20159 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20160 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020161 return;
20162 }
20163
20164 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 }
20166 else /* add a new variable */
20167 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020168 /* Can't add "v:" variable. */
20169 if (ht == &vimvarht)
20170 {
20171 EMSG2(_(e_illvar), name);
20172 return;
20173 }
20174
Bram Moolenaar92124a32005-06-17 22:03:40 +000020175 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020176 if (!valid_varname(varname))
20177 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020178
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020179 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20180 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020181 if (v == NULL)
20182 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020186 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 return;
20188 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020189 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020191
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020192 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020194 else
20195 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020196 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020197 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200}
20201
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020202/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020203 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 * Also give an error message.
20205 */
20206 static int
20207var_check_ro(flags, name)
20208 int flags;
20209 char_u *name;
20210{
20211 if (flags & DI_FLAGS_RO)
20212 {
20213 EMSG2(_(e_readonlyvar), name);
20214 return TRUE;
20215 }
20216 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20217 {
20218 EMSG2(_(e_readonlysbx), name);
20219 return TRUE;
20220 }
20221 return FALSE;
20222}
20223
20224/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020225 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20226 * Also give an error message.
20227 */
20228 static int
20229var_check_fixed(flags, name)
20230 int flags;
20231 char_u *name;
20232{
20233 if (flags & DI_FLAGS_FIX)
20234 {
20235 EMSG2(_("E795: Cannot delete variable %s"), name);
20236 return TRUE;
20237 }
20238 return FALSE;
20239}
20240
20241/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020242 * Check if a funcref is assigned to a valid variable name.
20243 * Return TRUE and give an error if not.
20244 */
20245 static int
20246var_check_func_name(name, new_var)
20247 char_u *name; /* points to start of variable name */
20248 int new_var; /* TRUE when creating the variable */
20249{
20250 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20251 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20252 ? name[2] : name[0]))
20253 {
20254 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20255 name);
20256 return TRUE;
20257 }
20258 /* Don't allow hiding a function. When "v" is not NULL we might be
20259 * assigning another function to the same var, the type is checked
20260 * below. */
20261 if (new_var && function_exists(name))
20262 {
20263 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20264 name);
20265 return TRUE;
20266 }
20267 return FALSE;
20268}
20269
20270/*
20271 * Check if a variable name is valid.
20272 * Return FALSE and give an error if not.
20273 */
20274 static int
20275valid_varname(varname)
20276 char_u *varname;
20277{
20278 char_u *p;
20279
20280 for (p = varname; *p != NUL; ++p)
20281 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20282 && *p != AUTOLOAD_CHAR)
20283 {
20284 EMSG2(_(e_illvar), varname);
20285 return FALSE;
20286 }
20287 return TRUE;
20288}
20289
20290/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020291 * Return TRUE if typeval "tv" is set to be locked (immutable).
20292 * Also give an error message, using "name".
20293 */
20294 static int
20295tv_check_lock(lock, name)
20296 int lock;
20297 char_u *name;
20298{
20299 if (lock & VAR_LOCKED)
20300 {
20301 EMSG2(_("E741: Value is locked: %s"),
20302 name == NULL ? (char_u *)_("Unknown") : name);
20303 return TRUE;
20304 }
20305 if (lock & VAR_FIXED)
20306 {
20307 EMSG2(_("E742: Cannot change value of %s"),
20308 name == NULL ? (char_u *)_("Unknown") : name);
20309 return TRUE;
20310 }
20311 return FALSE;
20312}
20313
20314/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020316 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020317 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020318 * It is OK for "from" and "to" to point to the same item. This is used to
20319 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020320 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020321 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020322copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 typval_T *from;
20324 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020326 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020327 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020328 switch (from->v_type)
20329 {
20330 case VAR_NUMBER:
20331 to->vval.v_number = from->vval.v_number;
20332 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020333#ifdef FEAT_FLOAT
20334 case VAR_FLOAT:
20335 to->vval.v_float = from->vval.v_float;
20336 break;
20337#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020338 case VAR_STRING:
20339 case VAR_FUNC:
20340 if (from->vval.v_string == NULL)
20341 to->vval.v_string = NULL;
20342 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020344 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020345 if (from->v_type == VAR_FUNC)
20346 func_ref(to->vval.v_string);
20347 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020348 break;
20349 case VAR_LIST:
20350 if (from->vval.v_list == NULL)
20351 to->vval.v_list = NULL;
20352 else
20353 {
20354 to->vval.v_list = from->vval.v_list;
20355 ++to->vval.v_list->lv_refcount;
20356 }
20357 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020358 case VAR_DICT:
20359 if (from->vval.v_dict == NULL)
20360 to->vval.v_dict = NULL;
20361 else
20362 {
20363 to->vval.v_dict = from->vval.v_dict;
20364 ++to->vval.v_dict->dv_refcount;
20365 }
20366 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020367 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020369 break;
20370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371}
20372
20373/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020374 * Make a copy of an item.
20375 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020376 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20377 * reference to an already copied list/dict can be used.
20378 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020379 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020380 static int
20381item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 typval_T *from;
20383 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020384 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020385 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020386{
20387 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020388 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020389
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020391 {
20392 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020393 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020394 }
20395 ++recurse;
20396
20397 switch (from->v_type)
20398 {
20399 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020400#ifdef FEAT_FLOAT
20401 case VAR_FLOAT:
20402#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020403 case VAR_STRING:
20404 case VAR_FUNC:
20405 copy_tv(from, to);
20406 break;
20407 case VAR_LIST:
20408 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020409 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020410 if (from->vval.v_list == NULL)
20411 to->vval.v_list = NULL;
20412 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20413 {
20414 /* use the copy made earlier */
20415 to->vval.v_list = from->vval.v_list->lv_copylist;
20416 ++to->vval.v_list->lv_refcount;
20417 }
20418 else
20419 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20420 if (to->vval.v_list == NULL)
20421 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020422 break;
20423 case VAR_DICT:
20424 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020425 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020426 if (from->vval.v_dict == NULL)
20427 to->vval.v_dict = NULL;
20428 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20429 {
20430 /* use the copy made earlier */
20431 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20432 ++to->vval.v_dict->dv_refcount;
20433 }
20434 else
20435 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20436 if (to->vval.v_dict == NULL)
20437 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020438 break;
20439 default:
20440 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020441 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020442 }
20443 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020444 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020445}
20446
20447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 * ":echo expr1 ..." print each argument separated with a space, add a
20449 * newline at the end.
20450 * ":echon expr1 ..." print each argument plain.
20451 */
20452 void
20453ex_echo(eap)
20454 exarg_T *eap;
20455{
20456 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020457 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020458 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 char_u *p;
20460 int needclr = TRUE;
20461 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020462 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463
20464 if (eap->skip)
20465 ++emsg_skip;
20466 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20467 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020468 /* If eval1() causes an error message the text from the command may
20469 * still need to be cleared. E.g., "echo 22,44". */
20470 need_clr_eos = needclr;
20471
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020473 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 {
20475 /*
20476 * Report the invalid expression unless the expression evaluation
20477 * has been cancelled due to an aborting error, an interrupt, or an
20478 * exception.
20479 */
20480 if (!aborting())
20481 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020482 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 break;
20484 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020485 need_clr_eos = FALSE;
20486
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 if (!eap->skip)
20488 {
20489 if (atstart)
20490 {
20491 atstart = FALSE;
20492 /* Call msg_start() after eval1(), evaluating the expression
20493 * may cause a message to appear. */
20494 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020495 {
20496 /* Put the output below the command, makes scrolling back
20497 * at more prompt work. */
20498 msg_didout = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 }
20502 else if (eap->cmdidx == CMD_echo)
20503 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020504 current_copyID += COPYID_INC;
20505 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020506 if (p != NULL)
20507 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020509 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020511 if (*p != TAB && needclr)
20512 {
20513 /* remove any text still there from the command */
20514 msg_clr_eos();
20515 needclr = FALSE;
20516 }
20517 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 }
20519 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020520 {
20521#ifdef FEAT_MBYTE
20522 if (has_mbyte)
20523 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020524 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020525
20526 (void)msg_outtrans_len_attr(p, i, echo_attr);
20527 p += i - 1;
20528 }
20529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020531 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020534 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020536 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 arg = skipwhite(arg);
20538 }
20539 eap->nextcmd = check_nextcmd(arg);
20540
20541 if (eap->skip)
20542 --emsg_skip;
20543 else
20544 {
20545 /* remove text that may still be there from the command */
20546 if (needclr)
20547 msg_clr_eos();
20548 if (eap->cmdidx == CMD_echo)
20549 msg_end();
20550 }
20551}
20552
20553/*
20554 * ":echohl {name}".
20555 */
20556 void
20557ex_echohl(eap)
20558 exarg_T *eap;
20559{
20560 int id;
20561
20562 id = syn_name2id(eap->arg);
20563 if (id == 0)
20564 echo_attr = 0;
20565 else
20566 echo_attr = syn_id2attr(id);
20567}
20568
20569/*
20570 * ":execute expr1 ..." execute the result of an expression.
20571 * ":echomsg expr1 ..." Print a message
20572 * ":echoerr expr1 ..." Print an error
20573 * Each gets spaces around each argument and a newline at the end for
20574 * echo commands
20575 */
20576 void
20577ex_execute(eap)
20578 exarg_T *eap;
20579{
20580 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020581 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 int ret = OK;
20583 char_u *p;
20584 garray_T ga;
20585 int len;
20586 int save_did_emsg;
20587
20588 ga_init2(&ga, 1, 80);
20589
20590 if (eap->skip)
20591 ++emsg_skip;
20592 while (*arg != NUL && *arg != '|' && *arg != '\n')
20593 {
20594 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020595 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 {
20597 /*
20598 * Report the invalid expression unless the expression evaluation
20599 * has been cancelled due to an aborting error, an interrupt, or an
20600 * exception.
20601 */
20602 if (!aborting())
20603 EMSG2(_(e_invexpr2), p);
20604 ret = FAIL;
20605 break;
20606 }
20607
20608 if (!eap->skip)
20609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020610 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 len = (int)STRLEN(p);
20612 if (ga_grow(&ga, len + 2) == FAIL)
20613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 ret = FAIL;
20616 break;
20617 }
20618 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 ga.ga_len += len;
20622 }
20623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 arg = skipwhite(arg);
20626 }
20627
20628 if (ret != FAIL && ga.ga_data != NULL)
20629 {
20630 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020633 out_flush();
20634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 else if (eap->cmdidx == CMD_echoerr)
20636 {
20637 /* We don't want to abort following commands, restore did_emsg. */
20638 save_did_emsg = did_emsg;
20639 EMSG((char_u *)ga.ga_data);
20640 if (!force_abort)
20641 did_emsg = save_did_emsg;
20642 }
20643 else if (eap->cmdidx == CMD_execute)
20644 do_cmdline((char_u *)ga.ga_data,
20645 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20646 }
20647
20648 ga_clear(&ga);
20649
20650 if (eap->skip)
20651 --emsg_skip;
20652
20653 eap->nextcmd = check_nextcmd(arg);
20654}
20655
20656/*
20657 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20658 * "arg" points to the "&" or '+' when called, to "option" when returning.
20659 * Returns NULL when no option name found. Otherwise pointer to the char
20660 * after the option name.
20661 */
20662 static char_u *
20663find_option_end(arg, opt_flags)
20664 char_u **arg;
20665 int *opt_flags;
20666{
20667 char_u *p = *arg;
20668
20669 ++p;
20670 if (*p == 'g' && p[1] == ':')
20671 {
20672 *opt_flags = OPT_GLOBAL;
20673 p += 2;
20674 }
20675 else if (*p == 'l' && p[1] == ':')
20676 {
20677 *opt_flags = OPT_LOCAL;
20678 p += 2;
20679 }
20680 else
20681 *opt_flags = 0;
20682
20683 if (!ASCII_ISALPHA(*p))
20684 return NULL;
20685 *arg = p;
20686
20687 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20688 p += 4; /* termcap option */
20689 else
20690 while (ASCII_ISALPHA(*p))
20691 ++p;
20692 return p;
20693}
20694
20695/*
20696 * ":function"
20697 */
20698 void
20699ex_function(eap)
20700 exarg_T *eap;
20701{
20702 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020703 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 int j;
20705 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 char_u *name = NULL;
20708 char_u *p;
20709 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020710 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 garray_T newargs;
20712 garray_T newlines;
20713 int varargs = FALSE;
20714 int mustend = FALSE;
20715 int flags = 0;
20716 ufunc_T *fp;
20717 int indent;
20718 int nesting;
20719 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020720 dictitem_T *v;
20721 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020722 static int func_nr = 0; /* number for nameless function */
20723 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020724 hashtab_T *ht;
20725 int todo;
20726 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020727 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728
20729 /*
20730 * ":function" without argument: list functions.
20731 */
20732 if (ends_excmd(*eap->arg))
20733 {
20734 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020736 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020737 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020738 {
20739 if (!HASHITEM_EMPTY(hi))
20740 {
20741 --todo;
20742 fp = HI2UF(hi);
20743 if (!isdigit(*fp->uf_name))
20744 list_func_head(fp, FALSE);
20745 }
20746 }
20747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 eap->nextcmd = check_nextcmd(eap->arg);
20749 return;
20750 }
20751
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020752 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020753 * ":function /pat": list functions matching pattern.
20754 */
20755 if (*eap->arg == '/')
20756 {
20757 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20758 if (!eap->skip)
20759 {
20760 regmatch_T regmatch;
20761
20762 c = *p;
20763 *p = NUL;
20764 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20765 *p = c;
20766 if (regmatch.regprog != NULL)
20767 {
20768 regmatch.rm_ic = p_ic;
20769
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020770 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020771 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20772 {
20773 if (!HASHITEM_EMPTY(hi))
20774 {
20775 --todo;
20776 fp = HI2UF(hi);
20777 if (!isdigit(*fp->uf_name)
20778 && vim_regexec(&regmatch, fp->uf_name, 0))
20779 list_func_head(fp, FALSE);
20780 }
20781 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020782 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020783 }
20784 }
20785 if (*p == '/')
20786 ++p;
20787 eap->nextcmd = check_nextcmd(p);
20788 return;
20789 }
20790
20791 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020792 * Get the function name. There are these situations:
20793 * func normal function name
20794 * "name" == func, "fudi.fd_dict" == NULL
20795 * dict.func new dictionary entry
20796 * "name" == NULL, "fudi.fd_dict" set,
20797 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20798 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020799 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020800 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20801 * dict.func existing dict entry that's not a Funcref
20802 * "name" == NULL, "fudi.fd_dict" set,
20803 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020806 name = trans_function_name(&p, eap->skip, 0, &fudi);
20807 paren = (vim_strchr(p, '(') != NULL);
20808 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 {
20810 /*
20811 * Return on an invalid expression in braces, unless the expression
20812 * evaluation has been cancelled due to an aborting error, an
20813 * interrupt, or an exception.
20814 */
20815 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020816 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020817 if (!eap->skip && fudi.fd_newkey != NULL)
20818 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020819 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 else
20823 eap->skip = TRUE;
20824 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020825
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 /* An error in a function call during evaluation of an expression in magic
20827 * braces should not cause the function not to be defined. */
20828 saved_did_emsg = did_emsg;
20829 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830
20831 /*
20832 * ":function func" with only function name: list function.
20833 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020834 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 {
20836 if (!ends_excmd(*skipwhite(p)))
20837 {
20838 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020839 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 }
20841 eap->nextcmd = check_nextcmd(p);
20842 if (eap->nextcmd != NULL)
20843 *p = NUL;
20844 if (!eap->skip && !got_int)
20845 {
20846 fp = find_func(name);
20847 if (fp != NULL)
20848 {
20849 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020850 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020852 if (FUNCLINE(fp, j) == NULL)
20853 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 msg_putchar('\n');
20855 msg_outnum((long)(j + 1));
20856 if (j < 9)
20857 msg_putchar(' ');
20858 if (j < 99)
20859 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020860 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 out_flush(); /* show a line at a time */
20862 ui_breakcheck();
20863 }
20864 if (!got_int)
20865 {
20866 msg_putchar('\n');
20867 msg_puts((char_u *)" endfunction");
20868 }
20869 }
20870 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020871 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020873 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 }
20875
20876 /*
20877 * ":function name(arg1, arg2)" Define function.
20878 */
20879 p = skipwhite(p);
20880 if (*p != '(')
20881 {
20882 if (!eap->skip)
20883 {
20884 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020885 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 }
20887 /* attempt to continue by skipping some text */
20888 if (vim_strchr(p, '(') != NULL)
20889 p = vim_strchr(p, '(');
20890 }
20891 p = skipwhite(p + 1);
20892
20893 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20894 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20895
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020896 if (!eap->skip)
20897 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020898 /* Check the name of the function. Unless it's a dictionary function
20899 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020900 if (name != NULL)
20901 arg = name;
20902 else
20903 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020904 if (arg != NULL && (fudi.fd_di == NULL
20905 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020906 {
20907 if (*arg == K_SPECIAL)
20908 j = 3;
20909 else
20910 j = 0;
20911 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20912 : eval_isnamec(arg[j])))
20913 ++j;
20914 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020915 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020916 }
20917 }
20918
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 /*
20920 * Isolate the arguments: "arg1, arg2, ...)"
20921 */
20922 while (*p != ')')
20923 {
20924 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20925 {
20926 varargs = TRUE;
20927 p += 3;
20928 mustend = TRUE;
20929 }
20930 else
20931 {
20932 arg = p;
20933 while (ASCII_ISALNUM(*p) || *p == '_')
20934 ++p;
20935 if (arg == p || isdigit(*arg)
20936 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20937 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20938 {
20939 if (!eap->skip)
20940 EMSG2(_("E125: Illegal argument: %s"), arg);
20941 break;
20942 }
20943 if (ga_grow(&newargs, 1) == FAIL)
20944 goto erret;
20945 c = *p;
20946 *p = NUL;
20947 arg = vim_strsave(arg);
20948 if (arg == NULL)
20949 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020950
20951 /* Check for duplicate argument name. */
20952 for (i = 0; i < newargs.ga_len; ++i)
20953 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20954 {
20955 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20956 goto erret;
20957 }
20958
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20960 *p = c;
20961 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 if (*p == ',')
20963 ++p;
20964 else
20965 mustend = TRUE;
20966 }
20967 p = skipwhite(p);
20968 if (mustend && *p != ')')
20969 {
20970 if (!eap->skip)
20971 EMSG2(_(e_invarg2), eap->arg);
20972 break;
20973 }
20974 }
20975 ++p; /* skip the ')' */
20976
Bram Moolenaare9a41262005-01-15 22:18:47 +000020977 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 for (;;)
20979 {
20980 p = skipwhite(p);
20981 if (STRNCMP(p, "range", 5) == 0)
20982 {
20983 flags |= FC_RANGE;
20984 p += 5;
20985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020986 else if (STRNCMP(p, "dict", 4) == 0)
20987 {
20988 flags |= FC_DICT;
20989 p += 4;
20990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 else if (STRNCMP(p, "abort", 5) == 0)
20992 {
20993 flags |= FC_ABORT;
20994 p += 5;
20995 }
20996 else
20997 break;
20998 }
20999
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021000 /* When there is a line break use what follows for the function body.
21001 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21002 if (*p == '\n')
21003 line_arg = p + 1;
21004 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 EMSG(_(e_trailing));
21006
21007 /*
21008 * Read the body of the function, until ":endfunction" is found.
21009 */
21010 if (KeyTyped)
21011 {
21012 /* Check if the function already exists, don't let the user type the
21013 * whole function before telling him it doesn't work! For a script we
21014 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021015 if (!eap->skip && !eap->forceit)
21016 {
21017 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21018 EMSG(_(e_funcdict));
21019 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021020 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021023 if (!eap->skip && did_emsg)
21024 goto erret;
21025
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 msg_putchar('\n'); /* don't overwrite the function name */
21027 cmdline_row = msg_row;
21028 }
21029
21030 indent = 2;
21031 nesting = 0;
21032 for (;;)
21033 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021034 if (KeyTyped)
21035 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021037 sourcing_lnum_off = sourcing_lnum;
21038
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021039 if (line_arg != NULL)
21040 {
21041 /* Use eap->arg, split up in parts by line breaks. */
21042 theline = line_arg;
21043 p = vim_strchr(theline, '\n');
21044 if (p == NULL)
21045 line_arg += STRLEN(line_arg);
21046 else
21047 {
21048 *p = NUL;
21049 line_arg = p + 1;
21050 }
21051 }
21052 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 theline = getcmdline(':', 0L, indent);
21054 else
21055 theline = eap->getline(':', eap->cookie, indent);
21056 if (KeyTyped)
21057 lines_left = Rows - 1;
21058 if (theline == NULL)
21059 {
21060 EMSG(_("E126: Missing :endfunction"));
21061 goto erret;
21062 }
21063
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021064 /* Detect line continuation: sourcing_lnum increased more than one. */
21065 if (sourcing_lnum > sourcing_lnum_off + 1)
21066 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21067 else
21068 sourcing_lnum_off = 0;
21069
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 if (skip_until != NULL)
21071 {
21072 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21073 * don't check for ":endfunc". */
21074 if (STRCMP(theline, skip_until) == 0)
21075 {
21076 vim_free(skip_until);
21077 skip_until = NULL;
21078 }
21079 }
21080 else
21081 {
21082 /* skip ':' and blanks*/
21083 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21084 ;
21085
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021086 /* Check for "endfunction". */
21087 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021089 if (line_arg == NULL)
21090 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 break;
21092 }
21093
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021094 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095 * at "end". */
21096 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21097 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021098 else if (STRNCMP(p, "if", 2) == 0
21099 || STRNCMP(p, "wh", 2) == 0
21100 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 || STRNCMP(p, "try", 3) == 0)
21102 indent += 2;
21103
21104 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021105 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021107 if (*p == '!')
21108 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 p += eval_fname_script(p);
21110 if (ASCII_ISALPHA(*p))
21111 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021112 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 if (*skipwhite(p) == '(')
21114 {
21115 ++nesting;
21116 indent += 2;
21117 }
21118 }
21119 }
21120
21121 /* Check for ":append" or ":insert". */
21122 p = skip_range(p, NULL);
21123 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21124 || (p[0] == 'i'
21125 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21126 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21127 skip_until = vim_strsave((char_u *)".");
21128
21129 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21130 arg = skipwhite(skiptowhite(p));
21131 if (arg[0] == '<' && arg[1] =='<'
21132 && ((p[0] == 'p' && p[1] == 'y'
21133 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21134 || (p[0] == 'p' && p[1] == 'e'
21135 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21136 || (p[0] == 't' && p[1] == 'c'
21137 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021138 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21139 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21141 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021142 || (p[0] == 'm' && p[1] == 'z'
21143 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 ))
21145 {
21146 /* ":python <<" continues until a dot, like ":append" */
21147 p = skipwhite(arg + 2);
21148 if (*p == NUL)
21149 skip_until = vim_strsave((char_u *)".");
21150 else
21151 skip_until = vim_strsave(p);
21152 }
21153 }
21154
21155 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021156 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021157 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021158 if (line_arg == NULL)
21159 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021161 }
21162
21163 /* Copy the line to newly allocated memory. get_one_sourceline()
21164 * allocates 250 bytes per line, this saves 80% on average. The cost
21165 * is an extra alloc/free. */
21166 p = vim_strsave(theline);
21167 if (p != NULL)
21168 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021169 if (line_arg == NULL)
21170 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021171 theline = p;
21172 }
21173
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021174 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21175
21176 /* Add NULL lines for continuation lines, so that the line count is
21177 * equal to the index in the growarray. */
21178 while (sourcing_lnum_off-- > 0)
21179 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021180
21181 /* Check for end of eap->arg. */
21182 if (line_arg != NULL && *line_arg == NUL)
21183 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 }
21185
21186 /* Don't define the function when skipping commands or when an error was
21187 * detected. */
21188 if (eap->skip || did_emsg)
21189 goto erret;
21190
21191 /*
21192 * If there are no errors, add the function
21193 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021194 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021195 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021196 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021197 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021198 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021199 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021200 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021201 goto erret;
21202 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021203
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021204 fp = find_func(name);
21205 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021207 if (!eap->forceit)
21208 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021209 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021210 goto erret;
21211 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021212 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021213 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021214 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021215 name);
21216 goto erret;
21217 }
21218 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021219 ga_clear_strings(&(fp->uf_args));
21220 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021221 vim_free(name);
21222 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 }
21225 else
21226 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021227 char numbuf[20];
21228
21229 fp = NULL;
21230 if (fudi.fd_newkey == NULL && !eap->forceit)
21231 {
21232 EMSG(_(e_funcdict));
21233 goto erret;
21234 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021235 if (fudi.fd_di == NULL)
21236 {
21237 /* Can't add a function to a locked dictionary */
21238 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21239 goto erret;
21240 }
21241 /* Can't change an existing function if it is locked */
21242 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21243 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244
21245 /* Give the function a sequential number. Can only be used with a
21246 * Funcref! */
21247 vim_free(name);
21248 sprintf(numbuf, "%d", ++func_nr);
21249 name = vim_strsave((char_u *)numbuf);
21250 if (name == NULL)
21251 goto erret;
21252 }
21253
21254 if (fp == NULL)
21255 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021256 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021257 {
21258 int slen, plen;
21259 char_u *scriptname;
21260
21261 /* Check that the autoload name matches the script name. */
21262 j = FAIL;
21263 if (sourcing_name != NULL)
21264 {
21265 scriptname = autoload_name(name);
21266 if (scriptname != NULL)
21267 {
21268 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021269 plen = (int)STRLEN(p);
21270 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021271 if (slen > plen && fnamecmp(p,
21272 sourcing_name + slen - plen) == 0)
21273 j = OK;
21274 vim_free(scriptname);
21275 }
21276 }
21277 if (j == FAIL)
21278 {
21279 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21280 goto erret;
21281 }
21282 }
21283
21284 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 if (fp == NULL)
21286 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021287
21288 if (fudi.fd_dict != NULL)
21289 {
21290 if (fudi.fd_di == NULL)
21291 {
21292 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021293 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 if (fudi.fd_di == NULL)
21295 {
21296 vim_free(fp);
21297 goto erret;
21298 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021299 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21300 {
21301 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021302 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021303 goto erret;
21304 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021305 }
21306 else
21307 /* overwrite existing dict entry */
21308 clear_tv(&fudi.fd_di->di_tv);
21309 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021310 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021311 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021312 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021313
21314 /* behave like "dict" was used */
21315 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021316 }
21317
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021319 STRCPY(fp->uf_name, name);
21320 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021322 fp->uf_args = newargs;
21323 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021324#ifdef FEAT_PROFILE
21325 fp->uf_tml_count = NULL;
21326 fp->uf_tml_total = NULL;
21327 fp->uf_tml_self = NULL;
21328 fp->uf_profiling = FALSE;
21329 if (prof_def_func())
21330 func_do_profile(fp);
21331#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021332 fp->uf_varargs = varargs;
21333 fp->uf_flags = flags;
21334 fp->uf_calls = 0;
21335 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021336 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337
21338erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 ga_clear_strings(&newargs);
21340 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021341ret_free:
21342 vim_free(skip_until);
21343 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346}
21347
21348/*
21349 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021350 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021352 * flags:
21353 * TFN_INT: internal function name OK
21354 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 * Advances "pp" to just after the function name (if no error).
21356 */
21357 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021358trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 char_u **pp;
21360 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021361 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021362 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021364 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 char_u *start;
21366 char_u *end;
21367 int lead;
21368 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021370 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021371
21372 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021373 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021375
21376 /* Check for hard coded <SNR>: already translated function ID (from a user
21377 * command). */
21378 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21379 && (*pp)[2] == (int)KE_SNR)
21380 {
21381 *pp += 3;
21382 len = get_id_len(pp) + 3;
21383 return vim_strnsave(start, len);
21384 }
21385
21386 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21387 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021389 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021391
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021392 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21393 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021394 if (end == start)
21395 {
21396 if (!skip)
21397 EMSG(_("E129: Function name required"));
21398 goto theend;
21399 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021400 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021401 {
21402 /*
21403 * Report an invalid expression in braces, unless the expression
21404 * evaluation has been cancelled due to an aborting error, an
21405 * interrupt, or an exception.
21406 */
21407 if (!aborting())
21408 {
21409 if (end != NULL)
21410 EMSG2(_(e_invarg2), start);
21411 }
21412 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021413 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021414 goto theend;
21415 }
21416
21417 if (lv.ll_tv != NULL)
21418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021419 if (fdp != NULL)
21420 {
21421 fdp->fd_dict = lv.ll_dict;
21422 fdp->fd_newkey = lv.ll_newkey;
21423 lv.ll_newkey = NULL;
21424 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021425 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021426 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21427 {
21428 name = vim_strsave(lv.ll_tv->vval.v_string);
21429 *pp = end;
21430 }
21431 else
21432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021433 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21434 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021435 EMSG(_(e_funcref));
21436 else
21437 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021438 name = NULL;
21439 }
21440 goto theend;
21441 }
21442
21443 if (lv.ll_name == NULL)
21444 {
21445 /* Error found, but continue after the function name. */
21446 *pp = end;
21447 goto theend;
21448 }
21449
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021450 /* Check if the name is a Funcref. If so, use the value. */
21451 if (lv.ll_exp_name != NULL)
21452 {
21453 len = (int)STRLEN(lv.ll_exp_name);
21454 name = deref_func_name(lv.ll_exp_name, &len);
21455 if (name == lv.ll_exp_name)
21456 name = NULL;
21457 }
21458 else
21459 {
21460 len = (int)(end - *pp);
21461 name = deref_func_name(*pp, &len);
21462 if (name == *pp)
21463 name = NULL;
21464 }
21465 if (name != NULL)
21466 {
21467 name = vim_strsave(name);
21468 *pp = end;
21469 goto theend;
21470 }
21471
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021472 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021473 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021474 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021475 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21476 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21477 {
21478 /* When there was "s:" already or the name expanded to get a
21479 * leading "s:" then remove it. */
21480 lv.ll_name += 2;
21481 len -= 2;
21482 lead = 2;
21483 }
21484 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021485 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021486 {
21487 if (lead == 2) /* skip over "s:" */
21488 lv.ll_name += 2;
21489 len = (int)(end - lv.ll_name);
21490 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021491
21492 /*
21493 * Copy the function name to allocated memory.
21494 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21495 * Accept <SNR>123_name() outside a script.
21496 */
21497 if (skip)
21498 lead = 0; /* do nothing */
21499 else if (lead > 0)
21500 {
21501 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021502 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21503 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021504 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021505 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021506 if (current_SID <= 0)
21507 {
21508 EMSG(_(e_usingsid));
21509 goto theend;
21510 }
21511 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21512 lead += (int)STRLEN(sid_buf);
21513 }
21514 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021515 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021516 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021517 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021518 goto theend;
21519 }
21520 name = alloc((unsigned)(len + lead + 1));
21521 if (name != NULL)
21522 {
21523 if (lead > 0)
21524 {
21525 name[0] = K_SPECIAL;
21526 name[1] = KS_EXTRA;
21527 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021528 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021529 STRCPY(name + 3, sid_buf);
21530 }
21531 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21532 name[len + lead] = NUL;
21533 }
21534 *pp = end;
21535
21536theend:
21537 clear_lval(&lv);
21538 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539}
21540
21541/*
21542 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21543 * Return 2 if "p" starts with "s:".
21544 * Return 0 otherwise.
21545 */
21546 static int
21547eval_fname_script(p)
21548 char_u *p;
21549{
21550 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21551 || STRNICMP(p + 1, "SNR>", 4) == 0))
21552 return 5;
21553 if (p[0] == 's' && p[1] == ':')
21554 return 2;
21555 return 0;
21556}
21557
21558/*
21559 * Return TRUE if "p" starts with "<SID>" or "s:".
21560 * Only works if eval_fname_script() returned non-zero for "p"!
21561 */
21562 static int
21563eval_fname_sid(p)
21564 char_u *p;
21565{
21566 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21567}
21568
21569/*
21570 * List the head of the function: "name(arg1, arg2)".
21571 */
21572 static void
21573list_func_head(fp, indent)
21574 ufunc_T *fp;
21575 int indent;
21576{
21577 int j;
21578
21579 msg_start();
21580 if (indent)
21581 MSG_PUTS(" ");
21582 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021583 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 {
21585 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021586 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 }
21588 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021589 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021591 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 {
21593 if (j)
21594 MSG_PUTS(", ");
21595 msg_puts(FUNCARG(fp, j));
21596 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021597 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 {
21599 if (j)
21600 MSG_PUTS(", ");
21601 MSG_PUTS("...");
21602 }
21603 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021604 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021605 if (p_verbose > 0)
21606 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607}
21608
21609/*
21610 * Find a function by name, return pointer to it in ufuncs.
21611 * Return NULL for unknown function.
21612 */
21613 static ufunc_T *
21614find_func(name)
21615 char_u *name;
21616{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021617 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021619 hi = hash_find(&func_hashtab, name);
21620 if (!HASHITEM_EMPTY(hi))
21621 return HI2UF(hi);
21622 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623}
21624
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021625#if defined(EXITFREE) || defined(PROTO)
21626 void
21627free_all_functions()
21628{
21629 hashitem_T *hi;
21630
21631 /* Need to start all over every time, because func_free() may change the
21632 * hash table. */
21633 while (func_hashtab.ht_used > 0)
21634 for (hi = func_hashtab.ht_array; ; ++hi)
21635 if (!HASHITEM_EMPTY(hi))
21636 {
21637 func_free(HI2UF(hi));
21638 break;
21639 }
21640}
21641#endif
21642
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021643/*
21644 * Return TRUE if a function "name" exists.
21645 */
21646 static int
21647function_exists(name)
21648 char_u *name;
21649{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021650 char_u *nm = name;
21651 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021652 int n = FALSE;
21653
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021654 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021655 nm = skipwhite(nm);
21656
21657 /* Only accept "funcname", "funcname ", "funcname (..." and
21658 * "funcname(...", not "funcname!...". */
21659 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021660 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021661 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021662 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021663 else
21664 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021665 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021666 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021667 return n;
21668}
21669
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021670/*
21671 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021672 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021673 */
21674 static int
21675builtin_function(name)
21676 char_u *name;
21677{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021678 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21679 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021680}
21681
Bram Moolenaar05159a02005-02-26 23:04:13 +000021682#if defined(FEAT_PROFILE) || defined(PROTO)
21683/*
21684 * Start profiling function "fp".
21685 */
21686 static void
21687func_do_profile(fp)
21688 ufunc_T *fp;
21689{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021690 int len = fp->uf_lines.ga_len;
21691
21692 if (len == 0)
21693 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021694 fp->uf_tm_count = 0;
21695 profile_zero(&fp->uf_tm_self);
21696 profile_zero(&fp->uf_tm_total);
21697 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021698 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021699 if (fp->uf_tml_total == NULL)
21700 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021701 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021702 if (fp->uf_tml_self == NULL)
21703 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021704 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021705 fp->uf_tml_idx = -1;
21706 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21707 || fp->uf_tml_self == NULL)
21708 return; /* out of memory */
21709
21710 fp->uf_profiling = TRUE;
21711}
21712
21713/*
21714 * Dump the profiling results for all functions in file "fd".
21715 */
21716 void
21717func_dump_profile(fd)
21718 FILE *fd;
21719{
21720 hashitem_T *hi;
21721 int todo;
21722 ufunc_T *fp;
21723 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021724 ufunc_T **sorttab;
21725 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021727 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021728 if (todo == 0)
21729 return; /* nothing to dump */
21730
Bram Moolenaar73830342005-02-28 22:48:19 +000021731 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21732
Bram Moolenaar05159a02005-02-26 23:04:13 +000021733 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21734 {
21735 if (!HASHITEM_EMPTY(hi))
21736 {
21737 --todo;
21738 fp = HI2UF(hi);
21739 if (fp->uf_profiling)
21740 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021741 if (sorttab != NULL)
21742 sorttab[st_len++] = fp;
21743
Bram Moolenaar05159a02005-02-26 23:04:13 +000021744 if (fp->uf_name[0] == K_SPECIAL)
21745 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21746 else
21747 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21748 if (fp->uf_tm_count == 1)
21749 fprintf(fd, "Called 1 time\n");
21750 else
21751 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21752 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21753 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21754 fprintf(fd, "\n");
21755 fprintf(fd, "count total (s) self (s)\n");
21756
21757 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21758 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021759 if (FUNCLINE(fp, i) == NULL)
21760 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021761 prof_func_line(fd, fp->uf_tml_count[i],
21762 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021763 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21764 }
21765 fprintf(fd, "\n");
21766 }
21767 }
21768 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021769
21770 if (sorttab != NULL && st_len > 0)
21771 {
21772 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21773 prof_total_cmp);
21774 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21775 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21776 prof_self_cmp);
21777 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21778 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021779
21780 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021781}
Bram Moolenaar73830342005-02-28 22:48:19 +000021782
21783 static void
21784prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21785 FILE *fd;
21786 ufunc_T **sorttab;
21787 int st_len;
21788 char *title;
21789 int prefer_self; /* when equal print only self time */
21790{
21791 int i;
21792 ufunc_T *fp;
21793
21794 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21795 fprintf(fd, "count total (s) self (s) function\n");
21796 for (i = 0; i < 20 && i < st_len; ++i)
21797 {
21798 fp = sorttab[i];
21799 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21800 prefer_self);
21801 if (fp->uf_name[0] == K_SPECIAL)
21802 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21803 else
21804 fprintf(fd, " %s()\n", fp->uf_name);
21805 }
21806 fprintf(fd, "\n");
21807}
21808
21809/*
21810 * Print the count and times for one function or function line.
21811 */
21812 static void
21813prof_func_line(fd, count, total, self, prefer_self)
21814 FILE *fd;
21815 int count;
21816 proftime_T *total;
21817 proftime_T *self;
21818 int prefer_self; /* when equal print only self time */
21819{
21820 if (count > 0)
21821 {
21822 fprintf(fd, "%5d ", count);
21823 if (prefer_self && profile_equal(total, self))
21824 fprintf(fd, " ");
21825 else
21826 fprintf(fd, "%s ", profile_msg(total));
21827 if (!prefer_self && profile_equal(total, self))
21828 fprintf(fd, " ");
21829 else
21830 fprintf(fd, "%s ", profile_msg(self));
21831 }
21832 else
21833 fprintf(fd, " ");
21834}
21835
21836/*
21837 * Compare function for total time sorting.
21838 */
21839 static int
21840#ifdef __BORLANDC__
21841_RTLENTRYF
21842#endif
21843prof_total_cmp(s1, s2)
21844 const void *s1;
21845 const void *s2;
21846{
21847 ufunc_T *p1, *p2;
21848
21849 p1 = *(ufunc_T **)s1;
21850 p2 = *(ufunc_T **)s2;
21851 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21852}
21853
21854/*
21855 * Compare function for self time sorting.
21856 */
21857 static int
21858#ifdef __BORLANDC__
21859_RTLENTRYF
21860#endif
21861prof_self_cmp(s1, s2)
21862 const void *s1;
21863 const void *s2;
21864{
21865 ufunc_T *p1, *p2;
21866
21867 p1 = *(ufunc_T **)s1;
21868 p2 = *(ufunc_T **)s2;
21869 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21870}
21871
Bram Moolenaar05159a02005-02-26 23:04:13 +000021872#endif
21873
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021874/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021875 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021876 * Return TRUE if a package was loaded.
21877 */
21878 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021879script_autoload(name, reload)
21880 char_u *name;
21881 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021882{
21883 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021884 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021885 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021886 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021887
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021888 /* Return quickly when autoload disabled. */
21889 if (no_autoload)
21890 return FALSE;
21891
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021892 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021893 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021894 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021895 return FALSE;
21896
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021897 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021898
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021899 /* Find the name in the list of previously loaded package names. Skip
21900 * "autoload/", it's always the same. */
21901 for (i = 0; i < ga_loaded.ga_len; ++i)
21902 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21903 break;
21904 if (!reload && i < ga_loaded.ga_len)
21905 ret = FALSE; /* was loaded already */
21906 else
21907 {
21908 /* Remember the name if it wasn't loaded already. */
21909 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21910 {
21911 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21912 tofree = NULL;
21913 }
21914
21915 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021916 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021917 ret = TRUE;
21918 }
21919
21920 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021921 return ret;
21922}
21923
21924/*
21925 * Return the autoload script name for a function or variable name.
21926 * Returns NULL when out of memory.
21927 */
21928 static char_u *
21929autoload_name(name)
21930 char_u *name;
21931{
21932 char_u *p;
21933 char_u *scriptname;
21934
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021935 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021936 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21937 if (scriptname == NULL)
21938 return FALSE;
21939 STRCPY(scriptname, "autoload/");
21940 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021941 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021942 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021943 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021944 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021945 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021946}
21947
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21949
21950/*
21951 * Function given to ExpandGeneric() to obtain the list of user defined
21952 * function names.
21953 */
21954 char_u *
21955get_user_func_name(xp, idx)
21956 expand_T *xp;
21957 int idx;
21958{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021959 static long_u done;
21960 static hashitem_T *hi;
21961 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962
21963 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021965 done = 0;
21966 hi = func_hashtab.ht_array;
21967 }
21968 if (done < func_hashtab.ht_used)
21969 {
21970 if (done++ > 0)
21971 ++hi;
21972 while (HASHITEM_EMPTY(hi))
21973 ++hi;
21974 fp = HI2UF(hi);
21975
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010021976 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010021977 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010021978
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021979 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21980 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981
21982 cat_func_name(IObuff, fp);
21983 if (xp->xp_context != EXPAND_USER_FUNC)
21984 {
21985 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021986 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 STRCAT(IObuff, ")");
21988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 return IObuff;
21990 }
21991 return NULL;
21992}
21993
21994#endif /* FEAT_CMDL_COMPL */
21995
21996/*
21997 * Copy the function name of "fp" to buffer "buf".
21998 * "buf" must be able to hold the function name plus three bytes.
21999 * Takes care of script-local function names.
22000 */
22001 static void
22002cat_func_name(buf, fp)
22003 char_u *buf;
22004 ufunc_T *fp;
22005{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022006 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 {
22008 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022009 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 }
22011 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022012 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013}
22014
22015/*
22016 * ":delfunction {name}"
22017 */
22018 void
22019ex_delfunction(eap)
22020 exarg_T *eap;
22021{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022022 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 char_u *p;
22024 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026
22027 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022028 name = trans_function_name(&p, eap->skip, 0, &fudi);
22029 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022031 {
22032 if (fudi.fd_dict != NULL && !eap->skip)
22033 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 if (!ends_excmd(*skipwhite(p)))
22037 {
22038 vim_free(name);
22039 EMSG(_(e_trailing));
22040 return;
22041 }
22042 eap->nextcmd = check_nextcmd(p);
22043 if (eap->nextcmd != NULL)
22044 *p = NUL;
22045
22046 if (!eap->skip)
22047 fp = find_func(name);
22048 vim_free(name);
22049
22050 if (!eap->skip)
22051 {
22052 if (fp == NULL)
22053 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022054 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 return;
22056 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022057 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 {
22059 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22060 return;
22061 }
22062
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022063 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022065 /* Delete the dict item that refers to the function, it will
22066 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022067 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022069 else
22070 func_free(fp);
22071 }
22072}
22073
22074/*
22075 * Free a function and remove it from the list of functions.
22076 */
22077 static void
22078func_free(fp)
22079 ufunc_T *fp;
22080{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022081 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022082
22083 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022084 ga_clear_strings(&(fp->uf_args));
22085 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022086#ifdef FEAT_PROFILE
22087 vim_free(fp->uf_tml_count);
22088 vim_free(fp->uf_tml_total);
22089 vim_free(fp->uf_tml_self);
22090#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022091
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022092 /* remove the function from the function hashtable */
22093 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22094 if (HASHITEM_EMPTY(hi))
22095 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022096 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022097 hash_remove(&func_hashtab, hi);
22098
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022099 vim_free(fp);
22100}
22101
22102/*
22103 * Unreference a Function: decrement the reference count and free it when it
22104 * becomes zero. Only for numbered functions.
22105 */
22106 static void
22107func_unref(name)
22108 char_u *name;
22109{
22110 ufunc_T *fp;
22111
22112 if (name != NULL && isdigit(*name))
22113 {
22114 fp = find_func(name);
22115 if (fp == NULL)
22116 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022117 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022118 {
22119 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022120 * when "uf_calls" becomes zero. */
22121 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022122 func_free(fp);
22123 }
22124 }
22125}
22126
22127/*
22128 * Count a reference to a Function.
22129 */
22130 static void
22131func_ref(name)
22132 char_u *name;
22133{
22134 ufunc_T *fp;
22135
22136 if (name != NULL && isdigit(*name))
22137 {
22138 fp = find_func(name);
22139 if (fp == NULL)
22140 EMSG2(_(e_intern2), "func_ref()");
22141 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022142 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 }
22144}
22145
22146/*
22147 * Call a user function.
22148 */
22149 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022150call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 ufunc_T *fp; /* pointer to function */
22152 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022153 typval_T *argvars; /* arguments */
22154 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 linenr_T firstline; /* first line of range */
22156 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158{
Bram Moolenaar33570922005-01-25 22:26:29 +000022159 char_u *save_sourcing_name;
22160 linenr_T save_sourcing_lnum;
22161 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022162 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 int save_did_emsg;
22164 static int depth = 0;
22165 dictitem_T *v;
22166 int fixvar_idx = 0; /* index in fixvar[] */
22167 int i;
22168 int ai;
22169 char_u numbuf[NUMBUFLEN];
22170 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022171#ifdef FEAT_PROFILE
22172 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022173 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175
22176 /* If depth of calling is getting too high, don't execute the function */
22177 if (depth >= p_mfd)
22178 {
22179 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022180 rettv->v_type = VAR_NUMBER;
22181 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 return;
22183 }
22184 ++depth;
22185
22186 line_breakcheck(); /* check for CTRL-C hit */
22187
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022188 fc = (funccall_T *)alloc(sizeof(funccall_T));
22189 fc->caller = current_funccal;
22190 current_funccal = fc;
22191 fc->func = fp;
22192 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022193 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022194 fc->linenr = 0;
22195 fc->returned = FALSE;
22196 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022198 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22199 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022202 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22204 * each argument variable and saves a lot of time.
22205 */
22206 /*
22207 * Init l: variables.
22208 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022209 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022210 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022211 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022212 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22213 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022214 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022215 name = v->di_key;
22216 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022218 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022219 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022220 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022221 v->di_tv.vval.v_dict = selfdict;
22222 ++selfdict->dv_refcount;
22223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022224
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 /*
22226 * Init a: variables.
22227 * Set a:0 to "argcount".
22228 * Set a:000 to a list with room for the "..." arguments.
22229 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022230 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22231 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022232 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022233 /* Use "name" to avoid a warning from some compiler that checks the
22234 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022235 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022236 name = v->di_key;
22237 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022239 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022240 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022241 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022242 v->di_tv.vval.v_list = &fc->l_varlist;
22243 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22244 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22245 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022246
22247 /*
22248 * Set a:firstline to "firstline" and a:lastline to "lastline".
22249 * Set a:name to named arguments.
22250 * Set a:N to the "..." arguments.
22251 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022252 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022254 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 (varnumber_T)lastline);
22256 for (i = 0; i < argcount; ++i)
22257 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022258 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 if (ai < 0)
22260 /* named argument a:name */
22261 name = FUNCARG(fp, i);
22262 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022263 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 /* "..." argument a:1, a:2, etc. */
22265 sprintf((char *)numbuf, "%d", ai + 1);
22266 name = numbuf;
22267 }
22268 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22269 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022270 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22272 }
22273 else
22274 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022275 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22276 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 if (v == NULL)
22278 break;
22279 v->di_flags = DI_FLAGS_RO;
22280 }
22281 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022282 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022283
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022284 /* Note: the values are copied directly to avoid alloc/free.
22285 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022287 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022288
22289 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22290 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022291 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22292 fc->l_listitems[ai].li_tv = argvars[i];
22293 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022294 }
22295 }
22296
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 /* Don't redraw while executing the function. */
22298 ++RedrawingDisabled;
22299 save_sourcing_name = sourcing_name;
22300 save_sourcing_lnum = sourcing_lnum;
22301 sourcing_lnum = 1;
22302 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022303 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 if (sourcing_name != NULL)
22305 {
22306 if (save_sourcing_name != NULL
22307 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22308 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22309 else
22310 STRCPY(sourcing_name, "function ");
22311 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22312
22313 if (p_verbose >= 12)
22314 {
22315 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022316 verbose_enter_scroll();
22317
Bram Moolenaar555b2802005-05-19 21:08:39 +000022318 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 if (p_verbose >= 14)
22320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022322 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022323 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022324 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325
22326 msg_puts((char_u *)"(");
22327 for (i = 0; i < argcount; ++i)
22328 {
22329 if (i > 0)
22330 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022331 if (argvars[i].v_type == VAR_NUMBER)
22332 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 else
22334 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022335 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22336 if (s != NULL)
22337 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022338 if (vim_strsize(s) > MSG_BUF_CLEN)
22339 {
22340 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22341 s = buf;
22342 }
22343 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022344 vim_free(tofree);
22345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 }
22347 }
22348 msg_puts((char_u *)")");
22349 }
22350 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022351
22352 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 --no_wait_return;
22354 }
22355 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022356#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022357 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022358 {
22359 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22360 func_do_profile(fp);
22361 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022362 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022363 {
22364 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022365 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022366 profile_zero(&fp->uf_tm_children);
22367 }
22368 script_prof_save(&wait_start);
22369 }
22370#endif
22371
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022373 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 save_did_emsg = did_emsg;
22375 did_emsg = FALSE;
22376
22377 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022378 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22380
22381 --RedrawingDisabled;
22382
22383 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022384 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022386 clear_tv(rettv);
22387 rettv->v_type = VAR_NUMBER;
22388 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 }
22390
Bram Moolenaar05159a02005-02-26 23:04:13 +000022391#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022392 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022393 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022394 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022395 profile_end(&call_start);
22396 profile_sub_wait(&wait_start, &call_start);
22397 profile_add(&fp->uf_tm_total, &call_start);
22398 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022399 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022400 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022401 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22402 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022403 }
22404 }
22405#endif
22406
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 /* when being verbose, mention the return value */
22408 if (p_verbose >= 12)
22409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022411 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022414 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022415 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022416 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022417 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022420 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022421 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022422 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022423 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022424
Bram Moolenaar555b2802005-05-19 21:08:39 +000022425 /* The value may be very long. Skip the middle part, so that we
22426 * have some idea how it starts and ends. smsg() would always
22427 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022428 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022429 if (s != NULL)
22430 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022431 if (vim_strsize(s) > MSG_BUF_CLEN)
22432 {
22433 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22434 s = buf;
22435 }
22436 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022437 vim_free(tofree);
22438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
22440 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022441
22442 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 --no_wait_return;
22444 }
22445
22446 vim_free(sourcing_name);
22447 sourcing_name = save_sourcing_name;
22448 sourcing_lnum = save_sourcing_lnum;
22449 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022450#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022451 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022452 script_prof_restore(&wait_start);
22453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454
22455 if (p_verbose >= 12 && sourcing_name != NULL)
22456 {
22457 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022458 verbose_enter_scroll();
22459
Bram Moolenaar555b2802005-05-19 21:08:39 +000022460 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022462
22463 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 --no_wait_return;
22465 }
22466
22467 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022468 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022470
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022471 /* If the a:000 list and the l: and a: dicts are not referenced we can
22472 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022473 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22474 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22475 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22476 {
22477 free_funccal(fc, FALSE);
22478 }
22479 else
22480 {
22481 hashitem_T *hi;
22482 listitem_T *li;
22483 int todo;
22484
22485 /* "fc" is still in use. This can happen when returning "a:000" or
22486 * assigning "l:" to a global variable.
22487 * Link "fc" in the list for garbage collection later. */
22488 fc->caller = previous_funccal;
22489 previous_funccal = fc;
22490
22491 /* Make a copy of the a: variables, since we didn't do that above. */
22492 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22493 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22494 {
22495 if (!HASHITEM_EMPTY(hi))
22496 {
22497 --todo;
22498 v = HI2DI(hi);
22499 copy_tv(&v->di_tv, &v->di_tv);
22500 }
22501 }
22502
22503 /* Make a copy of the a:000 items, since we didn't do that above. */
22504 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22505 copy_tv(&li->li_tv, &li->li_tv);
22506 }
22507}
22508
22509/*
22510 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022511 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022512 */
22513 static int
22514can_free_funccal(fc, copyID)
22515 funccall_T *fc;
22516 int copyID;
22517{
22518 return (fc->l_varlist.lv_copyID != copyID
22519 && fc->l_vars.dv_copyID != copyID
22520 && fc->l_avars.dv_copyID != copyID);
22521}
22522
22523/*
22524 * Free "fc" and what it contains.
22525 */
22526 static void
22527free_funccal(fc, free_val)
22528 funccall_T *fc;
22529 int free_val; /* a: vars were allocated */
22530{
22531 listitem_T *li;
22532
22533 /* The a: variables typevals may not have been allocated, only free the
22534 * allocated variables. */
22535 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22536
22537 /* free all l: variables */
22538 vars_clear(&fc->l_vars.dv_hashtab);
22539
22540 /* Free the a:000 variables if they were allocated. */
22541 if (free_val)
22542 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22543 clear_tv(&li->li_tv);
22544
22545 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546}
22547
22548/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 * Add a number variable "name" to dict "dp" with value "nr".
22550 */
22551 static void
22552add_nr_var(dp, v, name, nr)
22553 dict_T *dp;
22554 dictitem_T *v;
22555 char *name;
22556 varnumber_T nr;
22557{
22558 STRCPY(v->di_key, name);
22559 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22560 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22561 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022562 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 v->di_tv.vval.v_number = nr;
22564}
22565
22566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 * ":return [expr]"
22568 */
22569 void
22570ex_return(eap)
22571 exarg_T *eap;
22572{
22573 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 int returning = FALSE;
22576
22577 if (current_funccal == NULL)
22578 {
22579 EMSG(_("E133: :return not inside a function"));
22580 return;
22581 }
22582
22583 if (eap->skip)
22584 ++emsg_skip;
22585
22586 eap->nextcmd = NULL;
22587 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022588 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 {
22590 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022591 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022593 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 }
22595 /* It's safer to return also on error. */
22596 else if (!eap->skip)
22597 {
22598 /*
22599 * Return unless the expression evaluation has been cancelled due to an
22600 * aborting error, an interrupt, or an exception.
22601 */
22602 if (!aborting())
22603 returning = do_return(eap, FALSE, TRUE, NULL);
22604 }
22605
22606 /* When skipping or the return gets pending, advance to the next command
22607 * in this line (!returning). Otherwise, ignore the rest of the line.
22608 * Following lines will be ignored by get_func_line(). */
22609 if (returning)
22610 eap->nextcmd = NULL;
22611 else if (eap->nextcmd == NULL) /* no argument */
22612 eap->nextcmd = check_nextcmd(arg);
22613
22614 if (eap->skip)
22615 --emsg_skip;
22616}
22617
22618/*
22619 * Return from a function. Possibly makes the return pending. Also called
22620 * for a pending return at the ":endtry" or after returning from an extra
22621 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022622 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022623 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 * FALSE when the return gets pending.
22625 */
22626 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022627do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 exarg_T *eap;
22629 int reanimate;
22630 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022631 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632{
22633 int idx;
22634 struct condstack *cstack = eap->cstack;
22635
22636 if (reanimate)
22637 /* Undo the return. */
22638 current_funccal->returned = FALSE;
22639
22640 /*
22641 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22642 * not in its finally clause (which then is to be executed next) is found.
22643 * In this case, make the ":return" pending for execution at the ":endtry".
22644 * Otherwise, return normally.
22645 */
22646 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22647 if (idx >= 0)
22648 {
22649 cstack->cs_pending[idx] = CSTP_RETURN;
22650
22651 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022652 /* A pending return again gets pending. "rettv" points to an
22653 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022655 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 else
22657 {
22658 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022659 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022661 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 {
22665 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022666 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 else
22669 EMSG(_(e_outofmem));
22670 }
22671 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022672 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673
22674 if (reanimate)
22675 {
22676 /* The pending return value could be overwritten by a ":return"
22677 * without argument in a finally clause; reset the default
22678 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022679 current_funccal->rettv->v_type = VAR_NUMBER;
22680 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 }
22682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022683 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 }
22685 else
22686 {
22687 current_funccal->returned = TRUE;
22688
22689 /* If the return is carried out now, store the return value. For
22690 * a return immediately after reanimation, the value is already
22691 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022692 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022694 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022697 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 }
22699 }
22700
22701 return idx < 0;
22702}
22703
22704/*
22705 * Free the variable with a pending return value.
22706 */
22707 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022708discard_pending_return(rettv)
22709 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710{
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712}
22713
22714/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022715 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 * is an allocated string. Used by report_pending() for verbose messages.
22717 */
22718 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022719get_return_cmd(rettv)
22720 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022722 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022723 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022724 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022726 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022727 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022728 if (s == NULL)
22729 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022730
22731 STRCPY(IObuff, ":return ");
22732 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22733 if (STRLEN(s) + 8 >= IOSIZE)
22734 STRCPY(IObuff + IOSIZE - 4, "...");
22735 vim_free(tofree);
22736 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737}
22738
22739/*
22740 * Get next function line.
22741 * Called by do_cmdline() to get the next line.
22742 * Returns allocated string, or NULL for end of function.
22743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 char_u *
22745get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022746 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022748 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749{
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022751 ufunc_T *fp = fcp->func;
22752 char_u *retval;
22753 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754
22755 /* If breakpoints have been added/deleted need to check for it. */
22756 if (fcp->dbg_tick != debug_tick)
22757 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022758 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 sourcing_lnum);
22760 fcp->dbg_tick = debug_tick;
22761 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022762#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022763 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022764 func_line_end(cookie);
22765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766
Bram Moolenaar05159a02005-02-26 23:04:13 +000022767 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022768 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22769 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 retval = NULL;
22771 else
22772 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022773 /* Skip NULL lines (continuation lines). */
22774 while (fcp->linenr < gap->ga_len
22775 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22776 ++fcp->linenr;
22777 if (fcp->linenr >= gap->ga_len)
22778 retval = NULL;
22779 else
22780 {
22781 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22782 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022783#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022784 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022785 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022786#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 }
22789
22790 /* Did we encounter a breakpoint? */
22791 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022793 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022795 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 sourcing_lnum);
22797 fcp->dbg_tick = debug_tick;
22798 }
22799
22800 return retval;
22801}
22802
Bram Moolenaar05159a02005-02-26 23:04:13 +000022803#if defined(FEAT_PROFILE) || defined(PROTO)
22804/*
22805 * Called when starting to read a function line.
22806 * "sourcing_lnum" must be correct!
22807 * When skipping lines it may not actually be executed, but we won't find out
22808 * until later and we need to store the time now.
22809 */
22810 void
22811func_line_start(cookie)
22812 void *cookie;
22813{
22814 funccall_T *fcp = (funccall_T *)cookie;
22815 ufunc_T *fp = fcp->func;
22816
22817 if (fp->uf_profiling && sourcing_lnum >= 1
22818 && sourcing_lnum <= fp->uf_lines.ga_len)
22819 {
22820 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022821 /* Skip continuation lines. */
22822 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22823 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022824 fp->uf_tml_execed = FALSE;
22825 profile_start(&fp->uf_tml_start);
22826 profile_zero(&fp->uf_tml_children);
22827 profile_get_wait(&fp->uf_tml_wait);
22828 }
22829}
22830
22831/*
22832 * Called when actually executing a function line.
22833 */
22834 void
22835func_line_exec(cookie)
22836 void *cookie;
22837{
22838 funccall_T *fcp = (funccall_T *)cookie;
22839 ufunc_T *fp = fcp->func;
22840
22841 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22842 fp->uf_tml_execed = TRUE;
22843}
22844
22845/*
22846 * Called when done with a function line.
22847 */
22848 void
22849func_line_end(cookie)
22850 void *cookie;
22851{
22852 funccall_T *fcp = (funccall_T *)cookie;
22853 ufunc_T *fp = fcp->func;
22854
22855 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22856 {
22857 if (fp->uf_tml_execed)
22858 {
22859 ++fp->uf_tml_count[fp->uf_tml_idx];
22860 profile_end(&fp->uf_tml_start);
22861 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022862 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022863 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22864 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022865 }
22866 fp->uf_tml_idx = -1;
22867 }
22868}
22869#endif
22870
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871/*
22872 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022873 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 */
22875 int
22876func_has_ended(cookie)
22877 void *cookie;
22878{
Bram Moolenaar33570922005-01-25 22:26:29 +000022879 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880
22881 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22882 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022883 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 || fcp->returned);
22885}
22886
22887/*
22888 * return TRUE if cookie indicates a function which "abort"s on errors.
22889 */
22890 int
22891func_has_abort(cookie)
22892 void *cookie;
22893{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022894 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895}
22896
22897#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22898typedef enum
22899{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022900 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22901 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22902 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903} var_flavour_T;
22904
22905static var_flavour_T var_flavour __ARGS((char_u *varname));
22906
22907 static var_flavour_T
22908var_flavour(varname)
22909 char_u *varname;
22910{
22911 char_u *p = varname;
22912
22913 if (ASCII_ISUPPER(*p))
22914 {
22915 while (*(++p))
22916 if (ASCII_ISLOWER(*p))
22917 return VAR_FLAVOUR_SESSION;
22918 return VAR_FLAVOUR_VIMINFO;
22919 }
22920 else
22921 return VAR_FLAVOUR_DEFAULT;
22922}
22923#endif
22924
22925#if defined(FEAT_VIMINFO) || defined(PROTO)
22926/*
22927 * Restore global vars that start with a capital from the viminfo file
22928 */
22929 int
22930read_viminfo_varlist(virp, writing)
22931 vir_T *virp;
22932 int writing;
22933{
22934 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022935 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022936 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937
22938 if (!writing && (find_viminfo_parameter('!') != NULL))
22939 {
22940 tab = vim_strchr(virp->vir_line + 1, '\t');
22941 if (tab != NULL)
22942 {
22943 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022944 switch (*tab)
22945 {
22946 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022947#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022948 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022949#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022950 case 'D': type = VAR_DICT; break;
22951 case 'L': type = VAR_LIST; break;
22952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953
22954 tab = vim_strchr(tab, '\t');
22955 if (tab != NULL)
22956 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022957 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022958 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022959 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022961#ifdef FEAT_FLOAT
22962 else if (type == VAR_FLOAT)
22963 (void)string2float(tab + 1, &tv.vval.v_float);
22964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022966 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022967 if (type == VAR_DICT || type == VAR_LIST)
22968 {
22969 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22970
22971 if (etv == NULL)
22972 /* Failed to parse back the dict or list, use it as a
22973 * string. */
22974 tv.v_type = VAR_STRING;
22975 else
22976 {
22977 vim_free(tv.vval.v_string);
22978 tv = *etv;
22979 }
22980 }
22981
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022982 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022983
22984 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022985 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022986 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22987 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 }
22989 }
22990 }
22991
22992 return viminfo_readline(virp);
22993}
22994
22995/*
22996 * Write global vars that start with a capital to the viminfo file
22997 */
22998 void
22999write_viminfo_varlist(fp)
23000 FILE *fp;
23001{
Bram Moolenaar33570922005-01-25 22:26:29 +000023002 hashitem_T *hi;
23003 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023004 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023005 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023006 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023007 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023008 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009
23010 if (find_viminfo_parameter('!') == NULL)
23011 return;
23012
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023013 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023014
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023015 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023016 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023018 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023020 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 this_var = HI2DI(hi);
23022 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023023 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023025 {
23026 case VAR_STRING: s = "STR"; break;
23027 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023028#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023029 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023030#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023031 case VAR_DICT: s = "DIC"; break;
23032 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023033 default: continue;
23034 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023036 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023037 if (p != NULL)
23038 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023039 vim_free(tofree);
23040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 }
23042 }
23043}
23044#endif
23045
23046#if defined(FEAT_SESSION) || defined(PROTO)
23047 int
23048store_session_globals(fd)
23049 FILE *fd;
23050{
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 hashitem_T *hi;
23052 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023053 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 char_u *p, *t;
23055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023056 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023057 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023059 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023061 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 this_var = HI2DI(hi);
23063 if ((this_var->di_tv.v_type == VAR_NUMBER
23064 || this_var->di_tv.v_type == VAR_STRING)
23065 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023066 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023067 /* Escape special characters with a backslash. Turn a LF and
23068 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023070 (char_u *)"\\\"\n\r");
23071 if (p == NULL) /* out of memory */
23072 break;
23073 for (t = p; *t != NUL; ++t)
23074 if (*t == '\n')
23075 *t = 'n';
23076 else if (*t == '\r')
23077 *t = 'r';
23078 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023079 this_var->di_key,
23080 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23081 : ' ',
23082 p,
23083 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23084 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023085 || put_eol(fd) == FAIL)
23086 {
23087 vim_free(p);
23088 return FAIL;
23089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 vim_free(p);
23091 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023092#ifdef FEAT_FLOAT
23093 else if (this_var->di_tv.v_type == VAR_FLOAT
23094 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23095 {
23096 float_T f = this_var->di_tv.vval.v_float;
23097 int sign = ' ';
23098
23099 if (f < 0)
23100 {
23101 f = -f;
23102 sign = '-';
23103 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023104 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023105 this_var->di_key, sign, f) < 0)
23106 || put_eol(fd) == FAIL)
23107 return FAIL;
23108 }
23109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 }
23111 }
23112 return OK;
23113}
23114#endif
23115
Bram Moolenaar661b1822005-07-28 22:36:45 +000023116/*
23117 * Display script name where an item was last set.
23118 * Should only be invoked when 'verbose' is non-zero.
23119 */
23120 void
23121last_set_msg(scriptID)
23122 scid_T scriptID;
23123{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023124 char_u *p;
23125
Bram Moolenaar661b1822005-07-28 22:36:45 +000023126 if (scriptID != 0)
23127 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023128 p = home_replace_save(NULL, get_scriptname(scriptID));
23129 if (p != NULL)
23130 {
23131 verbose_enter();
23132 MSG_PUTS(_("\n\tLast set from "));
23133 MSG_PUTS(p);
23134 vim_free(p);
23135 verbose_leave();
23136 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023137 }
23138}
23139
Bram Moolenaard812df62008-11-09 12:46:09 +000023140/*
23141 * List v:oldfiles in a nice way.
23142 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023143 void
23144ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023145 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023146{
23147 list_T *l = vimvars[VV_OLDFILES].vv_list;
23148 listitem_T *li;
23149 int nr = 0;
23150
23151 if (l == NULL)
23152 msg((char_u *)_("No old files"));
23153 else
23154 {
23155 msg_start();
23156 msg_scroll = TRUE;
23157 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23158 {
23159 msg_outnum((long)++nr);
23160 MSG_PUTS(": ");
23161 msg_outtrans(get_tv_string(&li->li_tv));
23162 msg_putchar('\n');
23163 out_flush(); /* output one line at a time */
23164 ui_breakcheck();
23165 }
23166 /* Assume "got_int" was set to truncate the listing. */
23167 got_int = FALSE;
23168
23169#ifdef FEAT_BROWSE_CMD
23170 if (cmdmod.browse)
23171 {
23172 quit_more = FALSE;
23173 nr = prompt_for_number(FALSE);
23174 msg_starthere();
23175 if (nr > 0)
23176 {
23177 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23178 (long)nr);
23179
23180 if (p != NULL)
23181 {
23182 p = expand_env_save(p);
23183 eap->arg = p;
23184 eap->cmdidx = CMD_edit;
23185 cmdmod.browse = FALSE;
23186 do_exedit(eap, NULL);
23187 vim_free(p);
23188 }
23189 }
23190 }
23191#endif
23192 }
23193}
23194
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195#endif /* FEAT_EVAL */
23196
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023198#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199
23200#ifdef WIN3264
23201/*
23202 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23203 */
23204static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23205static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23206static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23207
23208/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023209 * Get the short path (8.3) for the filename in "fnamep".
23210 * Only works for a valid file name.
23211 * When the path gets longer "fnamep" is changed and the allocated buffer
23212 * is put in "bufp".
23213 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23214 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 */
23216 static int
23217get_short_pathname(fnamep, bufp, fnamelen)
23218 char_u **fnamep;
23219 char_u **bufp;
23220 int *fnamelen;
23221{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023222 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 char_u *newbuf;
23224
23225 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 l = GetShortPathName(*fnamep, *fnamep, len);
23227 if (l > len - 1)
23228 {
23229 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023230 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 newbuf = vim_strnsave(*fnamep, l);
23232 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234
23235 vim_free(*bufp);
23236 *fnamep = *bufp = newbuf;
23237
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023238 /* Really should always succeed, as the buffer is big enough. */
23239 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 }
23241
23242 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023243 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244}
23245
23246/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023247 * Get the short path (8.3) for the filename in "fname". The converted
23248 * path is returned in "bufp".
23249 *
23250 * Some of the directories specified in "fname" may not exist. This function
23251 * will shorten the existing directories at the beginning of the path and then
23252 * append the remaining non-existing path.
23253 *
23254 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023255 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023256 * bufp - Pointer to an allocated buffer for the filename.
23257 * fnamelen - Length of the filename pointed to by fname
23258 *
23259 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 */
23261 static int
23262shortpath_for_invalid_fname(fname, bufp, fnamelen)
23263 char_u **fname;
23264 char_u **bufp;
23265 int *fnamelen;
23266{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023267 char_u *short_fname, *save_fname, *pbuf_unused;
23268 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023270 int old_len, len;
23271 int new_len, sfx_len;
23272 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273
23274 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023275 old_len = *fnamelen;
23276 save_fname = vim_strnsave(*fname, old_len);
23277 pbuf_unused = NULL;
23278 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023280 endp = save_fname + old_len - 1; /* Find the end of the copy */
23281 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023283 /*
23284 * Try shortening the supplied path till it succeeds by removing one
23285 * directory at a time from the tail of the path.
23286 */
23287 len = 0;
23288 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023290 /* go back one path-separator */
23291 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23292 --endp;
23293 if (endp <= save_fname)
23294 break; /* processed the complete path */
23295
23296 /*
23297 * Replace the path separator with a NUL and try to shorten the
23298 * resulting path.
23299 */
23300 ch = *endp;
23301 *endp = 0;
23302 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023303 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023304 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23305 {
23306 retval = FAIL;
23307 goto theend;
23308 }
23309 *endp = ch; /* preserve the string */
23310
23311 if (len > 0)
23312 break; /* successfully shortened the path */
23313
23314 /* failed to shorten the path. Skip the path separator */
23315 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 }
23317
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023318 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023320 /*
23321 * Succeeded in shortening the path. Now concatenate the shortened
23322 * path with the remaining path at the tail.
23323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023325 /* Compute the length of the new path. */
23326 sfx_len = (int)(save_endp - endp) + 1;
23327 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023329 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023331 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023333 /* There is not enough space in the currently allocated string,
23334 * copy it to a buffer big enough. */
23335 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023337 {
23338 retval = FAIL;
23339 goto theend;
23340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 }
23342 else
23343 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023344 /* Transfer short_fname to the main buffer (it's big enough),
23345 * unless get_short_pathname() did its work in-place. */
23346 *fname = *bufp = save_fname;
23347 if (short_fname != save_fname)
23348 vim_strncpy(save_fname, short_fname, len);
23349 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023351
23352 /* concat the not-shortened part of the path */
23353 vim_strncpy(*fname + len, endp, sfx_len);
23354 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023356
23357theend:
23358 vim_free(pbuf_unused);
23359 vim_free(save_fname);
23360
23361 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362}
23363
23364/*
23365 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023366 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 */
23368 static int
23369shortpath_for_partial(fnamep, bufp, fnamelen)
23370 char_u **fnamep;
23371 char_u **bufp;
23372 int *fnamelen;
23373{
23374 int sepcount, len, tflen;
23375 char_u *p;
23376 char_u *pbuf, *tfname;
23377 int hasTilde;
23378
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023379 /* Count up the path separators from the RHS.. so we know which part
23380 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023382 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 if (vim_ispathsep(*p))
23384 ++sepcount;
23385
23386 /* Need full path first (use expand_env() to remove a "~/") */
23387 hasTilde = (**fnamep == '~');
23388 if (hasTilde)
23389 pbuf = tfname = expand_env_save(*fnamep);
23390 else
23391 pbuf = tfname = FullName_save(*fnamep, FALSE);
23392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023393 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023395 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23396 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397
23398 if (len == 0)
23399 {
23400 /* Don't have a valid filename, so shorten the rest of the
23401 * path if we can. This CAN give us invalid 8.3 filenames, but
23402 * there's not a lot of point in guessing what it might be.
23403 */
23404 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023405 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 }
23408
23409 /* Count the paths backward to find the beginning of the desired string. */
23410 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023411 {
23412#ifdef FEAT_MBYTE
23413 if (has_mbyte)
23414 p -= mb_head_off(tfname, p);
23415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 if (vim_ispathsep(*p))
23417 {
23418 if (sepcount == 0 || (hasTilde && sepcount == 1))
23419 break;
23420 else
23421 sepcount --;
23422 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 if (hasTilde)
23425 {
23426 --p;
23427 if (p >= tfname)
23428 *p = '~';
23429 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023430 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 }
23432 else
23433 ++p;
23434
23435 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23436 vim_free(*bufp);
23437 *fnamelen = (int)STRLEN(p);
23438 *bufp = pbuf;
23439 *fnamep = p;
23440
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023441 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442}
23443#endif /* WIN3264 */
23444
23445/*
23446 * Adjust a filename, according to a string of modifiers.
23447 * *fnamep must be NUL terminated when called. When returning, the length is
23448 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023449 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 * When there is an error, *fnamep is set to NULL.
23451 */
23452 int
23453modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23454 char_u *src; /* string with modifiers */
23455 int *usedlen; /* characters after src that are used */
23456 char_u **fnamep; /* file name so far */
23457 char_u **bufp; /* buffer for allocated file name or NULL */
23458 int *fnamelen; /* length of fnamep */
23459{
23460 int valid = 0;
23461 char_u *tail;
23462 char_u *s, *p, *pbuf;
23463 char_u dirname[MAXPATHL];
23464 int c;
23465 int has_fullname = 0;
23466#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023467 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 int has_shortname = 0;
23469#endif
23470
23471repeat:
23472 /* ":p" - full path/file_name */
23473 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23474 {
23475 has_fullname = 1;
23476
23477 valid |= VALID_PATH;
23478 *usedlen += 2;
23479
23480 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23481 if ((*fnamep)[0] == '~'
23482#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23483 && ((*fnamep)[1] == '/'
23484# ifdef BACKSLASH_IN_FILENAME
23485 || (*fnamep)[1] == '\\'
23486# endif
23487 || (*fnamep)[1] == NUL)
23488
23489#endif
23490 )
23491 {
23492 *fnamep = expand_env_save(*fnamep);
23493 vim_free(*bufp); /* free any allocated file name */
23494 *bufp = *fnamep;
23495 if (*fnamep == NULL)
23496 return -1;
23497 }
23498
23499 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023500 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 {
23502 if (vim_ispathsep(*p)
23503 && p[1] == '.'
23504 && (p[2] == NUL
23505 || vim_ispathsep(p[2])
23506 || (p[2] == '.'
23507 && (p[3] == NUL || vim_ispathsep(p[3])))))
23508 break;
23509 }
23510
23511 /* FullName_save() is slow, don't use it when not needed. */
23512 if (*p != NUL || !vim_isAbsName(*fnamep))
23513 {
23514 *fnamep = FullName_save(*fnamep, *p != NUL);
23515 vim_free(*bufp); /* free any allocated file name */
23516 *bufp = *fnamep;
23517 if (*fnamep == NULL)
23518 return -1;
23519 }
23520
23521 /* Append a path separator to a directory. */
23522 if (mch_isdir(*fnamep))
23523 {
23524 /* Make room for one or two extra characters. */
23525 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23526 vim_free(*bufp); /* free any allocated file name */
23527 *bufp = *fnamep;
23528 if (*fnamep == NULL)
23529 return -1;
23530 add_pathsep(*fnamep);
23531 }
23532 }
23533
23534 /* ":." - path relative to the current directory */
23535 /* ":~" - path relative to the home directory */
23536 /* ":8" - shortname path - postponed till after */
23537 while (src[*usedlen] == ':'
23538 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23539 {
23540 *usedlen += 2;
23541 if (c == '8')
23542 {
23543#ifdef WIN3264
23544 has_shortname = 1; /* Postpone this. */
23545#endif
23546 continue;
23547 }
23548 pbuf = NULL;
23549 /* Need full path first (use expand_env() to remove a "~/") */
23550 if (!has_fullname)
23551 {
23552 if (c == '.' && **fnamep == '~')
23553 p = pbuf = expand_env_save(*fnamep);
23554 else
23555 p = pbuf = FullName_save(*fnamep, FALSE);
23556 }
23557 else
23558 p = *fnamep;
23559
23560 has_fullname = 0;
23561
23562 if (p != NULL)
23563 {
23564 if (c == '.')
23565 {
23566 mch_dirname(dirname, MAXPATHL);
23567 s = shorten_fname(p, dirname);
23568 if (s != NULL)
23569 {
23570 *fnamep = s;
23571 if (pbuf != NULL)
23572 {
23573 vim_free(*bufp); /* free any allocated file name */
23574 *bufp = pbuf;
23575 pbuf = NULL;
23576 }
23577 }
23578 }
23579 else
23580 {
23581 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23582 /* Only replace it when it starts with '~' */
23583 if (*dirname == '~')
23584 {
23585 s = vim_strsave(dirname);
23586 if (s != NULL)
23587 {
23588 *fnamep = s;
23589 vim_free(*bufp);
23590 *bufp = s;
23591 }
23592 }
23593 }
23594 vim_free(pbuf);
23595 }
23596 }
23597
23598 tail = gettail(*fnamep);
23599 *fnamelen = (int)STRLEN(*fnamep);
23600
23601 /* ":h" - head, remove "/file_name", can be repeated */
23602 /* Don't remove the first "/" or "c:\" */
23603 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23604 {
23605 valid |= VALID_HEAD;
23606 *usedlen += 2;
23607 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023608 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023609 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 *fnamelen = (int)(tail - *fnamep);
23611#ifdef VMS
23612 if (*fnamelen > 0)
23613 *fnamelen += 1; /* the path separator is part of the path */
23614#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023615 if (*fnamelen == 0)
23616 {
23617 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23618 p = vim_strsave((char_u *)".");
23619 if (p == NULL)
23620 return -1;
23621 vim_free(*bufp);
23622 *bufp = *fnamep = tail = p;
23623 *fnamelen = 1;
23624 }
23625 else
23626 {
23627 while (tail > s && !after_pathsep(s, tail))
23628 mb_ptr_back(*fnamep, tail);
23629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 }
23631
23632 /* ":8" - shortname */
23633 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23634 {
23635 *usedlen += 2;
23636#ifdef WIN3264
23637 has_shortname = 1;
23638#endif
23639 }
23640
23641#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023642 /*
23643 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 */
23645 if (has_shortname)
23646 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023647 /* Copy the string if it is shortened by :h and when it wasn't copied
23648 * yet, because we are going to change it in place. Avoids changing
23649 * the buffer name for "%:8". */
23650 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 {
23652 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023653 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 return -1;
23655 vim_free(*bufp);
23656 *bufp = *fnamep = p;
23657 }
23658
23659 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023660 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 if (!has_fullname && !vim_isAbsName(*fnamep))
23662 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023663 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 return -1;
23665 }
23666 else
23667 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023668 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669
Bram Moolenaardc935552011-08-17 15:23:23 +020023670 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023672 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 return -1;
23674
23675 if (l == 0)
23676 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023677 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023679 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 return -1;
23681 }
23682 *fnamelen = l;
23683 }
23684 }
23685#endif /* WIN3264 */
23686
23687 /* ":t" - tail, just the basename */
23688 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23689 {
23690 *usedlen += 2;
23691 *fnamelen -= (int)(tail - *fnamep);
23692 *fnamep = tail;
23693 }
23694
23695 /* ":e" - extension, can be repeated */
23696 /* ":r" - root, without extension, can be repeated */
23697 while (src[*usedlen] == ':'
23698 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23699 {
23700 /* find a '.' in the tail:
23701 * - for second :e: before the current fname
23702 * - otherwise: The last '.'
23703 */
23704 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23705 s = *fnamep - 2;
23706 else
23707 s = *fnamep + *fnamelen - 1;
23708 for ( ; s > tail; --s)
23709 if (s[0] == '.')
23710 break;
23711 if (src[*usedlen + 1] == 'e') /* :e */
23712 {
23713 if (s > tail)
23714 {
23715 *fnamelen += (int)(*fnamep - (s + 1));
23716 *fnamep = s + 1;
23717#ifdef VMS
23718 /* cut version from the extension */
23719 s = *fnamep + *fnamelen - 1;
23720 for ( ; s > *fnamep; --s)
23721 if (s[0] == ';')
23722 break;
23723 if (s > *fnamep)
23724 *fnamelen = s - *fnamep;
23725#endif
23726 }
23727 else if (*fnamep <= tail)
23728 *fnamelen = 0;
23729 }
23730 else /* :r */
23731 {
23732 if (s > tail) /* remove one extension */
23733 *fnamelen = (int)(s - *fnamep);
23734 }
23735 *usedlen += 2;
23736 }
23737
23738 /* ":s?pat?foo?" - substitute */
23739 /* ":gs?pat?foo?" - global substitute */
23740 if (src[*usedlen] == ':'
23741 && (src[*usedlen + 1] == 's'
23742 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23743 {
23744 char_u *str;
23745 char_u *pat;
23746 char_u *sub;
23747 int sep;
23748 char_u *flags;
23749 int didit = FALSE;
23750
23751 flags = (char_u *)"";
23752 s = src + *usedlen + 2;
23753 if (src[*usedlen + 1] == 'g')
23754 {
23755 flags = (char_u *)"g";
23756 ++s;
23757 }
23758
23759 sep = *s++;
23760 if (sep)
23761 {
23762 /* find end of pattern */
23763 p = vim_strchr(s, sep);
23764 if (p != NULL)
23765 {
23766 pat = vim_strnsave(s, (int)(p - s));
23767 if (pat != NULL)
23768 {
23769 s = p + 1;
23770 /* find end of substitution */
23771 p = vim_strchr(s, sep);
23772 if (p != NULL)
23773 {
23774 sub = vim_strnsave(s, (int)(p - s));
23775 str = vim_strnsave(*fnamep, *fnamelen);
23776 if (sub != NULL && str != NULL)
23777 {
23778 *usedlen = (int)(p + 1 - src);
23779 s = do_string_sub(str, pat, sub, flags);
23780 if (s != NULL)
23781 {
23782 *fnamep = s;
23783 *fnamelen = (int)STRLEN(s);
23784 vim_free(*bufp);
23785 *bufp = s;
23786 didit = TRUE;
23787 }
23788 }
23789 vim_free(sub);
23790 vim_free(str);
23791 }
23792 vim_free(pat);
23793 }
23794 }
23795 /* after using ":s", repeat all the modifiers */
23796 if (didit)
23797 goto repeat;
23798 }
23799 }
23800
23801 return valid;
23802}
23803
23804/*
23805 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23806 * "flags" can be "g" to do a global substitute.
23807 * Returns an allocated string, NULL for error.
23808 */
23809 char_u *
23810do_string_sub(str, pat, sub, flags)
23811 char_u *str;
23812 char_u *pat;
23813 char_u *sub;
23814 char_u *flags;
23815{
23816 int sublen;
23817 regmatch_T regmatch;
23818 int i;
23819 int do_all;
23820 char_u *tail;
23821 garray_T ga;
23822 char_u *ret;
23823 char_u *save_cpo;
23824
23825 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23826 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023827 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828
23829 ga_init2(&ga, 1, 200);
23830
23831 do_all = (flags[0] == 'g');
23832
23833 regmatch.rm_ic = p_ic;
23834 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23835 if (regmatch.regprog != NULL)
23836 {
23837 tail = str;
23838 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23839 {
23840 /*
23841 * Get some space for a temporary buffer to do the substitution
23842 * into. It will contain:
23843 * - The text up to where the match is.
23844 * - The substituted text.
23845 * - The text after the match.
23846 */
23847 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23848 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23849 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23850 {
23851 ga_clear(&ga);
23852 break;
23853 }
23854
23855 /* copy the text up to where the match is */
23856 i = (int)(regmatch.startp[0] - tail);
23857 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23858 /* add the substituted text */
23859 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23860 + ga.ga_len + i, TRUE, TRUE, FALSE);
23861 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 /* avoid getting stuck on a match with an empty string */
23863 if (tail == regmatch.endp[0])
23864 {
23865 if (*tail == NUL)
23866 break;
23867 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23868 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 }
23870 else
23871 {
23872 tail = regmatch.endp[0];
23873 if (*tail == NUL)
23874 break;
23875 }
23876 if (!do_all)
23877 break;
23878 }
23879
23880 if (ga.ga_data != NULL)
23881 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23882
23883 vim_free(regmatch.regprog);
23884 }
23885
23886 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23887 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023888 if (p_cpo == empty_option)
23889 p_cpo = save_cpo;
23890 else
23891 /* Darn, evaluating {sub} expression changed the value. */
23892 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893
23894 return ret;
23895}
23896
23897#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */