blob: 76b41590d043e57567e3ebbfc100ee7a88e8c4a4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200460static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200690static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
693static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200694static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000697static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000698static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000701#ifdef FEAT_FLOAT
702static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
704#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000705static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200706static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707#ifdef HAVE_STRFTIME
708static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
709#endif
710static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200716static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200717static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000723static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200724static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000725static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000726static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000727static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000729static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000730static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000731static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000732static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200733#ifdef FEAT_FLOAT
734static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
736#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000737static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#ifdef FEAT_FLOAT
741static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
742#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200744static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200745static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000755static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000758static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100759static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000761static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000762static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int get_env_len __ARGS((char_u **arg));
764static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
767#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
768#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
769 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000770static 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 +0000771static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
774static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static typval_T *alloc_tv __ARGS((void));
776static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void init_tv __ARGS((typval_T *varp));
778static long get_tv_number __ARGS((typval_T *varp));
779static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000780static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static char_u *get_tv_string __ARGS((typval_T *varp));
782static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000785static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
787static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
788static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000789static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
790static 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 +0000791static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
792static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000793static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200794static int var_check_func_name __ARGS((char_u *name, int new_var));
795static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000796static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000797static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000798static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
799static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
800static int eval_fname_script __ARGS((char_u *p));
801static int eval_fname_sid __ARGS((char_u *p));
802static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static ufunc_T *find_func __ARGS((char_u *name));
804static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000805static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_PROFILE
807static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000808static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
809static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
810static int
811# ifdef __BORLANDC__
812 _RTLENTRYF
813# endif
814 prof_total_cmp __ARGS((const void *s1, const void *s2));
815static int
816# ifdef __BORLANDC__
817 _RTLENTRYF
818# endif
819 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000821static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000822static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000823static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static 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 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200854 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
855 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200879 set_reg_var(0); /* default for v:register is not 0 but '"' */
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 Moolenaar0cbba942012-07-25 16:47:03 +02001568call_vim_function(func, argc, argv, safe, str_arg_only, 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 Moolenaar0cbba942012-07-25 16:47:03 +02001573 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 long n;
1578 int len;
1579 int i;
1580 int doesrange;
1581 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001584 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 for (i = 0; i < argc; i++)
1589 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001590 /* Pass a NULL or empty argument as an empty string */
1591 if (argv[i] == NULL || *argv[i] == NUL)
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 continue;
1596 }
1597
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 if (str_arg_only)
1599 len = 0;
1600 else
1601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (len != 0 && len == (int)STRLEN(argv[i]))
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_NUMBER;
1606 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 else
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_STRING;
1611 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614
1615 if (safe)
1616 {
1617 save_funccalp = save_funccal();
1618 ++sandbox;
1619 }
1620
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1622 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (safe)
1626 {
1627 --sandbox;
1628 restore_funccal(save_funccalp);
1629 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 vim_free(argvars);
1631
1632 if (ret == FAIL)
1633 clear_tv(rettv);
1634
1635 return ret;
1636}
1637
Bram Moolenaar4f688582007-07-24 12:34:30 +00001638# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001639/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001640 * Call vimL function "func" and return the result as a string.
1641 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 * Uses argv[argc] for the function arguments.
1643 */
1644 void *
1645call_func_retstr(func, argc, argv, safe)
1646 char_u *func;
1647 int argc;
1648 char_u **argv;
1649 int safe; /* use the sandbox */
1650{
1651 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001652 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001654 /* All arguments are passed as strings, no conversion to number. */
1655 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 return NULL;
1657
1658 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 return retval;
1661}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001662# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663
Bram Moolenaar4f688582007-07-24 12:34:30 +00001664# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001666 * Call vimL function "func" and return the result as a number.
1667 * Returns -1 when calling the function fails.
1668 * Uses argv[argc] for the function arguments.
1669 */
1670 long
1671call_func_retnr(func, argc, argv, safe)
1672 char_u *func;
1673 int argc;
1674 char_u **argv;
1675 int safe; /* use the sandbox */
1676{
1677 typval_T rettv;
1678 long retval;
1679
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001680 /* All arguments are passed as strings, no conversion to number. */
1681 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682 return -1;
1683
1684 retval = get_tv_number_chk(&rettv, NULL);
1685 clear_tv(&rettv);
1686 return retval;
1687}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001688# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001689
1690/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001691 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001693 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 */
1695 void *
1696call_func_retlist(func, argc, argv, safe)
1697 char_u *func;
1698 int argc;
1699 char_u **argv;
1700 int safe; /* use the sandbox */
1701{
1702 typval_T rettv;
1703
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001704 /* All arguments are passed as strings, no conversion to number. */
1705 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 return NULL;
1707
1708 if (rettv.v_type != VAR_LIST)
1709 {
1710 clear_tv(&rettv);
1711 return NULL;
1712 }
1713
1714 return rettv.vval.v_list;
1715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719/*
1720 * Save the current function call pointer, and set it to NULL.
1721 * Used when executing autocommands and for ":source".
1722 */
1723 void *
1724save_funccal()
1725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 current_funccal = NULL;
1729 return (void *)fc;
1730}
1731
1732 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733restore_funccal(vfc)
1734 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 funccall_T *fc = (funccall_T *)vfc;
1737
1738 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#if defined(FEAT_PROFILE) || defined(PROTO)
1742/*
1743 * Prepare profiling for entering a child or something else that is not
1744 * counted for the script/function itself.
1745 * Should always be called in pair with prof_child_exit().
1746 */
1747 void
1748prof_child_enter(tm)
1749 proftime_T *tm; /* place to store waittime */
1750{
1751 funccall_T *fc = current_funccal;
1752
1753 if (fc != NULL && fc->func->uf_profiling)
1754 profile_start(&fc->prof_child);
1755 script_prof_save(tm);
1756}
1757
1758/*
1759 * Take care of time spent in a child.
1760 * Should always be called after prof_child_enter().
1761 */
1762 void
1763prof_child_exit(tm)
1764 proftime_T *tm; /* where waittime was stored */
1765{
1766 funccall_T *fc = current_funccal;
1767
1768 if (fc != NULL && fc->func->uf_profiling)
1769 {
1770 profile_end(&fc->prof_child);
1771 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1772 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1773 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1774 }
1775 script_prof_restore(tm);
1776}
1777#endif
1778
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_FOLDING
1781/*
1782 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1783 * it in "*cp". Doesn't give error messages.
1784 */
1785 int
1786eval_foldexpr(arg, cp)
1787 char_u *arg;
1788 int *cp;
1789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 int retval;
1792 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001797 if (use_sandbox)
1798 ++sandbox;
1799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (tv.v_type == VAR_NUMBER)
1807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a string, check if there is a non-digit before
1813 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (!VIM_ISDIGIT(*s) && *s != '-')
1816 *cp = *s++;
1817 retval = atol((char *)s);
1818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 --sandbox;
1824 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 return retval;
1827}
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let" list all variable values
1832 * ":let var1 var2" list variable values
1833 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 * ":let var += expr" assignment command.
1835 * ":let var -= expr" assignment command.
1836 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
1840ex_let(eap)
1841 exarg_T *eap;
1842{
1843 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 int var_count = 0;
1848 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 argend = skip_var_list(arg, &var_count, &semicolon);
1854 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001856 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1857 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001861 /*
1862 * ":let" without "=": list variables
1863 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (*arg == '[')
1865 EMSG(_(e_invarg));
1866 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_glob_vars(&first);
1873 list_buf_vars(&first);
1874 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_script_vars(&first);
1879 list_func_vars(&first);
1880 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 eap->nextcmd = check_nextcmd(arg);
1883 }
1884 else
1885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 op[0] = '=';
1887 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 {
1890 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1891 op[0] = expr[-1]; /* +=, -= or .= */
1892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (eap->skip)
1899 {
1900 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --emsg_skip;
1903 }
1904 else if (i != FAIL)
1905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911}
1912
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913/*
1914 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1915 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 * When "nextchars" is not NULL it points to a string with characters that
1917 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1918 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 * Returns OK or FAIL;
1920 */
1921 static int
1922ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1923 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int copy; /* copy values from "tv", don't move */
1926 int semicolon; /* from skip_var_list() */
1927 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929{
1930 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 listitem_T *item;
1934 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
1936 if (*arg != '[')
1937 {
1938 /*
1939 * ":let var = expr" or ":for var in list"
1940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return FAIL;
1943 return OK;
1944 }
1945
1946 /*
1947 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1948 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
1951 EMSG(_(e_listreq));
1952 return FAIL;
1953 }
1954
1955 i = list_len(l);
1956 if (semicolon == 0 && var_count < i)
1957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001958 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 return FAIL;
1960 }
1961 if (var_count - semicolon > i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966
1967 item = l->lv_first;
1968 while (*arg != ']')
1969 {
1970 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 item = item->li_next;
1973 if (arg == NULL)
1974 return FAIL;
1975
1976 arg = skipwhite(arg);
1977 if (*arg == ';')
1978 {
1979 /* Put the rest of the list (may be empty) in the var after ';'.
1980 * Create a new list for this. */
1981 l = list_alloc();
1982 if (l == NULL)
1983 return FAIL;
1984 while (item != NULL)
1985 {
1986 list_append_tv(l, &item->li_tv);
1987 item = item->li_next;
1988 }
1989
1990 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001991 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 ltv.vval.v_list = l;
1993 l->lv_refcount = 1;
1994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1996 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 clear_tv(&ltv);
1998 if (arg == NULL)
1999 return FAIL;
2000 break;
2001 }
2002 else if (*arg != ',' && *arg != ']')
2003 {
2004 EMSG2(_(e_intern2), "ex_let_vars()");
2005 return FAIL;
2006 }
2007 }
2008
2009 return OK;
2010}
2011
2012/*
2013 * Skip over assignable variable "var" or list of variables "[var, var]".
2014 * Used for ":let varvar = expr" and ":for varvar in expr".
2015 * For "[var, var]" increment "*var_count" for each variable.
2016 * for "[var, var; var]" set "semicolon".
2017 * Return NULL for an error.
2018 */
2019 static char_u *
2020skip_var_list(arg, var_count, semicolon)
2021 char_u *arg;
2022 int *var_count;
2023 int *semicolon;
2024{
2025 char_u *p, *s;
2026
2027 if (*arg == '[')
2028 {
2029 /* "[var, var]": find the matching ']'. */
2030 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002031 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
2033 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2034 s = skip_var_one(p);
2035 if (s == p)
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 ++*var_count;
2041
2042 p = skipwhite(s);
2043 if (*p == ']')
2044 break;
2045 else if (*p == ';')
2046 {
2047 if (*semicolon == 1)
2048 {
2049 EMSG(_("Double ; in list of variables"));
2050 return NULL;
2051 }
2052 *semicolon = 1;
2053 }
2054 else if (*p != ',')
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 }
2060 return p + 1;
2061 }
2062 else
2063 return skip_var_one(arg);
2064}
2065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002067 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 static char_u *
2071skip_var_one(arg)
2072 char_u *arg;
2073{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 if (*arg == '@' && arg[1] != NUL)
2075 return arg + 2;
2076 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2077 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078}
2079
Bram Moolenaara7043832005-01-21 11:56:39 +00002080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 * List variables for hashtab "ht" with prefix "prefix".
2082 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090{
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashitem_T *hi;
2092 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 int todo;
2094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2097 {
2098 if (!HASHITEM_EMPTY(hi))
2099 {
2100 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 di = HI2DI(hi);
2102 if (empty || di->di_tv.v_type != VAR_STRING
2103 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
2106 }
2107}
2108
2109/*
2110 * List global variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_glob_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
2119/*
2120 * List buffer variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_buf_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 char_u numbuf[NUMBUFLEN];
2127
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2129 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2133 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134}
2135
2136/*
2137 * List window variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_win_vars(first)
2141 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2144 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147#ifdef FEAT_WINDOWS
2148/*
2149 * List tab page variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_tab_vars(first)
2153 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2156 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157}
2158#endif
2159
Bram Moolenaara7043832005-01-21 11:56:39 +00002160/*
2161 * List Vim variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_vim_vars(first)
2165 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168}
2169
2170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171 * List script-local variables, if there is a script.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_script_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2179 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
2183 * List function variables, if there is a function.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_func_vars(first)
2187 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188{
2189 if (current_funccal != NULL)
2190 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192}
2193
2194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * List variables in "arg".
2196 */
2197 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 exarg_T *eap;
2200 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202{
2203 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 char_u *name_start;
2207 char_u *arg_subsc;
2208 char_u *tofree;
2209 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210
2211 while (!ends_excmd(*arg) && !got_int)
2212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2217 {
2218 emsg_severe = TRUE;
2219 EMSG(_(e_trailing));
2220 break;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* get_name_len() takes care of expanding curly braces */
2226 name_start = name = arg;
2227 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2228 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* This is mainly to keep test 49 working: when expanding
2231 * curly braces fails overrule the exception error message. */
2232 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 emsg_severe = TRUE;
2235 EMSG2(_(e_invarg2), arg);
2236 break;
2237 }
2238 error = TRUE;
2239 }
2240 else
2241 {
2242 if (tofree != NULL)
2243 name = tofree;
2244 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 /* handle d.key, l[idx], f(expr) */
2249 arg_subsc = arg;
2250 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'g': list_glob_vars(first); break;
2259 case 'b': list_buf_vars(first); break;
2260 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'v': list_vim_vars(first); break;
2265 case 's': list_script_vars(first); break;
2266 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 default:
2268 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
2271 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 {
2273 char_u numbuf[NUMBUFLEN];
2274 char_u *tf;
2275 int c;
2276 char_u *s;
2277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002278 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 c = *arg;
2280 *arg = NUL;
2281 list_one_var_a((char_u *)"",
2282 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 tv.v_type,
2284 s == NULL ? (char_u *)"" : s,
2285 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 *arg = c;
2287 vim_free(tf);
2288 }
2289 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296
2297 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 return arg;
2301}
2302
2303/*
2304 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2305 * Returns a pointer to the char just after the var name.
2306 * Returns NULL if there is an error.
2307 */
2308 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
2316 int c1;
2317 char_u *name;
2318 char_u *p;
2319 char_u *arg_end = NULL;
2320 int len;
2321 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
2324 /*
2325 * ":let $VAR = expr": Set environment variable.
2326 */
2327 if (*arg == '$')
2328 {
2329 /* Find the end of the name. */
2330 ++arg;
2331 name = arg;
2332 len = get_env_len(&arg);
2333 if (len == 0)
2334 EMSG2(_(e_invarg2), name - 1);
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 if (op != NULL && (*op == '+' || *op == '-'))
2338 EMSG2(_(e_letwrong), op);
2339 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002342 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
2344 c1 = name[len];
2345 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 p = get_tv_string_chk(tv);
2347 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 {
2349 int mustfree = FALSE;
2350 char_u *s = vim_getenv(name, &mustfree);
2351
2352 if (s != NULL)
2353 {
2354 p = tofree = concat_str(s, p);
2355 if (mustfree)
2356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 if (STRICMP(name, "HOME") == 0)
2363 init_homedir();
2364 else if (didset_vim && STRICMP(name, "VIM") == 0)
2365 didset_vim = FALSE;
2366 else if (didset_vimruntime
2367 && STRICMP(name, "VIMRUNTIME") == 0)
2368 didset_vimruntime = FALSE;
2369 arg_end = arg;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375 }
2376
2377 /*
2378 * ":let &option = expr": Set option value.
2379 * ":let &l:option = expr": Set local option value.
2380 * ":let &g:option = expr": Set global option value.
2381 */
2382 else if (*arg == '&')
2383 {
2384 /* Find the end of the name. */
2385 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 if (p == NULL || (endchars != NULL
2387 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
2389 else
2390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 long n;
2392 int opt_type;
2393 long numval;
2394 char_u *stringval = NULL;
2395 char_u *s;
2396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 c1 = *p;
2398 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399
2400 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 s = get_tv_string_chk(tv); /* != NULL if number or string */
2402 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 opt_type = get_option_value(arg, &numval,
2405 &stringval, opt_flags);
2406 if ((opt_type == 1 && *op == '.')
2407 || (opt_type == 0 && *op != '.'))
2408 EMSG2(_(e_letwrong), op);
2409 else
2410 {
2411 if (opt_type == 1) /* number */
2412 {
2413 if (*op == '+')
2414 n = numval + n;
2415 else
2416 n = numval - n;
2417 }
2418 else if (opt_type == 0 && stringval != NULL) /* string */
2419 {
2420 s = concat_str(stringval, s);
2421 vim_free(stringval);
2422 stringval = s;
2423 }
2424 }
2425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 if (s != NULL)
2427 {
2428 set_option_value(arg, n, s, opt_flags);
2429 arg_end = p;
2430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
2434 }
2435
2436 /*
2437 * ":let @r = expr": Set register contents.
2438 */
2439 else if (*arg == '@')
2440 {
2441 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (op != NULL && (*op == '+' || *op == '-'))
2443 EMSG2(_(e_letwrong), op);
2444 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 char_u *s;
2451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 p = get_tv_string_chk(tv);
2453 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002455 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (s != NULL)
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 vim_free(s);
2460 }
2461 }
2462 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 arg_end = arg + 1;
2466 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002479 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
2493 else
2494 EMSG2(_(e_invarg2), arg);
2495
2496 return arg_end;
2497}
2498
2499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2501 */
2502 static int
2503check_changedtick(arg)
2504 char_u *arg;
2505{
2506 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2507 {
2508 EMSG2(_(e_readonlyvar), arg);
2509 return TRUE;
2510 }
2511 return FALSE;
2512}
2513
2514/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * Get an lval: variable, Dict item or List item that can be assigned a value
2516 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2517 * "name.key", "name.key[expr]" etc.
2518 * Indexing only works if "name" is an existing List or Dictionary.
2519 * "name" points to the start of the name.
2520 * If "rettv" is not NULL it points to the value to be assigned.
2521 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2522 * wrong; must end in space or cmd separator.
2523 *
2524 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002525 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 */
2528 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 typval_T *rettv;
2532 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 int unlet;
2534 int skip;
2535 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002536 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 char_u *expr_start, *expr_end;
2540 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 dictitem_T *v;
2542 typval_T var1;
2543 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552
2553 if (skip)
2554 {
2555 /* When skipping just find the end of the name. */
2556 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002557 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 }
2559
2560 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002561 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 if (expr_start != NULL)
2563 {
2564 /* Don't expand the name when we already know there is an error. */
2565 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2566 && *p != '[' && *p != '.')
2567 {
2568 EMSG(_(e_trailing));
2569 return NULL;
2570 }
2571
2572 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2573 if (lp->ll_exp_name == NULL)
2574 {
2575 /* Report an invalid expression in braces, unless the
2576 * expression evaluation has been cancelled due to an
2577 * aborting error, an interrupt, or an exception. */
2578 if (!aborting() && !quiet)
2579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002580 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 EMSG2(_(e_invarg2), name);
2582 return NULL;
2583 }
2584 }
2585 lp->ll_name = lp->ll_exp_name;
2586 }
2587 else
2588 lp->ll_name = name;
2589
2590 /* Without [idx] or .key we are done. */
2591 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2592 return p;
2593
2594 cc = *p;
2595 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002596 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (v == NULL && !quiet)
2598 EMSG2(_(e_undefvar), lp->ll_name);
2599 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 if (v == NULL)
2601 return NULL;
2602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 /*
2604 * Loop until no more [idx] or .key is following.
2605 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002606 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2610 && !(lp->ll_tv->v_type == VAR_DICT
2611 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSG(_("E689: Can only index a List or Dictionary"));
2615 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_("E708: [:] must come last"));
2621 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 len = -1;
2625 if (*p == '.')
2626 {
2627 key = p + 1;
2628 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2629 ;
2630 if (len == 0)
2631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (!quiet)
2633 EMSG(_(e_emptykey));
2634 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 }
2636 p = key + len;
2637 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 else
2639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 if (*p == ':')
2643 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 else
2645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 empty1 = FALSE;
2647 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 if (get_tv_string_chk(&var1) == NULL)
2650 {
2651 /* not a number or string */
2652 clear_tv(&var1);
2653 return NULL;
2654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656
2657 /* Optionally get the second index [ :expr]. */
2658 if (*p == ':')
2659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002663 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 if (!empty1)
2665 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (rettv != NULL && (rettv->v_type != VAR_LIST
2669 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (!quiet)
2672 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677 p = skipwhite(p + 1);
2678 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 else
2681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002689 if (get_tv_string_chk(&var2) == NULL)
2690 {
2691 /* not a number or string */
2692 if (!empty1)
2693 clear_tv(&var1);
2694 clear_tv(&var2);
2695 return NULL;
2696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002702
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 if (!empty1)
2708 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
2713
2714 /* Skip to past ']'. */
2715 ++p;
2716 }
2717
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 {
2720 if (len == -1)
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002723 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*key == NUL)
2725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
2731 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002732 lp->ll_list = NULL;
2733 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002734 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002735
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002736 /* When assigning to a scope dictionary check that a function and
2737 * variable name is valid (only variable name unless it is l: or
2738 * g: dictionary). Disallow overwriting a builtin function. */
2739 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 int prevval;
2742 int wrong;
2743
2744 if (len != -1)
2745 {
2746 prevval = key[len];
2747 key[len] = NUL;
2748 }
2749 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2750 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002752 || !valid_varname(key);
2753 if (len != -1)
2754 key[len] = prevval;
2755 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 return NULL;
2757 }
2758
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 /* Can't add "v:" variable. */
2762 if (lp->ll_dict == &vimvardict)
2763 {
2764 EMSG2(_(e_illvar), name);
2765 return NULL;
2766 }
2767
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002768 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 if (len == -1)
2774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 }
2777 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 if (len == -1)
2782 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 p = NULL;
2785 break;
2786 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 /* existing variable, need to check if it can be changed */
2788 else if (var_check_ro(lp->ll_di->di_flags, name))
2789 return NULL;
2790
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 if (len == -1)
2792 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795 else
2796 {
2797 /*
2798 * Get the number and item for the only or first index of the List.
2799 */
2800 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
2803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002804 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 clear_tv(&var1);
2806 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_list = lp->ll_tv->vval.v_list;
2809 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2810 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002812 if (lp->ll_n1 < 0)
2813 {
2814 lp->ll_n1 = 0;
2815 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2816 }
2817 }
2818 if (lp->ll_li == NULL)
2819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002822 if (!quiet)
2823 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826
2827 /*
2828 * May need to find the item or absolute index for the second
2829 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 * When no index given: "lp->ll_empty2" is TRUE.
2831 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002835 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 {
2842 if (!quiet)
2843 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2850 if (lp->ll_n1 < 0)
2851 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2852 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 {
2854 if (!quiet)
2855 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002858 }
2859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002862 }
2863
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 return p;
2865}
2866
2867/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 */
2870 static void
2871clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873{
2874 vim_free(lp->ll_exp_name);
2875 vim_free(lp->ll_newkey);
2876}
2877
2878/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002879 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 */
2883 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890{
2891 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 listitem_T *ri;
2893 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894
2895 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 cc = *endp;
2900 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901 if (op != NULL && *op != '=')
2902 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002905 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002906 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002907 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 {
2909 if (tv_op(&tv, rettv, op) == OK)
2910 set_var(lp->ll_name, &tv, FALSE);
2911 clear_tv(&tv);
2912 }
2913 }
2914 else
2915 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919 else if (tv_check_lock(lp->ll_newkey == NULL
2920 ? lp->ll_tv->v_lock
2921 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2922 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 else if (lp->ll_range)
2924 {
2925 /*
2926 * Assign the List values to the list items.
2927 */
2928 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 if (op != NULL && *op != '=')
2931 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2932 else
2933 {
2934 clear_tv(&lp->ll_li->li_tv);
2935 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 ri = ri->li_next;
2938 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2939 break;
2940 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002943 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 ri = NULL;
2946 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 lp->ll_li = lp->ll_li->li_next;
2950 ++lp->ll_n1;
2951 }
2952 if (ri != NULL)
2953 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 else if (lp->ll_empty2
2955 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 : lp->ll_n1 != lp->ll_n2)
2957 EMSG(_("E711: List value has not enough items"));
2958 }
2959 else
2960 {
2961 /*
2962 * Assign to a List or Dictionary item.
2963 */
2964 if (lp->ll_newkey != NULL)
2965 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 if (op != NULL && *op != '=')
2967 {
2968 EMSG2(_(e_letwrong), op);
2969 return;
2970 }
2971
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002973 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 if (di == NULL)
2975 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002976 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2977 {
2978 vim_free(di);
2979 return;
2980 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 else if (op != NULL && *op != '=')
2984 {
2985 tv_op(lp->ll_tv, rettv, op);
2986 return;
2987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002988 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 /*
2992 * Assign the value to the variable or list item.
2993 */
2994 if (copy)
2995 copy_tv(rettv, lp->ll_tv);
2996 else
2997 {
2998 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002999 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 }
3002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003}
3004
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3007 * Returns OK or FAIL.
3008 */
3009 static int
3010tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 typval_T *tv1;
3012 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 char_u *op;
3014{
3015 long n;
3016 char_u numbuf[NUMBUFLEN];
3017 char_u *s;
3018
3019 /* Can't do anything with a Funcref or a Dict on the right. */
3020 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3021 {
3022 switch (tv1->v_type)
3023 {
3024 case VAR_DICT:
3025 case VAR_FUNC:
3026 break;
3027
3028 case VAR_LIST:
3029 if (*op != '+' || tv2->v_type != VAR_LIST)
3030 break;
3031 /* List += List */
3032 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3033 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3034 return OK;
3035
3036 case VAR_NUMBER:
3037 case VAR_STRING:
3038 if (tv2->v_type == VAR_LIST)
3039 break;
3040 if (*op == '+' || *op == '-')
3041 {
3042 /* nr += nr or nr -= nr*/
3043 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044#ifdef FEAT_FLOAT
3045 if (tv2->v_type == VAR_FLOAT)
3046 {
3047 float_T f = n;
3048
3049 if (*op == '+')
3050 f += tv2->vval.v_float;
3051 else
3052 f -= tv2->vval.v_float;
3053 clear_tv(tv1);
3054 tv1->v_type = VAR_FLOAT;
3055 tv1->vval.v_float = f;
3056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058#endif
3059 {
3060 if (*op == '+')
3061 n += get_tv_number(tv2);
3062 else
3063 n -= get_tv_number(tv2);
3064 clear_tv(tv1);
3065 tv1->v_type = VAR_NUMBER;
3066 tv1->vval.v_number = n;
3067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 }
3069 else
3070 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071 if (tv2->v_type == VAR_FLOAT)
3072 break;
3073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 /* str .= str */
3075 s = get_tv_string(tv1);
3076 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3077 clear_tv(tv1);
3078 tv1->v_type = VAR_STRING;
3079 tv1->vval.v_string = s;
3080 }
3081 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082
3083#ifdef FEAT_FLOAT
3084 case VAR_FLOAT:
3085 {
3086 float_T f;
3087
3088 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3089 && tv2->v_type != VAR_NUMBER
3090 && tv2->v_type != VAR_STRING))
3091 break;
3092 if (tv2->v_type == VAR_FLOAT)
3093 f = tv2->vval.v_float;
3094 else
3095 f = get_tv_number(tv2);
3096 if (*op == '+')
3097 tv1->vval.v_float += f;
3098 else
3099 tv1->vval.v_float -= f;
3100 }
3101 return OK;
3102#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 }
3104 }
3105
3106 EMSG2(_(e_letwrong), op);
3107 return FAIL;
3108}
3109
3110/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111 * Add a watcher to a list.
3112 */
3113 static void
3114list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 list_T *l;
3116 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117{
3118 lw->lw_next = l->lv_watch;
3119 l->lv_watch = lw;
3120}
3121
3122/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003123 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 * No warning when it isn't found...
3125 */
3126 static void
3127list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 list_T *l;
3129 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130{
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132
3133 lwp = &l->lv_watch;
3134 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3135 {
3136 if (lw == lwrem)
3137 {
3138 *lwp = lw->lw_next;
3139 break;
3140 }
3141 lwp = &lw->lw_next;
3142 }
3143}
3144
3145/*
3146 * Just before removing an item from a list: advance watchers to the next
3147 * item.
3148 */
3149 static void
3150list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 list_T *l;
3152 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155
3156 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3157 if (lw->lw_item == item)
3158 lw->lw_item = item->li_next;
3159}
3160
3161/*
3162 * Evaluate the expression used in a ":for var in expr" command.
3163 * "arg" points to "var".
3164 * Set "*errp" to TRUE for an error, FALSE otherwise;
3165 * Return a pointer that holds the info. Null when there is an error.
3166 */
3167 void *
3168eval_for_line(arg, errp, nextcmdp, skip)
3169 char_u *arg;
3170 int *errp;
3171 char_u **nextcmdp;
3172 int skip;
3173{
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 typval_T tv;
3177 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 *errp = TRUE; /* default: there is an error */
3180
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 if (fi == NULL)
3183 return NULL;
3184
3185 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3186 if (expr == NULL)
3187 return fi;
3188
3189 expr = skipwhite(expr);
3190 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3191 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003192 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 return fi;
3194 }
3195
3196 if (skip)
3197 ++emsg_skip;
3198 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3199 {
3200 *errp = FALSE;
3201 if (!skip)
3202 {
3203 l = tv.vval.v_list;
3204 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003207 clear_tv(&tv);
3208 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 else
3210 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003211 /* No need to increment the refcount, it's already set for the
3212 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 fi->fi_list = l;
3214 list_add_watch(l, &fi->fi_lw);
3215 fi->fi_lw.lw_item = l->lv_first;
3216 }
3217 }
3218 }
3219 if (skip)
3220 --emsg_skip;
3221
3222 return fi;
3223}
3224
3225/*
3226 * Use the first item in a ":for" list. Advance to the next.
3227 * Assign the values to the variable (list). "arg" points to the first one.
3228 * Return TRUE when a valid item was found, FALSE when at end of list or
3229 * something wrong.
3230 */
3231 int
3232next_for_item(fi_void, arg)
3233 void *fi_void;
3234 char_u *arg;
3235{
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 item = fi->fi_lw.lw_item;
3241 if (item == NULL)
3242 result = FALSE;
3243 else
3244 {
3245 fi->fi_lw.lw_item = item->li_next;
3246 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3247 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3248 }
3249 return result;
3250}
3251
3252/*
3253 * Free the structure used to store info used by ":for".
3254 */
3255 void
3256free_for_info(fi_void)
3257 void *fi_void;
3258{
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003261 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 list_unref(fi->fi_list);
3265 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 vim_free(fi);
3267}
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3270
3271 void
3272set_context_for_expression(xp, arg, cmdidx)
3273 expand_T *xp;
3274 char_u *arg;
3275 cmdidx_T cmdidx;
3276{
3277 int got_eq = FALSE;
3278 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 if (cmdidx == CMD_let)
3282 {
3283 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003284 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 {
3286 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003287 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 {
3289 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 if (vim_iswhite(*p))
3292 break;
3293 }
3294 return;
3295 }
3296 }
3297 else
3298 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3299 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 while ((xp->xp_pattern = vim_strpbrk(arg,
3301 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3302 {
3303 c = *xp->xp_pattern;
3304 if (c == '&')
3305 {
3306 c = xp->xp_pattern[1];
3307 if (c == '&')
3308 {
3309 ++xp->xp_pattern;
3310 xp->xp_context = cmdidx != CMD_let || got_eq
3311 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3312 }
3313 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003316 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3317 xp->xp_pattern += 2;
3318
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 else if (c == '$')
3322 {
3323 /* environment variable */
3324 xp->xp_context = EXPAND_ENV_VARS;
3325 }
3326 else if (c == '=')
3327 {
3328 got_eq = TRUE;
3329 xp->xp_context = EXPAND_EXPRESSION;
3330 }
3331 else if (c == '<'
3332 && xp->xp_context == EXPAND_FUNCTIONS
3333 && vim_strchr(xp->xp_pattern, '(') == NULL)
3334 {
3335 /* Function name can start with "<SNR>" */
3336 break;
3337 }
3338 else if (cmdidx != CMD_let || got_eq)
3339 {
3340 if (c == '"') /* string */
3341 {
3342 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3343 if (c == '\\' && xp->xp_pattern[1] != NUL)
3344 ++xp->xp_pattern;
3345 xp->xp_context = EXPAND_NOTHING;
3346 }
3347 else if (c == '\'') /* literal string */
3348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3351 /* skip */ ;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '|')
3355 {
3356 if (xp->xp_pattern[1] == '|')
3357 {
3358 ++xp->xp_pattern;
3359 xp->xp_context = EXPAND_EXPRESSION;
3360 }
3361 else
3362 xp->xp_context = EXPAND_COMMANDS;
3363 }
3364 else
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
3367 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 /* Doesn't look like something valid, expand as an expression
3369 * anyway. */
3370 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 arg = xp->xp_pattern;
3372 if (*arg != NUL)
3373 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3374 /* skip */ ;
3375 }
3376 xp->xp_pattern = arg;
3377}
3378
3379#endif /* FEAT_CMDL_COMPL */
3380
3381/*
3382 * ":1,25call func(arg1, arg2)" function call.
3383 */
3384 void
3385ex_call(eap)
3386 exarg_T *eap;
3387{
3388 char_u *arg = eap->arg;
3389 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003391 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 linenr_T lnum;
3395 int doesrange;
3396 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003399 if (eap->skip)
3400 {
3401 /* trans_function_name() doesn't work well when skipping, use eval0()
3402 * instead to skip to any following command, e.g. for:
3403 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003404 ++emsg_skip;
3405 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3406 clear_tv(&rettv);
3407 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003408 return;
3409 }
3410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003411 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003412 if (fudi.fd_newkey != NULL)
3413 {
3414 /* Still need to give an error message for missing key. */
3415 EMSG2(_(e_dictkey), fudi.fd_newkey);
3416 vim_free(fudi.fd_newkey);
3417 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 if (tofree == NULL)
3419 return;
3420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003421 /* Increase refcount on dictionary, it could get deleted when evaluating
3422 * the arguments. */
3423 if (fudi.fd_dict != NULL)
3424 ++fudi.fd_dict->dv_refcount;
3425
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003426 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003427 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaar532c7802005-01-27 14:44:31 +00003430 /* Skip white space to allow ":call func ()". Not good, but required for
3431 * backward compatibility. */
3432 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 if (*startarg != '(')
3436 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003437 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 goto end;
3439 }
3440
3441 /*
3442 * When skipping, evaluate the function once, to find the end of the
3443 * arguments.
3444 * When the function takes a range, this is discovered after the first
3445 * call, and the loop is broken.
3446 */
3447 if (eap->skip)
3448 {
3449 ++emsg_skip;
3450 lnum = eap->line2; /* do it once, also with an invalid range */
3451 }
3452 else
3453 lnum = eap->line1;
3454 for ( ; lnum <= eap->line2; ++lnum)
3455 {
3456 if (!eap->skip && eap->addr_count > 0)
3457 {
3458 curwin->w_cursor.lnum = lnum;
3459 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003460#ifdef FEAT_VIRTUALEDIT
3461 curwin->w_cursor.coladd = 0;
3462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003465 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 eap->line1, eap->line2, &doesrange,
3467 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 failed = TRUE;
3470 break;
3471 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003472
3473 /* Handle a function returning a Funcref, Dictionary or List. */
3474 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3475 {
3476 failed = TRUE;
3477 break;
3478 }
3479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (doesrange || eap->skip)
3482 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003485 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003487 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (aborting())
3489 break;
3490 }
3491 if (eap->skip)
3492 --emsg_skip;
3493
3494 if (!failed)
3495 {
3496 /* Check for trailing illegal characters and a following command. */
3497 if (!ends_excmd(*arg))
3498 {
3499 emsg_severe = TRUE;
3500 EMSG(_(e_trailing));
3501 }
3502 else
3503 eap->nextcmd = check_nextcmd(arg);
3504 }
3505
3506end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003507 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003508 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509}
3510
3511/*
3512 * ":unlet[!] var1 ... " command.
3513 */
3514 void
3515ex_unlet(eap)
3516 exarg_T *eap;
3517{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003518 ex_unletlock(eap, eap->arg, 0);
3519}
3520
3521/*
3522 * ":lockvar" and ":unlockvar" commands
3523 */
3524 void
3525ex_lockvar(eap)
3526 exarg_T *eap;
3527{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003529 int deep = 2;
3530
3531 if (eap->forceit)
3532 deep = -1;
3533 else if (vim_isdigit(*arg))
3534 {
3535 deep = getdigits(&arg);
3536 arg = skipwhite(arg);
3537 }
3538
3539 ex_unletlock(eap, arg, deep);
3540}
3541
3542/*
3543 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3544 */
3545 static void
3546ex_unletlock(eap, argstart, deep)
3547 exarg_T *eap;
3548 char_u *argstart;
3549 int deep;
3550{
3551 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 do
3557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003559 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3560 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (lv.ll_name == NULL)
3562 error = TRUE; /* error but continue parsing */
3563 if (name_end == NULL || (!vim_iswhite(*name_end)
3564 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 if (name_end != NULL)
3567 {
3568 emsg_severe = TRUE;
3569 EMSG(_(e_trailing));
3570 }
3571 if (!(eap->skip || error))
3572 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
3574 }
3575
3576 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 {
3578 if (eap->cmdidx == CMD_unlet)
3579 {
3580 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3581 error = TRUE;
3582 }
3583 else
3584 {
3585 if (do_lock_var(&lv, name_end, deep,
3586 eap->cmdidx == CMD_lockvar) == FAIL)
3587 error = TRUE;
3588 }
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 if (!eap->skip)
3592 clear_lval(&lv);
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 arg = skipwhite(name_end);
3595 } while (!ends_excmd(*arg));
3596
3597 eap->nextcmd = check_nextcmd(arg);
3598}
3599
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 int forceit;
3605{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 int ret = OK;
3607 int cc;
3608
3609 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 cc = *name_end;
3612 *name_end = NUL;
3613
3614 /* Normal name or expanded name. */
3615 if (check_changedtick(lp->ll_name))
3616 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3622 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 else if (lp->ll_range)
3624 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626
3627 /* Delete a range of List items. */
3628 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3629 {
3630 li = lp->ll_li->li_next;
3631 listitem_remove(lp->ll_list, lp->ll_li);
3632 lp->ll_li = li;
3633 ++lp->ll_n1;
3634 }
3635 }
3636 else
3637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 /* unlet a List item. */
3640 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003643 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 }
3645
3646 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649/*
3650 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 */
3653 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hashtab_T *ht;
3659 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003661 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 ht = find_var_ht(name, &varname);
3664 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003666 hi = hash_find(ht, varname);
3667 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003668 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003669 di = HI2DI(hi);
3670 if (var_check_fixed(di->di_flags, name)
3671 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 return FAIL;
3673 delete_var(ht, hi);
3674 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 if (forceit)
3678 return OK;
3679 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return FAIL;
3681}
3682
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683/*
3684 * Lock or unlock variable indicated by "lp".
3685 * "deep" is the levels to go (-1 for unlimited);
3686 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3687 */
3688 static int
3689do_lock_var(lp, name_end, deep, lock)
3690 lval_T *lp;
3691 char_u *name_end;
3692 int deep;
3693 int lock;
3694{
3695 int ret = OK;
3696 int cc;
3697 dictitem_T *di;
3698
3699 if (deep == 0) /* nothing to do */
3700 return OK;
3701
3702 if (lp->ll_tv == NULL)
3703 {
3704 cc = *name_end;
3705 *name_end = NUL;
3706
3707 /* Normal name or expanded name. */
3708 if (check_changedtick(lp->ll_name))
3709 ret = FAIL;
3710 else
3711 {
3712 di = find_var(lp->ll_name, NULL);
3713 if (di == NULL)
3714 ret = FAIL;
3715 else
3716 {
3717 if (lock)
3718 di->di_flags |= DI_FLAGS_LOCK;
3719 else
3720 di->di_flags &= ~DI_FLAGS_LOCK;
3721 item_lock(&di->di_tv, deep, lock);
3722 }
3723 }
3724 *name_end = cc;
3725 }
3726 else if (lp->ll_range)
3727 {
3728 listitem_T *li = lp->ll_li;
3729
3730 /* (un)lock a range of List items. */
3731 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3732 {
3733 item_lock(&li->li_tv, deep, lock);
3734 li = li->li_next;
3735 ++lp->ll_n1;
3736 }
3737 }
3738 else if (lp->ll_list != NULL)
3739 /* (un)lock a List item. */
3740 item_lock(&lp->ll_li->li_tv, deep, lock);
3741 else
3742 /* un(lock) a Dictionary item. */
3743 item_lock(&lp->ll_di->di_tv, deep, lock);
3744
3745 return ret;
3746}
3747
3748/*
3749 * Lock or unlock an item. "deep" is nr of levels to go.
3750 */
3751 static void
3752item_lock(tv, deep, lock)
3753 typval_T *tv;
3754 int deep;
3755 int lock;
3756{
3757 static int recurse = 0;
3758 list_T *l;
3759 listitem_T *li;
3760 dict_T *d;
3761 hashitem_T *hi;
3762 int todo;
3763
3764 if (recurse >= DICT_MAXNEST)
3765 {
3766 EMSG(_("E743: variable nested too deep for (un)lock"));
3767 return;
3768 }
3769 if (deep == 0)
3770 return;
3771 ++recurse;
3772
3773 /* lock/unlock the item itself */
3774 if (lock)
3775 tv->v_lock |= VAR_LOCKED;
3776 else
3777 tv->v_lock &= ~VAR_LOCKED;
3778
3779 switch (tv->v_type)
3780 {
3781 case VAR_LIST:
3782 if ((l = tv->vval.v_list) != NULL)
3783 {
3784 if (lock)
3785 l->lv_lock |= VAR_LOCKED;
3786 else
3787 l->lv_lock &= ~VAR_LOCKED;
3788 if (deep < 0 || deep > 1)
3789 /* recursive: lock/unlock the items the List contains */
3790 for (li = l->lv_first; li != NULL; li = li->li_next)
3791 item_lock(&li->li_tv, deep - 1, lock);
3792 }
3793 break;
3794 case VAR_DICT:
3795 if ((d = tv->vval.v_dict) != NULL)
3796 {
3797 if (lock)
3798 d->dv_lock |= VAR_LOCKED;
3799 else
3800 d->dv_lock &= ~VAR_LOCKED;
3801 if (deep < 0 || deep > 1)
3802 {
3803 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003804 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3806 {
3807 if (!HASHITEM_EMPTY(hi))
3808 {
3809 --todo;
3810 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3811 }
3812 }
3813 }
3814 }
3815 }
3816 --recurse;
3817}
3818
Bram Moolenaara40058a2005-07-11 22:42:07 +00003819/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003820 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3821 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003822 */
3823 static int
3824tv_islocked(tv)
3825 typval_T *tv;
3826{
3827 return (tv->v_lock & VAR_LOCKED)
3828 || (tv->v_type == VAR_LIST
3829 && tv->vval.v_list != NULL
3830 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3831 || (tv->v_type == VAR_DICT
3832 && tv->vval.v_dict != NULL
3833 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3834}
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3837/*
3838 * Delete all "menutrans_" variables.
3839 */
3840 void
3841del_menutrans_vars()
3842{
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003847 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003849 {
3850 if (!HASHITEM_EMPTY(hi))
3851 {
3852 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3854 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 }
3856 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858}
3859#endif
3860
3861#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3862
3863/*
3864 * Local string buffer for the next two functions to store a variable name
3865 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3866 * get_user_var_name().
3867 */
3868
3869static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3870
3871static char_u *varnamebuf = NULL;
3872static int varnamebuflen = 0;
3873
3874/*
3875 * Function to concatenate a prefix and a variable name.
3876 */
3877 static char_u *
3878cat_prefix_varname(prefix, name)
3879 int prefix;
3880 char_u *name;
3881{
3882 int len;
3883
3884 len = (int)STRLEN(name) + 3;
3885 if (len > varnamebuflen)
3886 {
3887 vim_free(varnamebuf);
3888 len += 10; /* some additional space */
3889 varnamebuf = alloc(len);
3890 if (varnamebuf == NULL)
3891 {
3892 varnamebuflen = 0;
3893 return NULL;
3894 }
3895 varnamebuflen = len;
3896 }
3897 *varnamebuf = prefix;
3898 varnamebuf[1] = ':';
3899 STRCPY(varnamebuf + 2, name);
3900 return varnamebuf;
3901}
3902
3903/*
3904 * Function given to ExpandGeneric() to obtain the list of user defined
3905 * (global/buffer/window/built-in) variable names.
3906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 char_u *
3908get_user_var_name(xp, idx)
3909 expand_T *xp;
3910 int idx;
3911{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003912 static long_u gdone;
3913 static long_u bdone;
3914 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915#ifdef FEAT_WINDOWS
3916 static long_u tdone;
3917#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003918 static int vidx;
3919 static hashitem_T *hi;
3920 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003923 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003924 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003925#ifdef FEAT_WINDOWS
3926 tdone = 0;
3927#endif
3928 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003929
3930 /* Global variables */
3931 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003935 else
3936 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 while (HASHITEM_EMPTY(hi))
3938 ++hi;
3939 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3940 return cat_prefix_varname('g', hi->hi_key);
3941 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003943
3944 /* b: variables */
3945 ht = &curbuf->b_vars.dv_hashtab;
3946 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003950 else
3951 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003952 while (HASHITEM_EMPTY(hi))
3953 ++hi;
3954 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 return (char_u *)"b:changedtick";
3960 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003961
3962 /* w: variables */
3963 ht = &curwin->w_vars.dv_hashtab;
3964 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003966 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003968 else
3969 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003970 while (HASHITEM_EMPTY(hi))
3971 ++hi;
3972 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003974
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003975#ifdef FEAT_WINDOWS
3976 /* t: variables */
3977 ht = &curtab->tp_vars.dv_hashtab;
3978 if (tdone < ht->ht_used)
3979 {
3980 if (tdone++ == 0)
3981 hi = ht->ht_array;
3982 else
3983 ++hi;
3984 while (HASHITEM_EMPTY(hi))
3985 ++hi;
3986 return cat_prefix_varname('t', hi->hi_key);
3987 }
3988#endif
3989
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 /* v: variables */
3991 if (vidx < VV_LEN)
3992 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 vim_free(varnamebuf);
3995 varnamebuf = NULL;
3996 varnamebuflen = 0;
3997 return NULL;
3998}
3999
4000#endif /* FEAT_CMDL_COMPL */
4001
4002/*
4003 * types for expressions.
4004 */
4005typedef enum
4006{
4007 TYPE_UNKNOWN = 0
4008 , TYPE_EQUAL /* == */
4009 , TYPE_NEQUAL /* != */
4010 , TYPE_GREATER /* > */
4011 , TYPE_GEQUAL /* >= */
4012 , TYPE_SMALLER /* < */
4013 , TYPE_SEQUAL /* <= */
4014 , TYPE_MATCH /* =~ */
4015 , TYPE_NOMATCH /* !~ */
4016} exptype_T;
4017
4018/*
4019 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4022 */
4023
4024/*
4025 * Handle zero level expression.
4026 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004028 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 * Return OK or FAIL.
4030 */
4031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 char_u **nextcmd;
4036 int evaluate;
4037{
4038 int ret;
4039 char_u *p;
4040
4041 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (ret == FAIL || !ends_excmd(*p))
4044 {
4045 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 /*
4048 * Report the invalid expression unless the expression evaluation has
4049 * been cancelled due to an aborting error, an interrupt, or an
4050 * exception.
4051 */
4052 if (!aborting())
4053 EMSG2(_(e_invexpr2), arg);
4054 ret = FAIL;
4055 }
4056 if (nextcmd != NULL)
4057 *nextcmd = check_nextcmd(p);
4058
4059 return ret;
4060}
4061
4062/*
4063 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004064 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 *
4066 * "arg" must point to the first non-white of the expression.
4067 * "arg" is advanced to the next non-white after the recognized expression.
4068 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004069 * Note: "rettv.v_lock" is not set.
4070 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * Return OK or FAIL.
4072 */
4073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int evaluate;
4078{
4079 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004080 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 /*
4083 * Get the first variable.
4084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return FAIL;
4087
4088 if ((*arg)[0] == '?')
4089 {
4090 result = FALSE;
4091 if (evaluate)
4092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 int error = FALSE;
4094
4095 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 if (error)
4099 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101
4102 /*
4103 * Get the second variable.
4104 */
4105 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return FAIL;
4108
4109 /*
4110 * Check for the ":".
4111 */
4112 if ((*arg)[0] != ':')
4113 {
4114 EMSG(_("E109: Missing ':' after '?'"));
4115 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return FAIL;
4118 }
4119
4120 /*
4121 * Get the third variable.
4122 */
4123 *arg = skipwhite(*arg + 1);
4124 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4125 {
4126 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return FAIL;
4129 }
4130 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133
4134 return OK;
4135}
4136
4137/*
4138 * Handle first level expression:
4139 * expr2 || expr2 || expr2 logical OR
4140 *
4141 * "arg" must point to the first non-white of the expression.
4142 * "arg" is advanced to the next non-white after the recognized expression.
4143 *
4144 * Return OK or FAIL.
4145 */
4146 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int evaluate;
4151{
Bram Moolenaar33570922005-01-25 22:26:29 +00004152 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 long result;
4154 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 /*
4158 * Get the first variable.
4159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 /*
4164 * Repeat until there is no following "||".
4165 */
4166 first = TRUE;
4167 result = FALSE;
4168 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4169 {
4170 if (evaluate && first)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (error)
4176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 first = FALSE;
4178 }
4179
4180 /*
4181 * Get the second variable.
4182 */
4183 *arg = skipwhite(*arg + 2);
4184 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4185 return FAIL;
4186
4187 /*
4188 * Compute the result.
4189 */
4190 if (evaluate && !result)
4191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 if (error)
4196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 if (evaluate)
4199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 rettv->v_type = VAR_NUMBER;
4201 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 }
4204
4205 return OK;
4206}
4207
4208/*
4209 * Handle second level expression:
4210 * expr3 && expr3 && expr3 logical AND
4211 *
4212 * "arg" must point to the first non-white of the expression.
4213 * "arg" is advanced to the next non-white after the recognized expression.
4214 *
4215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int evaluate;
4222{
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 long result;
4225 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /*
4229 * Get the first variable.
4230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return FAIL;
4233
4234 /*
4235 * Repeat until there is no following "&&".
4236 */
4237 first = TRUE;
4238 result = TRUE;
4239 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4240 {
4241 if (evaluate && first)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 first = FALSE;
4249 }
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 2);
4255 if (eval4(arg, &var2, evaluate && result) == FAIL)
4256 return FAIL;
4257
4258 /*
4259 * Compute the result.
4260 */
4261 if (evaluate && result)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 if (evaluate)
4270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 rettv->v_type = VAR_NUMBER;
4272 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275
4276 return OK;
4277}
4278
4279/*
4280 * Handle third level expression:
4281 * var1 == var2
4282 * var1 =~ var2
4283 * var1 != var2
4284 * var1 !~ var2
4285 * var1 > var2
4286 * var1 >= var2
4287 * var1 < var2
4288 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004289 * var1 is var2
4290 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 *
4292 * "arg" must point to the first non-white of the expression.
4293 * "arg" is advanced to the next non-white after the recognized expression.
4294 *
4295 * Return OK or FAIL.
4296 */
4297 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 int evaluate;
4302{
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u *p;
4305 int i;
4306 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004307 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int len = 2;
4309 long n1, n2;
4310 char_u *s1, *s2;
4311 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4312 regmatch_T regmatch;
4313 int ic;
4314 char_u *save_cpo;
4315
4316 /*
4317 * Get the first variable.
4318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 return FAIL;
4321
4322 p = *arg;
4323 switch (p[0])
4324 {
4325 case '=': if (p[1] == '=')
4326 type = TYPE_EQUAL;
4327 else if (p[1] == '~')
4328 type = TYPE_MATCH;
4329 break;
4330 case '!': if (p[1] == '=')
4331 type = TYPE_NEQUAL;
4332 else if (p[1] == '~')
4333 type = TYPE_NOMATCH;
4334 break;
4335 case '>': if (p[1] != '=')
4336 {
4337 type = TYPE_GREATER;
4338 len = 1;
4339 }
4340 else
4341 type = TYPE_GEQUAL;
4342 break;
4343 case '<': if (p[1] != '=')
4344 {
4345 type = TYPE_SMALLER;
4346 len = 1;
4347 }
4348 else
4349 type = TYPE_SEQUAL;
4350 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004351 case 'i': if (p[1] == 's')
4352 {
4353 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4354 len = 5;
4355 if (!vim_isIDc(p[len]))
4356 {
4357 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4358 type_is = TRUE;
4359 }
4360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363
4364 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 */
4367 if (type != TYPE_UNKNOWN)
4368 {
4369 /* extra question mark appended: ignore case */
4370 if (p[len] == '?')
4371 {
4372 ic = TRUE;
4373 ++len;
4374 }
4375 /* extra '#' appended: match case */
4376 else if (p[len] == '#')
4377 {
4378 ic = FALSE;
4379 ++len;
4380 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 else
4383 ic = p_ic;
4384
4385 /*
4386 * Get the second variable.
4387 */
4388 *arg = skipwhite(p + len);
4389 if (eval5(arg, &var2, evaluate) == FAIL)
4390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393 }
4394
4395 if (evaluate)
4396 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 if (type_is && rettv->v_type != var2.v_type)
4398 {
4399 /* For "is" a different type always means FALSE, for "notis"
4400 * it means TRUE. */
4401 n1 = (type == TYPE_NEQUAL);
4402 }
4403 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4404 {
4405 if (type_is)
4406 {
4407 n1 = (rettv->v_type == var2.v_type
4408 && rettv->vval.v_list == var2.vval.v_list);
4409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 else if (rettv->v_type != var2.v_type
4413 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4414 {
4415 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004416 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004418 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004419 clear_tv(rettv);
4420 clear_tv(&var2);
4421 return FAIL;
4422 }
4423 else
4424 {
4425 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004426 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4427 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 if (type == TYPE_NEQUAL)
4429 n1 = !n1;
4430 }
4431 }
4432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004433 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4434 {
4435 if (type_is)
4436 {
4437 n1 = (rettv->v_type == var2.v_type
4438 && rettv->vval.v_dict == var2.vval.v_dict);
4439 if (type == TYPE_NEQUAL)
4440 n1 = !n1;
4441 }
4442 else if (rettv->v_type != var2.v_type
4443 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4444 {
4445 if (rettv->v_type != var2.v_type)
4446 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4447 else
4448 EMSG(_("E736: Invalid operation for Dictionary"));
4449 clear_tv(rettv);
4450 clear_tv(&var2);
4451 return FAIL;
4452 }
4453 else
4454 {
4455 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004456 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4457 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004458 if (type == TYPE_NEQUAL)
4459 n1 = !n1;
4460 }
4461 }
4462
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4464 {
4465 if (rettv->v_type != var2.v_type
4466 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4467 {
4468 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004469 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004471 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004472 clear_tv(rettv);
4473 clear_tv(&var2);
4474 return FAIL;
4475 }
4476 else
4477 {
4478 /* Compare two Funcrefs for being equal or unequal. */
4479 if (rettv->vval.v_string == NULL
4480 || var2.vval.v_string == NULL)
4481 n1 = FALSE;
4482 else
4483 n1 = STRCMP(rettv->vval.v_string,
4484 var2.vval.v_string) == 0;
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 }
4489
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004490#ifdef FEAT_FLOAT
4491 /*
4492 * If one of the two variables is a float, compare as a float.
4493 * When using "=~" or "!~", always compare as string.
4494 */
4495 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4496 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4497 {
4498 float_T f1, f2;
4499
4500 if (rettv->v_type == VAR_FLOAT)
4501 f1 = rettv->vval.v_float;
4502 else
4503 f1 = get_tv_number(rettv);
4504 if (var2.v_type == VAR_FLOAT)
4505 f2 = var2.vval.v_float;
4506 else
4507 f2 = get_tv_number(&var2);
4508 n1 = FALSE;
4509 switch (type)
4510 {
4511 case TYPE_EQUAL: n1 = (f1 == f2); break;
4512 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4513 case TYPE_GREATER: n1 = (f1 > f2); break;
4514 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4515 case TYPE_SMALLER: n1 = (f1 < f2); break;
4516 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4517 case TYPE_UNKNOWN:
4518 case TYPE_MATCH:
4519 case TYPE_NOMATCH: break; /* avoid gcc warning */
4520 }
4521 }
4522#endif
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * If one of the two variables is a number, compare as a number.
4526 * When using "=~" or "!~", always compare as string.
4527 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004528 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 n1 = get_tv_number(rettv);
4532 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 switch (type)
4534 {
4535 case TYPE_EQUAL: n1 = (n1 == n2); break;
4536 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4537 case TYPE_GREATER: n1 = (n1 > n2); break;
4538 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4539 case TYPE_SMALLER: n1 = (n1 < n2); break;
4540 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4541 case TYPE_UNKNOWN:
4542 case TYPE_MATCH:
4543 case TYPE_NOMATCH: break; /* avoid gcc warning */
4544 }
4545 }
4546 else
4547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 s1 = get_tv_string_buf(rettv, buf1);
4549 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4551 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4552 else
4553 i = 0;
4554 n1 = FALSE;
4555 switch (type)
4556 {
4557 case TYPE_EQUAL: n1 = (i == 0); break;
4558 case TYPE_NEQUAL: n1 = (i != 0); break;
4559 case TYPE_GREATER: n1 = (i > 0); break;
4560 case TYPE_GEQUAL: n1 = (i >= 0); break;
4561 case TYPE_SMALLER: n1 = (i < 0); break;
4562 case TYPE_SEQUAL: n1 = (i <= 0); break;
4563
4564 case TYPE_MATCH:
4565 case TYPE_NOMATCH:
4566 /* avoid 'l' flag in 'cpoptions' */
4567 save_cpo = p_cpo;
4568 p_cpo = (char_u *)"";
4569 regmatch.regprog = vim_regcomp(s2,
4570 RE_MAGIC + RE_STRING);
4571 regmatch.rm_ic = ic;
4572 if (regmatch.regprog != NULL)
4573 {
4574 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4575 vim_free(regmatch.regprog);
4576 if (type == TYPE_NOMATCH)
4577 n1 = !n1;
4578 }
4579 p_cpo = save_cpo;
4580 break;
4581
4582 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4583 }
4584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 clear_tv(rettv);
4586 clear_tv(&var2);
4587 rettv->v_type = VAR_NUMBER;
4588 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 }
4591
4592 return OK;
4593}
4594
4595/*
4596 * Handle fourth level expression:
4597 * + number addition
4598 * - number subtraction
4599 * . string concatenation
4600 *
4601 * "arg" must point to the first non-white of the expression.
4602 * "arg" is advanced to the next non-white after the recognized expression.
4603 *
4604 * Return OK or FAIL.
4605 */
4606 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 int evaluate;
4611{
Bram Moolenaar33570922005-01-25 22:26:29 +00004612 typval_T var2;
4613 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 int op;
4615 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004616#ifdef FEAT_FLOAT
4617 float_T f1 = 0, f2 = 0;
4618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 char_u *s1, *s2;
4620 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4621 char_u *p;
4622
4623 /*
4624 * Get the first variable.
4625 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004626 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 return FAIL;
4628
4629 /*
4630 * Repeat computing, until no '+', '-' or '.' is following.
4631 */
4632 for (;;)
4633 {
4634 op = **arg;
4635 if (op != '+' && op != '-' && op != '.')
4636 break;
4637
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004638 if ((op != '+' || rettv->v_type != VAR_LIST)
4639#ifdef FEAT_FLOAT
4640 && (op == '.' || rettv->v_type != VAR_FLOAT)
4641#endif
4642 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 {
4644 /* For "list + ...", an illegal use of the first operand as
4645 * a number cannot be determined before evaluating the 2nd
4646 * operand: if this is also a list, all is ok.
4647 * For "something . ...", "something - ..." or "non-list + ...",
4648 * we know that the first operand needs to be a string or number
4649 * without evaluating the 2nd operand. So check before to avoid
4650 * side effects after an error. */
4651 if (evaluate && get_tv_string_chk(rettv) == NULL)
4652 {
4653 clear_tv(rettv);
4654 return FAIL;
4655 }
4656 }
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 /*
4659 * Get the second variable.
4660 */
4661 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004662 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 return FAIL;
4666 }
4667
4668 if (evaluate)
4669 {
4670 /*
4671 * Compute the result.
4672 */
4673 if (op == '.')
4674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4676 s2 = get_tv_string_buf_chk(&var2, buf2);
4677 if (s2 == NULL) /* type error ? */
4678 {
4679 clear_tv(rettv);
4680 clear_tv(&var2);
4681 return FAIL;
4682 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004683 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 clear_tv(rettv);
4685 rettv->v_type = VAR_STRING;
4686 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004688 else if (op == '+' && rettv->v_type == VAR_LIST
4689 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004690 {
4691 /* concatenate Lists */
4692 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4693 &var3) == FAIL)
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
4699 clear_tv(rettv);
4700 *rettv = var3;
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 else
4703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 int error = FALSE;
4705
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706#ifdef FEAT_FLOAT
4707 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004708 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709 f1 = rettv->vval.v_float;
4710 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712 else
4713#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 n1 = get_tv_number_chk(rettv, &error);
4716 if (error)
4717 {
4718 /* This can only happen for "list + non-list". For
4719 * "non-list + ..." or "something - ...", we returned
4720 * before evaluating the 2nd operand. */
4721 clear_tv(rettv);
4722 return FAIL;
4723 }
4724#ifdef FEAT_FLOAT
4725 if (var2.v_type == VAR_FLOAT)
4726 f1 = n1;
4727#endif
4728 }
4729#ifdef FEAT_FLOAT
4730 if (var2.v_type == VAR_FLOAT)
4731 {
4732 f2 = var2.vval.v_float;
4733 n2 = 0;
4734 }
4735 else
4736#endif
4737 {
4738 n2 = get_tv_number_chk(&var2, &error);
4739 if (error)
4740 {
4741 clear_tv(rettv);
4742 clear_tv(&var2);
4743 return FAIL;
4744 }
4745#ifdef FEAT_FLOAT
4746 if (rettv->v_type == VAR_FLOAT)
4747 f2 = n2;
4748#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751
4752#ifdef FEAT_FLOAT
4753 /* If there is a float on either side the result is a float. */
4754 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4755 {
4756 if (op == '+')
4757 f1 = f1 + f2;
4758 else
4759 f1 = f1 - f2;
4760 rettv->v_type = VAR_FLOAT;
4761 rettv->vval.v_float = f1;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764#endif
4765 {
4766 if (op == '+')
4767 n1 = n1 + n2;
4768 else
4769 n1 = n1 - n2;
4770 rettv->v_type = VAR_NUMBER;
4771 rettv->vval.v_number = n1;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 }
4777 return OK;
4778}
4779
4780/*
4781 * Handle fifth level expression:
4782 * * number multiplication
4783 * / number division
4784 * % number modulo
4785 *
4786 * "arg" must point to the first non-white of the expression.
4787 * "arg" is advanced to the next non-white after the recognized expression.
4788 *
4789 * Return OK or FAIL.
4790 */
4791 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004796 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaar33570922005-01-25 22:26:29 +00004798 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 int op;
4800 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801#ifdef FEAT_FLOAT
4802 int use_float = FALSE;
4803 float_T f1 = 0, f2;
4804#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
4808 * Get the first variable.
4809 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004810 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 return FAIL;
4812
4813 /*
4814 * Repeat computing, until no '*', '/' or '%' is following.
4815 */
4816 for (;;)
4817 {
4818 op = **arg;
4819 if (op != '*' && op != '/' && op != '%')
4820 break;
4821
4822 if (evaluate)
4823 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824#ifdef FEAT_FLOAT
4825 if (rettv->v_type == VAR_FLOAT)
4826 {
4827 f1 = rettv->vval.v_float;
4828 use_float = TRUE;
4829 n1 = 0;
4830 }
4831 else
4832#endif
4833 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 if (error)
4836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 else
4839 n1 = 0;
4840
4841 /*
4842 * Get the second variable.
4843 */
4844 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004845 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 return FAIL;
4847
4848 if (evaluate)
4849 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850#ifdef FEAT_FLOAT
4851 if (var2.v_type == VAR_FLOAT)
4852 {
4853 if (!use_float)
4854 {
4855 f1 = n1;
4856 use_float = TRUE;
4857 }
4858 f2 = var2.vval.v_float;
4859 n2 = 0;
4860 }
4861 else
4862#endif
4863 {
4864 n2 = get_tv_number_chk(&var2, &error);
4865 clear_tv(&var2);
4866 if (error)
4867 return FAIL;
4868#ifdef FEAT_FLOAT
4869 if (use_float)
4870 f2 = n2;
4871#endif
4872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881 if (op == '*')
4882 f1 = f1 * f2;
4883 else if (op == '/')
4884 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885# ifdef VMS
4886 /* VMS crashes on divide by zero, work around it */
4887 if (f2 == 0.0)
4888 {
4889 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004890 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004892 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004893 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004894 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004895 }
4896 else
4897 f1 = f1 / f2;
4898# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 /* We rely on the floating point library to handle divide
4900 * by zero to result in "inf" and not a crash. */
4901 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004906 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 return FAIL;
4908 }
4909 rettv->v_type = VAR_FLOAT;
4910 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 if (op == '*')
4916 n1 = n1 * n2;
4917 else if (op == '/')
4918 {
4919 if (n2 == 0) /* give an error message? */
4920 {
4921 if (n1 == 0)
4922 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4923 else if (n1 < 0)
4924 n1 = -0x7fffffffL;
4925 else
4926 n1 = 0x7fffffffL;
4927 }
4928 else
4929 n1 = n1 / n2;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 {
4933 if (n2 == 0) /* give an error message? */
4934 n1 = 0;
4935 else
4936 n1 = n1 % n2;
4937 }
4938 rettv->v_type = VAR_NUMBER;
4939 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 }
4943
4944 return OK;
4945}
4946
4947/*
4948 * Handle sixth level expression:
4949 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004950 * "string" string constant
4951 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * &option-name option value
4953 * @r register contents
4954 * identifier variable value
4955 * function() function call
4956 * $VAR environment variable
4957 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004958 * [expr, expr] List
4959 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 *
4961 * Also handle:
4962 * ! in front logical NOT
4963 * - in front unary minus
4964 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 * trailing [] subscript in String or List
4966 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * "arg" must point to the first non-white of the expression.
4969 * "arg" is advanced to the next non-white after the recognized expression.
4970 *
4971 * Return OK or FAIL.
4972 */
4973 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004974eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004978 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 long n;
4981 int len;
4982 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 char_u *start_leader, *end_leader;
4984 int ret = OK;
4985 char_u *alias;
4986
4987 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 /*
4994 * Skip '!' and '-' characters. They are handled later.
4995 */
4996 start_leader = *arg;
4997 while (**arg == '!' || **arg == '-' || **arg == '+')
4998 *arg = skipwhite(*arg + 1);
4999 end_leader = *arg;
5000
5001 switch (**arg)
5002 {
5003 /*
5004 * Number constant.
5005 */
5006 case '0':
5007 case '1':
5008 case '2':
5009 case '3':
5010 case '4':
5011 case '5':
5012 case '6':
5013 case '7':
5014 case '8':
5015 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005016 {
5017#ifdef FEAT_FLOAT
5018 char_u *p = skipdigits(*arg + 1);
5019 int get_float = FALSE;
5020
5021 /* We accept a float when the format matches
5022 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005023 * strict to avoid backwards compatibility problems.
5024 * Don't look for a float after the "." operator, so that
5025 * ":let vers = 1.2.3" doesn't fail. */
5026 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 get_float = TRUE;
5029 p = skipdigits(p + 2);
5030 if (*p == 'e' || *p == 'E')
5031 {
5032 ++p;
5033 if (*p == '-' || *p == '+')
5034 ++p;
5035 if (!vim_isdigit(*p))
5036 get_float = FALSE;
5037 else
5038 p = skipdigits(p + 1);
5039 }
5040 if (ASCII_ISALPHA(*p) || *p == '.')
5041 get_float = FALSE;
5042 }
5043 if (get_float)
5044 {
5045 float_T f;
5046
5047 *arg += string2float(*arg, &f);
5048 if (evaluate)
5049 {
5050 rettv->v_type = VAR_FLOAT;
5051 rettv->vval.v_float = f;
5052 }
5053 }
5054 else
5055#endif
5056 {
5057 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5058 *arg += len;
5059 if (evaluate)
5060 {
5061 rettv->v_type = VAR_NUMBER;
5062 rettv->vval.v_number = n;
5063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
5069 * String constant: "string".
5070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 break;
5079
5080 /*
5081 * List: [expr, expr]
5082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085
5086 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 * Dictionary: {key: val, key: val}
5088 */
5089 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5090 break;
5091
5092 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097
5098 /*
5099 * Environment variable: $VAR.
5100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /*
5105 * Register contents: @r.
5106 */
5107 case '@': ++*arg;
5108 if (evaluate)
5109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005110 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005111 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113 if (**arg != NUL)
5114 ++*arg;
5115 break;
5116
5117 /*
5118 * nested expression: (expression).
5119 */
5120 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (**arg == ')')
5123 ++*arg;
5124 else if (ret == OK)
5125 {
5126 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 ret = FAIL;
5129 }
5130 break;
5131
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 break;
5134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135
5136 if (ret == NOTDONE)
5137 {
5138 /*
5139 * Must be a variable or function name.
5140 * Can also be a curly-braces kind of name: {expr}.
5141 */
5142 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 if (alias != NULL)
5145 s = alias;
5146
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005147 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 ret = FAIL;
5149 else
5150 {
5151 if (**arg == '(') /* recursive! */
5152 {
5153 /* If "s" is the name of a variable of type VAR_FUNC
5154 * use its contents. */
5155 s = deref_func_name(s, &len);
5156
5157 /* Invoke the function. */
5158 ret = get_func_tv(s, len, rettv, arg,
5159 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005160 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 /* Stop the expression evaluation when immediately
5162 * aborting on error, or when an interrupt occurred or
5163 * an exception was thrown but not caught. */
5164 if (aborting())
5165 {
5166 if (ret == OK)
5167 clear_tv(rettv);
5168 ret = FAIL;
5169 }
5170 }
5171 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005172 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005173 else
5174 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005176 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 *arg = skipwhite(*arg);
5180
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5182 * expr(expr). */
5183 if (ret == OK)
5184 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186 /*
5187 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5188 */
5189 if (ret == OK && evaluate && end_leader > start_leader)
5190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005191 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005192 int val = 0;
5193#ifdef FEAT_FLOAT
5194 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005195
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 if (rettv->v_type == VAR_FLOAT)
5197 f = rettv->vval.v_float;
5198 else
5199#endif
5200 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 clear_tv(rettv);
5204 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 else
5207 {
5208 while (end_leader > start_leader)
5209 {
5210 --end_leader;
5211 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 {
5213#ifdef FEAT_FLOAT
5214 if (rettv->v_type == VAR_FLOAT)
5215 f = !f;
5216 else
5217#endif
5218 val = !val;
5219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005221 {
5222#ifdef FEAT_FLOAT
5223 if (rettv->v_type == VAR_FLOAT)
5224 f = -f;
5225 else
5226#endif
5227 val = -val;
5228 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 {
5233 clear_tv(rettv);
5234 rettv->vval.v_float = f;
5235 }
5236 else
5237#endif
5238 {
5239 clear_tv(rettv);
5240 rettv->v_type = VAR_NUMBER;
5241 rettv->vval.v_number = val;
5242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245
5246 return ret;
5247}
5248
5249/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005250 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5251 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5253 */
5254 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260{
5261 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 long len = -1;
5265 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 if (rettv->v_type == VAR_FUNC
5270#ifdef FEAT_FLOAT
5271 || rettv->v_type == VAR_FLOAT
5272#endif
5273 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 if (verbose)
5276 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 return FAIL;
5278 }
5279
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 /*
5283 * dict.name
5284 */
5285 key = *arg + 1;
5286 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5287 ;
5288 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 /*
5295 * something[idx]
5296 *
5297 * Get the (first) variable from inside the [].
5298 */
5299 *arg = skipwhite(*arg + 1);
5300 if (**arg == ':')
5301 empty1 = TRUE;
5302 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5303 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5305 {
5306 /* not a number or string */
5307 clear_tv(&var1);
5308 return FAIL;
5309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310
5311 /*
5312 * Get the second variable from inside the [:].
5313 */
5314 if (**arg == ':')
5315 {
5316 range = TRUE;
5317 *arg = skipwhite(*arg + 1);
5318 if (**arg == ']')
5319 empty2 = TRUE;
5320 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 if (!empty1)
5323 clear_tv(&var1);
5324 return FAIL;
5325 }
5326 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5327 {
5328 /* not a number or string */
5329 if (!empty1)
5330 clear_tv(&var1);
5331 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 return FAIL;
5333 }
5334 }
5335
5336 /* Check for the ']'. */
5337 if (**arg != ']')
5338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 if (verbose)
5340 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 clear_tv(&var1);
5342 if (range)
5343 clear_tv(&var2);
5344 return FAIL;
5345 }
5346 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 }
5348
5349 if (evaluate)
5350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 n1 = 0;
5352 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 n1 = get_tv_number(&var1);
5355 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 if (range)
5358 {
5359 if (empty2)
5360 n2 = -1;
5361 else
5362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n2 = get_tv_number(&var2);
5364 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 }
5367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
5370 case VAR_NUMBER:
5371 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 len = (long)STRLEN(s);
5374 if (range)
5375 {
5376 /* The resulting variable is a substring. If the indexes
5377 * are out of range the result is empty. */
5378 if (n1 < 0)
5379 {
5380 n1 = len + n1;
5381 if (n1 < 0)
5382 n1 = 0;
5383 }
5384 if (n2 < 0)
5385 n2 = len + n2;
5386 else if (n2 >= len)
5387 n2 = len;
5388 if (n1 >= len || n2 < 0 || n1 > n2)
5389 s = NULL;
5390 else
5391 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5392 }
5393 else
5394 {
5395 /* The resulting variable is a string of a single
5396 * character. If the index is too big or negative the
5397 * result is empty. */
5398 if (n1 >= len || n1 < 0)
5399 s = NULL;
5400 else
5401 s = vim_strnsave(s + n1, 1);
5402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 clear_tv(rettv);
5404 rettv->v_type = VAR_STRING;
5405 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 break;
5407
5408 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 if (n1 < 0)
5411 n1 = len + n1;
5412 if (!empty1 && (n1 < 0 || n1 >= len))
5413 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005414 /* For a range we allow invalid values and return an empty
5415 * list. A list index out of range is an error. */
5416 if (!range)
5417 {
5418 if (verbose)
5419 EMSGN(_(e_listidx), n1);
5420 return FAIL;
5421 }
5422 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 }
5424 if (range)
5425 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 list_T *l;
5427 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428
5429 if (n2 < 0)
5430 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005431 else if (n2 >= len)
5432 n2 = len - 1;
5433 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005434 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 l = list_alloc();
5436 if (l == NULL)
5437 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 n1 <= n2; ++n1)
5440 {
5441 if (list_append_tv(l, &item->li_tv) == FAIL)
5442 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005443 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 return FAIL;
5445 }
5446 item = item->li_next;
5447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 clear_tv(rettv);
5449 rettv->v_type = VAR_LIST;
5450 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005451 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453 else
5454 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 clear_tv(rettv);
5457 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460
5461 case VAR_DICT:
5462 if (range)
5463 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005464 if (verbose)
5465 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 if (len == -1)
5467 clear_tv(&var1);
5468 return FAIL;
5469 }
5470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005471 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472
5473 if (len == -1)
5474 {
5475 key = get_tv_string(&var1);
5476 if (*key == NUL)
5477 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005478 if (verbose)
5479 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 clear_tv(&var1);
5481 return FAIL;
5482 }
5483 }
5484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005488 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 if (len == -1)
5490 clear_tv(&var1);
5491 if (item == NULL)
5492 return FAIL;
5493
5494 copy_tv(&item->di_tv, &var1);
5495 clear_tv(rettv);
5496 *rettv = var1;
5497 }
5498 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 }
5501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 return OK;
5503}
5504
5505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * Get an option value.
5507 * "arg" points to the '&' or '+' before the option name.
5508 * "arg" is advanced to character after the option name.
5509 * Return OK or FAIL.
5510 */
5511 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 int evaluate;
5516{
5517 char_u *option_end;
5518 long numval;
5519 char_u *stringval;
5520 int opt_type;
5521 int c;
5522 int working = (**arg == '+'); /* has("+option") */
5523 int ret = OK;
5524 int opt_flags;
5525
5526 /*
5527 * Isolate the option name and find its value.
5528 */
5529 option_end = find_option_end(arg, &opt_flags);
5530 if (option_end == NULL)
5531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 EMSG2(_("E112: Option name missing: %s"), *arg);
5534 return FAIL;
5535 }
5536
5537 if (!evaluate)
5538 {
5539 *arg = option_end;
5540 return OK;
5541 }
5542
5543 c = *option_end;
5544 *option_end = NUL;
5545 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 if (opt_type == -3) /* invalid name */
5549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 EMSG2(_("E113: Unknown option: %s"), *arg);
5552 ret = FAIL;
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 if (opt_type == -2) /* hidden string option */
5557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 rettv->v_type = VAR_STRING;
5559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 else if (opt_type == -1) /* hidden number option */
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 rettv->v_type = VAR_NUMBER;
5564 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566 else if (opt_type == 1) /* number option */
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 rettv->v_type = VAR_NUMBER;
5569 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 else /* string option */
5572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv->v_type = VAR_STRING;
5574 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 }
5577 else if (working && (opt_type == -2 || opt_type == -1))
5578 ret = FAIL;
5579
5580 *option_end = c; /* put back for error messages */
5581 *arg = option_end;
5582
5583 return ret;
5584}
5585
5586/*
5587 * Allocate a variable for a string constant.
5588 * Return OK or FAIL.
5589 */
5590 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int evaluate;
5595{
5596 char_u *p;
5597 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 int extra = 0;
5599
5600 /*
5601 * Find the end of the string, skipping backslashed characters.
5602 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005603 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 if (*p == '\\' && p[1] != NUL)
5606 {
5607 ++p;
5608 /* A "\<x>" form occupies at least 4 characters, and produces up
5609 * to 6 characters: reserve space for 2 extra */
5610 if (*p == '<')
5611 extra += 2;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614
5615 if (*p != '"')
5616 {
5617 EMSG2(_("E114: Missing quote: %s"), *arg);
5618 return FAIL;
5619 }
5620
5621 /* If only parsing, set *arg and return here */
5622 if (!evaluate)
5623 {
5624 *arg = p + 1;
5625 return OK;
5626 }
5627
5628 /*
5629 * Copy the string into allocated memory, handling backslashed
5630 * characters.
5631 */
5632 name = alloc((unsigned)(p - *arg + extra));
5633 if (name == NULL)
5634 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 rettv->v_type = VAR_STRING;
5636 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
5640 if (*p == '\\')
5641 {
5642 switch (*++p)
5643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 case 'b': *name++ = BS; ++p; break;
5645 case 'e': *name++ = ESC; ++p; break;
5646 case 'f': *name++ = FF; ++p; break;
5647 case 'n': *name++ = NL; ++p; break;
5648 case 'r': *name++ = CAR; ++p; break;
5649 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 case 'X': /* hex: "\x1", "\x12" */
5652 case 'x':
5653 case 'u': /* Unicode: "\u0023" */
5654 case 'U':
5655 if (vim_isxdigit(p[1]))
5656 {
5657 int n, nr;
5658 int c = toupper(*p);
5659
5660 if (c == 'X')
5661 n = 2;
5662 else
5663 n = 4;
5664 nr = 0;
5665 while (--n >= 0 && vim_isxdigit(p[1]))
5666 {
5667 ++p;
5668 nr = (nr << 4) + hex2nr(*p);
5669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_MBYTE
5672 /* For "\u" store the number according to
5673 * 'encoding'. */
5674 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else
5677#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681
5682 /* octal: "\1", "\12", "\123" */
5683 case '0':
5684 case '1':
5685 case '2':
5686 case '3':
5687 case '4':
5688 case '5':
5689 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 case '7': *name = *p++ - '0';
5691 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 *name = (*name << 3) + *p++ - '0';
5694 if (*p >= '0' && *p <= '7')
5695 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 break;
5699
5700 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 if (extra != 0)
5703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 break;
5706 }
5707 /* FALLTHROUGH */
5708
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 break;
5711 }
5712 }
5713 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 *arg = p + 1;
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 return OK;
5721}
5722
5723/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Return OK or FAIL.
5726 */
5727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 int evaluate;
5732{
5733 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 int reduce = 0;
5736
5737 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 */
5740 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 break;
5746 ++reduce;
5747 ++p;
5748 }
5749 }
5750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 return FAIL;
5755 }
5756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 if (!evaluate)
5759 {
5760 *arg = p + 1;
5761 return OK;
5762 }
5763
5764 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 */
5767 str = alloc((unsigned)((p - *arg) - reduce));
5768 if (str == NULL)
5769 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 rettv->v_type = VAR_STRING;
5771 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 break;
5779 ++p;
5780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 *arg = p + 1;
5785
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 return OK;
5787}
5788
5789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 * Allocate a variable for a List and fill it from "*arg".
5791 * Return OK or FAIL.
5792 */
5793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005794get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 int evaluate;
5798{
Bram Moolenaar33570922005-01-25 22:26:29 +00005799 list_T *l = NULL;
5800 typval_T tv;
5801 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802
5803 if (evaluate)
5804 {
5805 l = list_alloc();
5806 if (l == NULL)
5807 return FAIL;
5808 }
5809
5810 *arg = skipwhite(*arg + 1);
5811 while (**arg != ']' && **arg != NUL)
5812 {
5813 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5814 goto failret;
5815 if (evaluate)
5816 {
5817 item = listitem_alloc();
5818 if (item != NULL)
5819 {
5820 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005821 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 list_append(l, item);
5823 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005824 else
5825 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 }
5827
5828 if (**arg == ']')
5829 break;
5830 if (**arg != ',')
5831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 goto failret;
5834 }
5835 *arg = skipwhite(*arg + 1);
5836 }
5837
5838 if (**arg != ']')
5839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841failret:
5842 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005843 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 return FAIL;
5845 }
5846
5847 *arg = skipwhite(*arg + 1);
5848 if (evaluate)
5849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005850 rettv->v_type = VAR_LIST;
5851 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 ++l->lv_refcount;
5853 }
5854
5855 return OK;
5856}
5857
5858/*
5859 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005860 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005862 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863list_alloc()
5864{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 list_T *l;
5866
5867 l = (list_T *)alloc_clear(sizeof(list_T));
5868 if (l != NULL)
5869 {
5870 /* Prepend the list to the list of lists for garbage collection. */
5871 if (first_list != NULL)
5872 first_list->lv_used_prev = l;
5873 l->lv_used_prev = NULL;
5874 l->lv_used_next = first_list;
5875 first_list = l;
5876 }
5877 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878}
5879
5880/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005881 * Allocate an empty list for a return value.
5882 * Returns OK or FAIL.
5883 */
5884 static int
5885rettv_list_alloc(rettv)
5886 typval_T *rettv;
5887{
5888 list_T *l = list_alloc();
5889
5890 if (l == NULL)
5891 return FAIL;
5892
5893 rettv->vval.v_list = l;
5894 rettv->v_type = VAR_LIST;
5895 ++l->lv_refcount;
5896 return OK;
5897}
5898
5899/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 * Unreference a list: decrement the reference count and free it when it
5901 * becomes zero.
5902 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005903 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005907 if (l != NULL && --l->lv_refcount <= 0)
5908 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
5912 * Free a list, including all items it points to.
5913 * Ignores the reference count.
5914 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005915 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916list_free(l, recurse)
5917 list_T *l;
5918 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919{
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005922 /* Remove the list from the list of lists for garbage collection. */
5923 if (l->lv_used_prev == NULL)
5924 first_list = l->lv_used_next;
5925 else
5926 l->lv_used_prev->lv_used_next = l->lv_used_next;
5927 if (l->lv_used_next != NULL)
5928 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5929
Bram Moolenaard9fba312005-06-26 22:34:35 +00005930 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 /* Remove the item before deleting it. */
5933 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005934 if (recurse || (item->li_tv.v_type != VAR_LIST
5935 && item->li_tv.v_type != VAR_DICT))
5936 clear_tv(&item->li_tv);
5937 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 }
5939 vim_free(l);
5940}
5941
5942/*
5943 * Allocate a list item.
5944 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005945 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946listitem_alloc()
5947{
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949}
5950
5951/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005952 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 */
5954 static void
5955listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 vim_free(item);
5960}
5961
5962/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005963 * Remove a list item from a List and free it. Also clears the value.
5964 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005965 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005966listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 list_T *l;
5968 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005969{
5970 list_remove(l, item, item);
5971 listitem_free(item);
5972}
5973
5974/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 * Get the number of items in a list.
5976 */
5977 static long
5978list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 if (l == NULL)
5982 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005983 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984}
5985
5986/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987 * Return TRUE when two lists have exactly the same values.
5988 */
5989 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005990list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 list_T *l1;
5992 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005994 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995{
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005998 if (l1 == NULL || l2 == NULL)
5999 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006000 if (l1 == l2)
6001 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006002 if (list_len(l1) != list_len(l2))
6003 return FALSE;
6004
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 for (item1 = l1->lv_first, item2 = l2->lv_first;
6006 item1 != NULL && item2 != NULL;
6007 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006008 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 return FALSE;
6010 return item1 == NULL && item2 == NULL;
6011}
6012
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006013#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6014 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015/*
6016 * Return the dictitem that an entry in a hashtable points to.
6017 */
6018 dictitem_T *
6019dict_lookup(hi)
6020 hashitem_T *hi;
6021{
6022 return HI2DI(hi);
6023}
6024#endif
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006027 * Return TRUE when two dictionaries have exactly the same key/values.
6028 */
6029 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006030dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 dict_T *d1;
6032 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006034 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 hashitem_T *hi;
6037 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006040 if (d1 == NULL || d2 == NULL)
6041 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006042 if (d1 == d2)
6043 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044 if (dict_len(d1) != dict_len(d2))
6045 return FALSE;
6046
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006047 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 if (!HASHITEM_EMPTY(hi))
6051 {
6052 item2 = dict_find(d2, hi->hi_key, -1);
6053 if (item2 == NULL)
6054 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 return FALSE;
6057 --todo;
6058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 }
6060 return TRUE;
6061}
6062
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063static int tv_equal_recurse_limit;
6064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006066 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006067 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006068 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 */
6070 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 typval_T *tv1;
6073 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006074 int ic; /* ignore case */
6075 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076{
6077 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006078 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006080 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006082 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006085 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 * recursiveness to a limit. We guess they are equal then.
6087 * A fixed limit has the problem of still taking an awful long time.
6088 * Reduce the limit every time running into it. That should work fine for
6089 * deeply linked structures that are not recursively linked and catch
6090 * recursiveness quickly. */
6091 if (!recursive)
6092 tv_equal_recurse_limit = 1000;
6093 if (recursive_cnt >= tv_equal_recurse_limit)
6094 {
6095 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006096 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098
6099 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006101 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 ++recursive_cnt;
6103 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6104 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006105 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106
6107 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 ++recursive_cnt;
6109 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6110 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 case VAR_FUNC:
6114 return (tv1->vval.v_string != NULL
6115 && tv2->vval.v_string != NULL
6116 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6117
6118 case VAR_NUMBER:
6119 return tv1->vval.v_number == tv2->vval.v_number;
6120
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006121#ifdef FEAT_FLOAT
6122 case VAR_FLOAT:
6123 return tv1->vval.v_float == tv2->vval.v_float;
6124#endif
6125
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006126 case VAR_STRING:
6127 s1 = get_tv_string_buf(tv1, buf1);
6128 s2 = get_tv_string_buf(tv2, buf2);
6129 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006131
6132 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 return TRUE;
6134}
6135
6136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Locate item with index "n" in list "l" and return it.
6138 * A negative index is counted from the end; -1 is the last item.
6139 * Returns NULL when "n" is out of range.
6140 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006141 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144 long n;
6145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 long idx;
6148
6149 if (l == NULL)
6150 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006151
6152 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006154 n = l->lv_len + n;
6155
6156 /* Check for index out of range. */
6157 if (n < 0 || n >= l->lv_len)
6158 return NULL;
6159
6160 /* When there is a cached index may start search from there. */
6161 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 if (n < l->lv_idx / 2)
6164 {
6165 /* closest to the start of the list */
6166 item = l->lv_first;
6167 idx = 0;
6168 }
6169 else if (n > (l->lv_idx + l->lv_len) / 2)
6170 {
6171 /* closest to the end of the list */
6172 item = l->lv_last;
6173 idx = l->lv_len - 1;
6174 }
6175 else
6176 {
6177 /* closest to the cached index */
6178 item = l->lv_idx_item;
6179 idx = l->lv_idx;
6180 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 }
6182 else
6183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_len / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197
6198 while (n > idx)
6199 {
6200 /* search forward */
6201 item = item->li_next;
6202 ++idx;
6203 }
6204 while (n < idx)
6205 {
6206 /* search backward */
6207 item = item->li_prev;
6208 --idx;
6209 }
6210
6211 /* cache the used index */
6212 l->lv_idx = idx;
6213 l->lv_idx_item = item;
6214
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006215 return item;
6216}
6217
6218/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006219 * Get list item "l[idx]" as a number.
6220 */
6221 static long
6222list_find_nr(l, idx, errorp)
6223 list_T *l;
6224 long idx;
6225 int *errorp; /* set to TRUE when something wrong */
6226{
6227 listitem_T *li;
6228
6229 li = list_find(l, idx);
6230 if (li == NULL)
6231 {
6232 if (errorp != NULL)
6233 *errorp = TRUE;
6234 return -1L;
6235 }
6236 return get_tv_number_chk(&li->li_tv, errorp);
6237}
6238
6239/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006240 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6241 */
6242 char_u *
6243list_find_str(l, idx)
6244 list_T *l;
6245 long idx;
6246{
6247 listitem_T *li;
6248
6249 li = list_find(l, idx - 1);
6250 if (li == NULL)
6251 {
6252 EMSGN(_(e_listidx), idx);
6253 return NULL;
6254 }
6255 return get_tv_string(&li->li_tv);
6256}
6257
6258/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006259 * Locate "item" list "l" and return its index.
6260 * Returns -1 when "item" is not in the list.
6261 */
6262 static long
6263list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *l;
6265 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006266{
6267 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006269
6270 if (l == NULL)
6271 return -1;
6272 idx = 0;
6273 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6274 ++idx;
6275 if (li == NULL)
6276 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006277 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278}
6279
6280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 * Append item "item" to the end of list "l".
6282 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287{
6288 if (l->lv_last == NULL)
6289 {
6290 /* empty list */
6291 l->lv_first = item;
6292 l->lv_last = item;
6293 item->li_prev = NULL;
6294 }
6295 else
6296 {
6297 l->lv_last->li_next = item;
6298 item->li_prev = l->lv_last;
6299 l->lv_last = item;
6300 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 item->li_next = NULL;
6303}
6304
6305/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006309 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 list_T *l;
6312 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006314 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 copy_tv(tv, &li->li_tv);
6319 list_append(l, li);
6320 return OK;
6321}
6322
6323/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006324 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 * Return FAIL when out of memory.
6326 */
6327 int
6328list_append_dict(list, dict)
6329 list_T *list;
6330 dict_T *dict;
6331{
6332 listitem_T *li = listitem_alloc();
6333
6334 if (li == NULL)
6335 return FAIL;
6336 li->li_tv.v_type = VAR_DICT;
6337 li->li_tv.v_lock = 0;
6338 li->li_tv.vval.v_dict = dict;
6339 list_append(list, li);
6340 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341 return OK;
6342}
6343
6344/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006345 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006346 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Returns FAIL when out of memory.
6348 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006350list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 list_T *l;
6352 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006353 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354{
6355 listitem_T *li = listitem_alloc();
6356
6357 if (li == NULL)
6358 return FAIL;
6359 list_append(l, li);
6360 li->li_tv.v_type = VAR_STRING;
6361 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006362 if (str == NULL)
6363 li->li_tv.vval.v_string = NULL;
6364 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006365 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 return FAIL;
6367 return OK;
6368}
6369
6370/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371 * Append "n" to list "l".
6372 * Returns FAIL when out of memory.
6373 */
6374 static int
6375list_append_number(l, n)
6376 list_T *l;
6377 varnumber_T n;
6378{
6379 listitem_T *li;
6380
6381 li = listitem_alloc();
6382 if (li == NULL)
6383 return FAIL;
6384 li->li_tv.v_type = VAR_NUMBER;
6385 li->li_tv.v_lock = 0;
6386 li->li_tv.vval.v_number = n;
6387 list_append(l, li);
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393 * If "item" is NULL append at the end.
6394 * Return FAIL when out of memory.
6395 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *l;
6399 typval_T *tv;
6400 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401{
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403
6404 if (ni == NULL)
6405 return FAIL;
6406 copy_tv(tv, &ni->li_tv);
6407 if (item == NULL)
6408 /* Append new item at end of list. */
6409 list_append(l, ni);
6410 else
6411 {
6412 /* Insert new item before existing item. */
6413 ni->li_prev = item->li_prev;
6414 ni->li_next = item;
6415 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006416 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 ++l->lv_idx;
6419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006421 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 l->lv_idx_item = NULL;
6424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006426 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 }
6428 return OK;
6429}
6430
6431/*
6432 * Extend "l1" with "l2".
6433 * If "bef" is NULL append at the end, otherwise insert before this item.
6434 * Returns FAIL when out of memory.
6435 */
6436 static int
6437list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 list_T *l1;
6439 list_T *l2;
6440 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441{
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006443 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 /* We also quit the loop when we have inserted the original item count of
6446 * the list, avoid a hang when we extend a list with itself. */
6447 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6449 return FAIL;
6450 return OK;
6451}
6452
6453/*
6454 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6455 * Return FAIL when out of memory.
6456 */
6457 static int
6458list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 list_T *l1;
6460 list_T *l2;
6461 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462{
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006465 if (l1 == NULL || l2 == NULL)
6466 return FAIL;
6467
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 if (l == NULL)
6471 return FAIL;
6472 tv->v_type = VAR_LIST;
6473 tv->vval.v_list = l;
6474
6475 /* append all items from the second list */
6476 return list_extend(l, l2, NULL);
6477}
6478
6479/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006480 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * Returns NULL when out of memory.
6484 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *copy;
6492 listitem_T *item;
6493 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494
6495 if (orig == NULL)
6496 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497
6498 copy = list_alloc();
6499 if (copy != NULL)
6500 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 if (copyID != 0)
6502 {
6503 /* Do this before adding the items, because one of the items may
6504 * refer back to this list. */
6505 orig->lv_copyID = copyID;
6506 orig->lv_copylist = copy;
6507 }
6508 for (item = orig->lv_first; item != NULL && !got_int;
6509 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 {
6511 ni = listitem_alloc();
6512 if (ni == NULL)
6513 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006514 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 {
6516 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6517 {
6518 vim_free(ni);
6519 break;
6520 }
6521 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006523 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 list_append(copy, ni);
6525 }
6526 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 if (item != NULL)
6528 {
6529 list_unref(copy);
6530 copy = NULL;
6531 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 }
6533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 return copy;
6535}
6536
6537/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006539 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006541 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006542list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 list_T *l;
6544 listitem_T *item;
6545 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546{
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 /* notify watchers */
6550 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 list_fix_watch(l, ip);
6554 if (ip == item2)
6555 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
6558 if (item2->li_next == NULL)
6559 l->lv_last = item->li_prev;
6560 else
6561 item2->li_next->li_prev = item->li_prev;
6562 if (item->li_prev == NULL)
6563 l->lv_first = item2->li_next;
6564 else
6565 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567}
6568
6569/*
6570 * Return an allocated string with the string representation of a list.
6571 * May return NULL.
6572 */
6573 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577{
6578 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579
6580 if (tv->vval.v_list == NULL)
6581 return NULL;
6582 ga_init2(&ga, (int)sizeof(char), 80);
6583 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006584 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 {
6586 vim_free(ga.ga_data);
6587 return NULL;
6588 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 ga_append(&ga, ']');
6590 ga_append(&ga, NUL);
6591 return (char_u *)ga.ga_data;
6592}
6593
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006594typedef struct join_S {
6595 char_u *s;
6596 char_u *tofree;
6597} join_T;
6598
6599 static int
6600list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6601 garray_T *gap; /* to store the result in */
6602 list_T *l;
6603 char_u *sep;
6604 int echo_style;
6605 int copyID;
6606 garray_T *join_gap; /* to keep each list item string */
6607{
6608 int i;
6609 join_T *p;
6610 int len;
6611 int sumlen = 0;
6612 int first = TRUE;
6613 char_u *tofree;
6614 char_u numbuf[NUMBUFLEN];
6615 listitem_T *item;
6616 char_u *s;
6617
6618 /* Stringify each item in the list. */
6619 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6620 {
6621 if (echo_style)
6622 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6623 else
6624 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6625 if (s == NULL)
6626 return FAIL;
6627
6628 len = (int)STRLEN(s);
6629 sumlen += len;
6630
6631 ga_grow(join_gap, 1);
6632 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6633 if (tofree != NULL || s != numbuf)
6634 {
6635 p->s = s;
6636 p->tofree = tofree;
6637 }
6638 else
6639 {
6640 p->s = vim_strnsave(s, len);
6641 p->tofree = p->s;
6642 }
6643
6644 line_breakcheck();
6645 }
6646
6647 /* Allocate result buffer with its total size, avoid re-allocation and
6648 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6649 if (join_gap->ga_len >= 2)
6650 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6651 if (ga_grow(gap, sumlen + 2) == FAIL)
6652 return FAIL;
6653
6654 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6655 {
6656 if (first)
6657 first = FALSE;
6658 else
6659 ga_concat(gap, sep);
6660 p = ((join_T *)join_gap->ga_data) + i;
6661
6662 if (p->s != NULL)
6663 ga_concat(gap, p->s);
6664 line_breakcheck();
6665 }
6666
6667 return OK;
6668}
6669
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006672 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006673 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006676list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006680 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006681 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683 garray_T join_ga;
6684 int retval;
6685 join_T *p;
6686 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006688 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6689 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6690
6691 /* Dispose each item in join_ga. */
6692 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694 p = (join_T *)join_ga.ga_data;
6695 for (i = 0; i < join_ga.ga_len; ++i)
6696 {
6697 vim_free(p->tofree);
6698 ++p;
6699 }
6700 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702
6703 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704}
6705
6706/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006707 * Garbage collection for lists and dictionaries.
6708 *
6709 * We use reference counts to be able to free most items right away when they
6710 * are no longer used. But for composite items it's possible that it becomes
6711 * unused while the reference count is > 0: When there is a recursive
6712 * reference. Example:
6713 * :let l = [1, 2, 3]
6714 * :let d = {9: l}
6715 * :let l[1] = d
6716 *
6717 * Since this is quite unusual we handle this with garbage collection: every
6718 * once in a while find out which lists and dicts are not referenced from any
6719 * variable.
6720 *
6721 * Here is a good reference text about garbage collection (refers to Python
6722 * but it applies to all reference-counting mechanisms):
6723 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006725
6726/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727 * Do garbage collection for lists and dicts.
6728 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006729 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006730 int
6731garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006732{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006733 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 buf_T *buf;
6735 win_T *wp;
6736 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006737 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006738 int did_free;
6739 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006740#ifdef FEAT_WINDOWS
6741 tabpage_T *tp;
6742#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006744 /* Only do this once. */
6745 want_garbage_collect = FALSE;
6746 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006747 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006748
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 /* We advance by two because we add one for items referenced through
6750 * previous_funccal. */
6751 current_copyID += COPYID_INC;
6752 copyID = current_copyID;
6753
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 /*
6755 * 1. Go through all accessible variables and mark all lists and dicts
6756 * with copyID.
6757 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758
6759 /* Don't free variables in the previous_funccal list unless they are only
6760 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006761 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006762 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6763 {
6764 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6765 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6766 }
6767
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 /* script-local variables */
6769 for (i = 1; i <= ga_scripts.ga_len; ++i)
6770 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6771
6772 /* buffer-local variables */
6773 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6774 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6775
6776 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006777 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6779
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006780#ifdef FEAT_WINDOWS
6781 /* tabpage-local variables */
6782 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6783 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6784#endif
6785
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786 /* global variables */
6787 set_ref_in_ht(&globvarht, copyID);
6788
6789 /* function-local variables */
6790 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6791 {
6792 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6793 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6794 }
6795
Bram Moolenaard812df62008-11-09 12:46:09 +00006796 /* v: vars */
6797 set_ref_in_ht(&vimvarht, copyID);
6798
Bram Moolenaar1dced572012-04-05 16:54:08 +02006799#ifdef FEAT_LUA
6800 set_ref_in_lua(copyID);
6801#endif
6802
Bram Moolenaardb913952012-06-29 12:54:53 +02006803#ifdef FEAT_PYTHON
6804 set_ref_in_python(copyID);
6805#endif
6806
6807#ifdef FEAT_PYTHON3
6808 set_ref_in_python3(copyID);
6809#endif
6810
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006811 /*
6812 * 2. Free lists and dictionaries that are not referenced.
6813 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006814 did_free = free_unref_items(copyID);
6815
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006816 /*
6817 * 3. Check if any funccal can be freed now.
6818 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006819 for (pfc = &previous_funccal; *pfc != NULL; )
6820 {
6821 if (can_free_funccal(*pfc, copyID))
6822 {
6823 fc = *pfc;
6824 *pfc = fc->caller;
6825 free_funccal(fc, TRUE);
6826 did_free = TRUE;
6827 did_free_funccal = TRUE;
6828 }
6829 else
6830 pfc = &(*pfc)->caller;
6831 }
6832 if (did_free_funccal)
6833 /* When a funccal was freed some more items might be garbage
6834 * collected, so run again. */
6835 (void)garbage_collect();
6836
6837 return did_free;
6838}
6839
6840/*
6841 * Free lists and dictionaries that are no longer referenced.
6842 */
6843 static int
6844free_unref_items(copyID)
6845 int copyID;
6846{
6847 dict_T *dd;
6848 list_T *ll;
6849 int did_free = FALSE;
6850
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 */
6854 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006857 /* Free the Dictionary and ordinary items it contains, but don't
6858 * recurse into Lists and Dictionaries, they will be in the list
6859 * of dicts or list of lists. */
6860 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 did_free = TRUE;
6862
6863 /* restart, next dict may also have been freed */
6864 dd = first_dict;
6865 }
6866 else
6867 dd = dd->dv_used_next;
6868
6869 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 * Go through the list of lists and free items without the copyID.
6871 * But don't free a list that has a watcher (used in a for loop), these
6872 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873 */
6874 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6876 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006878 /* Free the List and ordinary items it contains, but don't recurse
6879 * into Lists and Dictionaries, they will be in the list of dicts
6880 * or list of lists. */
6881 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 did_free = TRUE;
6883
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006884 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 ll = first_list;
6886 }
6887 else
6888 ll = ll->lv_used_next;
6889
6890 return did_free;
6891}
6892
6893/*
6894 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6895 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006896 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897set_ref_in_ht(ht, copyID)
6898 hashtab_T *ht;
6899 int copyID;
6900{
6901 int todo;
6902 hashitem_T *hi;
6903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006904 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 for (hi = ht->ht_array; todo > 0; ++hi)
6906 if (!HASHITEM_EMPTY(hi))
6907 {
6908 --todo;
6909 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6910 }
6911}
6912
6913/*
6914 * Mark all lists and dicts referenced through list "l" with "copyID".
6915 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006916 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917set_ref_in_list(l, copyID)
6918 list_T *l;
6919 int copyID;
6920{
6921 listitem_T *li;
6922
6923 for (li = l->lv_first; li != NULL; li = li->li_next)
6924 set_ref_in_item(&li->li_tv, copyID);
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_item(tv, copyID)
6932 typval_T *tv;
6933 int copyID;
6934{
6935 dict_T *dd;
6936 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006937
6938 switch (tv->v_type)
6939 {
6940 case VAR_DICT:
6941 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006942 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006943 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 /* Didn't see this dict yet. */
6945 dd->dv_copyID = copyID;
6946 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006947 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006948 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949
6950 case VAR_LIST:
6951 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006952 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 /* Didn't see this list yet. */
6955 ll->lv_copyID = copyID;
6956 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961}
6962
6963/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964 * Allocate an empty header for a dictionary.
6965 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006966 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967dict_alloc()
6968{
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006970
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006973 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006974 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 if (first_dict != NULL)
6976 first_dict->dv_used_prev = d;
6977 d->dv_used_next = first_dict;
6978 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006979 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006982 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006983 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006985 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006987 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006988}
6989
6990/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006991 * Allocate an empty dict for a return value.
6992 * Returns OK or FAIL.
6993 */
6994 static int
6995rettv_dict_alloc(rettv)
6996 typval_T *rettv;
6997{
6998 dict_T *d = dict_alloc();
6999
7000 if (d == NULL)
7001 return FAIL;
7002
7003 rettv->vval.v_dict = d;
7004 rettv->v_type = VAR_DICT;
7005 ++d->dv_refcount;
7006 return OK;
7007}
7008
7009
7010/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011 * Unreference a Dictionary: decrement the reference count and free it when it
7012 * becomes zero.
7013 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007014 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007016 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007018 if (d != NULL && --d->dv_refcount <= 0)
7019 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020}
7021
7022/*
7023 * Free a Dictionary, including all items it contains.
7024 * Ignores the reference count.
7025 */
7026 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007027dict_free(d, recurse)
7028 dict_T *d;
7029 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007031 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 /* Remove the dict from the list of dicts for garbage collection. */
7036 if (d->dv_used_prev == NULL)
7037 first_dict = d->dv_used_next;
7038 else
7039 d->dv_used_prev->dv_used_next = d->dv_used_next;
7040 if (d->dv_used_next != NULL)
7041 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7042
7043 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007044 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007045 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048 if (!HASHITEM_EMPTY(hi))
7049 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050 /* Remove the item before deleting it, just in case there is
7051 * something recursive causing trouble. */
7052 di = HI2DI(hi);
7053 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007054 if (recurse || (di->di_tv.v_type != VAR_LIST
7055 && di->di_tv.v_type != VAR_DICT))
7056 clear_tv(&di->di_tv);
7057 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 --todo;
7059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 vim_free(d);
7063}
7064
7065/*
7066 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007067 * The "key" is copied to the new item.
7068 * Note that the value of the item "di_tv" still needs to be initialized!
7069 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007071 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072dictitem_alloc(key)
7073 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074{
Bram Moolenaar33570922005-01-25 22:26:29 +00007075 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007077 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007079 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007081 di->di_flags = 0;
7082 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084}
7085
7086/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007087 * Make a copy of a Dictionary item.
7088 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007089 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092{
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007095 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7096 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097 if (di != NULL)
7098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007100 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007101 copy_tv(&org->di_tv, &di->di_tv);
7102 }
7103 return di;
7104}
7105
7106/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107 * Remove item "item" from Dictionary "dict" and free it.
7108 */
7109 static void
7110dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 dict_T *dict;
7112 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113{
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 if (HASHITEM_EMPTY(hi))
7118 EMSG2(_(e_intern2), "dictitem_remove()");
7119 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 dictitem_free(item);
7122}
7123
7124/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 * Free a dict item. Also clears the value.
7126 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007127 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 clear_tv(&item->di_tv);
7132 vim_free(item);
7133}
7134
7135/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7137 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007138 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139 * Returns NULL when out of memory.
7140 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007141 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007142dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007145 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146{
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dict_T *copy;
7148 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151
7152 if (orig == NULL)
7153 return NULL;
7154
7155 copy = dict_alloc();
7156 if (copy != NULL)
7157 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158 if (copyID != 0)
7159 {
7160 orig->dv_copyID = copyID;
7161 orig->dv_copydict = copy;
7162 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007163 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007164 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007165 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007166 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168 --todo;
7169
7170 di = dictitem_alloc(hi->hi_key);
7171 if (di == NULL)
7172 break;
7173 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007174 {
7175 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7176 copyID) == FAIL)
7177 {
7178 vim_free(di);
7179 break;
7180 }
7181 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 else
7183 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7184 if (dict_add(copy, di) == FAIL)
7185 {
7186 dictitem_free(di);
7187 break;
7188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 if (todo > 0)
7194 {
7195 dict_unref(copy);
7196 copy = NULL;
7197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 }
7199
7200 return copy;
7201}
7202
7203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007205 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007207 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 dict_T *d;
7210 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211{
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213}
7214
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007216 * Add a number or string entry to dictionary "d".
7217 * When "str" is NULL use number "nr", otherwise use "str".
7218 * Returns FAIL when out of memory and when key already exists.
7219 */
7220 int
7221dict_add_nr_str(d, key, nr, str)
7222 dict_T *d;
7223 char *key;
7224 long nr;
7225 char_u *str;
7226{
7227 dictitem_T *item;
7228
7229 item = dictitem_alloc((char_u *)key);
7230 if (item == NULL)
7231 return FAIL;
7232 item->di_tv.v_lock = 0;
7233 if (str == NULL)
7234 {
7235 item->di_tv.v_type = VAR_NUMBER;
7236 item->di_tv.vval.v_number = nr;
7237 }
7238 else
7239 {
7240 item->di_tv.v_type = VAR_STRING;
7241 item->di_tv.vval.v_string = vim_strsave(str);
7242 }
7243 if (dict_add(d, item) == FAIL)
7244 {
7245 dictitem_free(item);
7246 return FAIL;
7247 }
7248 return OK;
7249}
7250
7251/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007252 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007253 * Returns FAIL when out of memory and when key already exists.
7254 */
7255 int
7256dict_add_list(d, key, list)
7257 dict_T *d;
7258 char *key;
7259 list_T *list;
7260{
7261 dictitem_T *item;
7262
7263 item = dictitem_alloc((char_u *)key);
7264 if (item == NULL)
7265 return FAIL;
7266 item->di_tv.v_lock = 0;
7267 item->di_tv.v_type = VAR_LIST;
7268 item->di_tv.vval.v_list = list;
7269 if (dict_add(d, item) == FAIL)
7270 {
7271 dictitem_free(item);
7272 return FAIL;
7273 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007274 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007275 return OK;
7276}
7277
7278/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279 * Get the number of items in a Dictionary.
7280 */
7281 static long
7282dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285 if (d == NULL)
7286 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288}
7289
7290/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 * Find item "key[len]" in Dictionary "d".
7292 * If "len" is negative use strlen(key).
7293 * Returns NULL when not found.
7294 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007295 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 char_u *key;
7299 int len;
7300{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301#define AKEYLEN 200
7302 char_u buf[AKEYLEN];
7303 char_u *akey;
7304 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 if (len < 0)
7308 akey = key;
7309 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 tofree = akey = vim_strnsave(key, len);
7312 if (akey == NULL)
7313 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007314 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 else
7316 {
7317 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007318 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 akey = buf;
7320 }
7321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 vim_free(tofree);
7324 if (HASHITEM_EMPTY(hi))
7325 return NULL;
7326 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327}
7328
7329/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007330 * Get a string item from a dictionary.
7331 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007332 * Returns NULL if the entry doesn't exist or out of memory.
7333 */
7334 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007335get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007336 dict_T *d;
7337 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007338 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007339{
7340 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007341 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007342
7343 di = dict_find(d, key, -1);
7344 if (di == NULL)
7345 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 s = get_tv_string(&di->di_tv);
7347 if (save && s != NULL)
7348 s = vim_strsave(s);
7349 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007350}
7351
7352/*
7353 * Get a number item from a dictionary.
7354 * Returns 0 if the entry doesn't exist or out of memory.
7355 */
7356 long
7357get_dict_number(d, key)
7358 dict_T *d;
7359 char_u *key;
7360{
7361 dictitem_T *di;
7362
7363 di = dict_find(d, key, -1);
7364 if (di == NULL)
7365 return 0;
7366 return get_tv_number(&di->di_tv);
7367}
7368
7369/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370 * Return an allocated string with the string representation of a Dictionary.
7371 * May return NULL.
7372 */
7373 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007374dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007376 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007377{
7378 garray_T ga;
7379 int first = TRUE;
7380 char_u *tofree;
7381 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 return NULL;
7389 ga_init2(&ga, (int)sizeof(char), 80);
7390 ga_append(&ga, '{');
7391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007392 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 --todo;
7398
7399 if (first)
7400 first = FALSE;
7401 else
7402 ga_concat(&ga, (char_u *)", ");
7403
7404 tofree = string_quote(hi->hi_key, FALSE);
7405 if (tofree != NULL)
7406 {
7407 ga_concat(&ga, tofree);
7408 vim_free(tofree);
7409 }
7410 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007412 if (s != NULL)
7413 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007415 if (s == NULL)
7416 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007419 if (todo > 0)
7420 {
7421 vim_free(ga.ga_data);
7422 return NULL;
7423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424
7425 ga_append(&ga, '}');
7426 ga_append(&ga, NUL);
7427 return (char_u *)ga.ga_data;
7428}
7429
7430/*
7431 * Allocate a variable for a Dictionary and fill it from "*arg".
7432 * Return OK or FAIL. Returns NOTDONE for {expr}.
7433 */
7434 static int
7435get_dict_tv(arg, rettv, evaluate)
7436 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 int evaluate;
7439{
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 dict_T *d = NULL;
7441 typval_T tvkey;
7442 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007443 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447
7448 /*
7449 * First check if it's not a curly-braces thing: {expr}.
7450 * Must do this without evaluating, otherwise a function may be called
7451 * twice. Unfortunately this means we need to call eval1() twice for the
7452 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 if (*start != '}')
7456 {
7457 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7458 return FAIL;
7459 if (*start == '}')
7460 return NOTDONE;
7461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462
7463 if (evaluate)
7464 {
7465 d = dict_alloc();
7466 if (d == NULL)
7467 return FAIL;
7468 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007469 tvkey.v_type = VAR_UNKNOWN;
7470 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471
7472 *arg = skipwhite(*arg + 1);
7473 while (**arg != '}' && **arg != NUL)
7474 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007475 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476 goto failret;
7477 if (**arg != ':')
7478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007479 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 goto failret;
7482 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007483 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007485 key = get_tv_string_buf_chk(&tvkey, buf);
7486 if (key == NULL || *key == NUL)
7487 {
7488 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7489 if (key != NULL)
7490 EMSG(_(e_emptykey));
7491 clear_tv(&tvkey);
7492 goto failret;
7493 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495
7496 *arg = skipwhite(*arg + 1);
7497 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7498 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007499 if (evaluate)
7500 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 goto failret;
7502 }
7503 if (evaluate)
7504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 if (item != NULL)
7507 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007508 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 clear_tv(&tv);
7511 goto failret;
7512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 item = dictitem_alloc(key);
7514 clear_tv(&tvkey);
7515 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007518 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 if (dict_add(d, item) == FAIL)
7520 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 }
7522 }
7523
7524 if (**arg == '}')
7525 break;
7526 if (**arg != ',')
7527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007528 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 goto failret;
7530 }
7531 *arg = skipwhite(*arg + 1);
7532 }
7533
7534 if (**arg != '}')
7535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007536 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537failret:
7538 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007539 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 return FAIL;
7541 }
7542
7543 *arg = skipwhite(*arg + 1);
7544 if (evaluate)
7545 {
7546 rettv->v_type = VAR_DICT;
7547 rettv->vval.v_dict = d;
7548 ++d->dv_refcount;
7549 }
7550
7551 return OK;
7552}
7553
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 * Return a string with the string representation of a variable.
7556 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007557 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007558 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007559 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007560 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007561 */
7562 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007563echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007566 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569 static int recurse = 0;
7570 char_u *r = NULL;
7571
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007574 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 *tofree = NULL;
7576 return NULL;
7577 }
7578 ++recurse;
7579
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007580 switch (tv->v_type)
7581 {
7582 case VAR_FUNC:
7583 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007584 r = tv->vval.v_string;
7585 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007587 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007588 if (tv->vval.v_list == NULL)
7589 {
7590 *tofree = NULL;
7591 r = NULL;
7592 }
7593 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7594 {
7595 *tofree = NULL;
7596 r = (char_u *)"[...]";
7597 }
7598 else
7599 {
7600 tv->vval.v_list->lv_copyID = copyID;
7601 *tofree = list2string(tv, copyID);
7602 r = *tofree;
7603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607 if (tv->vval.v_dict == NULL)
7608 {
7609 *tofree = NULL;
7610 r = NULL;
7611 }
7612 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7613 {
7614 *tofree = NULL;
7615 r = (char_u *)"{...}";
7616 }
7617 else
7618 {
7619 tv->vval.v_dict->dv_copyID = copyID;
7620 *tofree = dict2string(tv, copyID);
7621 r = *tofree;
7622 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007623 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 case VAR_STRING:
7626 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007627 *tofree = NULL;
7628 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007631#ifdef FEAT_FLOAT
7632 case VAR_FLOAT:
7633 *tofree = NULL;
7634 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7635 r = numbuf;
7636 break;
7637#endif
7638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007640 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643
7644 --recurse;
7645 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646}
7647
7648/*
7649 * Return a string with the string representation of a variable.
7650 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7651 * "numbuf" is used for a number.
7652 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007653 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007654 */
7655 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007656tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 char_u **tofree;
7659 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007660 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007661{
7662 switch (tv->v_type)
7663 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664 case VAR_FUNC:
7665 *tofree = string_quote(tv->vval.v_string, TRUE);
7666 return *tofree;
7667 case VAR_STRING:
7668 *tofree = string_quote(tv->vval.v_string, FALSE);
7669 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 case VAR_FLOAT:
7672 *tofree = NULL;
7673 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7674 return numbuf;
7675#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007679 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007682 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007683 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007684}
7685
7686/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 * Return string "str" in ' quotes, doubling ' characters.
7688 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 */
7691 static char_u *
7692string_quote(str, function)
7693 char_u *str;
7694 int function;
7695{
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007697 char_u *p, *r, *s;
7698
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 len = (function ? 13 : 3);
7700 if (str != NULL)
7701 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007702 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 for (p = str; *p != NUL; mb_ptr_adv(p))
7704 if (*p == '\'')
7705 ++len;
7706 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007707 s = r = alloc(len);
7708 if (r != NULL)
7709 {
7710 if (function)
7711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 r += 10;
7714 }
7715 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 if (str != NULL)
7718 for (p = str; *p != NUL; )
7719 {
7720 if (*p == '\'')
7721 *r++ = '\'';
7722 MB_COPY_CHAR(p, r);
7723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007725 if (function)
7726 *r++ = ')';
7727 *r++ = NUL;
7728 }
7729 return s;
7730}
7731
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007732#ifdef FEAT_FLOAT
7733/*
7734 * Convert the string "text" to a floating point number.
7735 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7736 * this always uses a decimal point.
7737 * Returns the length of the text that was consumed.
7738 */
7739 static int
7740string2float(text, value)
7741 char_u *text;
7742 float_T *value; /* result stored here */
7743{
7744 char *s = (char *)text;
7745 float_T f;
7746
7747 f = strtod(s, &s);
7748 *value = f;
7749 return (int)((char_u *)s - text);
7750}
7751#endif
7752
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 * Get the value of an environment variable.
7755 * "arg" is pointing to the '$'. It is advanced to after the name.
7756 * If the environment variable was not set, silently assume it is empty.
7757 * Always return OK.
7758 */
7759 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 int evaluate;
7764{
7765 char_u *string = NULL;
7766 int len;
7767 int cc;
7768 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007769 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
7771 ++*arg;
7772 name = *arg;
7773 len = get_env_len(arg);
7774 if (evaluate)
7775 {
7776 if (len != 0)
7777 {
7778 cc = name[len];
7779 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007780 /* first try vim_getenv(), fast for normal environment vars */
7781 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007783 {
7784 if (!mustfree)
7785 string = vim_strsave(string);
7786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 else
7788 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 if (mustfree)
7790 vim_free(string);
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 /* next try expanding things like $VIM and ${HOME} */
7793 string = expand_env_save(name - 1);
7794 if (string != NULL && *string == '$')
7795 {
7796 vim_free(string);
7797 string = NULL;
7798 }
7799 }
7800 name[len] = cc;
7801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 rettv->v_type = VAR_STRING;
7803 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 }
7805
7806 return OK;
7807}
7808
7809/*
7810 * Array with names and number of arguments of all internal functions
7811 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7812 */
7813static struct fst
7814{
7815 char *f_name; /* function name */
7816 char f_min_argc; /* minimal number of arguments */
7817 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007819 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820} functions[] =
7821{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007822#ifdef FEAT_FLOAT
7823 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007824 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007826 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007827 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {"append", 2, 2, f_append},
7829 {"argc", 0, 0, f_argc},
7830 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007831 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007832#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007833 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007834 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007835 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007838 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"bufexists", 1, 1, f_bufexists},
7840 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7841 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7842 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7843 {"buflisted", 1, 1, f_buflisted},
7844 {"bufloaded", 1, 1, f_bufloaded},
7845 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007846 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"bufwinnr", 1, 1, f_bufwinnr},
7848 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007849 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851#ifdef FEAT_FLOAT
7852 {"ceil", 1, 1, f_ceil},
7853#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007854 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"char2nr", 1, 1, f_char2nr},
7856 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007857 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007859#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007860 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007861 {"complete_add", 1, 1, f_complete_add},
7862 {"complete_check", 0, 0, f_complete_check},
7863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
7867 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007868 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007870 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007872 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007873 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {"delete", 1, 1, f_delete},
7875 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007876 {"diff_filler", 1, 1, f_diff_filler},
7877 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007878 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"eventhandler", 0, 0, f_eventhandler},
7882 {"executable", 1, 1, f_executable},
7883 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007884#ifdef FEAT_FLOAT
7885 {"exp", 1, 1, f_exp},
7886#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007887 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007888 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007889 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7891 {"filereadable", 1, 1, f_filereadable},
7892 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007894 {"finddir", 1, 3, f_finddir},
7895 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896#ifdef FEAT_FLOAT
7897 {"float2nr", 1, 1, f_float2nr},
7898 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007899 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007900#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007901 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"fnamemodify", 2, 2, f_fnamemodify},
7903 {"foldclosed", 1, 1, f_foldclosed},
7904 {"foldclosedend", 1, 1, f_foldclosedend},
7905 {"foldlevel", 1, 1, f_foldlevel},
7906 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007907 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007909 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007910 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007911 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007912 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"getbufvar", 2, 2, f_getbufvar},
7914 {"getchar", 0, 1, f_getchar},
7915 {"getcharmod", 0, 0, f_getcharmod},
7916 {"getcmdline", 0, 0, f_getcmdline},
7917 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007918 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007920 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007921 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"getfsize", 1, 1, f_getfsize},
7923 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007924 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007925 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007926 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007927 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007928 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007929 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007930 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007931 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007933 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007934 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getwinposx", 0, 0, f_getwinposx},
7936 {"getwinposy", 0, 0, f_getwinposy},
7937 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007938 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007939 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007942 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007943 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7945 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7946 {"histadd", 2, 2, f_histadd},
7947 {"histdel", 1, 2, f_histdel},
7948 {"histget", 1, 2, f_histget},
7949 {"histnr", 1, 1, f_histnr},
7950 {"hlID", 1, 1, f_hlID},
7951 {"hlexists", 1, 1, f_hlexists},
7952 {"hostname", 0, 0, f_hostname},
7953 {"iconv", 3, 3, f_iconv},
7954 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007955 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007956 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007958 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"inputrestore", 0, 0, f_inputrestore},
7960 {"inputsave", 0, 0, f_inputsave},
7961 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007963 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007965 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"libcall", 3, 3, f_libcall},
7972 {"libcallnr", 3, 3, f_libcallnr},
7973 {"line", 1, 1, f_line},
7974 {"line2byte", 1, 1, f_line2byte},
7975 {"lispindent", 1, 1, f_lispindent},
7976 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007977#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007978 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007979 {"log10", 1, 1, f_log10},
7980#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007981#ifdef FEAT_LUA
7982 {"luaeval", 1, 2, f_luaeval},
7983#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007984 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007985 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007986 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007987 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007988 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007989 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007990 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007991 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007992 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007994 {"max", 1, 1, f_max},
7995 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007996#ifdef vim_mkdir
7997 {"mkdir", 1, 3, f_mkdir},
7998#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007999 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008000#ifdef FEAT_MZSCHEME
8001 {"mzeval", 1, 1, f_mzeval},
8002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"nextnonblank", 1, 1, f_nextnonblank},
8004 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008005 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008006 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008007#ifdef FEAT_FLOAT
8008 {"pow", 2, 2, f_pow},
8009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008011 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008012 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008013#ifdef FEAT_PYTHON3
8014 {"py3eval", 1, 1, f_py3eval},
8015#endif
8016#ifdef FEAT_PYTHON
8017 {"pyeval", 1, 1, f_pyeval},
8018#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008020 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008021 {"reltime", 0, 2, f_reltime},
8022 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"remote_expr", 2, 3, f_remote_expr},
8024 {"remote_foreground", 1, 1, f_remote_foreground},
8025 {"remote_peek", 1, 2, f_remote_peek},
8026 {"remote_read", 1, 1, f_remote_read},
8027 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008028 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008030 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008032 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008033#ifdef FEAT_FLOAT
8034 {"round", 1, 1, f_round},
8035#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00008036 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008037 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008038 {"searchpair", 3, 7, f_searchpair},
8039 {"searchpairpos", 3, 7, f_searchpairpos},
8040 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"server2client", 2, 2, f_server2client},
8042 {"serverlist", 0, 0, f_serverlist},
8043 {"setbufvar", 3, 3, f_setbufvar},
8044 {"setcmdpos", 1, 1, f_setcmdpos},
8045 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008046 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008047 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008048 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008049 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008051 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008052 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008054 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008055 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#ifdef FEAT_FLOAT
8058 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008059 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008061 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008062 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008063 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008064 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008065 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#ifdef FEAT_FLOAT
8067 {"sqrt", 1, 1, f_sqrt},
8068 {"str2float", 1, 1, f_str2float},
8069#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008070 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008071 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008072 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073#ifdef HAVE_STRFTIME
8074 {"strftime", 1, 2, f_strftime},
8075#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008077 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"strlen", 1, 1, f_strlen},
8079 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008080 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008082 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"submatch", 1, 1, f_submatch},
8084 {"substitute", 4, 4, f_substitute},
8085 {"synID", 3, 3, f_synID},
8086 {"synIDattr", 2, 3, f_synIDattr},
8087 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008088 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008089 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008090 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008091 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008092 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008093 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008094 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008095 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008096#ifdef FEAT_FLOAT
8097 {"tan", 1, 1, f_tan},
8098 {"tanh", 1, 1, f_tanh},
8099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008101 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"tolower", 1, 1, f_tolower},
8103 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008104 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"trunc", 1, 1, f_trunc},
8107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008109 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008110 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008111 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"virtcol", 1, 1, f_virtcol},
8113 {"visualmode", 0, 1, f_visualmode},
8114 {"winbufnr", 1, 1, f_winbufnr},
8115 {"wincol", 0, 0, f_wincol},
8116 {"winheight", 1, 1, f_winheight},
8117 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008118 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008120 {"winrestview", 1, 1, f_winrestview},
8121 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008123 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008124 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125};
8126
8127#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8128
8129/*
8130 * Function given to ExpandGeneric() to obtain the list of internal
8131 * or user defined function names.
8132 */
8133 char_u *
8134get_function_name(xp, idx)
8135 expand_T *xp;
8136 int idx;
8137{
8138 static int intidx = -1;
8139 char_u *name;
8140
8141 if (idx == 0)
8142 intidx = -1;
8143 if (intidx < 0)
8144 {
8145 name = get_user_func_name(xp, idx);
8146 if (name != NULL)
8147 return name;
8148 }
8149 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8150 {
8151 STRCPY(IObuff, functions[intidx].f_name);
8152 STRCAT(IObuff, "(");
8153 if (functions[intidx].f_max_argc == 0)
8154 STRCAT(IObuff, ")");
8155 return IObuff;
8156 }
8157
8158 return NULL;
8159}
8160
8161/*
8162 * Function given to ExpandGeneric() to obtain the list of internal or
8163 * user defined variable or function names.
8164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 char_u *
8166get_expr_name(xp, idx)
8167 expand_T *xp;
8168 int idx;
8169{
8170 static int intidx = -1;
8171 char_u *name;
8172
8173 if (idx == 0)
8174 intidx = -1;
8175 if (intidx < 0)
8176 {
8177 name = get_function_name(xp, idx);
8178 if (name != NULL)
8179 return name;
8180 }
8181 return get_user_var_name(xp, ++intidx);
8182}
8183
8184#endif /* FEAT_CMDL_COMPL */
8185
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008186#if defined(EBCDIC) || defined(PROTO)
8187/*
8188 * Compare struct fst by function name.
8189 */
8190 static int
8191compare_func_name(s1, s2)
8192 const void *s1;
8193 const void *s2;
8194{
8195 struct fst *p1 = (struct fst *)s1;
8196 struct fst *p2 = (struct fst *)s2;
8197
8198 return STRCMP(p1->f_name, p2->f_name);
8199}
8200
8201/*
8202 * Sort the function table by function name.
8203 * The sorting of the table above is ASCII dependant.
8204 * On machines using EBCDIC we have to sort it.
8205 */
8206 static void
8207sortFunctions()
8208{
8209 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8210
8211 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8212}
8213#endif
8214
8215
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216/*
8217 * Find internal function in table above.
8218 * Return index, or -1 if not found
8219 */
8220 static int
8221find_internal_func(name)
8222 char_u *name; /* name of the function */
8223{
8224 int first = 0;
8225 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8226 int cmp;
8227 int x;
8228
8229 /*
8230 * Find the function name in the table. Binary search.
8231 */
8232 while (first <= last)
8233 {
8234 x = first + ((unsigned)(last - first) >> 1);
8235 cmp = STRCMP(name, functions[x].f_name);
8236 if (cmp < 0)
8237 last = x - 1;
8238 else if (cmp > 0)
8239 first = x + 1;
8240 else
8241 return x;
8242 }
8243 return -1;
8244}
8245
8246/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008247 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8248 * name it contains, otherwise return "name".
8249 */
8250 static char_u *
8251deref_func_name(name, lenp)
8252 char_u *name;
8253 int *lenp;
8254{
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008256 int cc;
8257
8258 cc = name[*lenp];
8259 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008260 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008261 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008262 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008263 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008264 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 {
8266 *lenp = 0;
8267 return (char_u *)""; /* just in case */
8268 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008269 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 }
8272
8273 return name;
8274}
8275
8276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 * Allocate a variable for the result of a function.
8278 * Return OK or FAIL.
8279 */
8280 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8282 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 char_u *name; /* name of the function */
8284 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 char_u **arg; /* argument, pointing to the '(' */
8287 linenr_T firstline; /* first line of range */
8288 linenr_T lastline; /* last line of range */
8289 int *doesrange; /* return: function handled range */
8290 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 char_u *argp;
8294 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008295 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 int argcount = 0; /* number of arguments found */
8297
8298 /*
8299 * Get the arguments.
8300 */
8301 argp = *arg;
8302 while (argcount < MAX_FUNC_ARGS)
8303 {
8304 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8305 if (*argp == ')' || *argp == ',' || *argp == NUL)
8306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8308 {
8309 ret = FAIL;
8310 break;
8311 }
8312 ++argcount;
8313 if (*argp != ',')
8314 break;
8315 }
8316 if (*argp == ')')
8317 ++argp;
8318 else
8319 ret = FAIL;
8320
8321 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 {
8326 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008327 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008329 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331
8332 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334
8335 *arg = skipwhite(argp);
8336 return ret;
8337}
8338
8339
8340/*
8341 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008342 * Return OK when the function can't be called, FAIL otherwise.
8343 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 */
8345 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008346call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008348 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008352 typval_T *argvars; /* vars for arguments, must have "argcount"
8353 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 linenr_T firstline; /* first line of range */
8355 linenr_T lastline; /* last line of range */
8356 int *doesrange; /* return: function handled range */
8357 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361#define ERROR_UNKNOWN 0
8362#define ERROR_TOOMANY 1
8363#define ERROR_TOOFEW 2
8364#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008365#define ERROR_DICT 4
8366#define ERROR_NONE 5
8367#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 int error = ERROR_NONE;
8369 int i;
8370 int llen;
8371 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372#define FLEN_FIXED 40
8373 char_u fname_buf[FLEN_FIXED + 1];
8374 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008375 char_u *name;
8376
8377 /* Make a copy of the name, if it comes from a funcref variable it could
8378 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008379 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008380 if (name == NULL)
8381 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382
8383 /*
8384 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8385 * Change <SNR>123_name() to K_SNR 123_name().
8386 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 llen = eval_fname_script(name);
8389 if (llen > 0)
8390 {
8391 fname_buf[0] = K_SPECIAL;
8392 fname_buf[1] = KS_EXTRA;
8393 fname_buf[2] = (int)KE_SNR;
8394 i = 3;
8395 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8396 {
8397 if (current_SID <= 0)
8398 error = ERROR_SCRIPT;
8399 else
8400 {
8401 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8402 i = (int)STRLEN(fname_buf);
8403 }
8404 }
8405 if (i + STRLEN(name + llen) < FLEN_FIXED)
8406 {
8407 STRCPY(fname_buf + i, name + llen);
8408 fname = fname_buf;
8409 }
8410 else
8411 {
8412 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8413 if (fname == NULL)
8414 error = ERROR_OTHER;
8415 else
8416 {
8417 mch_memmove(fname, fname_buf, (size_t)i);
8418 STRCPY(fname + i, name + llen);
8419 }
8420 }
8421 }
8422 else
8423 fname = name;
8424
8425 *doesrange = FALSE;
8426
8427
8428 /* execute the function if no errors detected and executing */
8429 if (evaluate && error == ERROR_NONE)
8430 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008431 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8432 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 error = ERROR_UNKNOWN;
8434
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008435 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
8437 /*
8438 * User defined function.
8439 */
8440 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008443 /* Trigger FuncUndefined event, may load the function. */
8444 if (fp == NULL
8445 && apply_autocmds(EVENT_FUNCUNDEFINED,
8446 fname, fname, TRUE, NULL)
8447 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008449 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 fp = find_func(fname);
8451 }
8452#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008454 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008455 {
8456 /* loaded a package, search for the function again */
8457 fp = find_func(fname);
8458 }
8459
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (fp != NULL)
8461 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008462 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008464 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008466 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008468 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 else
8471 {
8472 /*
8473 * Call the user function.
8474 * Save and restore search patterns, script variables and
8475 * redo buffer.
8476 */
8477 save_search_patterns();
8478 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008479 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008482 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8483 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8484 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008485 /* Function was unreferenced while being used, free it
8486 * now. */
8487 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 restoreRedobuff();
8489 restore_search_patterns();
8490 error = ERROR_NONE;
8491 }
8492 }
8493 }
8494 else
8495 {
8496 /*
8497 * Find the function name in the table, call its implementation.
8498 */
8499 i = find_internal_func(fname);
8500 if (i >= 0)
8501 {
8502 if (argcount < functions[i].f_min_argc)
8503 error = ERROR_TOOFEW;
8504 else if (argcount > functions[i].f_max_argc)
8505 error = ERROR_TOOMANY;
8506 else
8507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 error = ERROR_NONE;
8511 }
8512 }
8513 }
8514 /*
8515 * The function call (or "FuncUndefined" autocommand sequence) might
8516 * have been aborted by an error, an interrupt, or an explicitly thrown
8517 * exception that has not been caught so far. This situation can be
8518 * tested for by calling aborting(). For an error in an internal
8519 * function or for the "E132" error in call_user_func(), however, the
8520 * throw point at which the "force_abort" flag (temporarily reset by
8521 * emsg()) is normally updated has not been reached yet. We need to
8522 * update that flag first to make aborting() reliable.
8523 */
8524 update_force_abort();
8525 }
8526 if (error == ERROR_NONE)
8527 ret = OK;
8528
8529 /*
8530 * Report an error unless the argument evaluation or function call has been
8531 * cancelled due to an aborting error, an interrupt, or an exception.
8532 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008533 if (!aborting())
8534 {
8535 switch (error)
8536 {
8537 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008538 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008539 break;
8540 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008541 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008542 break;
8543 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008544 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008545 name);
8546 break;
8547 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 name);
8550 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008551 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008553 name);
8554 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 }
8556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 if (fname != name && fname != fname_buf)
8559 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008560 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
8562 return ret;
8563}
8564
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008565/*
8566 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008567 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008568 */
8569 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008570emsg_funcname(ermsg, name)
8571 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008572 char_u *name;
8573{
8574 char_u *p;
8575
8576 if (*name == K_SPECIAL)
8577 p = concat_str((char_u *)"<SNR>", name + 3);
8578 else
8579 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008580 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008581 if (p != name)
8582 vim_free(p);
8583}
8584
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008585/*
8586 * Return TRUE for a non-zero Number and a non-empty String.
8587 */
8588 static int
8589non_zero_arg(argvars)
8590 typval_T *argvars;
8591{
8592 return ((argvars[0].v_type == VAR_NUMBER
8593 && argvars[0].vval.v_number != 0)
8594 || (argvars[0].v_type == VAR_STRING
8595 && argvars[0].vval.v_string != NULL
8596 && *argvars[0].vval.v_string != NUL));
8597}
8598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599/*********************************************
8600 * Implementation of the built-in functions
8601 */
8602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008603#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008604static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8605
8606/*
8607 * Get the float value of "argvars[0]" into "f".
8608 * Returns FAIL when the argument is not a Number or Float.
8609 */
8610 static int
8611get_float_arg(argvars, f)
8612 typval_T *argvars;
8613 float_T *f;
8614{
8615 if (argvars[0].v_type == VAR_FLOAT)
8616 {
8617 *f = argvars[0].vval.v_float;
8618 return OK;
8619 }
8620 if (argvars[0].v_type == VAR_NUMBER)
8621 {
8622 *f = (float_T)argvars[0].vval.v_number;
8623 return OK;
8624 }
8625 EMSG(_("E808: Number or Float required"));
8626 return FAIL;
8627}
8628
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008629/*
8630 * "abs(expr)" function
8631 */
8632 static void
8633f_abs(argvars, rettv)
8634 typval_T *argvars;
8635 typval_T *rettv;
8636{
8637 if (argvars[0].v_type == VAR_FLOAT)
8638 {
8639 rettv->v_type = VAR_FLOAT;
8640 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8641 }
8642 else
8643 {
8644 varnumber_T n;
8645 int error = FALSE;
8646
8647 n = get_tv_number_chk(&argvars[0], &error);
8648 if (error)
8649 rettv->vval.v_number = -1;
8650 else if (n > 0)
8651 rettv->vval.v_number = n;
8652 else
8653 rettv->vval.v_number = -n;
8654 }
8655}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008656
8657/*
8658 * "acos()" function
8659 */
8660 static void
8661f_acos(argvars, rettv)
8662 typval_T *argvars;
8663 typval_T *rettv;
8664{
8665 float_T f;
8666
8667 rettv->v_type = VAR_FLOAT;
8668 if (get_float_arg(argvars, &f) == OK)
8669 rettv->vval.v_float = acos(f);
8670 else
8671 rettv->vval.v_float = 0.0;
8672}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008673#endif
8674
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008676 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 */
8678 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008679f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008680 typval_T *argvars;
8681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008686 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008688 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008689 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008690 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008692 }
8693 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008694 EMSG(_(e_listreq));
8695}
8696
8697/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008698 * "and(expr, expr)" function
8699 */
8700 static void
8701f_and(argvars, rettv)
8702 typval_T *argvars;
8703 typval_T *rettv;
8704{
8705 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8706 & get_tv_number_chk(&argvars[1], NULL);
8707}
8708
8709/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008710 * "append(lnum, string/list)" function
8711 */
8712 static void
8713f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *argvars;
8715 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008716{
8717 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008719 list_T *l = NULL;
8720 listitem_T *li = NULL;
8721 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722 long added = 0;
8723
Bram Moolenaar0d660222005-01-07 21:51:51 +00008724 lnum = get_tv_lnum(argvars);
8725 if (lnum >= 0
8726 && lnum <= curbuf->b_ml.ml_line_count
8727 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008728 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008730 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731 l = argvars[1].vval.v_list;
8732 if (l == NULL)
8733 return;
8734 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008735 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736 for (;;)
8737 {
8738 if (l == NULL)
8739 tv = &argvars[1]; /* append a string */
8740 else if (li == NULL)
8741 break; /* end of list */
8742 else
8743 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 line = get_tv_string_chk(tv);
8745 if (line == NULL) /* type error */
8746 {
8747 rettv->vval.v_number = 1; /* Failed */
8748 break;
8749 }
8750 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008751 ++added;
8752 if (l == NULL)
8753 break;
8754 li = li->li_next;
8755 }
8756
8757 appended_lines_mark(lnum, added);
8758 if (curwin->w_cursor.lnum > lnum)
8759 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008761 else
8762 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
8766 * "argc()" function
8767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774}
8775
8776/*
8777 * "argidx()" function
8778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787/*
8788 * "argv(nr)" function
8789 */
8790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *argvars;
8793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 int idx;
8796
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008797 if (argvars[0].v_type != VAR_UNKNOWN)
8798 {
8799 idx = get_tv_number_chk(&argvars[0], NULL);
8800 if (idx >= 0 && idx < ARGCOUNT)
8801 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8802 else
8803 rettv->vval.v_string = NULL;
8804 rettv->v_type = VAR_STRING;
8805 }
8806 else if (rettv_list_alloc(rettv) == OK)
8807 for (idx = 0; idx < ARGCOUNT; ++idx)
8808 list_append_string(rettv->vval.v_list,
8809 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810}
8811
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008812#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008813/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008814 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008815 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008816 static void
8817f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008818 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008819 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008820{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008821 float_T f;
8822
8823 rettv->v_type = VAR_FLOAT;
8824 if (get_float_arg(argvars, &f) == OK)
8825 rettv->vval.v_float = asin(f);
8826 else
8827 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828}
8829
8830/*
8831 * "atan()" function
8832 */
8833 static void
8834f_atan(argvars, rettv)
8835 typval_T *argvars;
8836 typval_T *rettv;
8837{
8838 float_T f;
8839
8840 rettv->v_type = VAR_FLOAT;
8841 if (get_float_arg(argvars, &f) == OK)
8842 rettv->vval.v_float = atan(f);
8843 else
8844 rettv->vval.v_float = 0.0;
8845}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008846
8847/*
8848 * "atan2()" function
8849 */
8850 static void
8851f_atan2(argvars, rettv)
8852 typval_T *argvars;
8853 typval_T *rettv;
8854{
8855 float_T fx, fy;
8856
8857 rettv->v_type = VAR_FLOAT;
8858 if (get_float_arg(argvars, &fx) == OK
8859 && get_float_arg(&argvars[1], &fy) == OK)
8860 rettv->vval.v_float = atan2(fx, fy);
8861 else
8862 rettv->vval.v_float = 0.0;
8863}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864#endif
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866/*
8867 * "browse(save, title, initdir, default)" function
8868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008871 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874#ifdef FEAT_BROWSE
8875 int save;
8876 char_u *title;
8877 char_u *initdir;
8878 char_u *defname;
8879 char_u buf[NUMBUFLEN];
8880 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008883 save = get_tv_number_chk(&argvars[0], &error);
8884 title = get_tv_string_chk(&argvars[1]);
8885 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8886 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008888 if (error || title == NULL || initdir == NULL || defname == NULL)
8889 rettv->vval.v_string = NULL;
8890 else
8891 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008892 do_browse(save ? BROWSE_SAVE : 0,
8893 title, defname, NULL, initdir, NULL, curbuf);
8894#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008898}
8899
8900/*
8901 * "browsedir(title, initdir)" function
8902 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008905 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008906 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008907{
8908#ifdef FEAT_BROWSE
8909 char_u *title;
8910 char_u *initdir;
8911 char_u buf[NUMBUFLEN];
8912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008913 title = get_tv_string_chk(&argvars[0]);
8914 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 if (title == NULL || initdir == NULL)
8917 rettv->vval.v_string = NULL;
8918 else
8919 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008920 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925}
8926
Bram Moolenaar33570922005-01-25 22:26:29 +00008927static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008928
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929/*
8930 * Find a buffer by number or exact name.
8931 */
8932 static buf_T *
8933find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008938 if (avar->v_type == VAR_NUMBER)
8939 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008940 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008942 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008943 if (buf == NULL)
8944 {
8945 /* No full path name match, try a match with a URL or a "nofile"
8946 * buffer, these don't use the full path. */
8947 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8948 if (buf->b_fname != NULL
8949 && (path_with_url(buf->b_fname)
8950#ifdef FEAT_QUICKFIX
8951 || bt_nofile(buf)
8952#endif
8953 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008955 break;
8956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 return buf;
8959}
8960
8961/*
8962 * "bufexists(expr)" function
8963 */
8964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *argvars;
8967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970}
8971
8972/*
8973 * "buflisted(expr)" function
8974 */
8975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 typval_T *argvars;
8978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 buf_T *buf;
8981
8982 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
8987 * "bufloaded(expr)" function
8988 */
8989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 buf_T *buf;
8995
8996 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
Bram Moolenaar33570922005-01-25 22:26:29 +00009000static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002/*
9003 * Get buffer by number or pattern.
9004 */
9005 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 int save_magic;
9011 char_u *save_cpo;
9012 buf_T *buf;
9013
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 if (tv->v_type == VAR_NUMBER)
9015 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009016 if (tv->v_type != VAR_STRING)
9017 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 if (name == NULL || *name == NUL)
9019 return curbuf;
9020 if (name[0] == '$' && name[1] == NUL)
9021 return lastbuf;
9022
9023 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9024 save_magic = p_magic;
9025 p_magic = TRUE;
9026 save_cpo = p_cpo;
9027 p_cpo = (char_u *)"";
9028
9029 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9030 TRUE, FALSE));
9031
9032 p_magic = save_magic;
9033 p_cpo = save_cpo;
9034
9035 /* If not found, try expanding the name, like done for bufexists(). */
9036 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038
9039 return buf;
9040}
9041
9042/*
9043 * "bufname(expr)" function
9044 */
9045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 typval_T *argvars;
9048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 buf_T *buf;
9051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 buf = get_buf_tv(&argvars[0]);
9055 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 --emsg_off;
9061}
9062
9063/*
9064 * "bufnr(expr)" function
9065 */
9066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 typval_T *argvars;
9069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009072 int error = FALSE;
9073 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009078 --emsg_off;
9079
9080 /* If the buffer isn't found and the second argument is not zero create a
9081 * new buffer. */
9082 if (buf == NULL
9083 && argvars[1].v_type != VAR_UNKNOWN
9084 && get_tv_number_chk(&argvars[1], &error) != 0
9085 && !error
9086 && (name = get_tv_string_chk(&argvars[0])) != NULL
9087 && !error)
9088 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9089
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094}
9095
9096/*
9097 * "bufwinnr(nr)" function
9098 */
9099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009101 typval_T *argvars;
9102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103{
9104#ifdef FEAT_WINDOWS
9105 win_T *wp;
9106 int winnr = 0;
9107#endif
9108 buf_T *buf;
9109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113#ifdef FEAT_WINDOWS
9114 for (wp = firstwin; wp; wp = wp->w_next)
9115 {
9116 ++winnr;
9117 if (wp->w_buffer == buf)
9118 break;
9119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#endif
9124 --emsg_off;
9125}
9126
9127/*
9128 * "byte2line(byte)" function
9129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009132 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137#else
9138 long boff = 0;
9139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 (linenr_T)0, &boff);
9146#endif
9147}
9148
9149/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009150 * "byteidx()" function
9151 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009156{
9157#ifdef FEAT_MBYTE
9158 char_u *t;
9159#endif
9160 char_u *str;
9161 long idx;
9162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 str = get_tv_string_chk(&argvars[0]);
9164 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009166 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167 return;
9168
9169#ifdef FEAT_MBYTE
9170 t = str;
9171 for ( ; idx > 0; idx--)
9172 {
9173 if (*t == NUL) /* EOL reached */
9174 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009175 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009176 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009177 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009178#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009179 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009181#endif
9182}
9183
Bram Moolenaardb913952012-06-29 12:54:53 +02009184 int
9185func_call(name, args, selfdict, rettv)
9186 char_u *name;
9187 typval_T *args;
9188 dict_T *selfdict;
9189 typval_T *rettv;
9190{
9191 listitem_T *item;
9192 typval_T argv[MAX_FUNC_ARGS + 1];
9193 int argc = 0;
9194 int dummy;
9195 int r = 0;
9196
9197 for (item = args->vval.v_list->lv_first; item != NULL;
9198 item = item->li_next)
9199 {
9200 if (argc == MAX_FUNC_ARGS)
9201 {
9202 EMSG(_("E699: Too many arguments"));
9203 break;
9204 }
9205 /* Make a copy of each argument. This is needed to be able to set
9206 * v_lock to VAR_FIXED in the copy without changing the original list.
9207 */
9208 copy_tv(&item->li_tv, &argv[argc++]);
9209 }
9210
9211 if (item == NULL)
9212 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9213 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9214 &dummy, TRUE, selfdict);
9215
9216 /* Free the arguments. */
9217 while (argc > 0)
9218 clear_tv(&argv[--argc]);
9219
9220 return r;
9221}
9222
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009223/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009224 * "call(func, arglist)" function
9225 */
9226 static void
9227f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009230{
9231 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009233
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009234 if (argvars[1].v_type != VAR_LIST)
9235 {
9236 EMSG(_(e_listreq));
9237 return;
9238 }
9239 if (argvars[1].vval.v_list == NULL)
9240 return;
9241
9242 if (argvars[0].v_type == VAR_FUNC)
9243 func = argvars[0].vval.v_string;
9244 else
9245 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 if (*func == NUL)
9247 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009248
Bram Moolenaare9a41262005-01-15 22:18:47 +00009249 if (argvars[2].v_type != VAR_UNKNOWN)
9250 {
9251 if (argvars[2].v_type != VAR_DICT)
9252 {
9253 EMSG(_(e_dictreq));
9254 return;
9255 }
9256 selfdict = argvars[2].vval.v_dict;
9257 }
9258
Bram Moolenaardb913952012-06-29 12:54:53 +02009259 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009260}
9261
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009262#ifdef FEAT_FLOAT
9263/*
9264 * "ceil({float})" function
9265 */
9266 static void
9267f_ceil(argvars, rettv)
9268 typval_T *argvars;
9269 typval_T *rettv;
9270{
9271 float_T f;
9272
9273 rettv->v_type = VAR_FLOAT;
9274 if (get_float_arg(argvars, &f) == OK)
9275 rettv->vval.v_float = ceil(f);
9276 else
9277 rettv->vval.v_float = 0.0;
9278}
9279#endif
9280
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009281/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009282 * "changenr()" function
9283 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009284 static void
9285f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009286 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009287 typval_T *rettv;
9288{
9289 rettv->vval.v_number = curbuf->b_u_seq_cur;
9290}
9291
9292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 * "char2nr(string)" function
9294 */
9295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009297 typval_T *argvars;
9298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300#ifdef FEAT_MBYTE
9301 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 else
9304#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
9308/*
9309 * "cindent(lnum)" function
9310 */
9311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 typval_T *argvars;
9314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316#ifdef FEAT_CINDENT
9317 pos_T pos;
9318 linenr_T lnum;
9319
9320 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9323 {
9324 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 curwin->w_cursor = pos;
9327 }
9328 else
9329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
9332
9333/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009334 * "clearmatches()" function
9335 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009336 static void
9337f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009338 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009339 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009340{
9341#ifdef FEAT_SEARCH_EXTRA
9342 clear_matches(curwin);
9343#endif
9344}
9345
9346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 * "col(string)" function
9348 */
9349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009351 typval_T *argvars;
9352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 colnr_T col = 0;
9355 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009356 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009358 fp = var2fpos(&argvars[0], FALSE, &fnum);
9359 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 {
9361 if (fp->col == MAXCOL)
9362 {
9363 /* '> can be MAXCOL, get the length of the line then */
9364 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009365 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 else
9367 col = MAXCOL;
9368 }
9369 else
9370 {
9371 col = fp->col + 1;
9372#ifdef FEAT_VIRTUALEDIT
9373 /* col(".") when the cursor is on the NUL at the end of the line
9374 * because of "coladd" can be seen as an extra column. */
9375 if (virtual_active() && fp == &curwin->w_cursor)
9376 {
9377 char_u *p = ml_get_cursor();
9378
9379 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9380 curwin->w_virtcol - curwin->w_cursor.coladd))
9381 {
9382# ifdef FEAT_MBYTE
9383 int l;
9384
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009385 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 col += l;
9387# else
9388 if (*p != NUL && p[1] == NUL)
9389 ++col;
9390# endif
9391 }
9392 }
9393#endif
9394 }
9395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397}
9398
Bram Moolenaar572cb562005-08-05 21:35:02 +00009399#if defined(FEAT_INS_EXPAND)
9400/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009401 * "complete()" function
9402 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009403 static void
9404f_complete(argvars, rettv)
9405 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009406 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009407{
9408 int startcol;
9409
9410 if ((State & INSERT) == 0)
9411 {
9412 EMSG(_("E785: complete() can only be used in Insert mode"));
9413 return;
9414 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009415
9416 /* Check for undo allowed here, because if something was already inserted
9417 * the line was already saved for undo and this check isn't done. */
9418 if (!undo_allowed())
9419 return;
9420
Bram Moolenaarade00832006-03-10 21:46:58 +00009421 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9422 {
9423 EMSG(_(e_invarg));
9424 return;
9425 }
9426
9427 startcol = get_tv_number_chk(&argvars[0], NULL);
9428 if (startcol <= 0)
9429 return;
9430
9431 set_completion(startcol - 1, argvars[1].vval.v_list);
9432}
9433
9434/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009435 * "complete_add()" function
9436 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009437 static void
9438f_complete_add(argvars, rettv)
9439 typval_T *argvars;
9440 typval_T *rettv;
9441{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009442 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009443}
9444
9445/*
9446 * "complete_check()" function
9447 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009448 static void
9449f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009450 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009451 typval_T *rettv;
9452{
9453 int saved = RedrawingDisabled;
9454
9455 RedrawingDisabled = 0;
9456 ins_compl_check_keys(0);
9457 rettv->vval.v_number = compl_interrupted;
9458 RedrawingDisabled = saved;
9459}
9460#endif
9461
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462/*
9463 * "confirm(message, buttons[, default [, type]])" function
9464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009467 typval_T *argvars UNUSED;
9468 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9471 char_u *message;
9472 char_u *buttons = NULL;
9473 char_u buf[NUMBUFLEN];
9474 char_u buf2[NUMBUFLEN];
9475 int def = 1;
9476 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 char_u *typestr;
9478 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 message = get_tv_string_chk(&argvars[0]);
9481 if (message == NULL)
9482 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009483 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009485 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9486 if (buttons == NULL)
9487 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009490 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009491 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9494 if (typestr == NULL)
9495 error = TRUE;
9496 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 switch (TOUPPER_ASC(*typestr))
9499 {
9500 case 'E': type = VIM_ERROR; break;
9501 case 'Q': type = VIM_QUESTION; break;
9502 case 'I': type = VIM_INFO; break;
9503 case 'W': type = VIM_WARNING; break;
9504 case 'G': type = VIM_GENERIC; break;
9505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 }
9507 }
9508 }
9509 }
9510
9511 if (buttons == NULL || *buttons == NUL)
9512 buttons = (char_u *)_("&Ok");
9513
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009514 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009516 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517#endif
9518}
9519
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520/*
9521 * "copy()" function
9522 */
9523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009525 typval_T *argvars;
9526 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009528 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009531#ifdef FEAT_FLOAT
9532/*
9533 * "cos()" function
9534 */
9535 static void
9536f_cos(argvars, rettv)
9537 typval_T *argvars;
9538 typval_T *rettv;
9539{
9540 float_T f;
9541
9542 rettv->v_type = VAR_FLOAT;
9543 if (get_float_arg(argvars, &f) == OK)
9544 rettv->vval.v_float = cos(f);
9545 else
9546 rettv->vval.v_float = 0.0;
9547}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009548
9549/*
9550 * "cosh()" function
9551 */
9552 static void
9553f_cosh(argvars, rettv)
9554 typval_T *argvars;
9555 typval_T *rettv;
9556{
9557 float_T f;
9558
9559 rettv->v_type = VAR_FLOAT;
9560 if (get_float_arg(argvars, &f) == OK)
9561 rettv->vval.v_float = cosh(f);
9562 else
9563 rettv->vval.v_float = 0.0;
9564}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009565#endif
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009568 * "count()" function
9569 */
9570 static void
9571f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009572 typval_T *argvars;
9573 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009574{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009575 long n = 0;
9576 int ic = FALSE;
9577
Bram Moolenaare9a41262005-01-15 22:18:47 +00009578 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009579 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 listitem_T *li;
9581 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009583
Bram Moolenaare9a41262005-01-15 22:18:47 +00009584 if ((l = argvars[0].vval.v_list) != NULL)
9585 {
9586 li = l->lv_first;
9587 if (argvars[2].v_type != VAR_UNKNOWN)
9588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 int error = FALSE;
9590
9591 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 if (argvars[3].v_type != VAR_UNKNOWN)
9593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 idx = get_tv_number_chk(&argvars[3], &error);
9595 if (!error)
9596 {
9597 li = list_find(l, idx);
9598 if (li == NULL)
9599 EMSGN(_(e_listidx), idx);
9600 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009601 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 if (error)
9603 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 }
9605
9606 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009607 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 ++n;
9609 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 else if (argvars[0].v_type == VAR_DICT)
9612 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 int todo;
9614 dict_T *d;
9615 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009617 if ((d = argvars[0].vval.v_dict) != NULL)
9618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619 int error = FALSE;
9620
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 if (argvars[2].v_type != VAR_UNKNOWN)
9622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[3].v_type != VAR_UNKNOWN)
9625 EMSG(_(e_invarg));
9626 }
9627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009628 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009630 {
9631 if (!HASHITEM_EMPTY(hi))
9632 {
9633 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009634 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009635 ++n;
9636 }
9637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 }
9639 }
9640 else
9641 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 rettv->vval.v_number = n;
9643}
9644
9645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9647 *
9648 * Checks the existence of a cscope connection.
9649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009652 typval_T *argvars UNUSED;
9653 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655#ifdef FEAT_CSCOPE
9656 int num = 0;
9657 char_u *dbpath = NULL;
9658 char_u *prepend = NULL;
9659 char_u buf[NUMBUFLEN];
9660
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009661 if (argvars[0].v_type != VAR_UNKNOWN
9662 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 num = (int)get_tv_number(&argvars[0]);
9665 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 }
9669
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671#endif
9672}
9673
9674/*
9675 * "cursor(lnum, col)" function
9676 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009677 * Moves the cursor to the specified line and column.
9678 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009686#ifdef FEAT_VIRTUALEDIT
9687 long coladd = 0;
9688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009690 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009691 if (argvars[1].v_type == VAR_UNKNOWN)
9692 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009693 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009694
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009695 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009696 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009697 line = pos.lnum;
9698 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009699#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009700 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009701#endif
9702 }
9703 else
9704 {
9705 line = get_tv_lnum(argvars);
9706 col = get_tv_number_chk(&argvars[1], NULL);
9707#ifdef FEAT_VIRTUALEDIT
9708 if (argvars[2].v_type != VAR_UNKNOWN)
9709 coladd = get_tv_number_chk(&argvars[2], NULL);
9710#endif
9711 }
9712 if (line < 0 || col < 0
9713#ifdef FEAT_VIRTUALEDIT
9714 || coladd < 0
9715#endif
9716 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009717 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 if (line > 0)
9719 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 if (col > 0)
9721 curwin->w_cursor.col = col - 1;
9722#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009723 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724#endif
9725
9726 /* Make sure the cursor is in a valid position. */
9727 check_cursor();
9728#ifdef FEAT_MBYTE
9729 /* Correct cursor for multi-byte character. */
9730 if (has_mbyte)
9731 mb_adjust_cursor();
9732#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009733
9734 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009735 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736}
9737
9738/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009739 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 */
9741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009743 typval_T *argvars;
9744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009746 int noref = 0;
9747
9748 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009750 if (noref < 0 || noref > 1)
9751 EMSG(_(e_invarg));
9752 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009753 {
9754 current_copyID += COPYID_INC;
9755 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
9760 * "delete()" function
9761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773/*
9774 * "did_filetype()" function
9775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009778 typval_T *argvars UNUSED;
9779 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#endif
9784}
9785
9786/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009787 * "diff_filler()" function
9788 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009791 typval_T *argvars UNUSED;
9792 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009793{
9794#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009796#endif
9797}
9798
9799/*
9800 * "diff_hlID()" function
9801 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009804 typval_T *argvars UNUSED;
9805 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009806{
9807#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009809 static linenr_T prev_lnum = 0;
9810 static int changedtick = 0;
9811 static int fnum = 0;
9812 static int change_start = 0;
9813 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009814 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009815 int filler_lines;
9816 int col;
9817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 if (lnum < 0) /* ignore type error in {lnum} arg */
9819 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009820 if (lnum != prev_lnum
9821 || changedtick != curbuf->b_changedtick
9822 || fnum != curbuf->b_fnum)
9823 {
9824 /* New line, buffer, change: need to get the values. */
9825 filler_lines = diff_check(curwin, lnum);
9826 if (filler_lines < 0)
9827 {
9828 if (filler_lines == -1)
9829 {
9830 change_start = MAXCOL;
9831 change_end = -1;
9832 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9833 hlID = HLF_ADD; /* added line */
9834 else
9835 hlID = HLF_CHD; /* changed line */
9836 }
9837 else
9838 hlID = HLF_ADD; /* added line */
9839 }
9840 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009841 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 prev_lnum = lnum;
9843 changedtick = curbuf->b_changedtick;
9844 fnum = curbuf->b_fnum;
9845 }
9846
9847 if (hlID == HLF_CHD || hlID == HLF_TXD)
9848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009850 if (col >= change_start && col <= change_end)
9851 hlID = HLF_TXD; /* changed text */
9852 else
9853 hlID = HLF_CHD; /* changed line */
9854 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009855 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856#endif
9857}
9858
9859/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009860 * "empty({expr})" function
9861 */
9862 static void
9863f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009866{
9867 int n;
9868
9869 switch (argvars[0].v_type)
9870 {
9871 case VAR_STRING:
9872 case VAR_FUNC:
9873 n = argvars[0].vval.v_string == NULL
9874 || *argvars[0].vval.v_string == NUL;
9875 break;
9876 case VAR_NUMBER:
9877 n = argvars[0].vval.v_number == 0;
9878 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009879#ifdef FEAT_FLOAT
9880 case VAR_FLOAT:
9881 n = argvars[0].vval.v_float == 0.0;
9882 break;
9883#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009884 case VAR_LIST:
9885 n = argvars[0].vval.v_list == NULL
9886 || argvars[0].vval.v_list->lv_first == NULL;
9887 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009888 case VAR_DICT:
9889 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009891 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009892 default:
9893 EMSG2(_(e_intern2), "f_empty()");
9894 n = 0;
9895 }
9896
9897 rettv->vval.v_number = n;
9898}
9899
9900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 * "escape({string}, {chars})" function
9902 */
9903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 typval_T *argvars;
9906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907{
9908 char_u buf[NUMBUFLEN];
9909
Bram Moolenaar758711c2005-02-02 23:11:38 +00009910 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9911 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913}
9914
9915/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009916 * "eval()" function
9917 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009918 static void
9919f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T *argvars;
9921 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009922{
9923 char_u *s;
9924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 s = get_tv_string_chk(&argvars[0]);
9926 if (s != NULL)
9927 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9930 {
9931 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009934 else if (*s != NUL)
9935 EMSG(_(e_trailing));
9936}
9937
9938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 * "eventhandler()" function
9940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009943 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947}
9948
9949/*
9950 * "executable()" function
9951 */
9952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 typval_T *argvars;
9955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958}
9959
9960/*
9961 * "exists()" function
9962 */
9963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *argvars;
9966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968 char_u *p;
9969 char_u *name;
9970 int n = FALSE;
9971 int len = 0;
9972
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009973 no_autoload = TRUE;
9974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 if (*p == '$') /* environment variable */
9977 {
9978 /* first try "normal" environment variables (fast) */
9979 if (mch_getenv(p + 1) != NULL)
9980 n = TRUE;
9981 else
9982 {
9983 /* try expanding things like $VIM and ${HOME} */
9984 p = expand_env_save(p);
9985 if (p != NULL && *p != '$')
9986 n = TRUE;
9987 vim_free(p);
9988 }
9989 }
9990 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009993 if (*skipwhite(p) != NUL)
9994 n = FALSE; /* trailing garbage */
9995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 else if (*p == '*') /* internal or user defined function */
9997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009998 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 }
10000 else if (*p == ':')
10001 {
10002 n = cmd_exists(p + 1);
10003 }
10004 else if (*p == '#')
10005 {
10006#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010007 if (p[1] == '#')
10008 n = autocmd_supported(p + 2);
10009 else
10010 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011#endif
10012 }
10013 else /* internal variable */
10014 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010015 char_u *tofree;
10016 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010018 /* get_name_len() takes care of expanding curly braces */
10019 name = p;
10020 len = get_name_len(&p, &tofree, TRUE, FALSE);
10021 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010023 if (tofree != NULL)
10024 name = tofree;
10025 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10026 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010028 /* handle d.key, l[idx], f(expr) */
10029 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10030 if (n)
10031 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 }
10033 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010034 if (*p != NUL)
10035 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010037 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 }
10039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010041
10042 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043}
10044
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010045#ifdef FEAT_FLOAT
10046/*
10047 * "exp()" function
10048 */
10049 static void
10050f_exp(argvars, rettv)
10051 typval_T *argvars;
10052 typval_T *rettv;
10053{
10054 float_T f;
10055
10056 rettv->v_type = VAR_FLOAT;
10057 if (get_float_arg(argvars, &f) == OK)
10058 rettv->vval.v_float = exp(f);
10059 else
10060 rettv->vval.v_float = 0.0;
10061}
10062#endif
10063
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064/*
10065 * "expand()" function
10066 */
10067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 typval_T *argvars;
10070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071{
10072 char_u *s;
10073 int len;
10074 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010075 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010078 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010081 if (argvars[1].v_type != VAR_UNKNOWN
10082 && argvars[2].v_type != VAR_UNKNOWN
10083 && get_tv_number_chk(&argvars[2], &error)
10084 && !error)
10085 {
10086 rettv->v_type = VAR_LIST;
10087 rettv->vval.v_list = NULL;
10088 }
10089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 if (*s == '%' || *s == '#' || *s == '<')
10092 {
10093 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010094 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010096 if (rettv->v_type == VAR_LIST)
10097 {
10098 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10099 list_append_string(rettv->vval.v_list, result, -1);
10100 else
10101 vim_free(result);
10102 }
10103 else
10104 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 }
10106 else
10107 {
10108 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010109 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 if (argvars[1].v_type != VAR_UNKNOWN
10111 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010112 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 if (!error)
10114 {
10115 ExpandInit(&xpc);
10116 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010117 if (p_wic)
10118 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010119 if (rettv->v_type == VAR_STRING)
10120 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10121 options, WILD_ALL);
10122 else if (rettv_list_alloc(rettv) != FAIL)
10123 {
10124 int i;
10125
10126 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10127 for (i = 0; i < xpc.xp_numfiles; i++)
10128 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10129 ExpandCleanup(&xpc);
10130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 }
10132 else
10133 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135}
10136
10137/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010138 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010140 */
10141 static void
10142f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010143 typval_T *argvars;
10144 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010145{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010146 char *arg_errmsg = N_("extend() argument");
10147
Bram Moolenaare9a41262005-01-15 22:18:47 +000010148 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010150 list_T *l1, *l2;
10151 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010152 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010154
Bram Moolenaare9a41262005-01-15 22:18:47 +000010155 l1 = argvars[0].vval.v_list;
10156 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010157 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010158 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 {
10160 if (argvars[2].v_type != VAR_UNKNOWN)
10161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 before = get_tv_number_chk(&argvars[2], &error);
10163 if (error)
10164 return; /* type error; errmsg already given */
10165
Bram Moolenaar758711c2005-02-02 23:11:38 +000010166 if (before == l1->lv_len)
10167 item = NULL;
10168 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010170 item = list_find(l1, before);
10171 if (item == NULL)
10172 {
10173 EMSGN(_(e_listidx), before);
10174 return;
10175 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010176 }
10177 }
10178 else
10179 item = NULL;
10180 list_extend(l1, l2, item);
10181
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 copy_tv(&argvars[0], rettv);
10183 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010184 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010185 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10186 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010187 dict_T *d1, *d2;
10188 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 char_u *action;
10190 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010191 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010192 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010193
10194 d1 = argvars[0].vval.v_dict;
10195 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010196 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010197 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 {
10199 /* Check the third argument. */
10200 if (argvars[2].v_type != VAR_UNKNOWN)
10201 {
10202 static char *(av[]) = {"keep", "force", "error"};
10203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 action = get_tv_string_chk(&argvars[2]);
10205 if (action == NULL)
10206 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010207 for (i = 0; i < 3; ++i)
10208 if (STRCMP(action, av[i]) == 0)
10209 break;
10210 if (i == 3)
10211 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010212 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010213 return;
10214 }
10215 }
10216 else
10217 action = (char_u *)"force";
10218
10219 /* Go over all entries in the second dict and add them to the
10220 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010221 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010223 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010224 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010226 --todo;
10227 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010228 if (d1->dv_scope != 0)
10229 {
10230 /* Disallow replacing a builtin function in l: and g:.
10231 * Check the key to be valid when adding to any
10232 * scope. */
10233 if (d1->dv_scope == VAR_DEF_SCOPE
10234 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10235 && var_check_func_name(hi2->hi_key,
10236 di1 == NULL))
10237 break;
10238 if (!valid_varname(hi2->hi_key))
10239 break;
10240 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010241 if (di1 == NULL)
10242 {
10243 di1 = dictitem_copy(HI2DI(hi2));
10244 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10245 dictitem_free(di1);
10246 }
10247 else if (*action == 'e')
10248 {
10249 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10250 break;
10251 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010252 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010253 {
10254 clear_tv(&di1->di_tv);
10255 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 }
10258 }
10259
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 copy_tv(&argvars[0], rettv);
10261 }
10262 }
10263 else
10264 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010265}
10266
10267/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010268 * "feedkeys()" function
10269 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010270 static void
10271f_feedkeys(argvars, rettv)
10272 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010273 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010274{
10275 int remap = TRUE;
10276 char_u *keys, *flags;
10277 char_u nbuf[NUMBUFLEN];
10278 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010279 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010280
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010281 /* This is not allowed in the sandbox. If the commands would still be
10282 * executed in the sandbox it would be OK, but it probably happens later,
10283 * when "sandbox" is no longer set. */
10284 if (check_secure())
10285 return;
10286
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010287 keys = get_tv_string(&argvars[0]);
10288 if (*keys != NUL)
10289 {
10290 if (argvars[1].v_type != VAR_UNKNOWN)
10291 {
10292 flags = get_tv_string_buf(&argvars[1], nbuf);
10293 for ( ; *flags != NUL; ++flags)
10294 {
10295 switch (*flags)
10296 {
10297 case 'n': remap = FALSE; break;
10298 case 'm': remap = TRUE; break;
10299 case 't': typed = TRUE; break;
10300 }
10301 }
10302 }
10303
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010304 /* Need to escape K_SPECIAL and CSI before putting the string in the
10305 * typeahead buffer. */
10306 keys_esc = vim_strsave_escape_csi(keys);
10307 if (keys_esc != NULL)
10308 {
10309 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010310 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010311 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010312 if (vgetc_busy)
10313 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010314 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010315 }
10316}
10317
10318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 * "filereadable()" function
10320 */
10321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 typval_T *argvars;
10324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010326 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 char_u *p;
10328 int n;
10329
Bram Moolenaarc236c162008-07-13 17:41:49 +000010330#ifndef O_NONBLOCK
10331# define O_NONBLOCK 0
10332#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10335 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 {
10337 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010338 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 }
10340 else
10341 n = FALSE;
10342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010343 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344}
10345
10346/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010347 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 * rights to write into.
10349 */
10350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010351f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010352 typval_T *argvars;
10353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010355 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356}
10357
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010358static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010359
10360 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010361findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010362 typval_T *argvars;
10363 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010364 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010365{
10366#ifdef FEAT_SEARCHPATH
10367 char_u *fname;
10368 char_u *fresult = NULL;
10369 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10370 char_u *p;
10371 char_u pathbuf[NUMBUFLEN];
10372 int count = 1;
10373 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010374 int error = FALSE;
10375#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010376
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010377 rettv->vval.v_string = NULL;
10378 rettv->v_type = VAR_STRING;
10379
10380#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10386 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010387 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010388 else
10389 {
10390 if (*p != NUL)
10391 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010393 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010394 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010395 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010396 }
10397
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010398 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10399 error = TRUE;
10400
10401 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010403 do
10404 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010405 if (rettv->v_type == VAR_STRING)
10406 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 fresult = find_file_in_path_option(first ? fname : NULL,
10408 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010409 0, first, path,
10410 find_what,
10411 curbuf->b_ffname,
10412 find_what == FINDFILE_DIR
10413 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010414 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010415
10416 if (fresult != NULL && rettv->v_type == VAR_LIST)
10417 list_append_string(rettv->vval.v_list, fresult, -1);
10418
10419 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010421
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010422 if (rettv->v_type == VAR_STRING)
10423 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010424#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010425}
10426
Bram Moolenaar33570922005-01-25 22:26:29 +000010427static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10428static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010429
10430/*
10431 * Implementation of map() and filter().
10432 */
10433 static void
10434filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010437 int map;
10438{
10439 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010440 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010441 listitem_T *li, *nli;
10442 list_T *l = NULL;
10443 dictitem_T *di;
10444 hashtab_T *ht;
10445 hashitem_T *hi;
10446 dict_T *d = NULL;
10447 typval_T save_val;
10448 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010449 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010450 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010451 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10452 char *arg_errmsg = (map ? N_("map() argument")
10453 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010454 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010455 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010456
Bram Moolenaare9a41262005-01-15 22:18:47 +000010457 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010458 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010459 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010460 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010461 return;
10462 }
10463 else if (argvars[0].v_type == VAR_DICT)
10464 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010465 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010466 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010467 return;
10468 }
10469 else
10470 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010471 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010472 return;
10473 }
10474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 expr = get_tv_string_buf_chk(&argvars[1], buf);
10476 /* On type errors, the preceding call has already displayed an error
10477 * message. Avoid a misleading error message for an empty string that
10478 * was not passed as argument. */
10479 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 prepare_vimvar(VV_VAL, &save_val);
10482 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010483
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010484 /* We reset "did_emsg" to be able to detect whether an error
10485 * occurred during evaluation of the expression. */
10486 save_did_emsg = did_emsg;
10487 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010488
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010489 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 vimvars[VV_KEY].vv_type = VAR_STRING;
10493
10494 ht = &d->dv_hashtab;
10495 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010496 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 if (!HASHITEM_EMPTY(hi))
10500 {
10501 --todo;
10502 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010503 if (tv_check_lock(di->di_tv.v_lock,
10504 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 break;
10506 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010507 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010508 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 break;
10510 if (!map && rem)
10511 dictitem_remove(d, di);
10512 clear_tv(&vimvars[VV_KEY].vv_tv);
10513 }
10514 }
10515 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 }
10517 else
10518 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010519 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 for (li = l->lv_first; li != NULL; li = nli)
10522 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010523 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010524 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010526 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010527 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010528 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010529 break;
10530 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010532 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010533 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010534 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010535
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010536 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010538
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010539 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010540 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010541
10542 copy_tv(&argvars[0], rettv);
10543}
10544
10545 static int
10546filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010548 char_u *expr;
10549 int map;
10550 int *remp;
10551{
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010554 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 s = expr;
10558 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010559 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010560 if (*s != NUL) /* check for trailing chars after expr */
10561 {
10562 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010563 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 }
10565 if (map)
10566 {
10567 /* map(): replace the list item value */
10568 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010569 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 *tv = rettv;
10571 }
10572 else
10573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 int error = FALSE;
10575
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 /* On type error, nothing has been removed; return FAIL to stop the
10580 * loop. The error message was given by get_tv_number_chk(). */
10581 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010582 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010584 retval = OK;
10585theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010587 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588}
10589
10590/*
10591 * "filter()" function
10592 */
10593 static void
10594f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010595 typval_T *argvars;
10596 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010597{
10598 filter_map(argvars, rettv, FALSE);
10599}
10600
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010602 * "finddir({fname}[, {path}[, {count}]])" function
10603 */
10604 static void
10605f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 typval_T *argvars;
10607 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010608{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010609 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010610}
10611
10612/*
10613 * "findfile({fname}[, {path}[, {count}]])" function
10614 */
10615 static void
10616f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010617 typval_T *argvars;
10618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010619{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010620 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010621}
10622
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010623#ifdef FEAT_FLOAT
10624/*
10625 * "float2nr({float})" function
10626 */
10627 static void
10628f_float2nr(argvars, rettv)
10629 typval_T *argvars;
10630 typval_T *rettv;
10631{
10632 float_T f;
10633
10634 if (get_float_arg(argvars, &f) == OK)
10635 {
10636 if (f < -0x7fffffff)
10637 rettv->vval.v_number = -0x7fffffff;
10638 else if (f > 0x7fffffff)
10639 rettv->vval.v_number = 0x7fffffff;
10640 else
10641 rettv->vval.v_number = (varnumber_T)f;
10642 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010643}
10644
10645/*
10646 * "floor({float})" function
10647 */
10648 static void
10649f_floor(argvars, rettv)
10650 typval_T *argvars;
10651 typval_T *rettv;
10652{
10653 float_T f;
10654
10655 rettv->v_type = VAR_FLOAT;
10656 if (get_float_arg(argvars, &f) == OK)
10657 rettv->vval.v_float = floor(f);
10658 else
10659 rettv->vval.v_float = 0.0;
10660}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010661
10662/*
10663 * "fmod()" function
10664 */
10665 static void
10666f_fmod(argvars, rettv)
10667 typval_T *argvars;
10668 typval_T *rettv;
10669{
10670 float_T fx, fy;
10671
10672 rettv->v_type = VAR_FLOAT;
10673 if (get_float_arg(argvars, &fx) == OK
10674 && get_float_arg(&argvars[1], &fy) == OK)
10675 rettv->vval.v_float = fmod(fx, fy);
10676 else
10677 rettv->vval.v_float = 0.0;
10678}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010679#endif
10680
Bram Moolenaar0d660222005-01-07 21:51:51 +000010681/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010682 * "fnameescape({string})" function
10683 */
10684 static void
10685f_fnameescape(argvars, rettv)
10686 typval_T *argvars;
10687 typval_T *rettv;
10688{
10689 rettv->vval.v_string = vim_strsave_fnameescape(
10690 get_tv_string(&argvars[0]), FALSE);
10691 rettv->v_type = VAR_STRING;
10692}
10693
10694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 * "fnamemodify({fname}, {mods})" function
10696 */
10697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010699 typval_T *argvars;
10700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 char_u *fname;
10703 char_u *mods;
10704 int usedlen = 0;
10705 int len;
10706 char_u *fbuf = NULL;
10707 char_u buf[NUMBUFLEN];
10708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010709 fname = get_tv_string_chk(&argvars[0]);
10710 mods = get_tv_string_buf_chk(&argvars[1], buf);
10711 if (fname == NULL || mods == NULL)
10712 fname = NULL;
10713 else
10714 {
10715 len = (int)STRLEN(fname);
10716 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 vim_free(fbuf);
10725}
10726
Bram Moolenaar33570922005-01-25 22:26:29 +000010727static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728
10729/*
10730 * "foldclosed()" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 int end;
10737{
10738#ifdef FEAT_FOLDING
10739 linenr_T lnum;
10740 linenr_T first, last;
10741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10744 {
10745 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10746 {
10747 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 return;
10752 }
10753 }
10754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756}
10757
10758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010759 * "foldclosed()" function
10760 */
10761 static void
10762f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010765{
10766 foldclosed_both(argvars, rettv, FALSE);
10767}
10768
10769/*
10770 * "foldclosedend()" function
10771 */
10772 static void
10773f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010774 typval_T *argvars;
10775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010776{
10777 foldclosed_both(argvars, rettv, TRUE);
10778}
10779
10780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 * "foldlevel()" function
10782 */
10783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 typval_T *argvars;
10786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787{
10788#ifdef FEAT_FOLDING
10789 linenr_T lnum;
10790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795}
10796
10797/*
10798 * "foldtext()" function
10799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
10805#ifdef FEAT_FOLDING
10806 linenr_T lnum;
10807 char_u *s;
10808 char_u *r;
10809 int len;
10810 char *txt;
10811#endif
10812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->v_type = VAR_STRING;
10814 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10817 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10818 <= curbuf->b_ml.ml_line_count
10819 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
10821 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010822 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10823 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 {
10825 if (!linewhite(lnum))
10826 break;
10827 ++lnum;
10828 }
10829
10830 /* Find interesting text in this line. */
10831 s = skipwhite(ml_get(lnum));
10832 /* skip C comment-start */
10833 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010836 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010838 {
10839 s = skipwhite(ml_get(lnum + 1));
10840 if (*s == '*')
10841 s = skipwhite(s + 1);
10842 }
10843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 txt = _("+-%s%3ld lines: ");
10845 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010846 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 + 20 /* for %3ld */
10848 + STRLEN(s))); /* concatenated */
10849 if (r != NULL)
10850 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10852 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10853 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 len = (int)STRLEN(r);
10855 STRCAT(r, s);
10856 /* remove 'foldmarker' and 'commentstring' */
10857 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 }
10860 }
10861#endif
10862}
10863
10864/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010865 * "foldtextresult(lnum)" function
10866 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010869 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010871{
10872#ifdef FEAT_FOLDING
10873 linenr_T lnum;
10874 char_u *text;
10875 char_u buf[51];
10876 foldinfo_T foldinfo;
10877 int fold_count;
10878#endif
10879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->v_type = VAR_STRING;
10881 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010882#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 /* treat illegal types and illegal string values for {lnum} the same */
10885 if (lnum < 0)
10886 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010887 fold_count = foldedCount(curwin, lnum, &foldinfo);
10888 if (fold_count > 0)
10889 {
10890 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10891 &foldinfo, buf);
10892 if (text == buf)
10893 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010895 }
10896#endif
10897}
10898
10899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * "foreground()" function
10901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010904 typval_T *argvars UNUSED;
10905 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907#ifdef FEAT_GUI
10908 if (gui.in_use)
10909 gui_mch_set_foreground();
10910#else
10911# ifdef WIN32
10912 win32_set_foreground();
10913# endif
10914#endif
10915}
10916
10917/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010918 * "function()" function
10919 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *argvars;
10923 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924{
10925 char_u *s;
10926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010928 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010929 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010930 /* Don't check an autoload name for existence here. */
10931 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010932 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010933 else
10934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 rettv->vval.v_string = vim_strsave(s);
10936 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010937 }
10938}
10939
10940/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010941 * "garbagecollect()" function
10942 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010943 static void
10944f_garbagecollect(argvars, rettv)
10945 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010946 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010947{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010948 /* This is postponed until we are back at the toplevel, because we may be
10949 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10950 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010951
10952 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10953 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010954}
10955
10956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010957 * "get()" function
10958 */
10959 static void
10960f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010961 typval_T *argvars;
10962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010963{
Bram Moolenaar33570922005-01-25 22:26:29 +000010964 listitem_T *li;
10965 list_T *l;
10966 dictitem_T *di;
10967 dict_T *d;
10968 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010969
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 int error = FALSE;
10975
10976 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10977 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010979 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010980 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 else if (argvars[0].v_type == VAR_DICT)
10982 {
10983 if ((d = argvars[0].vval.v_dict) != NULL)
10984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010985 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 if (di != NULL)
10987 tv = &di->di_tv;
10988 }
10989 }
10990 else
10991 EMSG2(_(e_listdictarg), "get()");
10992
10993 if (tv == NULL)
10994 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010996 copy_tv(&argvars[2], rettv);
10997 }
10998 else
10999 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000}
11001
Bram Moolenaar342337a2005-07-21 21:11:17 +000011002static 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 +000011003
11004/*
11005 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011006 * Return a range (from start to end) of lines in rettv from the specified
11007 * buffer.
11008 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011009 */
11010 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011011get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011012 buf_T *buf;
11013 linenr_T start;
11014 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011015 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011016 typval_T *rettv;
11017{
11018 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011019
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011020 if (retlist && rettv_list_alloc(rettv) == FAIL)
11021 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011022
11023 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11024 return;
11025
11026 if (!retlist)
11027 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011028 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11029 p = ml_get_buf(buf, start, FALSE);
11030 else
11031 p = (char_u *)"";
11032
11033 rettv->v_type = VAR_STRING;
11034 rettv->vval.v_string = vim_strsave(p);
11035 }
11036 else
11037 {
11038 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011039 return;
11040
11041 if (start < 1)
11042 start = 1;
11043 if (end > buf->b_ml.ml_line_count)
11044 end = buf->b_ml.ml_line_count;
11045 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011046 if (list_append_string(rettv->vval.v_list,
11047 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011048 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011049 }
11050}
11051
11052/*
11053 * "getbufline()" function
11054 */
11055 static void
11056f_getbufline(argvars, rettv)
11057 typval_T *argvars;
11058 typval_T *rettv;
11059{
11060 linenr_T lnum;
11061 linenr_T end;
11062 buf_T *buf;
11063
11064 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11065 ++emsg_off;
11066 buf = get_buf_tv(&argvars[0]);
11067 --emsg_off;
11068
Bram Moolenaar661b1822005-07-28 22:36:45 +000011069 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011070 if (argvars[2].v_type == VAR_UNKNOWN)
11071 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011072 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011073 end = get_tv_lnum_buf(&argvars[2], buf);
11074
Bram Moolenaar342337a2005-07-21 21:11:17 +000011075 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011076}
11077
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078/*
11079 * "getbufvar()" function
11080 */
11081 static void
11082f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011083 typval_T *argvars;
11084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011085{
11086 buf_T *buf;
11087 buf_T *save_curbuf;
11088 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11092 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011093 ++emsg_off;
11094 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095
11096 rettv->v_type = VAR_STRING;
11097 rettv->vval.v_string = NULL;
11098
11099 if (buf != NULL && varname != NULL)
11100 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011101 /* set curbuf to be our buf, temporarily */
11102 save_curbuf = curbuf;
11103 curbuf = buf;
11104
Bram Moolenaar0d660222005-01-07 21:51:51 +000011105 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011107 else if (STRCMP(varname, "changedtick") == 0)
11108 {
11109 rettv->v_type = VAR_NUMBER;
11110 rettv->vval.v_number = curbuf->b_changedtick;
11111 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112 else
11113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 if (*varname == NUL)
11115 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11116 * scope prefix before the NUL byte is required by
11117 * find_var_in_ht(). */
11118 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011120 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011124
11125 /* restore previous notion of curbuf */
11126 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127 }
11128
11129 --emsg_off;
11130}
11131
11132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 * "getchar()" function
11134 */
11135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011137 typval_T *argvars;
11138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139{
11140 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011143 /* Position the cursor. Needed after a message that ends in a space. */
11144 windgoto(msg_row, msg_col);
11145
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 ++no_mapping;
11147 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011148 for (;;)
11149 {
11150 if (argvars[0].v_type == VAR_UNKNOWN)
11151 /* getchar(): blocking wait. */
11152 n = safe_vgetc();
11153 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11154 /* getchar(1): only check if char avail */
11155 n = vpeekc();
11156 else if (error || vpeekc() == NUL)
11157 /* illegal argument or getchar(0) and no char avail: return zero */
11158 n = 0;
11159 else
11160 /* getchar(0) and char avail: return char */
11161 n = safe_vgetc();
11162 if (n == K_IGNORE)
11163 continue;
11164 break;
11165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 --no_mapping;
11167 --allow_keys;
11168
Bram Moolenaar219b8702006-11-01 14:32:36 +000011169 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11170 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11171 vimvars[VV_MOUSE_COL].vv_nr = 0;
11172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 if (IS_SPECIAL(n) || mod_mask != 0)
11175 {
11176 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11177 int i = 0;
11178
11179 /* Turn a special key into three bytes, plus modifier. */
11180 if (mod_mask != 0)
11181 {
11182 temp[i++] = K_SPECIAL;
11183 temp[i++] = KS_MODIFIER;
11184 temp[i++] = mod_mask;
11185 }
11186 if (IS_SPECIAL(n))
11187 {
11188 temp[i++] = K_SPECIAL;
11189 temp[i++] = K_SECOND(n);
11190 temp[i++] = K_THIRD(n);
11191 }
11192#ifdef FEAT_MBYTE
11193 else if (has_mbyte)
11194 i += (*mb_char2bytes)(n, temp + i);
11195#endif
11196 else
11197 temp[i++] = n;
11198 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199 rettv->v_type = VAR_STRING;
11200 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011201
11202#ifdef FEAT_MOUSE
11203 if (n == K_LEFTMOUSE
11204 || n == K_LEFTMOUSE_NM
11205 || n == K_LEFTDRAG
11206 || n == K_LEFTRELEASE
11207 || n == K_LEFTRELEASE_NM
11208 || n == K_MIDDLEMOUSE
11209 || n == K_MIDDLEDRAG
11210 || n == K_MIDDLERELEASE
11211 || n == K_RIGHTMOUSE
11212 || n == K_RIGHTDRAG
11213 || n == K_RIGHTRELEASE
11214 || n == K_X1MOUSE
11215 || n == K_X1DRAG
11216 || n == K_X1RELEASE
11217 || n == K_X2MOUSE
11218 || n == K_X2DRAG
11219 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011220 || n == K_MOUSELEFT
11221 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011222 || n == K_MOUSEDOWN
11223 || n == K_MOUSEUP)
11224 {
11225 int row = mouse_row;
11226 int col = mouse_col;
11227 win_T *win;
11228 linenr_T lnum;
11229# ifdef FEAT_WINDOWS
11230 win_T *wp;
11231# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011232 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011233
11234 if (row >= 0 && col >= 0)
11235 {
11236 /* Find the window at the mouse coordinates and compute the
11237 * text position. */
11238 win = mouse_find_win(&row, &col);
11239 (void)mouse_comp_pos(win, &row, &col, &lnum);
11240# ifdef FEAT_WINDOWS
11241 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011242 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011243# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011244 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011245 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11246 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11247 }
11248 }
11249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 }
11251}
11252
11253/*
11254 * "getcharmod()" function
11255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011258 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262}
11263
11264/*
11265 * "getcmdline()" function
11266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->v_type = VAR_STRING;
11273 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274}
11275
11276/*
11277 * "getcmdpos()" function
11278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011281 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285}
11286
11287/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011288 * "getcmdtype()" function
11289 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011290 static void
11291f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011292 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011293 typval_T *rettv;
11294{
11295 rettv->v_type = VAR_STRING;
11296 rettv->vval.v_string = alloc(2);
11297 if (rettv->vval.v_string != NULL)
11298 {
11299 rettv->vval.v_string[0] = get_cmdline_type();
11300 rettv->vval.v_string[1] = NUL;
11301 }
11302}
11303
11304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 * "getcwd()" function
11306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011312 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011315 rettv->vval.v_string = NULL;
11316 cwd = alloc(MAXPATHL);
11317 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011319 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11320 {
11321 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011323 if (rettv->vval.v_string != NULL)
11324 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011326 }
11327 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 }
11329}
11330
11331/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011332 * "getfontname()" function
11333 */
11334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011336 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011337 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->v_type = VAR_STRING;
11340 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011341#ifdef FEAT_GUI
11342 if (gui.in_use)
11343 {
11344 GuiFont font;
11345 char_u *name = NULL;
11346
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011347 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011348 {
11349 /* Get the "Normal" font. Either the name saved by
11350 * hl_set_font_name() or from the font ID. */
11351 font = gui.norm_font;
11352 name = hl_get_font_name();
11353 }
11354 else
11355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011357 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11358 return;
11359 font = gui_mch_get_font(name, FALSE);
11360 if (font == NOFONT)
11361 return; /* Invalid font name, return empty string. */
11362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011364 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011365 gui_mch_free_font(font);
11366 }
11367#endif
11368}
11369
11370/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011371 * "getfperm({fname})" function
11372 */
11373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011375 typval_T *argvars;
11376 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011377{
11378 char_u *fname;
11379 struct stat st;
11380 char_u *perm = NULL;
11381 char_u flags[] = "rwx";
11382 int i;
11383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011387 if (mch_stat((char *)fname, &st) >= 0)
11388 {
11389 perm = vim_strsave((char_u *)"---------");
11390 if (perm != NULL)
11391 {
11392 for (i = 0; i < 9; i++)
11393 {
11394 if (st.st_mode & (1 << (8 - i)))
11395 perm[i] = flags[i % 3];
11396 }
11397 }
11398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011400}
11401
11402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 * "getfsize({fname})" function
11404 */
11405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011407 typval_T *argvars;
11408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409{
11410 char_u *fname;
11411 struct stat st;
11412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416
11417 if (mch_stat((char *)fname, &st) >= 0)
11418 {
11419 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011424
11425 /* non-perfect check for overflow */
11426 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11427 rettv->vval.v_number = -2;
11428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 }
11430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432}
11433
11434/*
11435 * "getftime({fname})" function
11436 */
11437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011439 typval_T *argvars;
11440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441{
11442 char_u *fname;
11443 struct stat st;
11444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446
11447 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451}
11452
11453/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011454 * "getftype({fname})" function
11455 */
11456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 typval_T *argvars;
11459 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011460{
11461 char_u *fname;
11462 struct stat st;
11463 char_u *type = NULL;
11464 char *t;
11465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011469 if (mch_lstat((char *)fname, &st) >= 0)
11470 {
11471#ifdef S_ISREG
11472 if (S_ISREG(st.st_mode))
11473 t = "file";
11474 else if (S_ISDIR(st.st_mode))
11475 t = "dir";
11476# ifdef S_ISLNK
11477 else if (S_ISLNK(st.st_mode))
11478 t = "link";
11479# endif
11480# ifdef S_ISBLK
11481 else if (S_ISBLK(st.st_mode))
11482 t = "bdev";
11483# endif
11484# ifdef S_ISCHR
11485 else if (S_ISCHR(st.st_mode))
11486 t = "cdev";
11487# endif
11488# ifdef S_ISFIFO
11489 else if (S_ISFIFO(st.st_mode))
11490 t = "fifo";
11491# endif
11492# ifdef S_ISSOCK
11493 else if (S_ISSOCK(st.st_mode))
11494 t = "fifo";
11495# endif
11496 else
11497 t = "other";
11498#else
11499# ifdef S_IFMT
11500 switch (st.st_mode & S_IFMT)
11501 {
11502 case S_IFREG: t = "file"; break;
11503 case S_IFDIR: t = "dir"; break;
11504# ifdef S_IFLNK
11505 case S_IFLNK: t = "link"; break;
11506# endif
11507# ifdef S_IFBLK
11508 case S_IFBLK: t = "bdev"; break;
11509# endif
11510# ifdef S_IFCHR
11511 case S_IFCHR: t = "cdev"; break;
11512# endif
11513# ifdef S_IFIFO
11514 case S_IFIFO: t = "fifo"; break;
11515# endif
11516# ifdef S_IFSOCK
11517 case S_IFSOCK: t = "socket"; break;
11518# endif
11519 default: t = "other";
11520 }
11521# else
11522 if (mch_isdir(fname))
11523 t = "dir";
11524 else
11525 t = "file";
11526# endif
11527#endif
11528 type = vim_strsave((char_u *)t);
11529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011531}
11532
11533/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011534 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 */
11536 static void
11537f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011538 typval_T *argvars;
11539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540{
11541 linenr_T lnum;
11542 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011543 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544
11545 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011546 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011547 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011548 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011549 retlist = FALSE;
11550 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011552 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011553 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011554 retlist = TRUE;
11555 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011556
Bram Moolenaar342337a2005-07-21 21:11:17 +000011557 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558}
11559
11560/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011561 * "getmatches()" function
11562 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011563 static void
11564f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011565 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011566 typval_T *rettv;
11567{
11568#ifdef FEAT_SEARCH_EXTRA
11569 dict_T *dict;
11570 matchitem_T *cur = curwin->w_match_head;
11571
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011572 if (rettv_list_alloc(rettv) == OK)
11573 {
11574 while (cur != NULL)
11575 {
11576 dict = dict_alloc();
11577 if (dict == NULL)
11578 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011579 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11580 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11581 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11582 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11583 list_append_dict(rettv->vval.v_list, dict);
11584 cur = cur->next;
11585 }
11586 }
11587#endif
11588}
11589
11590/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011591 * "getpid()" function
11592 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011593 static void
11594f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011595 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011596 typval_T *rettv;
11597{
11598 rettv->vval.v_number = mch_get_pid();
11599}
11600
11601/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011602 * "getpos(string)" function
11603 */
11604 static void
11605f_getpos(argvars, rettv)
11606 typval_T *argvars;
11607 typval_T *rettv;
11608{
11609 pos_T *fp;
11610 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011611 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011612
11613 if (rettv_list_alloc(rettv) == OK)
11614 {
11615 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011616 fp = var2fpos(&argvars[0], TRUE, &fnum);
11617 if (fnum != -1)
11618 list_append_number(l, (varnumber_T)fnum);
11619 else
11620 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011621 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11622 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011623 list_append_number(l, (fp != NULL)
11624 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011625 : (varnumber_T)0);
11626 list_append_number(l,
11627#ifdef FEAT_VIRTUALEDIT
11628 (fp != NULL) ? (varnumber_T)fp->coladd :
11629#endif
11630 (varnumber_T)0);
11631 }
11632 else
11633 rettv->vval.v_number = FALSE;
11634}
11635
11636/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011637 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011638 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011639 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011640f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011641 typval_T *argvars UNUSED;
11642 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011643{
11644#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011645 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011646#endif
11647
Bram Moolenaar2641f772005-03-25 21:58:17 +000011648#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011649 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011650 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011651 wp = NULL;
11652 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11653 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011654 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011655 if (wp == NULL)
11656 return;
11657 }
11658
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011659 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011660 }
11661#endif
11662}
11663
11664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 * "getreg()" function
11666 */
11667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 typval_T *argvars;
11670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671{
11672 char_u *strregname;
11673 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011674 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011677 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 strregname = get_tv_string_chk(&argvars[0]);
11680 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011681 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011685 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 regname = (strregname == NULL ? '"' : *strregname);
11687 if (regname == 0)
11688 regname = '"';
11689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 rettv->vval.v_string = error ? NULL :
11692 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693}
11694
11695/*
11696 * "getregtype()" function
11697 */
11698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 typval_T *argvars;
11701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702{
11703 char_u *strregname;
11704 int regname;
11705 char_u buf[NUMBUFLEN + 2];
11706 long reglen = 0;
11707
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011708 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 {
11710 strregname = get_tv_string_chk(&argvars[0]);
11711 if (strregname == NULL) /* type error; errmsg already given */
11712 {
11713 rettv->v_type = VAR_STRING;
11714 rettv->vval.v_string = NULL;
11715 return;
11716 }
11717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 else
11719 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011720 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721
11722 regname = (strregname == NULL ? '"' : *strregname);
11723 if (regname == 0)
11724 regname = '"';
11725
11726 buf[0] = NUL;
11727 buf[1] = NUL;
11728 switch (get_reg_type(regname, &reglen))
11729 {
11730 case MLINE: buf[0] = 'V'; break;
11731 case MCHAR: buf[0] = 'v'; break;
11732#ifdef FEAT_VISUAL
11733 case MBLOCK:
11734 buf[0] = Ctrl_V;
11735 sprintf((char *)buf + 1, "%ld", reglen + 1);
11736 break;
11737#endif
11738 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 rettv->v_type = VAR_STRING;
11740 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741}
11742
11743/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011744 * "gettabvar()" function
11745 */
11746 static void
11747f_gettabvar(argvars, rettv)
11748 typval_T *argvars;
11749 typval_T *rettv;
11750{
11751 tabpage_T *tp;
11752 dictitem_T *v;
11753 char_u *varname;
11754
11755 rettv->v_type = VAR_STRING;
11756 rettv->vval.v_string = NULL;
11757
11758 varname = get_tv_string_chk(&argvars[1]);
11759 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11760 if (tp != NULL && varname != NULL)
11761 {
11762 /* look up the variable */
11763 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11764 if (v != NULL)
11765 copy_tv(&v->di_tv, rettv);
11766 }
11767}
11768
11769/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011770 * "gettabwinvar()" function
11771 */
11772 static void
11773f_gettabwinvar(argvars, rettv)
11774 typval_T *argvars;
11775 typval_T *rettv;
11776{
11777 getwinvar(argvars, rettv, 1);
11778}
11779
11780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 * "getwinposx()" function
11782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789#ifdef FEAT_GUI
11790 if (gui.in_use)
11791 {
11792 int x, y;
11793
11794 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 }
11797#endif
11798}
11799
11800/*
11801 * "getwinposy()" function
11802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011805 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809#ifdef FEAT_GUI
11810 if (gui.in_use)
11811 {
11812 int x, y;
11813
11814 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 }
11817#endif
11818}
11819
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011820/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011821 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011822 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011823 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011824find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011825 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011826 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011827{
11828#ifdef FEAT_WINDOWS
11829 win_T *wp;
11830#endif
11831 int nr;
11832
11833 nr = get_tv_number_chk(vp, NULL);
11834
11835#ifdef FEAT_WINDOWS
11836 if (nr < 0)
11837 return NULL;
11838 if (nr == 0)
11839 return curwin;
11840
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011841 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11842 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011843 if (--nr <= 0)
11844 break;
11845 return wp;
11846#else
11847 if (nr == 0 || nr == 1)
11848 return curwin;
11849 return NULL;
11850#endif
11851}
11852
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853/*
11854 * "getwinvar()" function
11855 */
11856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857f_getwinvar(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 Moolenaar99ebf042006-04-15 20:28:54 +000011861 getwinvar(argvars, rettv, 0);
11862}
11863
11864/*
11865 * getwinvar() and gettabwinvar()
11866 */
11867 static void
11868getwinvar(argvars, rettv, off)
11869 typval_T *argvars;
11870 typval_T *rettv;
11871 int off; /* 1 for gettabwinvar() */
11872{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 win_T *win, *oldcurwin;
11874 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011876 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011878#ifdef FEAT_WINDOWS
11879 if (off == 1)
11880 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11881 else
11882 tp = curtab;
11883#endif
11884 win = find_win_by_nr(&argvars[off], tp);
11885 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->v_type = VAR_STRING;
11889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890
11891 if (win != NULL && varname != NULL)
11892 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011893 /* Set curwin to be our win, temporarily. Also set curbuf, so
11894 * that we can get buffer-local options. */
11895 oldcurwin = curwin;
11896 curwin = win;
11897 curbuf = win->w_buffer;
11898
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 else
11902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 if (*varname == NUL)
11904 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11905 * scope prefix before the NUL byte is required by
11906 * find_var_in_ht(). */
11907 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011909 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011911 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011913
11914 /* restore previous notion of curwin */
11915 curwin = oldcurwin;
11916 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 }
11918
11919 --emsg_off;
11920}
11921
11922/*
11923 * "glob()" function
11924 */
11925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *argvars;
11928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011930 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011932 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011934 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011935 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011937 if (argvars[1].v_type != VAR_UNKNOWN)
11938 {
11939 if (get_tv_number_chk(&argvars[1], &error))
11940 options |= WILD_KEEP_ALL;
11941 if (argvars[2].v_type != VAR_UNKNOWN
11942 && get_tv_number_chk(&argvars[2], &error))
11943 {
11944 rettv->v_type = VAR_LIST;
11945 rettv->vval.v_list = NULL;
11946 }
11947 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011948 if (!error)
11949 {
11950 ExpandInit(&xpc);
11951 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011952 if (p_wic)
11953 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011954 if (rettv->v_type == VAR_STRING)
11955 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011956 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011957 else if (rettv_list_alloc(rettv) != FAIL)
11958 {
11959 int i;
11960
11961 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11962 NULL, options, WILD_ALL_KEEP);
11963 for (i = 0; i < xpc.xp_numfiles; i++)
11964 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11965
11966 ExpandCleanup(&xpc);
11967 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011968 }
11969 else
11970 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971}
11972
11973/*
11974 * "globpath()" function
11975 */
11976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011978 typval_T *argvars;
11979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011981 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011984 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011986 /* When the optional second argument is non-zero, don't remove matches
11987 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11988 if (argvars[2].v_type != VAR_UNKNOWN
11989 && get_tv_number_chk(&argvars[2], &error))
11990 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011992 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 rettv->vval.v_string = NULL;
11994 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011995 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11996 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997}
11998
11999/*
12000 * "has()" function
12001 */
12002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012004 typval_T *argvars;
12005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006{
12007 int i;
12008 char_u *name;
12009 int n = FALSE;
12010 static char *(has_list[]) =
12011 {
12012#ifdef AMIGA
12013 "amiga",
12014# ifdef FEAT_ARP
12015 "arp",
12016# endif
12017#endif
12018#ifdef __BEOS__
12019 "beos",
12020#endif
12021#ifdef MSDOS
12022# ifdef DJGPP
12023 "dos32",
12024# else
12025 "dos16",
12026# endif
12027#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012028#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 "mac",
12030#endif
12031#if defined(MACOS_X_UNIX)
12032 "macunix",
12033#endif
12034#ifdef OS2
12035 "os2",
12036#endif
12037#ifdef __QNX__
12038 "qnx",
12039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040#ifdef UNIX
12041 "unix",
12042#endif
12043#ifdef VMS
12044 "vms",
12045#endif
12046#ifdef WIN16
12047 "win16",
12048#endif
12049#ifdef WIN32
12050 "win32",
12051#endif
12052#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12053 "win32unix",
12054#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012055#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 "win64",
12057#endif
12058#ifdef EBCDIC
12059 "ebcdic",
12060#endif
12061#ifndef CASE_INSENSITIVE_FILENAME
12062 "fname_case",
12063#endif
12064#ifdef FEAT_ARABIC
12065 "arabic",
12066#endif
12067#ifdef FEAT_AUTOCMD
12068 "autocmd",
12069#endif
12070#ifdef FEAT_BEVAL
12071 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012072# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12073 "balloon_multiline",
12074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075#endif
12076#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12077 "builtin_terms",
12078# ifdef ALL_BUILTIN_TCAPS
12079 "all_builtin_terms",
12080# endif
12081#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012082#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12083 || defined(FEAT_GUI_W32) \
12084 || defined(FEAT_GUI_MOTIF))
12085 "browsefilter",
12086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087#ifdef FEAT_BYTEOFF
12088 "byte_offset",
12089#endif
12090#ifdef FEAT_CINDENT
12091 "cindent",
12092#endif
12093#ifdef FEAT_CLIENTSERVER
12094 "clientserver",
12095#endif
12096#ifdef FEAT_CLIPBOARD
12097 "clipboard",
12098#endif
12099#ifdef FEAT_CMDL_COMPL
12100 "cmdline_compl",
12101#endif
12102#ifdef FEAT_CMDHIST
12103 "cmdline_hist",
12104#endif
12105#ifdef FEAT_COMMENTS
12106 "comments",
12107#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012108#ifdef FEAT_CONCEAL
12109 "conceal",
12110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111#ifdef FEAT_CRYPT
12112 "cryptv",
12113#endif
12114#ifdef FEAT_CSCOPE
12115 "cscope",
12116#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012117#ifdef FEAT_CURSORBIND
12118 "cursorbind",
12119#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012120#ifdef CURSOR_SHAPE
12121 "cursorshape",
12122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123#ifdef DEBUG
12124 "debug",
12125#endif
12126#ifdef FEAT_CON_DIALOG
12127 "dialog_con",
12128#endif
12129#ifdef FEAT_GUI_DIALOG
12130 "dialog_gui",
12131#endif
12132#ifdef FEAT_DIFF
12133 "diff",
12134#endif
12135#ifdef FEAT_DIGRAPHS
12136 "digraphs",
12137#endif
12138#ifdef FEAT_DND
12139 "dnd",
12140#endif
12141#ifdef FEAT_EMACS_TAGS
12142 "emacs_tags",
12143#endif
12144 "eval", /* always present, of course! */
12145#ifdef FEAT_EX_EXTRA
12146 "ex_extra",
12147#endif
12148#ifdef FEAT_SEARCH_EXTRA
12149 "extra_search",
12150#endif
12151#ifdef FEAT_FKMAP
12152 "farsi",
12153#endif
12154#ifdef FEAT_SEARCHPATH
12155 "file_in_path",
12156#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012157#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012158 "filterpipe",
12159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160#ifdef FEAT_FIND_ID
12161 "find_in_path",
12162#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012163#ifdef FEAT_FLOAT
12164 "float",
12165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166#ifdef FEAT_FOLDING
12167 "folding",
12168#endif
12169#ifdef FEAT_FOOTER
12170 "footer",
12171#endif
12172#if !defined(USE_SYSTEM) && defined(UNIX)
12173 "fork",
12174#endif
12175#ifdef FEAT_GETTEXT
12176 "gettext",
12177#endif
12178#ifdef FEAT_GUI
12179 "gui",
12180#endif
12181#ifdef FEAT_GUI_ATHENA
12182# ifdef FEAT_GUI_NEXTAW
12183 "gui_neXtaw",
12184# else
12185 "gui_athena",
12186# endif
12187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#ifdef FEAT_GUI_GTK
12189 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012192#ifdef FEAT_GUI_GNOME
12193 "gui_gnome",
12194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195#ifdef FEAT_GUI_MAC
12196 "gui_mac",
12197#endif
12198#ifdef FEAT_GUI_MOTIF
12199 "gui_motif",
12200#endif
12201#ifdef FEAT_GUI_PHOTON
12202 "gui_photon",
12203#endif
12204#ifdef FEAT_GUI_W16
12205 "gui_win16",
12206#endif
12207#ifdef FEAT_GUI_W32
12208 "gui_win32",
12209#endif
12210#ifdef FEAT_HANGULIN
12211 "hangul_input",
12212#endif
12213#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12214 "iconv",
12215#endif
12216#ifdef FEAT_INS_EXPAND
12217 "insert_expand",
12218#endif
12219#ifdef FEAT_JUMPLIST
12220 "jumplist",
12221#endif
12222#ifdef FEAT_KEYMAP
12223 "keymap",
12224#endif
12225#ifdef FEAT_LANGMAP
12226 "langmap",
12227#endif
12228#ifdef FEAT_LIBCALL
12229 "libcall",
12230#endif
12231#ifdef FEAT_LINEBREAK
12232 "linebreak",
12233#endif
12234#ifdef FEAT_LISP
12235 "lispindent",
12236#endif
12237#ifdef FEAT_LISTCMDS
12238 "listcmds",
12239#endif
12240#ifdef FEAT_LOCALMAP
12241 "localmap",
12242#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012243#ifdef FEAT_LUA
12244# ifndef DYNAMIC_LUA
12245 "lua",
12246# endif
12247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248#ifdef FEAT_MENU
12249 "menu",
12250#endif
12251#ifdef FEAT_SESSION
12252 "mksession",
12253#endif
12254#ifdef FEAT_MODIFY_FNAME
12255 "modify_fname",
12256#endif
12257#ifdef FEAT_MOUSE
12258 "mouse",
12259#endif
12260#ifdef FEAT_MOUSESHAPE
12261 "mouseshape",
12262#endif
12263#if defined(UNIX) || defined(VMS)
12264# ifdef FEAT_MOUSE_DEC
12265 "mouse_dec",
12266# endif
12267# ifdef FEAT_MOUSE_GPM
12268 "mouse_gpm",
12269# endif
12270# ifdef FEAT_MOUSE_JSB
12271 "mouse_jsbterm",
12272# endif
12273# ifdef FEAT_MOUSE_NET
12274 "mouse_netterm",
12275# endif
12276# ifdef FEAT_MOUSE_PTERM
12277 "mouse_pterm",
12278# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012279# ifdef FEAT_MOUSE_SGR
12280 "mouse_sgr",
12281# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012282# ifdef FEAT_SYSMOUSE
12283 "mouse_sysmouse",
12284# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012285# ifdef FEAT_MOUSE_URXVT
12286 "mouse_urxvt",
12287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288# ifdef FEAT_MOUSE_XTERM
12289 "mouse_xterm",
12290# endif
12291#endif
12292#ifdef FEAT_MBYTE
12293 "multi_byte",
12294#endif
12295#ifdef FEAT_MBYTE_IME
12296 "multi_byte_ime",
12297#endif
12298#ifdef FEAT_MULTI_LANG
12299 "multi_lang",
12300#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012301#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012302#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012303 "mzscheme",
12304#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306#ifdef FEAT_OLE
12307 "ole",
12308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309#ifdef FEAT_PATH_EXTRA
12310 "path_extra",
12311#endif
12312#ifdef FEAT_PERL
12313#ifndef DYNAMIC_PERL
12314 "perl",
12315#endif
12316#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012317#ifdef FEAT_PERSISTENT_UNDO
12318 "persistent_undo",
12319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320#ifdef FEAT_PYTHON
12321#ifndef DYNAMIC_PYTHON
12322 "python",
12323#endif
12324#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012325#ifdef FEAT_PYTHON3
12326#ifndef DYNAMIC_PYTHON3
12327 "python3",
12328#endif
12329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330#ifdef FEAT_POSTSCRIPT
12331 "postscript",
12332#endif
12333#ifdef FEAT_PRINTER
12334 "printer",
12335#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012336#ifdef FEAT_PROFILE
12337 "profile",
12338#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012339#ifdef FEAT_RELTIME
12340 "reltime",
12341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342#ifdef FEAT_QUICKFIX
12343 "quickfix",
12344#endif
12345#ifdef FEAT_RIGHTLEFT
12346 "rightleft",
12347#endif
12348#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12349 "ruby",
12350#endif
12351#ifdef FEAT_SCROLLBIND
12352 "scrollbind",
12353#endif
12354#ifdef FEAT_CMDL_INFO
12355 "showcmd",
12356 "cmdline_info",
12357#endif
12358#ifdef FEAT_SIGNS
12359 "signs",
12360#endif
12361#ifdef FEAT_SMARTINDENT
12362 "smartindent",
12363#endif
12364#ifdef FEAT_SNIFF
12365 "sniff",
12366#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012367#ifdef STARTUPTIME
12368 "startuptime",
12369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370#ifdef FEAT_STL_OPT
12371 "statusline",
12372#endif
12373#ifdef FEAT_SUN_WORKSHOP
12374 "sun_workshop",
12375#endif
12376#ifdef FEAT_NETBEANS_INTG
12377 "netbeans_intg",
12378#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012379#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012380 "spell",
12381#endif
12382#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 "syntax",
12384#endif
12385#if defined(USE_SYSTEM) || !defined(UNIX)
12386 "system",
12387#endif
12388#ifdef FEAT_TAG_BINS
12389 "tag_binary",
12390#endif
12391#ifdef FEAT_TAG_OLDSTATIC
12392 "tag_old_static",
12393#endif
12394#ifdef FEAT_TAG_ANYWHITE
12395 "tag_any_white",
12396#endif
12397#ifdef FEAT_TCL
12398# ifndef DYNAMIC_TCL
12399 "tcl",
12400# endif
12401#endif
12402#ifdef TERMINFO
12403 "terminfo",
12404#endif
12405#ifdef FEAT_TERMRESPONSE
12406 "termresponse",
12407#endif
12408#ifdef FEAT_TEXTOBJ
12409 "textobjects",
12410#endif
12411#ifdef HAVE_TGETENT
12412 "tgetent",
12413#endif
12414#ifdef FEAT_TITLE
12415 "title",
12416#endif
12417#ifdef FEAT_TOOLBAR
12418 "toolbar",
12419#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012420#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12421 "unnamedplus",
12422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423#ifdef FEAT_USR_CMDS
12424 "user-commands", /* was accidentally included in 5.4 */
12425 "user_commands",
12426#endif
12427#ifdef FEAT_VIMINFO
12428 "viminfo",
12429#endif
12430#ifdef FEAT_VERTSPLIT
12431 "vertsplit",
12432#endif
12433#ifdef FEAT_VIRTUALEDIT
12434 "virtualedit",
12435#endif
12436#ifdef FEAT_VISUAL
12437 "visual",
12438#endif
12439#ifdef FEAT_VISUALEXTRA
12440 "visualextra",
12441#endif
12442#ifdef FEAT_VREPLACE
12443 "vreplace",
12444#endif
12445#ifdef FEAT_WILDIGN
12446 "wildignore",
12447#endif
12448#ifdef FEAT_WILDMENU
12449 "wildmenu",
12450#endif
12451#ifdef FEAT_WINDOWS
12452 "windows",
12453#endif
12454#ifdef FEAT_WAK
12455 "winaltkeys",
12456#endif
12457#ifdef FEAT_WRITEBACKUP
12458 "writebackup",
12459#endif
12460#ifdef FEAT_XIM
12461 "xim",
12462#endif
12463#ifdef FEAT_XFONTSET
12464 "xfontset",
12465#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012466#ifdef FEAT_XPM_W32
12467 "xpm_w32",
12468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469#ifdef USE_XSMP
12470 "xsmp",
12471#endif
12472#ifdef USE_XSMP_INTERACT
12473 "xsmp_interact",
12474#endif
12475#ifdef FEAT_XCLIPBOARD
12476 "xterm_clipboard",
12477#endif
12478#ifdef FEAT_XTERM_SAVE
12479 "xterm_save",
12480#endif
12481#if defined(UNIX) && defined(FEAT_X11)
12482 "X11",
12483#endif
12484 NULL
12485 };
12486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 for (i = 0; has_list[i] != NULL; ++i)
12489 if (STRICMP(name, has_list[i]) == 0)
12490 {
12491 n = TRUE;
12492 break;
12493 }
12494
12495 if (n == FALSE)
12496 {
12497 if (STRNICMP(name, "patch", 5) == 0)
12498 n = has_patch(atoi((char *)name + 5));
12499 else if (STRICMP(name, "vim_starting") == 0)
12500 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012501#ifdef FEAT_MBYTE
12502 else if (STRICMP(name, "multi_byte_encoding") == 0)
12503 n = has_mbyte;
12504#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012505#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12506 else if (STRICMP(name, "balloon_multiline") == 0)
12507 n = multiline_balloon_available();
12508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509#ifdef DYNAMIC_TCL
12510 else if (STRICMP(name, "tcl") == 0)
12511 n = tcl_enabled(FALSE);
12512#endif
12513#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12514 else if (STRICMP(name, "iconv") == 0)
12515 n = iconv_enabled(FALSE);
12516#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012517#ifdef DYNAMIC_LUA
12518 else if (STRICMP(name, "lua") == 0)
12519 n = lua_enabled(FALSE);
12520#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012521#ifdef DYNAMIC_MZSCHEME
12522 else if (STRICMP(name, "mzscheme") == 0)
12523 n = mzscheme_enabled(FALSE);
12524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525#ifdef DYNAMIC_RUBY
12526 else if (STRICMP(name, "ruby") == 0)
12527 n = ruby_enabled(FALSE);
12528#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012529#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530#ifdef DYNAMIC_PYTHON
12531 else if (STRICMP(name, "python") == 0)
12532 n = python_enabled(FALSE);
12533#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012534#endif
12535#ifdef FEAT_PYTHON3
12536#ifdef DYNAMIC_PYTHON3
12537 else if (STRICMP(name, "python3") == 0)
12538 n = python3_enabled(FALSE);
12539#endif
12540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541#ifdef DYNAMIC_PERL
12542 else if (STRICMP(name, "perl") == 0)
12543 n = perl_enabled(FALSE);
12544#endif
12545#ifdef FEAT_GUI
12546 else if (STRICMP(name, "gui_running") == 0)
12547 n = (gui.in_use || gui.starting);
12548# ifdef FEAT_GUI_W32
12549 else if (STRICMP(name, "gui_win32s") == 0)
12550 n = gui_is_win32s();
12551# endif
12552# ifdef FEAT_BROWSE
12553 else if (STRICMP(name, "browse") == 0)
12554 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12555# endif
12556#endif
12557#ifdef FEAT_SYN_HL
12558 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012559 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560#endif
12561#if defined(WIN3264)
12562 else if (STRICMP(name, "win95") == 0)
12563 n = mch_windows95();
12564#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012565#ifdef FEAT_NETBEANS_INTG
12566 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012567 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 }
12570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573
12574/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012575 * "has_key()" function
12576 */
12577 static void
12578f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012579 typval_T *argvars;
12580 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012581{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012582 if (argvars[0].v_type != VAR_DICT)
12583 {
12584 EMSG(_(e_dictreq));
12585 return;
12586 }
12587 if (argvars[0].vval.v_dict == NULL)
12588 return;
12589
12590 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012591 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012592}
12593
12594/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012595 * "haslocaldir()" function
12596 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012597 static void
12598f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012599 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012600 typval_T *rettv;
12601{
12602 rettv->vval.v_number = (curwin->w_localdir != NULL);
12603}
12604
12605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 * "hasmapto()" function
12607 */
12608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012610 typval_T *argvars;
12611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612{
12613 char_u *name;
12614 char_u *mode;
12615 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012616 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012618 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012619 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 mode = (char_u *)"nvo";
12621 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012624 if (argvars[2].v_type != VAR_UNKNOWN)
12625 abbr = get_tv_number(&argvars[2]);
12626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627
Bram Moolenaar2c932302006-03-18 21:42:09 +000012628 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632}
12633
12634/*
12635 * "histadd()" function
12636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641{
12642#ifdef FEAT_CMDHIST
12643 int histype;
12644 char_u *str;
12645 char_u buf[NUMBUFLEN];
12646#endif
12647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 if (check_restricted() || check_secure())
12650 return;
12651#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012652 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12653 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 if (histype >= 0)
12655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657 if (*str != NUL)
12658 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012659 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 return;
12663 }
12664 }
12665#endif
12666}
12667
12668/*
12669 * "histdel()" function
12670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012672f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012673 typval_T *argvars UNUSED;
12674 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675{
12676#ifdef FEAT_CMDHIST
12677 int n;
12678 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012681 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12682 if (str == NULL)
12683 n = 0;
12684 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012687 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 else
12692 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 get_tv_string_buf(&argvars[1], buf));
12695 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696#endif
12697}
12698
12699/*
12700 * "histget()" function
12701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012704 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706{
12707#ifdef FEAT_CMDHIST
12708 int type;
12709 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012710 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012712 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12713 if (str == NULL)
12714 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716 {
12717 type = get_histtype(str);
12718 if (argvars[1].v_type == VAR_UNKNOWN)
12719 idx = get_history_idx(type);
12720 else
12721 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12722 /* -1 on type error */
12723 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729}
12730
12731/*
12732 * "histnr()" function
12733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738{
12739 int i;
12740
12741#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 char_u *history = get_tv_string_chk(&argvars[0]);
12743
12744 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 if (i >= HIST_CMD && i < HIST_COUNT)
12746 i = get_history_idx(i);
12747 else
12748#endif
12749 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751}
12752
12753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 * "highlightID(name)" function
12755 */
12756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012758 typval_T *argvars;
12759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762}
12763
12764/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012765 * "highlight_exists()" function
12766 */
12767 static void
12768f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012769 typval_T *argvars;
12770 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012771{
12772 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12773}
12774
12775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 * "hostname()" function
12777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782{
12783 char_u hostname[256];
12784
12785 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->v_type = VAR_STRING;
12787 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788}
12789
12790/*
12791 * iconv() function
12792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797{
12798#ifdef FEAT_MBYTE
12799 char_u buf1[NUMBUFLEN];
12800 char_u buf2[NUMBUFLEN];
12801 char_u *from, *to, *str;
12802 vimconv_T vimconv;
12803#endif
12804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 rettv->v_type = VAR_STRING;
12806 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807
12808#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 str = get_tv_string(&argvars[0]);
12810 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12811 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 vimconv.vc_type = CONV_NONE;
12813 convert_setup(&vimconv, from, to);
12814
12815 /* If the encodings are equal, no conversion needed. */
12816 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820
12821 convert_setup(&vimconv, NULL, NULL);
12822 vim_free(from);
12823 vim_free(to);
12824#endif
12825}
12826
12827/*
12828 * "indent()" function
12829 */
12830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *argvars;
12833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834{
12835 linenr_T lnum;
12836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842}
12843
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012844/*
12845 * "index()" function
12846 */
12847 static void
12848f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012851{
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 list_T *l;
12853 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012854 long idx = 0;
12855 int ic = FALSE;
12856
12857 rettv->vval.v_number = -1;
12858 if (argvars[0].v_type != VAR_LIST)
12859 {
12860 EMSG(_(e_listreq));
12861 return;
12862 }
12863 l = argvars[0].vval.v_list;
12864 if (l != NULL)
12865 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012866 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869 int error = FALSE;
12870
Bram Moolenaar758711c2005-02-02 23:11:38 +000012871 /* Start at specified item. Use the cached index that list_find()
12872 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012874 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012875 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012876 ic = get_tv_number_chk(&argvars[3], &error);
12877 if (error)
12878 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012879 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012880
Bram Moolenaar758711c2005-02-02 23:11:38 +000012881 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012882 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012883 {
12884 rettv->vval.v_number = idx;
12885 break;
12886 }
12887 }
12888}
12889
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890static int inputsecret_flag = 0;
12891
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012892static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12893
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012895 * This function is used by f_input() and f_inputdialog() functions. The third
12896 * argument to f_input() specifies the type of completion to use at the
12897 * prompt. The third argument to f_inputdialog() specifies the value to return
12898 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 */
12900 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012901get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012902 typval_T *argvars;
12903 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012904 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012906 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 char_u *p = NULL;
12908 int c;
12909 char_u buf[NUMBUFLEN];
12910 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012911 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012912 int xp_type = EXPAND_NOTHING;
12913 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012916 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917
12918#ifdef NO_CONSOLE_INPUT
12919 /* While starting up, there is no place to enter text. */
12920 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#endif
12923
12924 cmd_silent = FALSE; /* Want to see the prompt. */
12925 if (prompt != NULL)
12926 {
12927 /* Only the part of the message after the last NL is considered as
12928 * prompt for the command line */
12929 p = vim_strrchr(prompt, '\n');
12930 if (p == NULL)
12931 p = prompt;
12932 else
12933 {
12934 ++p;
12935 c = *p;
12936 *p = NUL;
12937 msg_start();
12938 msg_clr_eos();
12939 msg_puts_attr(prompt, echo_attr);
12940 msg_didout = FALSE;
12941 msg_starthere();
12942 *p = c;
12943 }
12944 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012946 if (argvars[1].v_type != VAR_UNKNOWN)
12947 {
12948 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12949 if (defstr != NULL)
12950 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012952 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012953 {
12954 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012955 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012956 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012957
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012958 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012959 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012960
Bram Moolenaar4463f292005-09-25 22:20:24 +000012961 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12962 if (xp_name == NULL)
12963 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012964
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012965 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012966
Bram Moolenaar4463f292005-09-25 22:20:24 +000012967 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12968 &xp_arg) == FAIL)
12969 return;
12970 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012971 }
12972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012973 if (defstr != NULL)
12974 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012975 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12976 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020012977 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012978 && argvars[1].v_type != VAR_UNKNOWN
12979 && argvars[2].v_type != VAR_UNKNOWN)
12980 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
12981 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012982
12983 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 /* since the user typed this, no need to wait for return */
12986 need_wait_return = FALSE;
12987 msg_didout = FALSE;
12988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 cmd_silent = cmd_silent_save;
12990}
12991
12992/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012993 * "input()" function
12994 * Also handles inputsecret() when inputsecret is set.
12995 */
12996 static void
12997f_input(argvars, rettv)
12998 typval_T *argvars;
12999 typval_T *rettv;
13000{
13001 get_user_input(argvars, rettv, FALSE);
13002}
13003
13004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 * "inputdialog()" function
13006 */
13007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013008f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013009 typval_T *argvars;
13010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011{
13012#if defined(FEAT_GUI_TEXTDIALOG)
13013 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13014 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13015 {
13016 char_u *message;
13017 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 message = get_tv_string_chk(&argvars[0]);
13021 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013022 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013023 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 else
13025 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 if (message != NULL && defstr != NULL
13027 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013028 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013029 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 else
13031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 if (message != NULL && defstr != NULL
13033 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013034 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->vval.v_string = vim_strsave(
13036 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 }
13042 else
13043#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013044 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045}
13046
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013047/*
13048 * "inputlist()" function
13049 */
13050 static void
13051f_inputlist(argvars, rettv)
13052 typval_T *argvars;
13053 typval_T *rettv;
13054{
13055 listitem_T *li;
13056 int selected;
13057 int mouse_used;
13058
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013059#ifdef NO_CONSOLE_INPUT
13060 /* While starting up, there is no place to enter text. */
13061 if (no_console_input())
13062 return;
13063#endif
13064 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13065 {
13066 EMSG2(_(e_listarg), "inputlist()");
13067 return;
13068 }
13069
13070 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013071 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013072 lines_left = Rows; /* avoid more prompt */
13073 msg_scroll = TRUE;
13074 msg_clr_eos();
13075
13076 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13077 {
13078 msg_puts(get_tv_string(&li->li_tv));
13079 msg_putchar('\n');
13080 }
13081
13082 /* Ask for choice. */
13083 selected = prompt_for_number(&mouse_used);
13084 if (mouse_used)
13085 selected -= lines_left;
13086
13087 rettv->vval.v_number = selected;
13088}
13089
13090
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13092
13093/*
13094 * "inputrestore()" function
13095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100{
13101 if (ga_userinput.ga_len > 0)
13102 {
13103 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13105 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013106 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 }
13108 else if (p_verbose > 1)
13109 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013110 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 }
13113}
13114
13115/*
13116 * "inputsave()" function
13117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013120 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013123 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 if (ga_grow(&ga_userinput, 1) == OK)
13125 {
13126 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13127 + ga_userinput.ga_len);
13128 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013129 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 }
13131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133}
13134
13135/*
13136 * "inputsecret()" function
13137 */
13138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013140 typval_T *argvars;
13141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142{
13143 ++cmdline_star;
13144 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 --cmdline_star;
13147 --inputsecret_flag;
13148}
13149
13150/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013151 * "insert()" function
13152 */
13153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013155 typval_T *argvars;
13156 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157{
13158 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 listitem_T *item;
13160 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013162
13163 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013164 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013165 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013166 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 {
13168 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013169 before = get_tv_number_chk(&argvars[2], &error);
13170 if (error)
13171 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013172
Bram Moolenaar758711c2005-02-02 23:11:38 +000013173 if (before == l->lv_len)
13174 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013175 else
13176 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013177 item = list_find(l, before);
13178 if (item == NULL)
13179 {
13180 EMSGN(_(e_listidx), before);
13181 l = NULL;
13182 }
13183 }
13184 if (l != NULL)
13185 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013186 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013187 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013188 }
13189 }
13190}
13191
13192/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013193 * "invert(expr)" function
13194 */
13195 static void
13196f_invert(argvars, rettv)
13197 typval_T *argvars;
13198 typval_T *rettv;
13199{
13200 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13201}
13202
13203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 * "isdirectory()" function
13205 */
13206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013208 typval_T *argvars;
13209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212}
13213
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013214/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013215 * "islocked()" function
13216 */
13217 static void
13218f_islocked(argvars, rettv)
13219 typval_T *argvars;
13220 typval_T *rettv;
13221{
13222 lval_T lv;
13223 char_u *end;
13224 dictitem_T *di;
13225
13226 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013227 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13228 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013229 if (end != NULL && lv.ll_name != NULL)
13230 {
13231 if (*end != NUL)
13232 EMSG(_(e_trailing));
13233 else
13234 {
13235 if (lv.ll_tv == NULL)
13236 {
13237 if (check_changedtick(lv.ll_name))
13238 rettv->vval.v_number = 1; /* always locked */
13239 else
13240 {
13241 di = find_var(lv.ll_name, NULL);
13242 if (di != NULL)
13243 {
13244 /* Consider a variable locked when:
13245 * 1. the variable itself is locked
13246 * 2. the value of the variable is locked.
13247 * 3. the List or Dict value is locked.
13248 */
13249 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13250 || tv_islocked(&di->di_tv));
13251 }
13252 }
13253 }
13254 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013255 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013256 else if (lv.ll_newkey != NULL)
13257 EMSG2(_(e_dictkey), lv.ll_newkey);
13258 else if (lv.ll_list != NULL)
13259 /* List item. */
13260 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13261 else
13262 /* Dictionary item. */
13263 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13264 }
13265 }
13266
13267 clear_lval(&lv);
13268}
13269
Bram Moolenaar33570922005-01-25 22:26:29 +000013270static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013271
13272/*
13273 * Turn a dict into a list:
13274 * "what" == 0: list of keys
13275 * "what" == 1: list of values
13276 * "what" == 2: list of items
13277 */
13278 static void
13279dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 typval_T *argvars;
13281 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013282 int what;
13283{
Bram Moolenaar33570922005-01-25 22:26:29 +000013284 list_T *l2;
13285 dictitem_T *di;
13286 hashitem_T *hi;
13287 listitem_T *li;
13288 listitem_T *li2;
13289 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013290 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013291
Bram Moolenaar8c711452005-01-14 21:53:12 +000013292 if (argvars[0].v_type != VAR_DICT)
13293 {
13294 EMSG(_(e_dictreq));
13295 return;
13296 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013297 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013298 return;
13299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013301 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013303 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013306 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013308 --todo;
13309 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013310
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 li = listitem_alloc();
13312 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013313 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013314 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013315
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013316 if (what == 0)
13317 {
13318 /* keys() */
13319 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013320 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013321 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13322 }
13323 else if (what == 1)
13324 {
13325 /* values() */
13326 copy_tv(&di->di_tv, &li->li_tv);
13327 }
13328 else
13329 {
13330 /* items() */
13331 l2 = list_alloc();
13332 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013333 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 li->li_tv.vval.v_list = l2;
13335 if (l2 == NULL)
13336 break;
13337 ++l2->lv_refcount;
13338
13339 li2 = listitem_alloc();
13340 if (li2 == NULL)
13341 break;
13342 list_append(l2, li2);
13343 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013344 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013345 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13346
13347 li2 = listitem_alloc();
13348 if (li2 == NULL)
13349 break;
13350 list_append(l2, li2);
13351 copy_tv(&di->di_tv, &li2->li_tv);
13352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353 }
13354 }
13355}
13356
13357/*
13358 * "items(dict)" function
13359 */
13360 static void
13361f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364{
13365 dict_list(argvars, rettv, 2);
13366}
13367
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013369 * "join()" function
13370 */
13371 static void
13372f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 typval_T *argvars;
13374 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013375{
13376 garray_T ga;
13377 char_u *sep;
13378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013379 if (argvars[0].v_type != VAR_LIST)
13380 {
13381 EMSG(_(e_listreq));
13382 return;
13383 }
13384 if (argvars[0].vval.v_list == NULL)
13385 return;
13386 if (argvars[1].v_type == VAR_UNKNOWN)
13387 sep = (char_u *)" ";
13388 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013390
13391 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392
13393 if (sep != NULL)
13394 {
13395 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013396 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397 ga_append(&ga, NUL);
13398 rettv->vval.v_string = (char_u *)ga.ga_data;
13399 }
13400 else
13401 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013402}
13403
13404/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013405 * "keys()" function
13406 */
13407 static void
13408f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013411{
13412 dict_list(argvars, rettv, 0);
13413}
13414
13415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 * "last_buffer_nr()" function.
13417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013420 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422{
13423 int n = 0;
13424 buf_T *buf;
13425
13426 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13427 if (n < buf->b_fnum)
13428 n = buf->b_fnum;
13429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013431}
13432
13433/*
13434 * "len()" function
13435 */
13436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013437f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013438 typval_T *argvars;
13439 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013440{
13441 switch (argvars[0].v_type)
13442 {
13443 case VAR_STRING:
13444 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445 rettv->vval.v_number = (varnumber_T)STRLEN(
13446 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013447 break;
13448 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013450 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013451 case VAR_DICT:
13452 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13453 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013454 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013455 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013456 break;
13457 }
13458}
13459
Bram Moolenaar33570922005-01-25 22:26:29 +000013460static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013461
13462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 typval_T *argvars;
13465 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013466 int type;
13467{
13468#ifdef FEAT_LIBCALL
13469 char_u *string_in;
13470 char_u **string_result;
13471 int nr_result;
13472#endif
13473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013475 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013477
13478 if (check_restricted() || check_secure())
13479 return;
13480
13481#ifdef FEAT_LIBCALL
13482 /* The first two args must be strings, otherwise its meaningless */
13483 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13484 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013485 string_in = NULL;
13486 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013487 string_in = argvars[2].vval.v_string;
13488 if (type == VAR_NUMBER)
13489 string_result = NULL;
13490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 if (mch_libcall(argvars[0].vval.v_string,
13493 argvars[1].vval.v_string,
13494 string_in,
13495 argvars[2].vval.v_number,
13496 string_result,
13497 &nr_result) == OK
13498 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013500 }
13501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502}
13503
13504/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013505 * "libcall()" function
13506 */
13507 static void
13508f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 typval_T *argvars;
13510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013511{
13512 libcall_common(argvars, rettv, VAR_STRING);
13513}
13514
13515/*
13516 * "libcallnr()" function
13517 */
13518 static void
13519f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *argvars;
13521 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013522{
13523 libcall_common(argvars, rettv, VAR_NUMBER);
13524}
13525
13526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 * "line(string)" function
13528 */
13529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013531 typval_T *argvars;
13532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533{
13534 linenr_T lnum = 0;
13535 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013536 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013538 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 if (fp != NULL)
13540 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013541 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542}
13543
13544/*
13545 * "line2byte(lnum)" function
13546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551{
13552#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554#else
13555 linenr_T lnum;
13556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13562 if (rettv->vval.v_number >= 0)
13563 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564#endif
13565}
13566
13567/*
13568 * "lispindent(lnum)" function
13569 */
13570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 typval_T *argvars;
13573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574{
13575#ifdef FEAT_LISP
13576 pos_T pos;
13577 linenr_T lnum;
13578
13579 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13582 {
13583 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 curwin->w_cursor = pos;
13586 }
13587 else
13588#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590}
13591
13592/*
13593 * "localtime()" function
13594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013597 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601}
13602
Bram Moolenaar33570922005-01-25 22:26:29 +000013603static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604
13605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013607 typval_T *argvars;
13608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 int exact;
13610{
13611 char_u *keys;
13612 char_u *which;
13613 char_u buf[NUMBUFLEN];
13614 char_u *keys_buf = NULL;
13615 char_u *rhs;
13616 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013617 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013618 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013619 mapblock_T *mp;
13620 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621
13622 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->v_type = VAR_STRING;
13624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 if (*keys == NUL)
13628 return;
13629
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013630 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013634 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013635 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013636 if (argvars[3].v_type != VAR_UNKNOWN)
13637 get_dict = get_tv_number(&argvars[3]);
13638 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 else
13641 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 if (which == NULL)
13643 return;
13644
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 mode = get_map_mode(&which, 0);
13646
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013647 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013648 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013650
13651 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013653 /* Return a string. */
13654 if (rhs != NULL)
13655 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaarbd743252010-10-20 21:23:33 +020013657 }
13658 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13659 {
13660 /* Return a dictionary. */
13661 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13662 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13663 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664
Bram Moolenaarbd743252010-10-20 21:23:33 +020013665 dict_add_nr_str(dict, "lhs", 0L, lhs);
13666 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13667 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13668 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13669 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13670 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13671 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13672 dict_add_nr_str(dict, "mode", 0L, mapmode);
13673
13674 vim_free(lhs);
13675 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 }
13677}
13678
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013679#ifdef FEAT_FLOAT
13680/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013681 * "log()" function
13682 */
13683 static void
13684f_log(argvars, rettv)
13685 typval_T *argvars;
13686 typval_T *rettv;
13687{
13688 float_T f;
13689
13690 rettv->v_type = VAR_FLOAT;
13691 if (get_float_arg(argvars, &f) == OK)
13692 rettv->vval.v_float = log(f);
13693 else
13694 rettv->vval.v_float = 0.0;
13695}
13696
13697/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013698 * "log10()" function
13699 */
13700 static void
13701f_log10(argvars, rettv)
13702 typval_T *argvars;
13703 typval_T *rettv;
13704{
13705 float_T f;
13706
13707 rettv->v_type = VAR_FLOAT;
13708 if (get_float_arg(argvars, &f) == OK)
13709 rettv->vval.v_float = log10(f);
13710 else
13711 rettv->vval.v_float = 0.0;
13712}
13713#endif
13714
Bram Moolenaar1dced572012-04-05 16:54:08 +020013715#ifdef FEAT_LUA
13716/*
13717 * "luaeval()" function
13718 */
13719 static void
13720f_luaeval(argvars, rettv)
13721 typval_T *argvars;
13722 typval_T *rettv;
13723{
13724 char_u *str;
13725 char_u buf[NUMBUFLEN];
13726
13727 str = get_tv_string_buf(&argvars[0], buf);
13728 do_luaeval(str, argvars + 1, rettv);
13729}
13730#endif
13731
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013733 * "map()" function
13734 */
13735 static void
13736f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013737 typval_T *argvars;
13738 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013739{
13740 filter_map(argvars, rettv, TRUE);
13741}
13742
13743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 */
13746 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752}
13753
13754/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 */
13757 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *argvars;
13760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013762 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763}
13764
Bram Moolenaar33570922005-01-25 22:26:29 +000013765static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766
13767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 typval_T *argvars;
13770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 int type;
13772{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013773 char_u *str = NULL;
13774 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 char_u *pat;
13776 regmatch_T regmatch;
13777 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013778 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 char_u *save_cpo;
13780 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013781 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013782 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013783 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 list_T *l = NULL;
13785 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013786 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013787 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788
13789 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13790 save_cpo = p_cpo;
13791 p_cpo = (char_u *)"";
13792
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013793 rettv->vval.v_number = -1;
13794 if (type == 3)
13795 {
13796 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013797 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013798 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013799 }
13800 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013802 rettv->v_type = VAR_STRING;
13803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013806 if (argvars[0].v_type == VAR_LIST)
13807 {
13808 if ((l = argvars[0].vval.v_list) == NULL)
13809 goto theend;
13810 li = l->lv_first;
13811 }
13812 else
13813 expr = str = get_tv_string(&argvars[0]);
13814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13816 if (pat == NULL)
13817 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013818
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 int error = FALSE;
13822
13823 start = get_tv_number_chk(&argvars[2], &error);
13824 if (error)
13825 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 if (l != NULL)
13827 {
13828 li = list_find(l, start);
13829 if (li == NULL)
13830 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013831 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013832 }
13833 else
13834 {
13835 if (start < 0)
13836 start = 0;
13837 if (start > (long)STRLEN(str))
13838 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013839 /* When "count" argument is there ignore matches before "start",
13840 * otherwise skip part of the string. Differs when pattern is "^"
13841 * or "\<". */
13842 if (argvars[3].v_type != VAR_UNKNOWN)
13843 startcol = start;
13844 else
13845 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013847
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013848 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 nth = get_tv_number_chk(&argvars[3], &error);
13850 if (error)
13851 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 }
13853
13854 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13855 if (regmatch.regprog != NULL)
13856 {
13857 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013858
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013859 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013860 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013861 if (l != NULL)
13862 {
13863 if (li == NULL)
13864 {
13865 match = FALSE;
13866 break;
13867 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013868 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013869 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013870 if (str == NULL)
13871 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013872 }
13873
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013874 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013875
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013877 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013878 if (l == NULL && !match)
13879 break;
13880
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013881 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013882 if (l != NULL)
13883 {
13884 li = li->li_next;
13885 ++idx;
13886 }
13887 else
13888 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013889#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013890 startcol = (colnr_T)(regmatch.startp[0]
13891 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013892#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013893 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013894#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013895 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013896 }
13897
13898 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013900 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013902 int i;
13903
13904 /* return list with matched string and submatches */
13905 for (i = 0; i < NSUBEXP; ++i)
13906 {
13907 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013908 {
13909 if (list_append_string(rettv->vval.v_list,
13910 (char_u *)"", 0) == FAIL)
13911 break;
13912 }
13913 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013914 regmatch.startp[i],
13915 (int)(regmatch.endp[i] - regmatch.startp[i]))
13916 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013917 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013918 }
13919 }
13920 else if (type == 2)
13921 {
13922 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 if (l != NULL)
13924 copy_tv(&li->li_tv, rettv);
13925 else
13926 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928 }
13929 else if (l != NULL)
13930 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 else
13932 {
13933 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 (varnumber_T)(regmatch.startp[0] - str);
13936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013939 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 }
13941 }
13942 vim_free(regmatch.regprog);
13943 }
13944
13945theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013946 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 p_cpo = save_cpo;
13948}
13949
13950/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013951 * "match()" function
13952 */
13953 static void
13954f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 typval_T *argvars;
13956 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957{
13958 find_some_match(argvars, rettv, 1);
13959}
13960
13961/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013962 * "matchadd()" function
13963 */
13964 static void
13965f_matchadd(argvars, rettv)
13966 typval_T *argvars;
13967 typval_T *rettv;
13968{
13969#ifdef FEAT_SEARCH_EXTRA
13970 char_u buf[NUMBUFLEN];
13971 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13972 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13973 int prio = 10; /* default priority */
13974 int id = -1;
13975 int error = FALSE;
13976
13977 rettv->vval.v_number = -1;
13978
13979 if (grp == NULL || pat == NULL)
13980 return;
13981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013982 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013983 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013984 if (argvars[3].v_type != VAR_UNKNOWN)
13985 id = get_tv_number_chk(&argvars[3], &error);
13986 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013987 if (error == TRUE)
13988 return;
13989 if (id >= 1 && id <= 3)
13990 {
13991 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13992 return;
13993 }
13994
13995 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13996#endif
13997}
13998
13999/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014000 * "matcharg()" function
14001 */
14002 static void
14003f_matcharg(argvars, rettv)
14004 typval_T *argvars;
14005 typval_T *rettv;
14006{
14007 if (rettv_list_alloc(rettv) == OK)
14008 {
14009#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014010 int id = get_tv_number(&argvars[0]);
14011 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014012
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014013 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014014 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014015 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14016 {
14017 list_append_string(rettv->vval.v_list,
14018 syn_id2name(m->hlg_id), -1);
14019 list_append_string(rettv->vval.v_list, m->pattern, -1);
14020 }
14021 else
14022 {
14023 list_append_string(rettv->vval.v_list, NUL, -1);
14024 list_append_string(rettv->vval.v_list, NUL, -1);
14025 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014026 }
14027#endif
14028 }
14029}
14030
14031/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014032 * "matchdelete()" function
14033 */
14034 static void
14035f_matchdelete(argvars, rettv)
14036 typval_T *argvars;
14037 typval_T *rettv;
14038{
14039#ifdef FEAT_SEARCH_EXTRA
14040 rettv->vval.v_number = match_delete(curwin,
14041 (int)get_tv_number(&argvars[0]), TRUE);
14042#endif
14043}
14044
14045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014046 * "matchend()" function
14047 */
14048 static void
14049f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014050 typval_T *argvars;
14051 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052{
14053 find_some_match(argvars, rettv, 0);
14054}
14055
14056/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014057 * "matchlist()" function
14058 */
14059 static void
14060f_matchlist(argvars, rettv)
14061 typval_T *argvars;
14062 typval_T *rettv;
14063{
14064 find_some_match(argvars, rettv, 3);
14065}
14066
14067/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014068 * "matchstr()" function
14069 */
14070 static void
14071f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014072 typval_T *argvars;
14073 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074{
14075 find_some_match(argvars, rettv, 2);
14076}
14077
Bram Moolenaar33570922005-01-25 22:26:29 +000014078static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014079
14080 static void
14081max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014082 typval_T *argvars;
14083 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014084 int domax;
14085{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014086 long n = 0;
14087 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014088 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014089
14090 if (argvars[0].v_type == VAR_LIST)
14091 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 list_T *l;
14093 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014094
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014095 l = argvars[0].vval.v_list;
14096 if (l != NULL)
14097 {
14098 li = l->lv_first;
14099 if (li != NULL)
14100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014102 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014103 {
14104 li = li->li_next;
14105 if (li == NULL)
14106 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014107 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014108 if (domax ? i > n : i < n)
14109 n = i;
14110 }
14111 }
14112 }
14113 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014114 else if (argvars[0].v_type == VAR_DICT)
14115 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014117 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014118 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014119 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014120
14121 d = argvars[0].vval.v_dict;
14122 if (d != NULL)
14123 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014124 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014126 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014127 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014128 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014129 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 if (first)
14132 {
14133 n = i;
14134 first = FALSE;
14135 }
14136 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014137 n = i;
14138 }
14139 }
14140 }
14141 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014142 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014143 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014145}
14146
14147/*
14148 * "max()" function
14149 */
14150 static void
14151f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *argvars;
14153 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014154{
14155 max_min(argvars, rettv, TRUE);
14156}
14157
14158/*
14159 * "min()" function
14160 */
14161 static void
14162f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 typval_T *argvars;
14164 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014165{
14166 max_min(argvars, rettv, FALSE);
14167}
14168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014169static int mkdir_recurse __ARGS((char_u *dir, int prot));
14170
14171/*
14172 * Create the directory in which "dir" is located, and higher levels when
14173 * needed.
14174 */
14175 static int
14176mkdir_recurse(dir, prot)
14177 char_u *dir;
14178 int prot;
14179{
14180 char_u *p;
14181 char_u *updir;
14182 int r = FAIL;
14183
14184 /* Get end of directory name in "dir".
14185 * We're done when it's "/" or "c:/". */
14186 p = gettail_sep(dir);
14187 if (p <= get_past_head(dir))
14188 return OK;
14189
14190 /* If the directory exists we're done. Otherwise: create it.*/
14191 updir = vim_strnsave(dir, (int)(p - dir));
14192 if (updir == NULL)
14193 return FAIL;
14194 if (mch_isdir(updir))
14195 r = OK;
14196 else if (mkdir_recurse(updir, prot) == OK)
14197 r = vim_mkdir_emsg(updir, prot);
14198 vim_free(updir);
14199 return r;
14200}
14201
14202#ifdef vim_mkdir
14203/*
14204 * "mkdir()" function
14205 */
14206 static void
14207f_mkdir(argvars, rettv)
14208 typval_T *argvars;
14209 typval_T *rettv;
14210{
14211 char_u *dir;
14212 char_u buf[NUMBUFLEN];
14213 int prot = 0755;
14214
14215 rettv->vval.v_number = FAIL;
14216 if (check_restricted() || check_secure())
14217 return;
14218
14219 dir = get_tv_string_buf(&argvars[0], buf);
14220 if (argvars[1].v_type != VAR_UNKNOWN)
14221 {
14222 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 prot = get_tv_number_chk(&argvars[2], NULL);
14224 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014225 mkdir_recurse(dir, prot);
14226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014228}
14229#endif
14230
Bram Moolenaar0d660222005-01-07 21:51:51 +000014231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 * "mode()" function
14233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *argvars;
14237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014239 char_u buf[3];
14240
14241 buf[1] = NUL;
14242 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243
14244#ifdef FEAT_VISUAL
14245 if (VIsual_active)
14246 {
14247 if (VIsual_select)
14248 buf[0] = VIsual_mode + 's' - 'v';
14249 else
14250 buf[0] = VIsual_mode;
14251 }
14252 else
14253#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014254 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14255 || State == CONFIRM)
14256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014258 if (State == ASKMORE)
14259 buf[1] = 'm';
14260 else if (State == CONFIRM)
14261 buf[1] = '?';
14262 }
14263 else if (State == EXTERNCMD)
14264 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 else if (State & INSERT)
14266 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014267#ifdef FEAT_VREPLACE
14268 if (State & VREPLACE_FLAG)
14269 {
14270 buf[0] = 'R';
14271 buf[1] = 'v';
14272 }
14273 else
14274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 if (State & REPLACE_FLAG)
14276 buf[0] = 'R';
14277 else
14278 buf[0] = 'i';
14279 }
14280 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014283 if (exmode_active)
14284 buf[1] = 'v';
14285 }
14286 else if (exmode_active)
14287 {
14288 buf[0] = 'c';
14289 buf[1] = 'e';
14290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014294 if (finish_op)
14295 buf[1] = 'o';
14296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014298 /* Clear out the minor mode when the argument is not a non-zero number or
14299 * non-empty string. */
14300 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014301 buf[1] = NUL;
14302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014303 rettv->vval.v_string = vim_strsave(buf);
14304 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305}
14306
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014307#ifdef FEAT_MZSCHEME
14308/*
14309 * "mzeval()" function
14310 */
14311 static void
14312f_mzeval(argvars, rettv)
14313 typval_T *argvars;
14314 typval_T *rettv;
14315{
14316 char_u *str;
14317 char_u buf[NUMBUFLEN];
14318
14319 str = get_tv_string_buf(&argvars[0], buf);
14320 do_mzeval(str, rettv);
14321}
14322#endif
14323
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014325 * "nextnonblank()" function
14326 */
14327 static void
14328f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331{
14332 linenr_T lnum;
14333
14334 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014337 {
14338 lnum = 0;
14339 break;
14340 }
14341 if (*skipwhite(ml_get(lnum)) != NUL)
14342 break;
14343 }
14344 rettv->vval.v_number = lnum;
14345}
14346
14347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 * "nr2char()" function
14349 */
14350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 typval_T *argvars;
14353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354{
14355 char_u buf[NUMBUFLEN];
14356
14357#ifdef FEAT_MBYTE
14358 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 else
14361#endif
14362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 buf[1] = NUL;
14365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->v_type = VAR_STRING;
14367 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368}
14369
14370/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014371 * "or(expr, expr)" function
14372 */
14373 static void
14374f_or(argvars, rettv)
14375 typval_T *argvars;
14376 typval_T *rettv;
14377{
14378 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14379 | get_tv_number_chk(&argvars[1], NULL);
14380}
14381
14382/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014383 * "pathshorten()" function
14384 */
14385 static void
14386f_pathshorten(argvars, rettv)
14387 typval_T *argvars;
14388 typval_T *rettv;
14389{
14390 char_u *p;
14391
14392 rettv->v_type = VAR_STRING;
14393 p = get_tv_string_chk(&argvars[0]);
14394 if (p == NULL)
14395 rettv->vval.v_string = NULL;
14396 else
14397 {
14398 p = vim_strsave(p);
14399 rettv->vval.v_string = p;
14400 if (p != NULL)
14401 shorten_dir(p);
14402 }
14403}
14404
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014405#ifdef FEAT_FLOAT
14406/*
14407 * "pow()" function
14408 */
14409 static void
14410f_pow(argvars, rettv)
14411 typval_T *argvars;
14412 typval_T *rettv;
14413{
14414 float_T fx, fy;
14415
14416 rettv->v_type = VAR_FLOAT;
14417 if (get_float_arg(argvars, &fx) == OK
14418 && get_float_arg(&argvars[1], &fy) == OK)
14419 rettv->vval.v_float = pow(fx, fy);
14420 else
14421 rettv->vval.v_float = 0.0;
14422}
14423#endif
14424
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014425/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 * "prevnonblank()" function
14427 */
14428 static void
14429f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 typval_T *argvars;
14431 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432{
14433 linenr_T lnum;
14434
14435 lnum = get_tv_lnum(argvars);
14436 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14437 lnum = 0;
14438 else
14439 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14440 --lnum;
14441 rettv->vval.v_number = lnum;
14442}
14443
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014444#ifdef HAVE_STDARG_H
14445/* This dummy va_list is here because:
14446 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14447 * - locally in the function results in a "used before set" warning
14448 * - using va_start() to initialize it gives "function with fixed args" error */
14449static va_list ap;
14450#endif
14451
Bram Moolenaar8c711452005-01-14 21:53:12 +000014452/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014453 * "printf()" function
14454 */
14455 static void
14456f_printf(argvars, rettv)
14457 typval_T *argvars;
14458 typval_T *rettv;
14459{
14460 rettv->v_type = VAR_STRING;
14461 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014462#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014463 {
14464 char_u buf[NUMBUFLEN];
14465 int len;
14466 char_u *s;
14467 int saved_did_emsg = did_emsg;
14468 char *fmt;
14469
14470 /* Get the required length, allocate the buffer and do it for real. */
14471 did_emsg = FALSE;
14472 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014473 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014474 if (!did_emsg)
14475 {
14476 s = alloc(len + 1);
14477 if (s != NULL)
14478 {
14479 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014480 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014481 }
14482 }
14483 did_emsg |= saved_did_emsg;
14484 }
14485#endif
14486}
14487
14488/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014489 * "pumvisible()" function
14490 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014491 static void
14492f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014493 typval_T *argvars UNUSED;
14494 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014495{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014496#ifdef FEAT_INS_EXPAND
14497 if (pum_visible())
14498 rettv->vval.v_number = 1;
14499#endif
14500}
14501
Bram Moolenaardb913952012-06-29 12:54:53 +020014502#ifdef FEAT_PYTHON3
14503/*
14504 * "py3eval()" function
14505 */
14506 static void
14507f_py3eval(argvars, rettv)
14508 typval_T *argvars;
14509 typval_T *rettv;
14510{
14511 char_u *str;
14512 char_u buf[NUMBUFLEN];
14513
14514 str = get_tv_string_buf(&argvars[0], buf);
14515 do_py3eval(str, rettv);
14516}
14517#endif
14518
14519#ifdef FEAT_PYTHON
14520/*
14521 * "pyeval()" function
14522 */
14523 static void
14524f_pyeval(argvars, rettv)
14525 typval_T *argvars;
14526 typval_T *rettv;
14527{
14528 char_u *str;
14529 char_u buf[NUMBUFLEN];
14530
14531 str = get_tv_string_buf(&argvars[0], buf);
14532 do_pyeval(str, rettv);
14533}
14534#endif
14535
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014537 * "range()" function
14538 */
14539 static void
14540f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 typval_T *argvars;
14542 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543{
14544 long start;
14545 long end;
14546 long stride = 1;
14547 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014551 if (argvars[1].v_type == VAR_UNKNOWN)
14552 {
14553 end = start - 1;
14554 start = 0;
14555 }
14556 else
14557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014561 }
14562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 if (error)
14564 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014565 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014566 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014567 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014568 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014569 else
14570 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014571 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014572 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014573 if (list_append_number(rettv->vval.v_list,
14574 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014576 }
14577}
14578
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014579/*
14580 * "readfile()" function
14581 */
14582 static void
14583f_readfile(argvars, rettv)
14584 typval_T *argvars;
14585 typval_T *rettv;
14586{
14587 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014588 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014589 char_u *fname;
14590 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014591 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14592 int io_size = sizeof(buf);
14593 int readlen; /* size of last fread() */
14594 char_u *prev = NULL; /* previously read bytes, if any */
14595 long prevlen = 0; /* length of data in prev */
14596 long prevsize = 0; /* size of prev buffer */
14597 long maxline = MAXLNUM;
14598 long cnt = 0;
14599 char_u *p; /* position in buf */
14600 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014601
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014602 if (argvars[1].v_type != VAR_UNKNOWN)
14603 {
14604 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14605 binary = TRUE;
14606 if (argvars[2].v_type != VAR_UNKNOWN)
14607 maxline = get_tv_number(&argvars[2]);
14608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014609
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014610 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014611 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014612
14613 /* Always open the file in binary mode, library functions have a mind of
14614 * their own about CR-LF conversion. */
14615 fname = get_tv_string(&argvars[0]);
14616 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14617 {
14618 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14619 return;
14620 }
14621
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014622 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014623 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014624 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014625
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014626 /* This for loop processes what was read, but is also entered at end
14627 * of file so that either:
14628 * - an incomplete line gets written
14629 * - a "binary" file gets an empty line at the end if it ends in a
14630 * newline. */
14631 for (p = buf, start = buf;
14632 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14633 ++p)
14634 {
14635 if (*p == '\n' || readlen <= 0)
14636 {
14637 listitem_T *li;
14638 char_u *s = NULL;
14639 long_u len = p - start;
14640
14641 /* Finished a line. Remove CRs before NL. */
14642 if (readlen > 0 && !binary)
14643 {
14644 while (len > 0 && start[len - 1] == '\r')
14645 --len;
14646 /* removal may cross back to the "prev" string */
14647 if (len == 0)
14648 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14649 --prevlen;
14650 }
14651 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014652 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014653 else
14654 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014655 /* Change "prev" buffer to be the right size. This way
14656 * the bytes are only copied once, and very long lines are
14657 * allocated only once. */
14658 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014660 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014661 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014662 prev = NULL; /* the list will own the string */
14663 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664 }
14665 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014666 if (s == NULL)
14667 {
14668 do_outofmem_msg((long_u) prevlen + len + 1);
14669 failed = TRUE;
14670 break;
14671 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014672
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014673 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014674 {
14675 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014676 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014677 break;
14678 }
14679 li->li_tv.v_type = VAR_STRING;
14680 li->li_tv.v_lock = 0;
14681 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014682 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014683
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014684 start = p + 1; /* step over newline */
14685 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014686 break;
14687 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014688 else if (*p == NUL)
14689 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014690#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014691 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14692 * when finding the BF and check the previous two bytes. */
14693 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014694 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014695 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14696 * + 1, these may be in the "prev" string. */
14697 char_u back1 = p >= buf + 1 ? p[-1]
14698 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14699 char_u back2 = p >= buf + 2 ? p[-2]
14700 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14701 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014702
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014703 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014704 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014705 char_u *dest = p - 2;
14706
14707 /* Usually a BOM is at the beginning of a file, and so at
14708 * the beginning of a line; then we can just step over it.
14709 */
14710 if (start == dest)
14711 start = p + 1;
14712 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014713 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014714 /* have to shuffle buf to close gap */
14715 int adjust_prevlen = 0;
14716
14717 if (dest < buf)
14718 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014719 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014720 dest = buf;
14721 }
14722 if (readlen > p - buf + 1)
14723 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14724 readlen -= 3 - adjust_prevlen;
14725 prevlen -= adjust_prevlen;
14726 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014727 }
14728 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014730#endif
14731 } /* for */
14732
14733 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14734 break;
14735 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014736 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014737 /* There's part of a line in buf, store it in "prev". */
14738 if (p - start + prevlen >= prevsize)
14739 {
14740 /* need bigger "prev" buffer */
14741 char_u *newprev;
14742
14743 /* A common use case is ordinary text files and "prev" gets a
14744 * fragment of a line, so the first allocation is made
14745 * small, to avoid repeatedly 'allocing' large and
14746 * 'reallocing' small. */
14747 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014748 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014749 else
14750 {
14751 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014752 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 prevsize = grow50pc > growmin ? grow50pc : growmin;
14754 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014755 newprev = prev == NULL ? alloc(prevsize)
14756 : vim_realloc(prev, prevsize);
14757 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 {
14759 do_outofmem_msg((long_u)prevsize);
14760 failed = TRUE;
14761 break;
14762 }
14763 prev = newprev;
14764 }
14765 /* Add the line part to end of "prev". */
14766 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014767 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014768 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014769 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014770
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014771 /*
14772 * For a negative line count use only the lines at the end of the file,
14773 * free the rest.
14774 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014776 while (cnt > -maxline)
14777 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014778 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014779 --cnt;
14780 }
14781
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014782 if (failed)
14783 {
14784 list_free(rettv->vval.v_list, TRUE);
14785 /* readfile doc says an empty list is returned on error */
14786 rettv->vval.v_list = list_alloc();
14787 }
14788
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014789 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014790 fclose(fd);
14791}
14792
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014793#if defined(FEAT_RELTIME)
14794static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14795
14796/*
14797 * Convert a List to proftime_T.
14798 * Return FAIL when there is something wrong.
14799 */
14800 static int
14801list2proftime(arg, tm)
14802 typval_T *arg;
14803 proftime_T *tm;
14804{
14805 long n1, n2;
14806 int error = FALSE;
14807
14808 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14809 || arg->vval.v_list->lv_len != 2)
14810 return FAIL;
14811 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14812 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14813# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014814 tm->HighPart = n1;
14815 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014816# else
14817 tm->tv_sec = n1;
14818 tm->tv_usec = n2;
14819# endif
14820 return error ? FAIL : OK;
14821}
14822#endif /* FEAT_RELTIME */
14823
14824/*
14825 * "reltime()" function
14826 */
14827 static void
14828f_reltime(argvars, rettv)
14829 typval_T *argvars;
14830 typval_T *rettv;
14831{
14832#ifdef FEAT_RELTIME
14833 proftime_T res;
14834 proftime_T start;
14835
14836 if (argvars[0].v_type == VAR_UNKNOWN)
14837 {
14838 /* No arguments: get current time. */
14839 profile_start(&res);
14840 }
14841 else if (argvars[1].v_type == VAR_UNKNOWN)
14842 {
14843 if (list2proftime(&argvars[0], &res) == FAIL)
14844 return;
14845 profile_end(&res);
14846 }
14847 else
14848 {
14849 /* Two arguments: compute the difference. */
14850 if (list2proftime(&argvars[0], &start) == FAIL
14851 || list2proftime(&argvars[1], &res) == FAIL)
14852 return;
14853 profile_sub(&res, &start);
14854 }
14855
14856 if (rettv_list_alloc(rettv) == OK)
14857 {
14858 long n1, n2;
14859
14860# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014861 n1 = res.HighPart;
14862 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014863# else
14864 n1 = res.tv_sec;
14865 n2 = res.tv_usec;
14866# endif
14867 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14868 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14869 }
14870#endif
14871}
14872
14873/*
14874 * "reltimestr()" function
14875 */
14876 static void
14877f_reltimestr(argvars, rettv)
14878 typval_T *argvars;
14879 typval_T *rettv;
14880{
14881#ifdef FEAT_RELTIME
14882 proftime_T tm;
14883#endif
14884
14885 rettv->v_type = VAR_STRING;
14886 rettv->vval.v_string = NULL;
14887#ifdef FEAT_RELTIME
14888 if (list2proftime(&argvars[0], &tm) == OK)
14889 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14890#endif
14891}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014892
Bram Moolenaar0d660222005-01-07 21:51:51 +000014893#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14894static void make_connection __ARGS((void));
14895static int check_connection __ARGS((void));
14896
14897 static void
14898make_connection()
14899{
14900 if (X_DISPLAY == NULL
14901# ifdef FEAT_GUI
14902 && !gui.in_use
14903# endif
14904 )
14905 {
14906 x_force_connect = TRUE;
14907 setup_term_clip();
14908 x_force_connect = FALSE;
14909 }
14910}
14911
14912 static int
14913check_connection()
14914{
14915 make_connection();
14916 if (X_DISPLAY == NULL)
14917 {
14918 EMSG(_("E240: No connection to Vim server"));
14919 return FAIL;
14920 }
14921 return OK;
14922}
14923#endif
14924
14925#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014926static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927
14928 static void
14929remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014930 typval_T *argvars;
14931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014932 int expr;
14933{
14934 char_u *server_name;
14935 char_u *keys;
14936 char_u *r = NULL;
14937 char_u buf[NUMBUFLEN];
14938# ifdef WIN32
14939 HWND w;
14940# else
14941 Window w;
14942# endif
14943
14944 if (check_restricted() || check_secure())
14945 return;
14946
14947# ifdef FEAT_X11
14948 if (check_connection() == FAIL)
14949 return;
14950# endif
14951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014952 server_name = get_tv_string_chk(&argvars[0]);
14953 if (server_name == NULL)
14954 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 keys = get_tv_string_buf(&argvars[1], buf);
14956# ifdef WIN32
14957 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14958# else
14959 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14960 < 0)
14961# endif
14962 {
14963 if (r != NULL)
14964 EMSG(r); /* sending worked but evaluation failed */
14965 else
14966 EMSG2(_("E241: Unable to send to %s"), server_name);
14967 return;
14968 }
14969
14970 rettv->vval.v_string = r;
14971
14972 if (argvars[2].v_type != VAR_UNKNOWN)
14973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014974 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014975 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014976 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014978 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014979 v.di_tv.v_type = VAR_STRING;
14980 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014981 idvar = get_tv_string_chk(&argvars[2]);
14982 if (idvar != NULL)
14983 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014984 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 }
14986}
14987#endif
14988
14989/*
14990 * "remote_expr()" function
14991 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992 static void
14993f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014995 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996{
14997 rettv->v_type = VAR_STRING;
14998 rettv->vval.v_string = NULL;
14999#ifdef FEAT_CLIENTSERVER
15000 remote_common(argvars, rettv, TRUE);
15001#endif
15002}
15003
15004/*
15005 * "remote_foreground()" function
15006 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 static void
15008f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015009 typval_T *argvars UNUSED;
15010 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015012#ifdef FEAT_CLIENTSERVER
15013# ifdef WIN32
15014 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 {
15016 char_u *server_name = get_tv_string_chk(&argvars[0]);
15017
15018 if (server_name != NULL)
15019 serverForeground(server_name);
15020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021# else
15022 /* Send a foreground() expression to the server. */
15023 argvars[1].v_type = VAR_STRING;
15024 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15025 argvars[2].v_type = VAR_UNKNOWN;
15026 remote_common(argvars, rettv, TRUE);
15027 vim_free(argvars[1].vval.v_string);
15028# endif
15029#endif
15030}
15031
Bram Moolenaar0d660222005-01-07 21:51:51 +000015032 static void
15033f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036{
15037#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015039 char_u *s = NULL;
15040# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015041 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015042# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015043 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044
15045 if (check_restricted() || check_secure())
15046 {
15047 rettv->vval.v_number = -1;
15048 return;
15049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 serverid = get_tv_string_chk(&argvars[0]);
15051 if (serverid == NULL)
15052 {
15053 rettv->vval.v_number = -1;
15054 return; /* type error; errmsg already given */
15055 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015057 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 if (n == 0)
15059 rettv->vval.v_number = -1;
15060 else
15061 {
15062 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15063 rettv->vval.v_number = (s != NULL);
15064 }
15065# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 if (check_connection() == FAIL)
15067 return;
15068
15069 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071# endif
15072
15073 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015075 char_u *retvar;
15076
Bram Moolenaar33570922005-01-25 22:26:29 +000015077 v.di_tv.v_type = VAR_STRING;
15078 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015079 retvar = get_tv_string_chk(&argvars[1]);
15080 if (retvar != NULL)
15081 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 }
15084#else
15085 rettv->vval.v_number = -1;
15086#endif
15087}
15088
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 static void
15090f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093{
15094 char_u *r = NULL;
15095
15096#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 char_u *serverid = get_tv_string_chk(&argvars[0]);
15098
15099 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 {
15101# ifdef WIN32
15102 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015103 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015105 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 if (n != 0)
15107 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15108 if (r == NULL)
15109# else
15110 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015111 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112# endif
15113 EMSG(_("E277: Unable to read a server reply"));
15114 }
15115#endif
15116 rettv->v_type = VAR_STRING;
15117 rettv->vval.v_string = r;
15118}
15119
15120/*
15121 * "remote_send()" function
15122 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 static void
15124f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015125 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127{
15128 rettv->v_type = VAR_STRING;
15129 rettv->vval.v_string = NULL;
15130#ifdef FEAT_CLIENTSERVER
15131 remote_common(argvars, rettv, FALSE);
15132#endif
15133}
15134
15135/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015136 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015137 */
15138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015140 typval_T *argvars;
15141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015142{
Bram Moolenaar33570922005-01-25 22:26:29 +000015143 list_T *l;
15144 listitem_T *item, *item2;
15145 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015146 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015147 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015148 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015149 dict_T *d;
15150 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015151 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015152
Bram Moolenaar8c711452005-01-14 21:53:12 +000015153 if (argvars[0].v_type == VAR_DICT)
15154 {
15155 if (argvars[2].v_type != VAR_UNKNOWN)
15156 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015157 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015158 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015160 key = get_tv_string_chk(&argvars[1]);
15161 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015163 di = dict_find(d, key, -1);
15164 if (di == NULL)
15165 EMSG2(_(e_dictkey), key);
15166 else
15167 {
15168 *rettv = di->di_tv;
15169 init_tv(&di->di_tv);
15170 dictitem_remove(d, di);
15171 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015173 }
15174 }
15175 else if (argvars[0].v_type != VAR_LIST)
15176 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015177 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015178 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 int error = FALSE;
15181
15182 idx = get_tv_number_chk(&argvars[1], &error);
15183 if (error)
15184 ; /* type error: do nothing, errmsg already given */
15185 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015186 EMSGN(_(e_listidx), idx);
15187 else
15188 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015189 if (argvars[2].v_type == VAR_UNKNOWN)
15190 {
15191 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015192 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015193 *rettv = item->li_tv;
15194 vim_free(item);
15195 }
15196 else
15197 {
15198 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015199 end = get_tv_number_chk(&argvars[2], &error);
15200 if (error)
15201 ; /* type error: do nothing */
15202 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015203 EMSGN(_(e_listidx), end);
15204 else
15205 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015206 int cnt = 0;
15207
15208 for (li = item; li != NULL; li = li->li_next)
15209 {
15210 ++cnt;
15211 if (li == item2)
15212 break;
15213 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015214 if (li == NULL) /* didn't find "item2" after "item" */
15215 EMSG(_(e_invrange));
15216 else
15217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015218 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015219 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015220 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015221 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015222 l->lv_first = item;
15223 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015224 item->li_prev = NULL;
15225 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015226 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015227 }
15228 }
15229 }
15230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015231 }
15232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233}
15234
15235/*
15236 * "rename({from}, {to})" function
15237 */
15238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *argvars;
15241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242{
15243 char_u buf[NUMBUFLEN];
15244
15245 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015248 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15249 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250}
15251
15252/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015253 * "repeat()" function
15254 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015255 static void
15256f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *argvars;
15258 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015259{
15260 char_u *p;
15261 int n;
15262 int slen;
15263 int len;
15264 char_u *r;
15265 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015266
15267 n = get_tv_number(&argvars[1]);
15268 if (argvars[0].v_type == VAR_LIST)
15269 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015270 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015271 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015272 if (list_extend(rettv->vval.v_list,
15273 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015274 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 }
15276 else
15277 {
15278 p = get_tv_string(&argvars[0]);
15279 rettv->v_type = VAR_STRING;
15280 rettv->vval.v_string = NULL;
15281
15282 slen = (int)STRLEN(p);
15283 len = slen * n;
15284 if (len <= 0)
15285 return;
15286
15287 r = alloc(len + 1);
15288 if (r != NULL)
15289 {
15290 for (i = 0; i < n; i++)
15291 mch_memmove(r + i * slen, p, (size_t)slen);
15292 r[len] = NUL;
15293 }
15294
15295 rettv->vval.v_string = r;
15296 }
15297}
15298
15299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 * "resolve()" function
15301 */
15302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015304 typval_T *argvars;
15305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306{
15307 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015308#ifdef HAVE_READLINK
15309 char_u *buf = NULL;
15310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313#ifdef FEAT_SHORTCUT
15314 {
15315 char_u *v = NULL;
15316
15317 v = mch_resolve_shortcut(p);
15318 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015321 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 }
15323#else
15324# ifdef HAVE_READLINK
15325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 char_u *cpy;
15327 int len;
15328 char_u *remain = NULL;
15329 char_u *q;
15330 int is_relative_to_current = FALSE;
15331 int has_trailing_pathsep = FALSE;
15332 int limit = 100;
15333
15334 p = vim_strsave(p);
15335
15336 if (p[0] == '.' && (vim_ispathsep(p[1])
15337 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15338 is_relative_to_current = TRUE;
15339
15340 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015341 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015344 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346
15347 q = getnextcomp(p);
15348 if (*q != NUL)
15349 {
15350 /* Separate the first path component in "p", and keep the
15351 * remainder (beginning with the path separator). */
15352 remain = vim_strsave(q - 1);
15353 q[-1] = NUL;
15354 }
15355
Bram Moolenaard9462e32011-04-11 21:35:11 +020015356 buf = alloc(MAXPATHL + 1);
15357 if (buf == NULL)
15358 goto fail;
15359
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 for (;;)
15361 {
15362 for (;;)
15363 {
15364 len = readlink((char *)p, (char *)buf, MAXPATHL);
15365 if (len <= 0)
15366 break;
15367 buf[len] = NUL;
15368
15369 if (limit-- == 0)
15370 {
15371 vim_free(p);
15372 vim_free(remain);
15373 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 goto fail;
15376 }
15377
15378 /* Ensure that the result will have a trailing path separator
15379 * if the argument has one. */
15380 if (remain == NULL && has_trailing_pathsep)
15381 add_pathsep(buf);
15382
15383 /* Separate the first path component in the link value and
15384 * concatenate the remainders. */
15385 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15386 if (*q != NUL)
15387 {
15388 if (remain == NULL)
15389 remain = vim_strsave(q - 1);
15390 else
15391 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015392 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 if (cpy != NULL)
15394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 vim_free(remain);
15396 remain = cpy;
15397 }
15398 }
15399 q[-1] = NUL;
15400 }
15401
15402 q = gettail(p);
15403 if (q > p && *q == NUL)
15404 {
15405 /* Ignore trailing path separator. */
15406 q[-1] = NUL;
15407 q = gettail(p);
15408 }
15409 if (q > p && !mch_isFullName(buf))
15410 {
15411 /* symlink is relative to directory of argument */
15412 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15413 if (cpy != NULL)
15414 {
15415 STRCPY(cpy, p);
15416 STRCPY(gettail(cpy), buf);
15417 vim_free(p);
15418 p = cpy;
15419 }
15420 }
15421 else
15422 {
15423 vim_free(p);
15424 p = vim_strsave(buf);
15425 }
15426 }
15427
15428 if (remain == NULL)
15429 break;
15430
15431 /* Append the first path component of "remain" to "p". */
15432 q = getnextcomp(remain + 1);
15433 len = q - remain - (*q != NUL);
15434 cpy = vim_strnsave(p, STRLEN(p) + len);
15435 if (cpy != NULL)
15436 {
15437 STRNCAT(cpy, remain, len);
15438 vim_free(p);
15439 p = cpy;
15440 }
15441 /* Shorten "remain". */
15442 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015443 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 else
15445 {
15446 vim_free(remain);
15447 remain = NULL;
15448 }
15449 }
15450
15451 /* If the result is a relative path name, make it explicitly relative to
15452 * the current directory if and only if the argument had this form. */
15453 if (!vim_ispathsep(*p))
15454 {
15455 if (is_relative_to_current
15456 && *p != NUL
15457 && !(p[0] == '.'
15458 && (p[1] == NUL
15459 || vim_ispathsep(p[1])
15460 || (p[1] == '.'
15461 && (p[2] == NUL
15462 || vim_ispathsep(p[2]))))))
15463 {
15464 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015465 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 if (cpy != NULL)
15467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 vim_free(p);
15469 p = cpy;
15470 }
15471 }
15472 else if (!is_relative_to_current)
15473 {
15474 /* Strip leading "./". */
15475 q = p;
15476 while (q[0] == '.' && vim_ispathsep(q[1]))
15477 q += 2;
15478 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015479 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 }
15481 }
15482
15483 /* Ensure that the result will have no trailing path separator
15484 * if the argument had none. But keep "/" or "//". */
15485 if (!has_trailing_pathsep)
15486 {
15487 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015488 if (after_pathsep(p, q))
15489 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 }
15491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 }
15494# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015495 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496# endif
15497#endif
15498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500
15501#ifdef HAVE_READLINK
15502fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015503 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015505 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506}
15507
15508/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015509 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 */
15511 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015513 typval_T *argvars;
15514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515{
Bram Moolenaar33570922005-01-25 22:26:29 +000015516 list_T *l;
15517 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518
Bram Moolenaar0d660222005-01-07 21:51:51 +000015519 if (argvars[0].v_type != VAR_LIST)
15520 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015521 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015522 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 {
15524 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015525 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015526 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527 while (li != NULL)
15528 {
15529 ni = li->li_prev;
15530 list_append(l, li);
15531 li = ni;
15532 }
15533 rettv->vval.v_list = l;
15534 rettv->v_type = VAR_LIST;
15535 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015536 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538}
15539
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015540#define SP_NOMOVE 0x01 /* don't move cursor */
15541#define SP_REPEAT 0x02 /* repeat to find outer pair */
15542#define SP_RETCOUNT 0x04 /* return matchcount */
15543#define SP_SETPCMARK 0x08 /* set previous context mark */
15544#define SP_START 0x10 /* accept match at start position */
15545#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15546#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015547
Bram Moolenaar33570922005-01-25 22:26:29 +000015548static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549
15550/*
15551 * Get flags for a search function.
15552 * Possibly sets "p_ws".
15553 * Returns BACKWARD, FORWARD or zero (for an error).
15554 */
15555 static int
15556get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015557 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015558 int *flagsp;
15559{
15560 int dir = FORWARD;
15561 char_u *flags;
15562 char_u nbuf[NUMBUFLEN];
15563 int mask;
15564
15565 if (varp->v_type != VAR_UNKNOWN)
15566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 flags = get_tv_string_buf_chk(varp, nbuf);
15568 if (flags == NULL)
15569 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015570 while (*flags != NUL)
15571 {
15572 switch (*flags)
15573 {
15574 case 'b': dir = BACKWARD; break;
15575 case 'w': p_ws = TRUE; break;
15576 case 'W': p_ws = FALSE; break;
15577 default: mask = 0;
15578 if (flagsp != NULL)
15579 switch (*flags)
15580 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015581 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015582 case 'e': mask = SP_END; break;
15583 case 'm': mask = SP_RETCOUNT; break;
15584 case 'n': mask = SP_NOMOVE; break;
15585 case 'p': mask = SP_SUBPAT; break;
15586 case 'r': mask = SP_REPEAT; break;
15587 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 }
15589 if (mask == 0)
15590 {
15591 EMSG2(_(e_invarg2), flags);
15592 dir = 0;
15593 }
15594 else
15595 *flagsp |= mask;
15596 }
15597 if (dir == 0)
15598 break;
15599 ++flags;
15600 }
15601 }
15602 return dir;
15603}
15604
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015606 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015608 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015609search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015611 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015612 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015614 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 char_u *pat;
15616 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015617 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 int save_p_ws = p_ws;
15619 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015620 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015621 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015622 proftime_T tm;
15623#ifdef FEAT_RELTIME
15624 long time_limit = 0;
15625#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015626 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015627 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015630 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015631 if (dir == 0)
15632 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015633 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015634 if (flags & SP_START)
15635 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015636 if (flags & SP_END)
15637 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015638
Bram Moolenaar76929292008-01-06 19:07:36 +000015639 /* Optional arguments: line number to stop searching and timeout. */
15640 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015641 {
15642 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15643 if (lnum_stop < 0)
15644 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015645#ifdef FEAT_RELTIME
15646 if (argvars[3].v_type != VAR_UNKNOWN)
15647 {
15648 time_limit = get_tv_number_chk(&argvars[3], NULL);
15649 if (time_limit < 0)
15650 goto theend;
15651 }
15652#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015653 }
15654
Bram Moolenaar76929292008-01-06 19:07:36 +000015655#ifdef FEAT_RELTIME
15656 /* Set the time limit, if there is one. */
15657 profile_setlimit(time_limit, &tm);
15658#endif
15659
Bram Moolenaar231334e2005-07-25 20:46:57 +000015660 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015661 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015662 * Check to make sure only those flags are set.
15663 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15664 * flags cannot be set. Check for that condition also.
15665 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015666 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015667 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015670 goto theend;
15671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015673 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015674 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015675 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015676 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015678 if (flags & SP_SUBPAT)
15679 retval = subpatnum;
15680 else
15681 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015682 if (flags & SP_SETPCMARK)
15683 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015685 if (match_pos != NULL)
15686 {
15687 /* Store the match cursor position */
15688 match_pos->lnum = pos.lnum;
15689 match_pos->col = pos.col + 1;
15690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 /* "/$" will put the cursor after the end of the line, may need to
15692 * correct that here */
15693 check_cursor();
15694 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015695
15696 /* If 'n' flag is used: restore cursor position. */
15697 if (flags & SP_NOMOVE)
15698 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015699 else
15700 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015701theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015703
15704 return retval;
15705}
15706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015707#ifdef FEAT_FLOAT
15708/*
15709 * "round({float})" function
15710 */
15711 static void
15712f_round(argvars, rettv)
15713 typval_T *argvars;
15714 typval_T *rettv;
15715{
15716 float_T f;
15717
15718 rettv->v_type = VAR_FLOAT;
15719 if (get_float_arg(argvars, &f) == OK)
15720 /* round() is not in C90, use ceil() or floor() instead. */
15721 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15722 else
15723 rettv->vval.v_float = 0.0;
15724}
15725#endif
15726
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015727/*
15728 * "search()" function
15729 */
15730 static void
15731f_search(argvars, rettv)
15732 typval_T *argvars;
15733 typval_T *rettv;
15734{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015735 int flags = 0;
15736
15737 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738}
15739
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015741 * "searchdecl()" function
15742 */
15743 static void
15744f_searchdecl(argvars, rettv)
15745 typval_T *argvars;
15746 typval_T *rettv;
15747{
15748 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015749 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015750 int error = FALSE;
15751 char_u *name;
15752
15753 rettv->vval.v_number = 1; /* default: FAIL */
15754
15755 name = get_tv_string_chk(&argvars[0]);
15756 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015757 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015758 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015759 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15760 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15761 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015762 if (!error && name != NULL)
15763 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015764 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015765}
15766
15767/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015768 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015770 static int
15771searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015773 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774{
15775 char_u *spat, *mpat, *epat;
15776 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 int dir;
15779 int flags = 0;
15780 char_u nbuf1[NUMBUFLEN];
15781 char_u nbuf2[NUMBUFLEN];
15782 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015783 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015784 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015785 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015788 spat = get_tv_string_chk(&argvars[0]);
15789 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15790 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15791 if (spat == NULL || mpat == NULL || epat == NULL)
15792 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 /* Handle the optional fourth argument: flags */
15795 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015796 if (dir == 0)
15797 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015798
15799 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015800 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15801 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015802 if ((flags & (SP_END | SP_SUBPAT)) != 0
15803 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015804 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015805 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015806 goto theend;
15807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015809 /* Using 'r' implies 'W', otherwise it doesn't work. */
15810 if (flags & SP_REPEAT)
15811 p_ws = FALSE;
15812
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015813 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015814 if (argvars[3].v_type == VAR_UNKNOWN
15815 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 skip = (char_u *)"";
15817 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015820 if (argvars[5].v_type != VAR_UNKNOWN)
15821 {
15822 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15823 if (lnum_stop < 0)
15824 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015825#ifdef FEAT_RELTIME
15826 if (argvars[6].v_type != VAR_UNKNOWN)
15827 {
15828 time_limit = get_tv_number_chk(&argvars[6], NULL);
15829 if (time_limit < 0)
15830 goto theend;
15831 }
15832#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015833 }
15834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 if (skip == NULL)
15836 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015838 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015839 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015840
15841theend:
15842 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015843
15844 return retval;
15845}
15846
15847/*
15848 * "searchpair()" function
15849 */
15850 static void
15851f_searchpair(argvars, rettv)
15852 typval_T *argvars;
15853 typval_T *rettv;
15854{
15855 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15856}
15857
15858/*
15859 * "searchpairpos()" function
15860 */
15861 static void
15862f_searchpairpos(argvars, rettv)
15863 typval_T *argvars;
15864 typval_T *rettv;
15865{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015866 pos_T match_pos;
15867 int lnum = 0;
15868 int col = 0;
15869
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015870 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015871 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015872
15873 if (searchpair_cmn(argvars, &match_pos) > 0)
15874 {
15875 lnum = match_pos.lnum;
15876 col = match_pos.col;
15877 }
15878
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015879 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15880 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015881}
15882
15883/*
15884 * Search for a start/middle/end thing.
15885 * Used by searchpair(), see its documentation for the details.
15886 * Returns 0 or -1 for no match,
15887 */
15888 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015889do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15890 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015891 char_u *spat; /* start pattern */
15892 char_u *mpat; /* middle pattern */
15893 char_u *epat; /* end pattern */
15894 int dir; /* BACKWARD or FORWARD */
15895 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015896 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015897 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015898 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015899 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015900{
15901 char_u *save_cpo;
15902 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15903 long retval = 0;
15904 pos_T pos;
15905 pos_T firstpos;
15906 pos_T foundpos;
15907 pos_T save_cursor;
15908 pos_T save_pos;
15909 int n;
15910 int r;
15911 int nest = 1;
15912 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015913 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015914 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015915
15916 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15917 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015918 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015919
Bram Moolenaar76929292008-01-06 19:07:36 +000015920#ifdef FEAT_RELTIME
15921 /* Set the time limit, if there is one. */
15922 profile_setlimit(time_limit, &tm);
15923#endif
15924
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015925 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15926 * start/middle/end (pat3, for the top pair). */
15927 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15928 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15929 if (pat2 == NULL || pat3 == NULL)
15930 goto theend;
15931 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15932 if (*mpat == NUL)
15933 STRCPY(pat3, pat2);
15934 else
15935 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15936 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015937 if (flags & SP_START)
15938 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015939
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 save_cursor = curwin->w_cursor;
15941 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015942 clearpos(&firstpos);
15943 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 pat = pat3;
15945 for (;;)
15946 {
15947 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015948 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15950 /* didn't find it or found the first match again: FAIL */
15951 break;
15952
15953 if (firstpos.lnum == 0)
15954 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015955 if (equalpos(pos, foundpos))
15956 {
15957 /* Found the same position again. Can happen with a pattern that
15958 * has "\zs" at the end and searching backwards. Advance one
15959 * character and try again. */
15960 if (dir == BACKWARD)
15961 decl(&pos);
15962 else
15963 incl(&pos);
15964 }
15965 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015967 /* clear the start flag to avoid getting stuck here */
15968 options &= ~SEARCH_START;
15969
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 /* If the skip pattern matches, ignore this match. */
15971 if (*skip != NUL)
15972 {
15973 save_pos = curwin->w_cursor;
15974 curwin->w_cursor = pos;
15975 r = eval_to_bool(skip, &err, NULL, FALSE);
15976 curwin->w_cursor = save_pos;
15977 if (err)
15978 {
15979 /* Evaluating {skip} caused an error, break here. */
15980 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015981 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 break;
15983 }
15984 if (r)
15985 continue;
15986 }
15987
15988 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15989 {
15990 /* Found end when searching backwards or start when searching
15991 * forward: nested pair. */
15992 ++nest;
15993 pat = pat2; /* nested, don't search for middle */
15994 }
15995 else
15996 {
15997 /* Found end when searching forward or start when searching
15998 * backward: end of (nested) pair; or found middle in outer pair. */
15999 if (--nest == 1)
16000 pat = pat3; /* outer level, search for middle */
16001 }
16002
16003 if (nest == 0)
16004 {
16005 /* Found the match: return matchcount or line number. */
16006 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016007 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016009 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016010 if (flags & SP_SETPCMARK)
16011 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 curwin->w_cursor = pos;
16013 if (!(flags & SP_REPEAT))
16014 break;
16015 nest = 1; /* search for next unmatched */
16016 }
16017 }
16018
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016019 if (match_pos != NULL)
16020 {
16021 /* Store the match cursor position */
16022 match_pos->lnum = curwin->w_cursor.lnum;
16023 match_pos->col = curwin->w_cursor.col + 1;
16024 }
16025
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016027 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 curwin->w_cursor = save_cursor;
16029
16030theend:
16031 vim_free(pat2);
16032 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016033 if (p_cpo == empty_option)
16034 p_cpo = save_cpo;
16035 else
16036 /* Darn, evaluating the {skip} expression changed the value. */
16037 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016038
16039 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040}
16041
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016042/*
16043 * "searchpos()" function
16044 */
16045 static void
16046f_searchpos(argvars, rettv)
16047 typval_T *argvars;
16048 typval_T *rettv;
16049{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050 pos_T match_pos;
16051 int lnum = 0;
16052 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016053 int n;
16054 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016055
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016056 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016057 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016058
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016059 n = search_cmn(argvars, &match_pos, &flags);
16060 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016061 {
16062 lnum = match_pos.lnum;
16063 col = match_pos.col;
16064 }
16065
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016066 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16067 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016068 if (flags & SP_SUBPAT)
16069 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016070}
16071
16072
Bram Moolenaar0d660222005-01-07 21:51:51 +000016073 static void
16074f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016075 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016078#ifdef FEAT_CLIENTSERVER
16079 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 char_u *server = get_tv_string_chk(&argvars[0]);
16081 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016084 if (server == NULL || reply == NULL)
16085 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016086 if (check_restricted() || check_secure())
16087 return;
16088# ifdef FEAT_X11
16089 if (check_connection() == FAIL)
16090 return;
16091# endif
16092
16093 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016095 EMSG(_("E258: Unable to send to client"));
16096 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098 rettv->vval.v_number = 0;
16099#else
16100 rettv->vval.v_number = -1;
16101#endif
16102}
16103
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104 static void
16105f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016106 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016108{
16109 char_u *r = NULL;
16110
16111#ifdef FEAT_CLIENTSERVER
16112# ifdef WIN32
16113 r = serverGetVimNames();
16114# else
16115 make_connection();
16116 if (X_DISPLAY != NULL)
16117 r = serverGetVimNames(X_DISPLAY);
16118# endif
16119#endif
16120 rettv->v_type = VAR_STRING;
16121 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122}
16123
16124/*
16125 * "setbufvar()" function
16126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016128f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016129 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016130 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131{
16132 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 char_u nbuf[NUMBUFLEN];
16137
16138 if (check_restricted() || check_secure())
16139 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016140 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16141 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016142 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 varp = &argvars[2];
16144
16145 if (buf != NULL && varname != NULL && varp != NULL)
16146 {
16147 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149
16150 if (*varname == '&')
16151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 long numval;
16153 char_u *strval;
16154 int error = FALSE;
16155
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 numval = get_tv_number_chk(varp, &error);
16158 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016159 if (!error && strval != NULL)
16160 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 }
16162 else
16163 {
16164 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16165 if (bufvarname != NULL)
16166 {
16167 STRCPY(bufvarname, "b:");
16168 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016169 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 vim_free(bufvarname);
16171 }
16172 }
16173
16174 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177}
16178
16179/*
16180 * "setcmdpos()" function
16181 */
16182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016184 typval_T *argvars;
16185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016187 int pos = (int)get_tv_number(&argvars[0]) - 1;
16188
16189 if (pos >= 0)
16190 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
16193/*
16194 * "setline()" function
16195 */
16196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 typval_T *argvars;
16199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200{
16201 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016202 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016203 list_T *l = NULL;
16204 listitem_T *li = NULL;
16205 long added = 0;
16206 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016208 lnum = get_tv_lnum(&argvars[0]);
16209 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016211 l = argvars[1].vval.v_list;
16212 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016214 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016215 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016216
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016217 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016218 for (;;)
16219 {
16220 if (l != NULL)
16221 {
16222 /* list argument, get next string */
16223 if (li == NULL)
16224 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016225 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016226 li = li->li_next;
16227 }
16228
16229 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016230 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016231 break;
16232 if (lnum <= curbuf->b_ml.ml_line_count)
16233 {
16234 /* existing line, replace it */
16235 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16236 {
16237 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016238 if (lnum == curwin->w_cursor.lnum)
16239 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016240 rettv->vval.v_number = 0; /* OK */
16241 }
16242 }
16243 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16244 {
16245 /* lnum is one past the last line, append the line */
16246 ++added;
16247 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16248 rettv->vval.v_number = 0; /* OK */
16249 }
16250
16251 if (l == NULL) /* only one string argument */
16252 break;
16253 ++lnum;
16254 }
16255
16256 if (added > 0)
16257 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258}
16259
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016260static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16261
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016263 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016264 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016265 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016266set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016267 win_T *wp UNUSED;
16268 typval_T *list_arg UNUSED;
16269 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016270 typval_T *rettv;
16271{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016272#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016273 char_u *act;
16274 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016275#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016276
Bram Moolenaar2641f772005-03-25 21:58:17 +000016277 rettv->vval.v_number = -1;
16278
16279#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016280 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016281 EMSG(_(e_listreq));
16282 else
16283 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016284 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016285
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016286 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016287 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016288 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 if (act == NULL)
16290 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016291 if (*act == 'a' || *act == 'r')
16292 action = *act;
16293 }
16294
Bram Moolenaar81484f42012-12-05 15:16:47 +010016295 if (l != NULL && set_errorlist(wp, l, action,
16296 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016297 rettv->vval.v_number = 0;
16298 }
16299#endif
16300}
16301
16302/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016303 * "setloclist()" function
16304 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016305 static void
16306f_setloclist(argvars, rettv)
16307 typval_T *argvars;
16308 typval_T *rettv;
16309{
16310 win_T *win;
16311
16312 rettv->vval.v_number = -1;
16313
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016314 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016315 if (win != NULL)
16316 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16317}
16318
16319/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016320 * "setmatches()" function
16321 */
16322 static void
16323f_setmatches(argvars, rettv)
16324 typval_T *argvars;
16325 typval_T *rettv;
16326{
16327#ifdef FEAT_SEARCH_EXTRA
16328 list_T *l;
16329 listitem_T *li;
16330 dict_T *d;
16331
16332 rettv->vval.v_number = -1;
16333 if (argvars[0].v_type != VAR_LIST)
16334 {
16335 EMSG(_(e_listreq));
16336 return;
16337 }
16338 if ((l = argvars[0].vval.v_list) != NULL)
16339 {
16340
16341 /* To some extent make sure that we are dealing with a list from
16342 * "getmatches()". */
16343 li = l->lv_first;
16344 while (li != NULL)
16345 {
16346 if (li->li_tv.v_type != VAR_DICT
16347 || (d = li->li_tv.vval.v_dict) == NULL)
16348 {
16349 EMSG(_(e_invarg));
16350 return;
16351 }
16352 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16353 && dict_find(d, (char_u *)"pattern", -1) != NULL
16354 && dict_find(d, (char_u *)"priority", -1) != NULL
16355 && dict_find(d, (char_u *)"id", -1) != NULL))
16356 {
16357 EMSG(_(e_invarg));
16358 return;
16359 }
16360 li = li->li_next;
16361 }
16362
16363 clear_matches(curwin);
16364 li = l->lv_first;
16365 while (li != NULL)
16366 {
16367 d = li->li_tv.vval.v_dict;
16368 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16369 get_dict_string(d, (char_u *)"pattern", FALSE),
16370 (int)get_dict_number(d, (char_u *)"priority"),
16371 (int)get_dict_number(d, (char_u *)"id"));
16372 li = li->li_next;
16373 }
16374 rettv->vval.v_number = 0;
16375 }
16376#endif
16377}
16378
16379/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016380 * "setpos()" function
16381 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016382 static void
16383f_setpos(argvars, rettv)
16384 typval_T *argvars;
16385 typval_T *rettv;
16386{
16387 pos_T pos;
16388 int fnum;
16389 char_u *name;
16390
Bram Moolenaar08250432008-02-13 11:42:46 +000016391 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016392 name = get_tv_string_chk(argvars);
16393 if (name != NULL)
16394 {
16395 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16396 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016397 if (--pos.col < 0)
16398 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016399 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016400 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016401 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016402 if (fnum == curbuf->b_fnum)
16403 {
16404 curwin->w_cursor = pos;
16405 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016406 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016407 }
16408 else
16409 EMSG(_(e_invarg));
16410 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016411 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16412 {
16413 /* set mark */
16414 if (setmark_pos(name[1], &pos, fnum) == OK)
16415 rettv->vval.v_number = 0;
16416 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016417 else
16418 EMSG(_(e_invarg));
16419 }
16420 }
16421}
16422
16423/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016424 * "setqflist()" function
16425 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016426 static void
16427f_setqflist(argvars, rettv)
16428 typval_T *argvars;
16429 typval_T *rettv;
16430{
16431 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16432}
16433
16434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 * "setreg()" function
16436 */
16437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 typval_T *argvars;
16440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441{
16442 int regname;
16443 char_u *strregname;
16444 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 int append;
16447 char_u yank_type;
16448 long block_len;
16449
16450 block_len = -1;
16451 yank_type = MAUTO;
16452 append = FALSE;
16453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016455 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 if (strregname == NULL)
16458 return; /* type error; errmsg already given */
16459 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 if (regname == 0 || regname == '@')
16461 regname = '"';
16462 else if (regname == '=')
16463 return;
16464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016465 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016467 stropt = get_tv_string_chk(&argvars[2]);
16468 if (stropt == NULL)
16469 return; /* type error */
16470 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 switch (*stropt)
16472 {
16473 case 'a': case 'A': /* append */
16474 append = TRUE;
16475 break;
16476 case 'v': case 'c': /* character-wise selection */
16477 yank_type = MCHAR;
16478 break;
16479 case 'V': case 'l': /* line-wise selection */
16480 yank_type = MLINE;
16481 break;
16482#ifdef FEAT_VISUAL
16483 case 'b': case Ctrl_V: /* block-wise selection */
16484 yank_type = MBLOCK;
16485 if (VIM_ISDIGIT(stropt[1]))
16486 {
16487 ++stropt;
16488 block_len = getdigits(&stropt) - 1;
16489 --stropt;
16490 }
16491 break;
16492#endif
16493 }
16494 }
16495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016496 strval = get_tv_string_chk(&argvars[1]);
16497 if (strval != NULL)
16498 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501}
16502
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016503/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016504 * "settabvar()" function
16505 */
16506 static void
16507f_settabvar(argvars, rettv)
16508 typval_T *argvars;
16509 typval_T *rettv;
16510{
16511 tabpage_T *save_curtab;
16512 char_u *varname, *tabvarname;
16513 typval_T *varp;
16514 tabpage_T *tp;
16515
16516 rettv->vval.v_number = 0;
16517
16518 if (check_restricted() || check_secure())
16519 return;
16520
16521 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16522 varname = get_tv_string_chk(&argvars[1]);
16523 varp = &argvars[2];
16524
16525 if (tp != NULL && varname != NULL && varp != NULL)
16526 {
16527 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016528 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016529
16530 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16531 if (tabvarname != NULL)
16532 {
16533 STRCPY(tabvarname, "t:");
16534 STRCPY(tabvarname + 2, varname);
16535 set_var(tabvarname, varp, TRUE);
16536 vim_free(tabvarname);
16537 }
16538
16539 /* Restore current tabpage */
16540 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016541 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016542 }
16543}
16544
16545/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016546 * "settabwinvar()" function
16547 */
16548 static void
16549f_settabwinvar(argvars, rettv)
16550 typval_T *argvars;
16551 typval_T *rettv;
16552{
16553 setwinvar(argvars, rettv, 1);
16554}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555
16556/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016557 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016561 typval_T *argvars;
16562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016564 setwinvar(argvars, rettv, 0);
16565}
16566
16567/*
16568 * "setwinvar()" and "settabwinvar()" functions
16569 */
16570 static void
16571setwinvar(argvars, rettv, off)
16572 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016573 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016574 int off;
16575{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 win_T *win;
16577#ifdef FEAT_WINDOWS
16578 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016579 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580#endif
16581 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016584 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585
16586 if (check_restricted() || check_secure())
16587 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016588
16589#ifdef FEAT_WINDOWS
16590 if (off == 1)
16591 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16592 else
16593 tp = curtab;
16594#endif
16595 win = find_win_by_nr(&argvars[off], tp);
16596 varname = get_tv_string_chk(&argvars[off + 1]);
16597 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598
16599 if (win != NULL && varname != NULL && varp != NULL)
16600 {
16601#ifdef FEAT_WINDOWS
16602 /* set curwin to be our win, temporarily */
16603 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016604 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016605 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016606 if (!win_valid(win))
16607 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 curwin = win;
16609 curbuf = curwin->w_buffer;
16610#endif
16611
16612 if (*varname == '&')
16613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 long numval;
16615 char_u *strval;
16616 int error = FALSE;
16617
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016619 numval = get_tv_number_chk(varp, &error);
16620 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016621 if (!error && strval != NULL)
16622 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 }
16624 else
16625 {
16626 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16627 if (winvarname != NULL)
16628 {
16629 STRCPY(winvarname, "w:");
16630 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016631 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 vim_free(winvarname);
16633 }
16634 }
16635
16636#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016637 /* Restore current tabpage and window, if still valid (autocomands can
16638 * make them invalid). */
16639 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016640 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 if (win_valid(save_curwin))
16642 {
16643 curwin = save_curwin;
16644 curbuf = curwin->w_buffer;
16645 }
16646#endif
16647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648}
16649
16650/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016651 * "shellescape({string})" function
16652 */
16653 static void
16654f_shellescape(argvars, rettv)
16655 typval_T *argvars;
16656 typval_T *rettv;
16657{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016658 rettv->vval.v_string = vim_strsave_shellescape(
16659 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016660 rettv->v_type = VAR_STRING;
16661}
16662
16663/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016664 * shiftwidth() function
16665 */
16666 static void
16667f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016668 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016669 typval_T *rettv;
16670{
16671 rettv->vval.v_number = get_sw_value();
16672}
16673
16674/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 */
16677 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016679 typval_T *argvars;
16680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016682 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 p = get_tv_string(&argvars[0]);
16685 rettv->vval.v_string = vim_strsave(p);
16686 simplify_filename(rettv->vval.v_string); /* simplify in place */
16687 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688}
16689
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016690#ifdef FEAT_FLOAT
16691/*
16692 * "sin()" function
16693 */
16694 static void
16695f_sin(argvars, rettv)
16696 typval_T *argvars;
16697 typval_T *rettv;
16698{
16699 float_T f;
16700
16701 rettv->v_type = VAR_FLOAT;
16702 if (get_float_arg(argvars, &f) == OK)
16703 rettv->vval.v_float = sin(f);
16704 else
16705 rettv->vval.v_float = 0.0;
16706}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016707
16708/*
16709 * "sinh()" function
16710 */
16711 static void
16712f_sinh(argvars, rettv)
16713 typval_T *argvars;
16714 typval_T *rettv;
16715{
16716 float_T f;
16717
16718 rettv->v_type = VAR_FLOAT;
16719 if (get_float_arg(argvars, &f) == OK)
16720 rettv->vval.v_float = sinh(f);
16721 else
16722 rettv->vval.v_float = 0.0;
16723}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016724#endif
16725
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726static int
16727#ifdef __BORLANDC__
16728 _RTLENTRYF
16729#endif
16730 item_compare __ARGS((const void *s1, const void *s2));
16731static int
16732#ifdef __BORLANDC__
16733 _RTLENTRYF
16734#endif
16735 item_compare2 __ARGS((const void *s1, const void *s2));
16736
16737static int item_compare_ic;
16738static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016739static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016740static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016741#define ITEM_COMPARE_FAIL 999
16742
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016744 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016746 static int
16747#ifdef __BORLANDC__
16748_RTLENTRYF
16749#endif
16750item_compare(s1, s2)
16751 const void *s1;
16752 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 char_u *p1, *p2;
16755 char_u *tofree1, *tofree2;
16756 int res;
16757 char_u numbuf1[NUMBUFLEN];
16758 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016760 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16761 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016762 if (p1 == NULL)
16763 p1 = (char_u *)"";
16764 if (p2 == NULL)
16765 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016766 if (item_compare_ic)
16767 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769 res = STRCMP(p1, p2);
16770 vim_free(tofree1);
16771 vim_free(tofree2);
16772 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773}
16774
16775 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016776#ifdef __BORLANDC__
16777_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016779item_compare2(s1, s2)
16780 const void *s1;
16781 const void *s2;
16782{
16783 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016784 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016785 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016786 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016788 /* shortcut after failure in previous call; compare all items equal */
16789 if (item_compare_func_err)
16790 return 0;
16791
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016792 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16793 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016794 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16795 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796
16797 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016798 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016799 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16800 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 clear_tv(&argv[0]);
16802 clear_tv(&argv[1]);
16803
16804 if (res == FAIL)
16805 res = ITEM_COMPARE_FAIL;
16806 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016807 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16808 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016809 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810 clear_tv(&rettv);
16811 return res;
16812}
16813
16814/*
16815 * "sort({list})" function
16816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016818f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016819 typval_T *argvars;
16820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821{
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 list_T *l;
16823 listitem_T *li;
16824 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825 long len;
16826 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828 if (argvars[0].v_type != VAR_LIST)
16829 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 else
16831 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016832 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016833 if (l == NULL || tv_check_lock(l->lv_lock,
16834 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016835 return;
16836 rettv->vval.v_list = l;
16837 rettv->v_type = VAR_LIST;
16838 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839
Bram Moolenaar0d660222005-01-07 21:51:51 +000016840 len = list_len(l);
16841 if (len <= 1)
16842 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844 item_compare_ic = FALSE;
16845 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016846 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847 if (argvars[1].v_type != VAR_UNKNOWN)
16848 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016849 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016851 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852 else
16853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 int error = FALSE;
16855
16856 i = get_tv_number_chk(&argvars[1], &error);
16857 if (error)
16858 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 if (i == 1)
16860 item_compare_ic = TRUE;
16861 else
16862 item_compare_func = get_tv_string(&argvars[1]);
16863 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016864
16865 if (argvars[2].v_type != VAR_UNKNOWN)
16866 {
16867 /* optional third argument: {dict} */
16868 if (argvars[2].v_type != VAR_DICT)
16869 {
16870 EMSG(_(e_dictreq));
16871 return;
16872 }
16873 item_compare_selfdict = argvars[2].vval.v_dict;
16874 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016878 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879 if (ptrs == NULL)
16880 return;
16881 i = 0;
16882 for (li = l->lv_first; li != NULL; li = li->li_next)
16883 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016885 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016886 /* test the compare function */
16887 if (item_compare_func != NULL
16888 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16889 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016890 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892 {
16893 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 item_compare_func == NULL ? item_compare : item_compare2);
16896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016897 if (!item_compare_func_err)
16898 {
16899 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016900 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 l->lv_len = 0;
16902 for (i = 0; i < len; ++i)
16903 list_append(l, ptrs[i]);
16904 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 }
16906
16907 vim_free(ptrs);
16908 }
16909}
16910
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016911/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016912 * "soundfold({word})" function
16913 */
16914 static void
16915f_soundfold(argvars, rettv)
16916 typval_T *argvars;
16917 typval_T *rettv;
16918{
16919 char_u *s;
16920
16921 rettv->v_type = VAR_STRING;
16922 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016923#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016924 rettv->vval.v_string = eval_soundfold(s);
16925#else
16926 rettv->vval.v_string = vim_strsave(s);
16927#endif
16928}
16929
16930/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016931 * "spellbadword()" function
16932 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016933 static void
16934f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016935 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016936 typval_T *rettv;
16937{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016938 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016939 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016940 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016942 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016943 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016944
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016945#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016946 if (argvars[0].v_type == VAR_UNKNOWN)
16947 {
16948 /* Find the start and length of the badly spelled word. */
16949 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16950 if (len != 0)
16951 word = ml_get_cursor();
16952 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016953 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016954 {
16955 char_u *str = get_tv_string_chk(&argvars[0]);
16956 int capcol = -1;
16957
16958 if (str != NULL)
16959 {
16960 /* Check the argument for spelling. */
16961 while (*str != NUL)
16962 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016963 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016964 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016965 {
16966 word = str;
16967 break;
16968 }
16969 str += len;
16970 }
16971 }
16972 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016973#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016974
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016975 list_append_string(rettv->vval.v_list, word, len);
16976 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016977 attr == HLF_SPB ? "bad" :
16978 attr == HLF_SPR ? "rare" :
16979 attr == HLF_SPL ? "local" :
16980 attr == HLF_SPC ? "caps" :
16981 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016982}
16983
16984/*
16985 * "spellsuggest()" function
16986 */
16987 static void
16988f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016989 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016990 typval_T *rettv;
16991{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016992#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016993 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016994 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016995 int maxcount;
16996 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016997 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016998 listitem_T *li;
16999 int need_capital = FALSE;
17000#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017001
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017002 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017003 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017004
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017005#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017006 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017007 {
17008 str = get_tv_string(&argvars[0]);
17009 if (argvars[1].v_type != VAR_UNKNOWN)
17010 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017011 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017012 if (maxcount <= 0)
17013 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017014 if (argvars[2].v_type != VAR_UNKNOWN)
17015 {
17016 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17017 if (typeerr)
17018 return;
17019 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017020 }
17021 else
17022 maxcount = 25;
17023
Bram Moolenaar4770d092006-01-12 23:22:24 +000017024 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017025
17026 for (i = 0; i < ga.ga_len; ++i)
17027 {
17028 str = ((char_u **)ga.ga_data)[i];
17029
17030 li = listitem_alloc();
17031 if (li == NULL)
17032 vim_free(str);
17033 else
17034 {
17035 li->li_tv.v_type = VAR_STRING;
17036 li->li_tv.v_lock = 0;
17037 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017038 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017039 }
17040 }
17041 ga_clear(&ga);
17042 }
17043#endif
17044}
17045
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017047f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 typval_T *argvars;
17049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050{
17051 char_u *str;
17052 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017053 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 regmatch_T regmatch;
17055 char_u patbuf[NUMBUFLEN];
17056 char_u *save_cpo;
17057 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017059 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017060 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061
17062 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17063 save_cpo = p_cpo;
17064 p_cpo = (char_u *)"";
17065
17066 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017067 if (argvars[1].v_type != VAR_UNKNOWN)
17068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17070 if (pat == NULL)
17071 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017074 }
17075 if (pat == NULL || *pat == NUL)
17076 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017078 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 if (typeerr)
17081 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17084 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017087 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017089 if (*str == NUL)
17090 match = FALSE; /* empty item at the end */
17091 else
17092 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 if (match)
17094 end = regmatch.startp[0];
17095 else
17096 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017097 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17098 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017100 if (list_append_string(rettv->vval.v_list, str,
17101 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103 }
17104 if (!match)
17105 break;
17106 /* Advance to just after the match. */
17107 if (regmatch.endp[0] > str)
17108 col = 0;
17109 else
17110 {
17111 /* Don't get stuck at the same match. */
17112#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017113 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114#else
17115 col = 1;
17116#endif
17117 }
17118 str = regmatch.endp[0];
17119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125}
17126
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017127#ifdef FEAT_FLOAT
17128/*
17129 * "sqrt()" function
17130 */
17131 static void
17132f_sqrt(argvars, rettv)
17133 typval_T *argvars;
17134 typval_T *rettv;
17135{
17136 float_T f;
17137
17138 rettv->v_type = VAR_FLOAT;
17139 if (get_float_arg(argvars, &f) == OK)
17140 rettv->vval.v_float = sqrt(f);
17141 else
17142 rettv->vval.v_float = 0.0;
17143}
17144
17145/*
17146 * "str2float()" function
17147 */
17148 static void
17149f_str2float(argvars, rettv)
17150 typval_T *argvars;
17151 typval_T *rettv;
17152{
17153 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17154
17155 if (*p == '+')
17156 p = skipwhite(p + 1);
17157 (void)string2float(p, &rettv->vval.v_float);
17158 rettv->v_type = VAR_FLOAT;
17159}
17160#endif
17161
Bram Moolenaar2c932302006-03-18 21:42:09 +000017162/*
17163 * "str2nr()" function
17164 */
17165 static void
17166f_str2nr(argvars, rettv)
17167 typval_T *argvars;
17168 typval_T *rettv;
17169{
17170 int base = 10;
17171 char_u *p;
17172 long n;
17173
17174 if (argvars[1].v_type != VAR_UNKNOWN)
17175 {
17176 base = get_tv_number(&argvars[1]);
17177 if (base != 8 && base != 10 && base != 16)
17178 {
17179 EMSG(_(e_invarg));
17180 return;
17181 }
17182 }
17183
17184 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017185 if (*p == '+')
17186 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017187 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17188 rettv->vval.v_number = n;
17189}
17190
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191#ifdef HAVE_STRFTIME
17192/*
17193 * "strftime({format}[, {time}])" function
17194 */
17195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017196f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017197 typval_T *argvars;
17198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199{
17200 char_u result_buf[256];
17201 struct tm *curtime;
17202 time_t seconds;
17203 char_u *p;
17204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017207 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017208 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 seconds = time(NULL);
17210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 curtime = localtime(&seconds);
17213 /* MSVC returns NULL for an invalid value of seconds. */
17214 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017215 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 else
17217 {
17218# ifdef FEAT_MBYTE
17219 vimconv_T conv;
17220 char_u *enc;
17221
17222 conv.vc_type = CONV_NONE;
17223 enc = enc_locale();
17224 convert_setup(&conv, p_enc, enc);
17225 if (conv.vc_type != CONV_NONE)
17226 p = string_convert(&conv, p, NULL);
17227# endif
17228 if (p != NULL)
17229 (void)strftime((char *)result_buf, sizeof(result_buf),
17230 (char *)p, curtime);
17231 else
17232 result_buf[0] = NUL;
17233
17234# ifdef FEAT_MBYTE
17235 if (conv.vc_type != CONV_NONE)
17236 vim_free(p);
17237 convert_setup(&conv, enc, p_enc);
17238 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 else
17241# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017242 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243
17244# ifdef FEAT_MBYTE
17245 /* Release conversion descriptors */
17246 convert_setup(&conv, NULL, NULL);
17247 vim_free(enc);
17248# endif
17249 }
17250}
17251#endif
17252
17253/*
17254 * "stridx()" function
17255 */
17256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017258 typval_T *argvars;
17259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260{
17261 char_u buf[NUMBUFLEN];
17262 char_u *needle;
17263 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017266 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017268 needle = get_tv_string_chk(&argvars[1]);
17269 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017270 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017271 if (needle == NULL || haystack == NULL)
17272 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 if (argvars[2].v_type != VAR_UNKNOWN)
17275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017276 int error = FALSE;
17277
17278 start_idx = get_tv_number_chk(&argvars[2], &error);
17279 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017280 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017281 if (start_idx >= 0)
17282 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 }
17284
17285 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17286 if (pos != NULL)
17287 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288}
17289
17290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 * "string()" function
17292 */
17293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017295 typval_T *argvars;
17296 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017297{
17298 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017299 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017302 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017303 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017304 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306}
17307
17308/*
17309 * "strlen()" function
17310 */
17311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 typval_T *argvars;
17314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017316 rettv->vval.v_number = (varnumber_T)(STRLEN(
17317 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318}
17319
17320/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017321 * "strchars()" function
17322 */
17323 static void
17324f_strchars(argvars, rettv)
17325 typval_T *argvars;
17326 typval_T *rettv;
17327{
17328 char_u *s = get_tv_string(&argvars[0]);
17329#ifdef FEAT_MBYTE
17330 varnumber_T len = 0;
17331
17332 while (*s != NUL)
17333 {
17334 mb_cptr2char_adv(&s);
17335 ++len;
17336 }
17337 rettv->vval.v_number = len;
17338#else
17339 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17340#endif
17341}
17342
17343/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017344 * "strdisplaywidth()" function
17345 */
17346 static void
17347f_strdisplaywidth(argvars, rettv)
17348 typval_T *argvars;
17349 typval_T *rettv;
17350{
17351 char_u *s = get_tv_string(&argvars[0]);
17352 int col = 0;
17353
17354 if (argvars[1].v_type != VAR_UNKNOWN)
17355 col = get_tv_number(&argvars[1]);
17356
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017357 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017358}
17359
17360/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017361 * "strwidth()" function
17362 */
17363 static void
17364f_strwidth(argvars, rettv)
17365 typval_T *argvars;
17366 typval_T *rettv;
17367{
17368 char_u *s = get_tv_string(&argvars[0]);
17369
17370 rettv->vval.v_number = (varnumber_T)(
17371#ifdef FEAT_MBYTE
17372 mb_string2cells(s, -1)
17373#else
17374 STRLEN(s)
17375#endif
17376 );
17377}
17378
17379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 * "strpart()" function
17381 */
17382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 typval_T *argvars;
17385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386{
17387 char_u *p;
17388 int n;
17389 int len;
17390 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017391 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 slen = (int)STRLEN(p);
17395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017396 n = get_tv_number_chk(&argvars[1], &error);
17397 if (error)
17398 len = 0;
17399 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 else
17402 len = slen - n; /* default len: all bytes that are available. */
17403
17404 /*
17405 * Only return the overlap between the specified part and the actual
17406 * string.
17407 */
17408 if (n < 0)
17409 {
17410 len += n;
17411 n = 0;
17412 }
17413 else if (n > slen)
17414 n = slen;
17415 if (len < 0)
17416 len = 0;
17417 else if (n + len > slen)
17418 len = slen - n;
17419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420 rettv->v_type = VAR_STRING;
17421 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422}
17423
17424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425 * "strridx()" function
17426 */
17427 static void
17428f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017429 typval_T *argvars;
17430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017431{
17432 char_u buf[NUMBUFLEN];
17433 char_u *needle;
17434 char_u *haystack;
17435 char_u *rest;
17436 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017437 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017439 needle = get_tv_string_chk(&argvars[1]);
17440 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441
17442 rettv->vval.v_number = -1;
17443 if (needle == NULL || haystack == NULL)
17444 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017445
17446 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017447 if (argvars[2].v_type != VAR_UNKNOWN)
17448 {
17449 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017450 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017451 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017452 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017453 }
17454 else
17455 end_idx = haystack_len;
17456
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017458 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017460 lastmatch = haystack + end_idx;
17461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017462 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017463 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017464 for (rest = haystack; *rest != '\0'; ++rest)
17465 {
17466 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017467 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017468 break;
17469 lastmatch = rest;
17470 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017471 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017472
17473 if (lastmatch == NULL)
17474 rettv->vval.v_number = -1;
17475 else
17476 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17477}
17478
17479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 * "strtrans()" function
17481 */
17482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017483f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017484 typval_T *argvars;
17485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017487 rettv->v_type = VAR_STRING;
17488 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489}
17490
17491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017492 * "submatch()" function
17493 */
17494 static void
17495f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 typval_T *argvars;
17497 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017498{
17499 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017500 rettv->vval.v_string =
17501 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017502}
17503
17504/*
17505 * "substitute()" function
17506 */
17507 static void
17508f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T *argvars;
17510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017511{
17512 char_u patbuf[NUMBUFLEN];
17513 char_u subbuf[NUMBUFLEN];
17514 char_u flagsbuf[NUMBUFLEN];
17515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 char_u *str = get_tv_string_chk(&argvars[0]);
17517 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17518 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17519 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17520
Bram Moolenaar0d660222005-01-07 21:51:51 +000017521 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017522 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17523 rettv->vval.v_string = NULL;
17524 else
17525 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526}
17527
17528/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017529 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017532f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535{
17536 int id = 0;
17537#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017538 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 long col;
17540 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017541 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017543 lnum = get_tv_lnum(argvars); /* -1 on type error */
17544 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17545 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017547 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017548 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017549 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550#endif
17551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553}
17554
17555/*
17556 * "synIDattr(id, what [, mode])" function
17557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017559f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017560 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562{
17563 char_u *p = NULL;
17564#ifdef FEAT_SYN_HL
17565 int id;
17566 char_u *what;
17567 char_u *mode;
17568 char_u modebuf[NUMBUFLEN];
17569 int modec;
17570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017571 id = get_tv_number(&argvars[0]);
17572 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017573 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017575 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017577 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 modec = 0; /* replace invalid with current */
17579 }
17580 else
17581 {
17582#ifdef FEAT_GUI
17583 if (gui.in_use)
17584 modec = 'g';
17585 else
17586#endif
17587 if (t_colors > 1)
17588 modec = 'c';
17589 else
17590 modec = 't';
17591 }
17592
17593
17594 switch (TOLOWER_ASC(what[0]))
17595 {
17596 case 'b':
17597 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17598 p = highlight_color(id, what, modec);
17599 else /* bold */
17600 p = highlight_has_attr(id, HL_BOLD, modec);
17601 break;
17602
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017603 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 p = highlight_color(id, what, modec);
17605 break;
17606
17607 case 'i':
17608 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17609 p = highlight_has_attr(id, HL_INVERSE, modec);
17610 else /* italic */
17611 p = highlight_has_attr(id, HL_ITALIC, modec);
17612 break;
17613
17614 case 'n': /* name */
17615 p = get_highlight_name(NULL, id - 1);
17616 break;
17617
17618 case 'r': /* reverse */
17619 p = highlight_has_attr(id, HL_INVERSE, modec);
17620 break;
17621
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017622 case 's':
17623 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17624 p = highlight_color(id, what, modec);
17625 else /* standout */
17626 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 break;
17628
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017629 case 'u':
17630 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17631 /* underline */
17632 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17633 else
17634 /* undercurl */
17635 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 break;
17637 }
17638
17639 if (p != NULL)
17640 p = vim_strsave(p);
17641#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017642 rettv->v_type = VAR_STRING;
17643 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644}
17645
17646/*
17647 * "synIDtrans(id)" function
17648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017651 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653{
17654 int id;
17655
17656#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017657 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658
17659 if (id > 0)
17660 id = syn_get_final_id(id);
17661 else
17662#endif
17663 id = 0;
17664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017665 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666}
17667
17668/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017669 * "synconcealed(lnum, col)" function
17670 */
17671 static void
17672f_synconcealed(argvars, rettv)
17673 typval_T *argvars UNUSED;
17674 typval_T *rettv;
17675{
17676#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17677 long lnum;
17678 long col;
17679 int syntax_flags = 0;
17680 int cchar;
17681 int matchid = 0;
17682 char_u str[NUMBUFLEN];
17683#endif
17684
17685 rettv->v_type = VAR_LIST;
17686 rettv->vval.v_list = NULL;
17687
17688#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17689 lnum = get_tv_lnum(argvars); /* -1 on type error */
17690 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17691
17692 vim_memset(str, NUL, sizeof(str));
17693
17694 if (rettv_list_alloc(rettv) != FAIL)
17695 {
17696 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17697 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17698 && curwin->w_p_cole > 0)
17699 {
17700 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17701 syntax_flags = get_syntax_info(&matchid);
17702
17703 /* get the conceal character */
17704 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17705 {
17706 cchar = syn_get_sub_char();
17707 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17708 cchar = lcs_conceal;
17709 if (cchar != NUL)
17710 {
17711# ifdef FEAT_MBYTE
17712 if (has_mbyte)
17713 (*mb_char2bytes)(cchar, str);
17714 else
17715# endif
17716 str[0] = cchar;
17717 }
17718 }
17719 }
17720
17721 list_append_number(rettv->vval.v_list,
17722 (syntax_flags & HL_CONCEAL) != 0);
17723 /* -1 to auto-determine strlen */
17724 list_append_string(rettv->vval.v_list, str, -1);
17725 list_append_number(rettv->vval.v_list, matchid);
17726 }
17727#endif
17728}
17729
17730/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017731 * "synstack(lnum, col)" function
17732 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017733 static void
17734f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017735 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017736 typval_T *rettv;
17737{
17738#ifdef FEAT_SYN_HL
17739 long lnum;
17740 long col;
17741 int i;
17742 int id;
17743#endif
17744
17745 rettv->v_type = VAR_LIST;
17746 rettv->vval.v_list = NULL;
17747
17748#ifdef FEAT_SYN_HL
17749 lnum = get_tv_lnum(argvars); /* -1 on type error */
17750 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17751
17752 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017753 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017754 && rettv_list_alloc(rettv) != FAIL)
17755 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017756 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017757 for (i = 0; ; ++i)
17758 {
17759 id = syn_get_stack_item(i);
17760 if (id < 0)
17761 break;
17762 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17763 break;
17764 }
17765 }
17766#endif
17767}
17768
17769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 * "system()" function
17771 */
17772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017773f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017774 typval_T *argvars;
17775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017777 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017779 char_u *infile = NULL;
17780 char_u buf[NUMBUFLEN];
17781 int err = FALSE;
17782 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017784 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017785 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017786
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017787 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017788 {
17789 /*
17790 * Write the string to a temp file, to be used for input of the shell
17791 * command.
17792 */
17793 if ((infile = vim_tempname('i')) == NULL)
17794 {
17795 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017796 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017797 }
17798
17799 fd = mch_fopen((char *)infile, WRITEBIN);
17800 if (fd == NULL)
17801 {
17802 EMSG2(_(e_notopen), infile);
17803 goto done;
17804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017805 p = get_tv_string_buf_chk(&argvars[1], buf);
17806 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017807 {
17808 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017810 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017811 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17812 err = TRUE;
17813 if (fclose(fd) != 0)
17814 err = TRUE;
17815 if (err)
17816 {
17817 EMSG(_("E677: Error writing temp file"));
17818 goto done;
17819 }
17820 }
17821
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017822 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17823 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017824
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825#ifdef USE_CR
17826 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017827 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 {
17829 char_u *s;
17830
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017831 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 {
17833 if (*s == CAR)
17834 *s = NL;
17835 }
17836 }
17837#else
17838# ifdef USE_CRNL
17839 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017840 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 {
17842 char_u *s, *d;
17843
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017844 d = res;
17845 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 {
17847 if (s[0] == CAR && s[1] == NL)
17848 ++s;
17849 *d++ = *s;
17850 }
17851 *d = NUL;
17852 }
17853# endif
17854#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017855
17856done:
17857 if (infile != NULL)
17858 {
17859 mch_remove(infile);
17860 vim_free(infile);
17861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017862 rettv->v_type = VAR_STRING;
17863 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864}
17865
17866/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017867 * "tabpagebuflist()" function
17868 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017869 static void
17870f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017871 typval_T *argvars UNUSED;
17872 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017873{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017874#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017875 tabpage_T *tp;
17876 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017877
17878 if (argvars[0].v_type == VAR_UNKNOWN)
17879 wp = firstwin;
17880 else
17881 {
17882 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17883 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017884 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017885 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017886 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017887 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017888 for (; wp != NULL; wp = wp->w_next)
17889 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017890 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017891 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017892 }
17893#endif
17894}
17895
17896
17897/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017898 * "tabpagenr()" function
17899 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017900 static void
17901f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017902 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017903 typval_T *rettv;
17904{
17905 int nr = 1;
17906#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017907 char_u *arg;
17908
17909 if (argvars[0].v_type != VAR_UNKNOWN)
17910 {
17911 arg = get_tv_string_chk(&argvars[0]);
17912 nr = 0;
17913 if (arg != NULL)
17914 {
17915 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017916 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017917 else
17918 EMSG2(_(e_invexpr2), arg);
17919 }
17920 }
17921 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017922 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017923#endif
17924 rettv->vval.v_number = nr;
17925}
17926
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017927
17928#ifdef FEAT_WINDOWS
17929static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17930
17931/*
17932 * Common code for tabpagewinnr() and winnr().
17933 */
17934 static int
17935get_winnr(tp, argvar)
17936 tabpage_T *tp;
17937 typval_T *argvar;
17938{
17939 win_T *twin;
17940 int nr = 1;
17941 win_T *wp;
17942 char_u *arg;
17943
17944 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17945 if (argvar->v_type != VAR_UNKNOWN)
17946 {
17947 arg = get_tv_string_chk(argvar);
17948 if (arg == NULL)
17949 nr = 0; /* type error; errmsg already given */
17950 else if (STRCMP(arg, "$") == 0)
17951 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17952 else if (STRCMP(arg, "#") == 0)
17953 {
17954 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17955 if (twin == NULL)
17956 nr = 0;
17957 }
17958 else
17959 {
17960 EMSG2(_(e_invexpr2), arg);
17961 nr = 0;
17962 }
17963 }
17964
17965 if (nr > 0)
17966 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17967 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017968 {
17969 if (wp == NULL)
17970 {
17971 /* didn't find it in this tabpage */
17972 nr = 0;
17973 break;
17974 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017975 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017976 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017977 return nr;
17978}
17979#endif
17980
17981/*
17982 * "tabpagewinnr()" function
17983 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017984 static void
17985f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017986 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017987 typval_T *rettv;
17988{
17989 int nr = 1;
17990#ifdef FEAT_WINDOWS
17991 tabpage_T *tp;
17992
17993 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17994 if (tp == NULL)
17995 nr = 0;
17996 else
17997 nr = get_winnr(tp, &argvars[1]);
17998#endif
17999 rettv->vval.v_number = nr;
18000}
18001
18002
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018003/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018004 * "tagfiles()" function
18005 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018006 static void
18007f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018008 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018009 typval_T *rettv;
18010{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018011 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018012 tagname_T tn;
18013 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018014
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018015 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018016 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018017 fname = alloc(MAXPATHL);
18018 if (fname == NULL)
18019 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018020
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018021 for (first = TRUE; ; first = FALSE)
18022 if (get_tagfname(&tn, first, fname) == FAIL
18023 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018024 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018025 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018026 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018027}
18028
18029/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018030 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018031 */
18032 static void
18033f_taglist(argvars, rettv)
18034 typval_T *argvars;
18035 typval_T *rettv;
18036{
18037 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018038
18039 tag_pattern = get_tv_string(&argvars[0]);
18040
18041 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018042 if (*tag_pattern == NUL)
18043 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018044
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018045 if (rettv_list_alloc(rettv) == OK)
18046 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018047}
18048
18049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 * "tempname()" function
18051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018053f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056{
18057 static int x = 'A';
18058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 rettv->v_type = VAR_STRING;
18060 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061
18062 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18063 * names. Skip 'I' and 'O', they are used for shell redirection. */
18064 do
18065 {
18066 if (x == 'Z')
18067 x = '0';
18068 else if (x == '9')
18069 x = 'A';
18070 else
18071 {
18072#ifdef EBCDIC
18073 if (x == 'I')
18074 x = 'J';
18075 else if (x == 'R')
18076 x = 'S';
18077 else
18078#endif
18079 ++x;
18080 }
18081 } while (x == 'I' || x == 'O');
18082}
18083
18084/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018085 * "test(list)" function: Just checking the walls...
18086 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018087 static void
18088f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018089 typval_T *argvars UNUSED;
18090 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018091{
18092 /* Used for unit testing. Change the code below to your liking. */
18093#if 0
18094 listitem_T *li;
18095 list_T *l;
18096 char_u *bad, *good;
18097
18098 if (argvars[0].v_type != VAR_LIST)
18099 return;
18100 l = argvars[0].vval.v_list;
18101 if (l == NULL)
18102 return;
18103 li = l->lv_first;
18104 if (li == NULL)
18105 return;
18106 bad = get_tv_string(&li->li_tv);
18107 li = li->li_next;
18108 if (li == NULL)
18109 return;
18110 good = get_tv_string(&li->li_tv);
18111 rettv->vval.v_number = test_edit_score(bad, good);
18112#endif
18113}
18114
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018115#ifdef FEAT_FLOAT
18116/*
18117 * "tan()" function
18118 */
18119 static void
18120f_tan(argvars, rettv)
18121 typval_T *argvars;
18122 typval_T *rettv;
18123{
18124 float_T f;
18125
18126 rettv->v_type = VAR_FLOAT;
18127 if (get_float_arg(argvars, &f) == OK)
18128 rettv->vval.v_float = tan(f);
18129 else
18130 rettv->vval.v_float = 0.0;
18131}
18132
18133/*
18134 * "tanh()" function
18135 */
18136 static void
18137f_tanh(argvars, rettv)
18138 typval_T *argvars;
18139 typval_T *rettv;
18140{
18141 float_T f;
18142
18143 rettv->v_type = VAR_FLOAT;
18144 if (get_float_arg(argvars, &f) == OK)
18145 rettv->vval.v_float = tanh(f);
18146 else
18147 rettv->vval.v_float = 0.0;
18148}
18149#endif
18150
Bram Moolenaard52d9742005-08-21 22:20:28 +000018151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 * "tolower(string)" function
18153 */
18154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018156 typval_T *argvars;
18157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158{
18159 char_u *p;
18160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018161 p = vim_strsave(get_tv_string(&argvars[0]));
18162 rettv->v_type = VAR_STRING;
18163 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
18165 if (p != NULL)
18166 while (*p != NUL)
18167 {
18168#ifdef FEAT_MBYTE
18169 int l;
18170
18171 if (enc_utf8)
18172 {
18173 int c, lc;
18174
18175 c = utf_ptr2char(p);
18176 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018177 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 /* TODO: reallocate string when byte count changes. */
18179 if (utf_char2len(lc) == l)
18180 utf_char2bytes(lc, p);
18181 p += l;
18182 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018183 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 p += l; /* skip multi-byte character */
18185 else
18186#endif
18187 {
18188 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18189 ++p;
18190 }
18191 }
18192}
18193
18194/*
18195 * "toupper(string)" function
18196 */
18197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018198f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018199 typval_T *argvars;
18200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018203 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204}
18205
18206/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018207 * "tr(string, fromstr, tostr)" function
18208 */
18209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018210f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018211 typval_T *argvars;
18212 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018213{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018214 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018215 char_u *fromstr;
18216 char_u *tostr;
18217 char_u *p;
18218#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018219 int inlen;
18220 int fromlen;
18221 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018222 int idx;
18223 char_u *cpstr;
18224 int cplen;
18225 int first = TRUE;
18226#endif
18227 char_u buf[NUMBUFLEN];
18228 char_u buf2[NUMBUFLEN];
18229 garray_T ga;
18230
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018231 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018232 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18233 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018234
18235 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018236 rettv->v_type = VAR_STRING;
18237 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018238 if (fromstr == NULL || tostr == NULL)
18239 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018240 ga_init2(&ga, (int)sizeof(char), 80);
18241
18242#ifdef FEAT_MBYTE
18243 if (!has_mbyte)
18244#endif
18245 /* not multi-byte: fromstr and tostr must be the same length */
18246 if (STRLEN(fromstr) != STRLEN(tostr))
18247 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018248#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018249error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018250#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018251 EMSG2(_(e_invarg2), fromstr);
18252 ga_clear(&ga);
18253 return;
18254 }
18255
18256 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018257 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018258 {
18259#ifdef FEAT_MBYTE
18260 if (has_mbyte)
18261 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018262 inlen = (*mb_ptr2len)(in_str);
18263 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018264 cplen = inlen;
18265 idx = 0;
18266 for (p = fromstr; *p != NUL; p += fromlen)
18267 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018268 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018269 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018270 {
18271 for (p = tostr; *p != NUL; p += tolen)
18272 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018273 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018274 if (idx-- == 0)
18275 {
18276 cplen = tolen;
18277 cpstr = p;
18278 break;
18279 }
18280 }
18281 if (*p == NUL) /* tostr is shorter than fromstr */
18282 goto error;
18283 break;
18284 }
18285 ++idx;
18286 }
18287
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018288 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018289 {
18290 /* Check that fromstr and tostr have the same number of
18291 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018292 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018293 first = FALSE;
18294 for (p = tostr; *p != NUL; p += tolen)
18295 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018296 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018297 --idx;
18298 }
18299 if (idx != 0)
18300 goto error;
18301 }
18302
18303 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018304 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018305 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018306
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018307 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018308 }
18309 else
18310#endif
18311 {
18312 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018313 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018314 if (p != NULL)
18315 ga_append(&ga, tostr[p - fromstr]);
18316 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018317 ga_append(&ga, *in_str);
18318 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018319 }
18320 }
18321
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018322 /* add a terminating NUL */
18323 ga_grow(&ga, 1);
18324 ga_append(&ga, NUL);
18325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018327}
18328
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018329#ifdef FEAT_FLOAT
18330/*
18331 * "trunc({float})" function
18332 */
18333 static void
18334f_trunc(argvars, rettv)
18335 typval_T *argvars;
18336 typval_T *rettv;
18337{
18338 float_T f;
18339
18340 rettv->v_type = VAR_FLOAT;
18341 if (get_float_arg(argvars, &f) == OK)
18342 /* trunc() is not in C90, use floor() or ceil() instead. */
18343 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18344 else
18345 rettv->vval.v_float = 0.0;
18346}
18347#endif
18348
Bram Moolenaar8299df92004-07-10 09:47:34 +000018349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 * "type(expr)" function
18351 */
18352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018353f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018354 typval_T *argvars;
18355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018357 int n;
18358
18359 switch (argvars[0].v_type)
18360 {
18361 case VAR_NUMBER: n = 0; break;
18362 case VAR_STRING: n = 1; break;
18363 case VAR_FUNC: n = 2; break;
18364 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018365 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018366#ifdef FEAT_FLOAT
18367 case VAR_FLOAT: n = 5; break;
18368#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018369 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18370 }
18371 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372}
18373
18374/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018375 * "undofile(name)" function
18376 */
18377 static void
18378f_undofile(argvars, rettv)
18379 typval_T *argvars;
18380 typval_T *rettv;
18381{
18382 rettv->v_type = VAR_STRING;
18383#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018384 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018385 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018386
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018387 if (*fname == NUL)
18388 {
18389 /* If there is no file name there will be no undo file. */
18390 rettv->vval.v_string = NULL;
18391 }
18392 else
18393 {
18394 char_u *ffname = FullName_save(fname, FALSE);
18395
18396 if (ffname != NULL)
18397 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18398 vim_free(ffname);
18399 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018400 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018401#else
18402 rettv->vval.v_string = NULL;
18403#endif
18404}
18405
18406/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018407 * "undotree()" function
18408 */
18409 static void
18410f_undotree(argvars, rettv)
18411 typval_T *argvars UNUSED;
18412 typval_T *rettv;
18413{
18414 if (rettv_dict_alloc(rettv) == OK)
18415 {
18416 dict_T *dict = rettv->vval.v_dict;
18417 list_T *list;
18418
Bram Moolenaar730cde92010-06-27 05:18:54 +020018419 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018420 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018421 dict_add_nr_str(dict, "save_last",
18422 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018423 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18424 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018425 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018426
18427 list = list_alloc();
18428 if (list != NULL)
18429 {
18430 u_eval_tree(curbuf->b_u_oldhead, list);
18431 dict_add_list(dict, "entries", list);
18432 }
18433 }
18434}
18435
18436/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018437 * "values(dict)" function
18438 */
18439 static void
18440f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 typval_T *argvars;
18442 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018443{
18444 dict_list(argvars, rettv, 1);
18445}
18446
18447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 * "virtcol(string)" function
18449 */
18450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018451f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 typval_T *argvars;
18453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454{
18455 colnr_T vcol = 0;
18456 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018457 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018459 fp = var2fpos(&argvars[0], FALSE, &fnum);
18460 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18461 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 {
18463 getvvcol(curwin, fp, NULL, NULL, &vcol);
18464 ++vcol;
18465 }
18466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468}
18469
18470/*
18471 * "visualmode()" function
18472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018475 typval_T *argvars UNUSED;
18476 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477{
18478#ifdef FEAT_VISUAL
18479 char_u str[2];
18480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 str[0] = curbuf->b_visual_mode_eval;
18483 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485
18486 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018487 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489#endif
18490}
18491
18492/*
18493 * "winbufnr(nr)" function
18494 */
18495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018496f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018497 typval_T *argvars;
18498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499{
18500 win_T *wp;
18501
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018502 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018506 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507}
18508
18509/*
18510 * "wincol()" function
18511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018513f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018514 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516{
18517 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018518 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519}
18520
18521/*
18522 * "winheight(nr)" function
18523 */
18524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018525f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018526 typval_T *argvars;
18527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528{
18529 win_T *wp;
18530
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018531 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536}
18537
18538/*
18539 * "winline()" function
18540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018542f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545{
18546 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548}
18549
18550/*
18551 * "winnr()" function
18552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018555 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557{
18558 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018559
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018561 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564}
18565
18566/*
18567 * "winrestcmd()" function
18568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573{
18574#ifdef FEAT_WINDOWS
18575 win_T *wp;
18576 int winnr = 1;
18577 garray_T ga;
18578 char_u buf[50];
18579
18580 ga_init2(&ga, (int)sizeof(char), 70);
18581 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18582 {
18583 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18584 ga_concat(&ga, buf);
18585# ifdef FEAT_VERTSPLIT
18586 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18587 ga_concat(&ga, buf);
18588# endif
18589 ++winnr;
18590 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018591 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018593 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598}
18599
18600/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018601 * "winrestview()" function
18602 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018603 static void
18604f_winrestview(argvars, rettv)
18605 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018606 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018607{
18608 dict_T *dict;
18609
18610 if (argvars[0].v_type != VAR_DICT
18611 || (dict = argvars[0].vval.v_dict) == NULL)
18612 EMSG(_(e_invarg));
18613 else
18614 {
18615 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18616 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18617#ifdef FEAT_VIRTUALEDIT
18618 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18619#endif
18620 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018621 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018622
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018623 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018624#ifdef FEAT_DIFF
18625 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18626#endif
18627 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18628 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18629
18630 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018631 win_new_height(curwin, curwin->w_height);
18632# ifdef FEAT_VERTSPLIT
18633 win_new_width(curwin, W_WIDTH(curwin));
18634# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018635 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018636
18637 if (curwin->w_topline == 0)
18638 curwin->w_topline = 1;
18639 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18640 curwin->w_topline = curbuf->b_ml.ml_line_count;
18641#ifdef FEAT_DIFF
18642 check_topfill(curwin, TRUE);
18643#endif
18644 }
18645}
18646
18647/*
18648 * "winsaveview()" function
18649 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018650 static void
18651f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018652 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018653 typval_T *rettv;
18654{
18655 dict_T *dict;
18656
Bram Moolenaara800b422010-06-27 01:15:55 +020018657 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018658 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018659 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018660
18661 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18662 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18663#ifdef FEAT_VIRTUALEDIT
18664 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18665#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018666 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018667 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18668
18669 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18670#ifdef FEAT_DIFF
18671 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18672#endif
18673 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18674 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18675}
18676
18677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 * "winwidth(nr)" function
18679 */
18680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018682 typval_T *argvars;
18683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684{
18685 win_T *wp;
18686
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018687 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 else
18691#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695#endif
18696}
18697
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018699 * "writefile()" function
18700 */
18701 static void
18702f_writefile(argvars, rettv)
18703 typval_T *argvars;
18704 typval_T *rettv;
18705{
18706 int binary = FALSE;
18707 char_u *fname;
18708 FILE *fd;
18709 listitem_T *li;
18710 char_u *s;
18711 int ret = 0;
18712 int c;
18713
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018714 if (check_restricted() || check_secure())
18715 return;
18716
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018717 if (argvars[0].v_type != VAR_LIST)
18718 {
18719 EMSG2(_(e_listarg), "writefile()");
18720 return;
18721 }
18722 if (argvars[0].vval.v_list == NULL)
18723 return;
18724
18725 if (argvars[2].v_type != VAR_UNKNOWN
18726 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18727 binary = TRUE;
18728
18729 /* Always open the file in binary mode, library functions have a mind of
18730 * their own about CR-LF conversion. */
18731 fname = get_tv_string(&argvars[1]);
18732 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18733 {
18734 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18735 ret = -1;
18736 }
18737 else
18738 {
18739 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18740 li = li->li_next)
18741 {
18742 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18743 {
18744 if (*s == '\n')
18745 c = putc(NUL, fd);
18746 else
18747 c = putc(*s, fd);
18748 if (c == EOF)
18749 {
18750 ret = -1;
18751 break;
18752 }
18753 }
18754 if (!binary || li->li_next != NULL)
18755 if (putc('\n', fd) == EOF)
18756 {
18757 ret = -1;
18758 break;
18759 }
18760 if (ret < 0)
18761 {
18762 EMSG(_(e_write));
18763 break;
18764 }
18765 }
18766 fclose(fd);
18767 }
18768
18769 rettv->vval.v_number = ret;
18770}
18771
18772/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018773 * "xor(expr, expr)" function
18774 */
18775 static void
18776f_xor(argvars, rettv)
18777 typval_T *argvars;
18778 typval_T *rettv;
18779{
18780 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18781 ^ get_tv_number_chk(&argvars[1], NULL);
18782}
18783
18784
18785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018787 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 */
18789 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018790var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018792 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018793 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018795 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018797 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798
Bram Moolenaara5525202006-03-02 22:52:09 +000018799 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018800 if (varp->v_type == VAR_LIST)
18801 {
18802 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018803 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018804 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018805 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018806
18807 l = varp->vval.v_list;
18808 if (l == NULL)
18809 return NULL;
18810
18811 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018812 pos.lnum = list_find_nr(l, 0L, &error);
18813 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018814 return NULL; /* invalid line number */
18815
18816 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018817 pos.col = list_find_nr(l, 1L, &error);
18818 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018819 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018820 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018821
18822 /* We accept "$" for the column number: last column. */
18823 li = list_find(l, 1L);
18824 if (li != NULL && li->li_tv.v_type == VAR_STRING
18825 && li->li_tv.vval.v_string != NULL
18826 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18827 pos.col = len + 1;
18828
Bram Moolenaara5525202006-03-02 22:52:09 +000018829 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018830 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018831 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018832 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018833
Bram Moolenaara5525202006-03-02 22:52:09 +000018834#ifdef FEAT_VIRTUALEDIT
18835 /* Get the virtual offset. Defaults to zero. */
18836 pos.coladd = list_find_nr(l, 2L, &error);
18837 if (error)
18838 pos.coladd = 0;
18839#endif
18840
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018841 return &pos;
18842 }
18843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018844 name = get_tv_string_chk(varp);
18845 if (name == NULL)
18846 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018847 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018849#ifdef FEAT_VISUAL
18850 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18851 {
18852 if (VIsual_active)
18853 return &VIsual;
18854 return &curwin->w_cursor;
18855 }
18856#endif
18857 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018859 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18861 return NULL;
18862 return pp;
18863 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018864
18865#ifdef FEAT_VIRTUALEDIT
18866 pos.coladd = 0;
18867#endif
18868
Bram Moolenaar477933c2007-07-17 14:32:23 +000018869 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018870 {
18871 pos.col = 0;
18872 if (name[1] == '0') /* "w0": first visible line */
18873 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018874 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018875 pos.lnum = curwin->w_topline;
18876 return &pos;
18877 }
18878 else if (name[1] == '$') /* "w$": last visible line */
18879 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018880 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018881 pos.lnum = curwin->w_botline - 1;
18882 return &pos;
18883 }
18884 }
18885 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018887 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 {
18889 pos.lnum = curbuf->b_ml.ml_line_count;
18890 pos.col = 0;
18891 }
18892 else
18893 {
18894 pos.lnum = curwin->w_cursor.lnum;
18895 pos.col = (colnr_T)STRLEN(ml_get_curline());
18896 }
18897 return &pos;
18898 }
18899 return NULL;
18900}
18901
18902/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018903 * Convert list in "arg" into a position and optional file number.
18904 * When "fnump" is NULL there is no file number, only 3 items.
18905 * Note that the column is passed on as-is, the caller may want to decrement
18906 * it to use 1 for the first column.
18907 * Return FAIL when conversion is not possible, doesn't check the position for
18908 * validity.
18909 */
18910 static int
18911list2fpos(arg, posp, fnump)
18912 typval_T *arg;
18913 pos_T *posp;
18914 int *fnump;
18915{
18916 list_T *l = arg->vval.v_list;
18917 long i = 0;
18918 long n;
18919
Bram Moolenaarbde35262006-07-23 20:12:24 +000018920 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18921 * when "fnump" isn't NULL and "coladd" is optional. */
18922 if (arg->v_type != VAR_LIST
18923 || l == NULL
18924 || l->lv_len < (fnump == NULL ? 2 : 3)
18925 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018926 return FAIL;
18927
18928 if (fnump != NULL)
18929 {
18930 n = list_find_nr(l, i++, NULL); /* fnum */
18931 if (n < 0)
18932 return FAIL;
18933 if (n == 0)
18934 n = curbuf->b_fnum; /* current buffer */
18935 *fnump = n;
18936 }
18937
18938 n = list_find_nr(l, i++, NULL); /* lnum */
18939 if (n < 0)
18940 return FAIL;
18941 posp->lnum = n;
18942
18943 n = list_find_nr(l, i++, NULL); /* col */
18944 if (n < 0)
18945 return FAIL;
18946 posp->col = n;
18947
18948#ifdef FEAT_VIRTUALEDIT
18949 n = list_find_nr(l, i, NULL);
18950 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018951 posp->coladd = 0;
18952 else
18953 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018954#endif
18955
18956 return OK;
18957}
18958
18959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 * Get the length of an environment variable name.
18961 * Advance "arg" to the first character after the name.
18962 * Return 0 for error.
18963 */
18964 static int
18965get_env_len(arg)
18966 char_u **arg;
18967{
18968 char_u *p;
18969 int len;
18970
18971 for (p = *arg; vim_isIDc(*p); ++p)
18972 ;
18973 if (p == *arg) /* no name found */
18974 return 0;
18975
18976 len = (int)(p - *arg);
18977 *arg = p;
18978 return len;
18979}
18980
18981/*
18982 * Get the length of the name of a function or internal variable.
18983 * "arg" is advanced to the first non-white character after the name.
18984 * Return 0 if something is wrong.
18985 */
18986 static int
18987get_id_len(arg)
18988 char_u **arg;
18989{
18990 char_u *p;
18991 int len;
18992
18993 /* Find the end of the name. */
18994 for (p = *arg; eval_isnamec(*p); ++p)
18995 ;
18996 if (p == *arg) /* no name found */
18997 return 0;
18998
18999 len = (int)(p - *arg);
19000 *arg = skipwhite(p);
19001
19002 return len;
19003}
19004
19005/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019006 * Get the length of the name of a variable or function.
19007 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019009 * Return -1 if curly braces expansion failed.
19010 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 * If the name contains 'magic' {}'s, expand them and return the
19012 * expanded name in an allocated string via 'alias' - caller must free.
19013 */
19014 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019015get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 char_u **arg;
19017 char_u **alias;
19018 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019019 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020{
19021 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 char_u *p;
19023 char_u *expr_start;
19024 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
19026 *alias = NULL; /* default to no alias */
19027
19028 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19029 && (*arg)[2] == (int)KE_SNR)
19030 {
19031 /* hard coded <SNR>, already translated */
19032 *arg += 3;
19033 return get_id_len(arg) + 3;
19034 }
19035 len = eval_fname_script(*arg);
19036 if (len > 0)
19037 {
19038 /* literal "<SID>", "s:" or "<SNR>" */
19039 *arg += len;
19040 }
19041
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019045 p = find_name_end(*arg, &expr_start, &expr_end,
19046 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 if (expr_start != NULL)
19048 {
19049 char_u *temp_string;
19050
19051 if (!evaluate)
19052 {
19053 len += (int)(p - *arg);
19054 *arg = skipwhite(p);
19055 return len;
19056 }
19057
19058 /*
19059 * Include any <SID> etc in the expanded string:
19060 * Thus the -len here.
19061 */
19062 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19063 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019064 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 *alias = temp_string;
19066 *arg = skipwhite(p);
19067 return (int)STRLEN(temp_string);
19068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
19070 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019071 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 EMSG2(_(e_invexpr2), *arg);
19073
19074 return len;
19075}
19076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019077/*
19078 * Find the end of a variable or function name, taking care of magic braces.
19079 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19080 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019081 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019082 * Return a pointer to just after the name. Equal to "arg" if there is no
19083 * valid name.
19084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019086find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 char_u *arg;
19088 char_u **expr_start;
19089 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019090 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 int mb_nest = 0;
19093 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 char_u *p;
19095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098 *expr_start = NULL;
19099 *expr_end = NULL;
19100 }
19101
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019102 /* Quick check for valid starting character. */
19103 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19104 return arg;
19105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106 for (p = arg; *p != NUL
19107 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019108 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019109 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019110 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019111 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019112 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019113 if (*p == '\'')
19114 {
19115 /* skip over 'string' to avoid counting [ and ] inside it. */
19116 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19117 ;
19118 if (*p == NUL)
19119 break;
19120 }
19121 else if (*p == '"')
19122 {
19123 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19124 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19125 if (*p == '\\' && p[1] != NUL)
19126 ++p;
19127 if (*p == NUL)
19128 break;
19129 }
19130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019131 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019133 if (*p == '[')
19134 ++br_nest;
19135 else if (*p == ']')
19136 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019141 if (*p == '{')
19142 {
19143 mb_nest++;
19144 if (expr_start != NULL && *expr_start == NULL)
19145 *expr_start = p;
19146 }
19147 else if (*p == '}')
19148 {
19149 mb_nest--;
19150 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19151 *expr_end = p;
19152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 }
19155
19156 return p;
19157}
19158
19159/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019160 * Expands out the 'magic' {}'s in a variable/function name.
19161 * Note that this can call itself recursively, to deal with
19162 * constructs like foo{bar}{baz}{bam}
19163 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19164 * "in_start" ^
19165 * "expr_start" ^
19166 * "expr_end" ^
19167 * "in_end" ^
19168 *
19169 * Returns a new allocated string, which the caller must free.
19170 * Returns NULL for failure.
19171 */
19172 static char_u *
19173make_expanded_name(in_start, expr_start, expr_end, in_end)
19174 char_u *in_start;
19175 char_u *expr_start;
19176 char_u *expr_end;
19177 char_u *in_end;
19178{
19179 char_u c1;
19180 char_u *retval = NULL;
19181 char_u *temp_result;
19182 char_u *nextcmd = NULL;
19183
19184 if (expr_end == NULL || in_end == NULL)
19185 return NULL;
19186 *expr_start = NUL;
19187 *expr_end = NUL;
19188 c1 = *in_end;
19189 *in_end = NUL;
19190
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019191 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019192 if (temp_result != NULL && nextcmd == NULL)
19193 {
19194 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19195 + (in_end - expr_end) + 1));
19196 if (retval != NULL)
19197 {
19198 STRCPY(retval, in_start);
19199 STRCAT(retval, temp_result);
19200 STRCAT(retval, expr_end + 1);
19201 }
19202 }
19203 vim_free(temp_result);
19204
19205 *in_end = c1; /* put char back for error messages */
19206 *expr_start = '{';
19207 *expr_end = '}';
19208
19209 if (retval != NULL)
19210 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019211 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019212 if (expr_start != NULL)
19213 {
19214 /* Further expansion! */
19215 temp_result = make_expanded_name(retval, expr_start,
19216 expr_end, temp_result);
19217 vim_free(retval);
19218 retval = temp_result;
19219 }
19220 }
19221
19222 return retval;
19223}
19224
19225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019227 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 */
19229 static int
19230eval_isnamec(c)
19231 int c;
19232{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019233 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19234}
19235
19236/*
19237 * Return TRUE if character "c" can be used as the first character in a
19238 * variable or function name (excluding '{' and '}').
19239 */
19240 static int
19241eval_isnamec1(c)
19242 int c;
19243{
19244 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245}
19246
19247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 * Set number v: variable to "val".
19249 */
19250 void
19251set_vim_var_nr(idx, val)
19252 int idx;
19253 long val;
19254{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019255 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256}
19257
19258/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019259 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260 */
19261 long
19262get_vim_var_nr(idx)
19263 int idx;
19264{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019265 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266}
19267
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019268/*
19269 * Get string v: variable value. Uses a static buffer, can only be used once.
19270 */
19271 char_u *
19272get_vim_var_str(idx)
19273 int idx;
19274{
19275 return get_tv_string(&vimvars[idx].vv_tv);
19276}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019277
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019279 * Get List v: variable value. Caller must take care of reference count when
19280 * needed.
19281 */
19282 list_T *
19283get_vim_var_list(idx)
19284 int idx;
19285{
19286 return vimvars[idx].vv_list;
19287}
19288
19289/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019290 * Set v:char to character "c".
19291 */
19292 void
19293set_vim_var_char(c)
19294 int c;
19295{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019296 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019297
19298#ifdef FEAT_MBYTE
19299 if (has_mbyte)
19300 buf[(*mb_char2bytes)(c, buf)] = NUL;
19301 else
19302#endif
19303 {
19304 buf[0] = c;
19305 buf[1] = NUL;
19306 }
19307 set_vim_var_string(VV_CHAR, buf, -1);
19308}
19309
19310/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019311 * Set v:count to "count" and v:count1 to "count1".
19312 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 */
19314 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019315set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 long count;
19317 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019318 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019320 if (set_prevcount)
19321 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019322 vimvars[VV_COUNT].vv_nr = count;
19323 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324}
19325
19326/*
19327 * Set string v: variable to a copy of "val".
19328 */
19329 void
19330set_vim_var_string(idx, val, len)
19331 int idx;
19332 char_u *val;
19333 int len; /* length of "val" to use or -1 (whole string) */
19334{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019335 /* Need to do this (at least) once, since we can't initialize a union.
19336 * Will always be invoked when "v:progname" is set. */
19337 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19338
Bram Moolenaare9a41262005-01-15 22:18:47 +000019339 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019341 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019343 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019345 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346}
19347
19348/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019349 * Set List v: variable to "val".
19350 */
19351 void
19352set_vim_var_list(idx, val)
19353 int idx;
19354 list_T *val;
19355{
19356 list_unref(vimvars[idx].vv_list);
19357 vimvars[idx].vv_list = val;
19358 if (val != NULL)
19359 ++val->lv_refcount;
19360}
19361
19362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 * Set v:register if needed.
19364 */
19365 void
19366set_reg_var(c)
19367 int c;
19368{
19369 char_u regname;
19370
19371 if (c == 0 || c == ' ')
19372 regname = '"';
19373 else
19374 regname = c;
19375 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019376 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 set_vim_var_string(VV_REG, &regname, 1);
19378}
19379
19380/*
19381 * Get or set v:exception. If "oldval" == NULL, return the current value.
19382 * Otherwise, restore the value to "oldval" and return NULL.
19383 * Must always be called in pairs to save and restore v:exception! Does not
19384 * take care of memory allocations.
19385 */
19386 char_u *
19387v_exception(oldval)
19388 char_u *oldval;
19389{
19390 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019391 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392
Bram Moolenaare9a41262005-01-15 22:18:47 +000019393 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 return NULL;
19395}
19396
19397/*
19398 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19399 * Otherwise, restore the value to "oldval" and return NULL.
19400 * Must always be called in pairs to save and restore v:throwpoint! Does not
19401 * take care of memory allocations.
19402 */
19403 char_u *
19404v_throwpoint(oldval)
19405 char_u *oldval;
19406{
19407 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019408 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409
Bram Moolenaare9a41262005-01-15 22:18:47 +000019410 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 return NULL;
19412}
19413
19414#if defined(FEAT_AUTOCMD) || defined(PROTO)
19415/*
19416 * Set v:cmdarg.
19417 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19418 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19419 * Must always be called in pairs!
19420 */
19421 char_u *
19422set_cmdarg(eap, oldarg)
19423 exarg_T *eap;
19424 char_u *oldarg;
19425{
19426 char_u *oldval;
19427 char_u *newval;
19428 unsigned len;
19429
Bram Moolenaare9a41262005-01-15 22:18:47 +000019430 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019431 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019433 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019434 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019435 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 }
19437
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019438 if (eap->force_bin == FORCE_BIN)
19439 len = 6;
19440 else if (eap->force_bin == FORCE_NOBIN)
19441 len = 8;
19442 else
19443 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019444
19445 if (eap->read_edit)
19446 len += 7;
19447
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019448 if (eap->force_ff != 0)
19449 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19450# ifdef FEAT_MBYTE
19451 if (eap->force_enc != 0)
19452 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019453 if (eap->bad_char != 0)
19454 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019455# endif
19456
19457 newval = alloc(len + 1);
19458 if (newval == NULL)
19459 return NULL;
19460
19461 if (eap->force_bin == FORCE_BIN)
19462 sprintf((char *)newval, " ++bin");
19463 else if (eap->force_bin == FORCE_NOBIN)
19464 sprintf((char *)newval, " ++nobin");
19465 else
19466 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019467
19468 if (eap->read_edit)
19469 STRCAT(newval, " ++edit");
19470
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019471 if (eap->force_ff != 0)
19472 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19473 eap->cmd + eap->force_ff);
19474# ifdef FEAT_MBYTE
19475 if (eap->force_enc != 0)
19476 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19477 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019478 if (eap->bad_char == BAD_KEEP)
19479 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19480 else if (eap->bad_char == BAD_DROP)
19481 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19482 else if (eap->bad_char != 0)
19483 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019484# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019485 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019486 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487}
19488#endif
19489
19490/*
19491 * Get the value of internal variable "name".
19492 * Return OK or FAIL.
19493 */
19494 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019495get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 char_u *name;
19497 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019499 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500{
19501 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 typval_T *tv = NULL;
19503 typval_T atv;
19504 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506
19507 /* truncate the name, so that we can use strcmp() */
19508 cc = name[len];
19509 name[len] = NUL;
19510
19511 /*
19512 * Check for "b:changedtick".
19513 */
19514 if (STRCMP(name, "b:changedtick") == 0)
19515 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019516 atv.v_type = VAR_NUMBER;
19517 atv.vval.v_number = curbuf->b_changedtick;
19518 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 }
19520
19521 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 * Check for user-defined variables.
19523 */
19524 else
19525 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019526 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019528 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 }
19530
Bram Moolenaare9a41262005-01-15 22:18:47 +000019531 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019533 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019534 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 ret = FAIL;
19536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019538 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539
19540 name[len] = cc;
19541
19542 return ret;
19543}
19544
19545/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019546 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19547 * Also handle function call with Funcref variable: func(expr)
19548 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19549 */
19550 static int
19551handle_subscript(arg, rettv, evaluate, verbose)
19552 char_u **arg;
19553 typval_T *rettv;
19554 int evaluate; /* do more than finding the end */
19555 int verbose; /* give error messages */
19556{
19557 int ret = OK;
19558 dict_T *selfdict = NULL;
19559 char_u *s;
19560 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019561 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019562
19563 while (ret == OK
19564 && (**arg == '['
19565 || (**arg == '.' && rettv->v_type == VAR_DICT)
19566 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19567 && !vim_iswhite(*(*arg - 1)))
19568 {
19569 if (**arg == '(')
19570 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019571 /* need to copy the funcref so that we can clear rettv */
19572 functv = *rettv;
19573 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019574
19575 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019576 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019577 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019578 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19579 &len, evaluate, selfdict);
19580
19581 /* Clear the funcref afterwards, so that deleting it while
19582 * evaluating the arguments is possible (see test55). */
19583 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019584
19585 /* Stop the expression evaluation when immediately aborting on
19586 * error, or when an interrupt occurred or an exception was thrown
19587 * but not caught. */
19588 if (aborting())
19589 {
19590 if (ret == OK)
19591 clear_tv(rettv);
19592 ret = FAIL;
19593 }
19594 dict_unref(selfdict);
19595 selfdict = NULL;
19596 }
19597 else /* **arg == '[' || **arg == '.' */
19598 {
19599 dict_unref(selfdict);
19600 if (rettv->v_type == VAR_DICT)
19601 {
19602 selfdict = rettv->vval.v_dict;
19603 if (selfdict != NULL)
19604 ++selfdict->dv_refcount;
19605 }
19606 else
19607 selfdict = NULL;
19608 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19609 {
19610 clear_tv(rettv);
19611 ret = FAIL;
19612 }
19613 }
19614 }
19615 dict_unref(selfdict);
19616 return ret;
19617}
19618
19619/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019620 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019621 * value).
19622 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019625{
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019627}
19628
19629/*
19630 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 * The string "s" must have been allocated, it is consumed.
19632 * Return NULL for out of memory, the variable otherwise.
19633 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 char_u *s;
19637{
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019640 rettv = alloc_tv();
19641 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643 rettv->v_type = VAR_STRING;
19644 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 }
19646 else
19647 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649}
19650
19651/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019652 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019654 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019655free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019656 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657{
19658 if (varp != NULL)
19659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019660 switch (varp->v_type)
19661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019662 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019663 func_unref(varp->vval.v_string);
19664 /*FALLTHROUGH*/
19665 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019666 vim_free(varp->vval.v_string);
19667 break;
19668 case VAR_LIST:
19669 list_unref(varp->vval.v_list);
19670 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019671 case VAR_DICT:
19672 dict_unref(varp->vval.v_dict);
19673 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019674 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019675#ifdef FEAT_FLOAT
19676 case VAR_FLOAT:
19677#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019678 case VAR_UNKNOWN:
19679 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019680 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019681 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019682 break;
19683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 vim_free(varp);
19685 }
19686}
19687
19688/*
19689 * Free the memory for a variable value and set the value to NULL or 0.
19690 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019691 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694{
19695 if (varp != NULL)
19696 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019697 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019699 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019700 func_unref(varp->vval.v_string);
19701 /*FALLTHROUGH*/
19702 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019703 vim_free(varp->vval.v_string);
19704 varp->vval.v_string = NULL;
19705 break;
19706 case VAR_LIST:
19707 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019708 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019710 case VAR_DICT:
19711 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019712 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019713 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019715 varp->vval.v_number = 0;
19716 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019717#ifdef FEAT_FLOAT
19718 case VAR_FLOAT:
19719 varp->vval.v_float = 0.0;
19720 break;
19721#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 case VAR_UNKNOWN:
19723 break;
19724 default:
19725 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019727 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 }
19729}
19730
19731/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 * Set the value of a variable to NULL without freeing items.
19733 */
19734 static void
19735init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019736 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019737{
19738 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019740}
19741
19742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 * Get the number value of a variable.
19744 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019745 * For incompatible types, return 0.
19746 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19747 * caller of incompatible types: it sets *denote to TRUE if "denote"
19748 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 */
19750 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019751get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019754 int error = FALSE;
19755
19756 return get_tv_number_chk(varp, &error); /* return 0L on error */
19757}
19758
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019759 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019760get_tv_number_chk(varp, denote)
19761 typval_T *varp;
19762 int *denote;
19763{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019764 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019766 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019768 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019769 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019770#ifdef FEAT_FLOAT
19771 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019772 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019773 break;
19774#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019775 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019776 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019777 break;
19778 case VAR_STRING:
19779 if (varp->vval.v_string != NULL)
19780 vim_str2nr(varp->vval.v_string, NULL, NULL,
19781 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019782 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019783 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019784 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019785 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019786 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019787 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019788 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019789 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019790 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019791 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019793 if (denote == NULL) /* useful for values that must be unsigned */
19794 n = -1;
19795 else
19796 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019797 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798}
19799
19800/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019801 * Get the lnum from the first argument.
19802 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 */
19805 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019806get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019807 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808{
Bram Moolenaar33570922005-01-25 22:26:29 +000019809 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 linenr_T lnum;
19811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019812 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 if (lnum == 0) /* no valid number, try using line() */
19814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 rettv.v_type = VAR_NUMBER;
19816 f_line(argvars, &rettv);
19817 lnum = rettv.vval.v_number;
19818 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 }
19820 return lnum;
19821}
19822
19823/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019824 * Get the lnum from the first argument.
19825 * Also accepts "$", then "buf" is used.
19826 * Returns 0 on error.
19827 */
19828 static linenr_T
19829get_tv_lnum_buf(argvars, buf)
19830 typval_T *argvars;
19831 buf_T *buf;
19832{
19833 if (argvars[0].v_type == VAR_STRING
19834 && argvars[0].vval.v_string != NULL
19835 && argvars[0].vval.v_string[0] == '$'
19836 && buf != NULL)
19837 return buf->b_ml.ml_line_count;
19838 return get_tv_number_chk(&argvars[0], NULL);
19839}
19840
19841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 * Get the string value of a variable.
19843 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019844 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19845 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 * If the String variable has never been set, return an empty string.
19847 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019848 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19849 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 */
19851 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854{
19855 static char_u mybuf[NUMBUFLEN];
19856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858}
19859
19860 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019863 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019865 char_u *res = get_tv_string_buf_chk(varp, buf);
19866
19867 return res != NULL ? res : (char_u *)"";
19868}
19869
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019870 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019871get_tv_string_chk(varp)
19872 typval_T *varp;
19873{
19874 static char_u mybuf[NUMBUFLEN];
19875
19876 return get_tv_string_buf_chk(varp, mybuf);
19877}
19878
19879 static char_u *
19880get_tv_string_buf_chk(varp, buf)
19881 typval_T *varp;
19882 char_u *buf;
19883{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019884 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019886 case VAR_NUMBER:
19887 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19888 return buf;
19889 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019890 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891 break;
19892 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019893 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019894 break;
19895 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019896 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019897 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019898#ifdef FEAT_FLOAT
19899 case VAR_FLOAT:
19900 EMSG(_("E806: using Float as a String"));
19901 break;
19902#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019903 case VAR_STRING:
19904 if (varp->vval.v_string != NULL)
19905 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019906 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019911 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912}
19913
19914/*
19915 * Find variable "name" in the list of variables.
19916 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019917 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019918 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019919 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019921 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019922find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928
Bram Moolenaara7043832005-01-21 11:56:39 +000019929 ht = find_var_ht(name, &varname);
19930 if (htp != NULL)
19931 *htp = ht;
19932 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019934 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935}
19936
19937/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019938 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019939 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019942find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019944 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019945 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019946{
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 hashitem_T *hi;
19948
19949 if (*varname == NUL)
19950 {
19951 /* Must be something like "s:", otherwise "ht" would be NULL. */
19952 switch (varname[-2])
19953 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019954 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 case 'g': return &globvars_var;
19956 case 'v': return &vimvars_var;
19957 case 'b': return &curbuf->b_bufvar;
19958 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019959#ifdef FEAT_WINDOWS
19960 case 't': return &curtab->tp_winvar;
19961#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019962 case 'l': return current_funccal == NULL
19963 ? NULL : &current_funccal->l_vars_var;
19964 case 'a': return current_funccal == NULL
19965 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 }
19967 return NULL;
19968 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019969
19970 hi = hash_find(ht, varname);
19971 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019972 {
19973 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019974 * worked find the variable again. Don't auto-load a script if it was
19975 * loaded already, otherwise it would be loaded every time when
19976 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019977 if (ht == &globvarht && !writing)
19978 {
19979 /* Note: script_autoload() may make "hi" invalid. It must either
19980 * be obtained again or not used. */
19981 if (!script_autoload(varname, FALSE) || aborting())
19982 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019983 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019984 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019985 if (HASHITEM_EMPTY(hi))
19986 return NULL;
19987 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019989}
19990
19991/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019992 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019993 * Set "varname" to the start of name without ':'.
19994 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019995 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019996find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 char_u *name;
19998 char_u **varname;
19999{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020000 hashitem_T *hi;
20001
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 if (name[1] != ':')
20003 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020004 /* The name must not start with a colon or #. */
20005 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 return NULL;
20007 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020008
20009 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020010 hi = hash_find(&compat_hashtab, name);
20011 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020012 return &compat_hashtab;
20013
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020015 return &globvarht; /* global variable */
20016 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 }
20018 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020019 if (*name == 'g') /* global variable */
20020 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020021 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20022 */
20023 if (vim_strchr(name + 2, ':') != NULL
20024 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020025 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020027 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020030#ifdef FEAT_WINDOWS
20031 if (*name == 't') /* tab page variable */
20032 return &curtab->tp_vars.dv_hashtab;
20033#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 if (*name == 'v') /* v: variable */
20035 return &vimvarht;
20036 if (*name == 'a' && current_funccal != NULL) /* function argument */
20037 return &current_funccal->l_avars.dv_hashtab;
20038 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20039 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 if (*name == 's' /* script variable */
20041 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20042 return &SCRIPT_VARS(current_SID);
20043 return NULL;
20044}
20045
20046/*
20047 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020048 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 * Returns NULL when it doesn't exist.
20050 */
20051 char_u *
20052get_var_value(name)
20053 char_u *name;
20054{
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056
Bram Moolenaara7043832005-01-21 11:56:39 +000020057 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 if (v == NULL)
20059 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061}
20062
20063/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 * sourcing this script and when executing functions defined in the script.
20066 */
20067 void
20068new_script_vars(id)
20069 scid_T id;
20070{
Bram Moolenaara7043832005-01-21 11:56:39 +000020071 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 hashtab_T *ht;
20073 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020074
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20076 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020077 /* Re-allocating ga_data means that an ht_array pointing to
20078 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020079 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020080 for (i = 1; i <= ga_scripts.ga_len; ++i)
20081 {
20082 ht = &SCRIPT_VARS(i);
20083 if (ht->ht_mask == HT_INIT_SIZE - 1)
20084 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020085 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020087 }
20088
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 while (ga_scripts.ga_len < id)
20090 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020091 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020092 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020093 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 }
20096 }
20097}
20098
20099/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20101 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 */
20103 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020104init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020105 dict_T *dict;
20106 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020107 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108{
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020110 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020111 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020112 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020113 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 dict_var->di_tv.vval.v_dict = dict;
20115 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020116 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20118 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119}
20120
20121/*
20122 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020123 * Frees all allocated variables and the value they contain.
20124 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 */
20126 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020127vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 hashtab_T *ht;
20129{
20130 vars_clear_ext(ht, TRUE);
20131}
20132
20133/*
20134 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20135 */
20136 static void
20137vars_clear_ext(ht, free_val)
20138 hashtab_T *ht;
20139 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140{
Bram Moolenaara7043832005-01-21 11:56:39 +000020141 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 hashitem_T *hi;
20143 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020146 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020147 for (hi = ht->ht_array; todo > 0; ++hi)
20148 {
20149 if (!HASHITEM_EMPTY(hi))
20150 {
20151 --todo;
20152
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020154 * ht_array might change then. hash_clear() takes care of it
20155 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 v = HI2DI(hi);
20157 if (free_val)
20158 clear_tv(&v->di_tv);
20159 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20160 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020161 }
20162 }
20163 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020164 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165}
20166
Bram Moolenaara7043832005-01-21 11:56:39 +000020167/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 * Delete a variable from hashtab "ht" at item "hi".
20169 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020172delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 hashtab_T *ht;
20174 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175{
Bram Moolenaar33570922005-01-25 22:26:29 +000020176 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020177
20178 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020179 clear_tv(&di->di_tv);
20180 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181}
20182
20183/*
20184 * List the value of one internal variable.
20185 */
20186 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020187list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020188 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020190 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020192 char_u *tofree;
20193 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020194 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020195
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020196 current_copyID += COPYID_INC;
20197 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020199 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020200 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201}
20202
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020204list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 char_u *prefix;
20206 char_u *name;
20207 int type;
20208 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020209 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210{
Bram Moolenaar31859182007-08-14 20:41:13 +000020211 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20212 msg_start();
20213 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 if (name != NULL) /* "a:" vars don't have a name stored */
20215 msg_puts(name);
20216 msg_putchar(' ');
20217 msg_advance(22);
20218 if (type == VAR_NUMBER)
20219 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020220 else if (type == VAR_FUNC)
20221 msg_putchar('*');
20222 else if (type == VAR_LIST)
20223 {
20224 msg_putchar('[');
20225 if (*string == '[')
20226 ++string;
20227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020228 else if (type == VAR_DICT)
20229 {
20230 msg_putchar('{');
20231 if (*string == '{')
20232 ++string;
20233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 else
20235 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020236
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020238
20239 if (type == VAR_FUNC)
20240 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020241 if (*first)
20242 {
20243 msg_clr_eos();
20244 *first = FALSE;
20245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246}
20247
20248/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 * If the variable already exists, the value is updated.
20251 * Otherwise the variable is created.
20252 */
20253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020254set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020256 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258{
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020263 ht = find_var_ht(name, &varname);
20264 if (ht == NULL || *varname == NUL)
20265 {
20266 EMSG2(_(e_illvar), name);
20267 return;
20268 }
20269 v = find_var_in_ht(ht, varname, TRUE);
20270
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020271 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20272 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020273
Bram Moolenaar33570922005-01-25 22:26:29 +000020274 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020276 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020277 if (var_check_ro(v->di_flags, name)
20278 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020279 return;
20280 if (v->di_tv.v_type != tv->v_type
20281 && !((v->di_tv.v_type == VAR_STRING
20282 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020283 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020284 || tv->v_type == VAR_NUMBER))
20285#ifdef FEAT_FLOAT
20286 && !((v->di_tv.v_type == VAR_NUMBER
20287 || v->di_tv.v_type == VAR_FLOAT)
20288 && (tv->v_type == VAR_NUMBER
20289 || tv->v_type == VAR_FLOAT))
20290#endif
20291 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020293 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020294 return;
20295 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020296
20297 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020298 * Handle setting internal v: variables separately: we don't change
20299 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 */
20301 if (ht == &vimvarht)
20302 {
20303 if (v->di_tv.v_type == VAR_STRING)
20304 {
20305 vim_free(v->di_tv.vval.v_string);
20306 if (copy || tv->v_type != VAR_STRING)
20307 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20308 else
20309 {
20310 /* Take over the string to avoid an extra alloc/free. */
20311 v->di_tv.vval.v_string = tv->vval.v_string;
20312 tv->vval.v_string = NULL;
20313 }
20314 }
20315 else if (v->di_tv.v_type != VAR_NUMBER)
20316 EMSG2(_(e_intern2), "set_var()");
20317 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020320 if (STRCMP(varname, "searchforward") == 0)
20321 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20322 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 return;
20324 }
20325
20326 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 }
20328 else /* add a new variable */
20329 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020330 /* Can't add "v:" variable. */
20331 if (ht == &vimvarht)
20332 {
20333 EMSG2(_(e_illvar), name);
20334 return;
20335 }
20336
Bram Moolenaar92124a32005-06-17 22:03:40 +000020337 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020338 if (!valid_varname(varname))
20339 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020340
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020341 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20342 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020343 if (v == NULL)
20344 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020345 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020348 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 return;
20350 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020351 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020353
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020354 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020355 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020356 else
20357 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020358 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020359 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020360 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362}
20363
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020364/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020365 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 * Also give an error message.
20367 */
20368 static int
20369var_check_ro(flags, name)
20370 int flags;
20371 char_u *name;
20372{
20373 if (flags & DI_FLAGS_RO)
20374 {
20375 EMSG2(_(e_readonlyvar), name);
20376 return TRUE;
20377 }
20378 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20379 {
20380 EMSG2(_(e_readonlysbx), name);
20381 return TRUE;
20382 }
20383 return FALSE;
20384}
20385
20386/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020387 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20388 * Also give an error message.
20389 */
20390 static int
20391var_check_fixed(flags, name)
20392 int flags;
20393 char_u *name;
20394{
20395 if (flags & DI_FLAGS_FIX)
20396 {
20397 EMSG2(_("E795: Cannot delete variable %s"), name);
20398 return TRUE;
20399 }
20400 return FALSE;
20401}
20402
20403/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020404 * Check if a funcref is assigned to a valid variable name.
20405 * Return TRUE and give an error if not.
20406 */
20407 static int
20408var_check_func_name(name, new_var)
20409 char_u *name; /* points to start of variable name */
20410 int new_var; /* TRUE when creating the variable */
20411{
20412 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20413 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20414 ? name[2] : name[0]))
20415 {
20416 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20417 name);
20418 return TRUE;
20419 }
20420 /* Don't allow hiding a function. When "v" is not NULL we might be
20421 * assigning another function to the same var, the type is checked
20422 * below. */
20423 if (new_var && function_exists(name))
20424 {
20425 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20426 name);
20427 return TRUE;
20428 }
20429 return FALSE;
20430}
20431
20432/*
20433 * Check if a variable name is valid.
20434 * Return FALSE and give an error if not.
20435 */
20436 static int
20437valid_varname(varname)
20438 char_u *varname;
20439{
20440 char_u *p;
20441
20442 for (p = varname; *p != NUL; ++p)
20443 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20444 && *p != AUTOLOAD_CHAR)
20445 {
20446 EMSG2(_(e_illvar), varname);
20447 return FALSE;
20448 }
20449 return TRUE;
20450}
20451
20452/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020453 * Return TRUE if typeval "tv" is set to be locked (immutable).
20454 * Also give an error message, using "name".
20455 */
20456 static int
20457tv_check_lock(lock, name)
20458 int lock;
20459 char_u *name;
20460{
20461 if (lock & VAR_LOCKED)
20462 {
20463 EMSG2(_("E741: Value is locked: %s"),
20464 name == NULL ? (char_u *)_("Unknown") : name);
20465 return TRUE;
20466 }
20467 if (lock & VAR_FIXED)
20468 {
20469 EMSG2(_("E742: Cannot change value of %s"),
20470 name == NULL ? (char_u *)_("Unknown") : name);
20471 return TRUE;
20472 }
20473 return FALSE;
20474}
20475
20476/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020478 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020479 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020480 * It is OK for "from" and "to" to point to the same item. This is used to
20481 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020482 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020483 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020484copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 typval_T *from;
20486 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020488 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020489 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020490 switch (from->v_type)
20491 {
20492 case VAR_NUMBER:
20493 to->vval.v_number = from->vval.v_number;
20494 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020495#ifdef FEAT_FLOAT
20496 case VAR_FLOAT:
20497 to->vval.v_float = from->vval.v_float;
20498 break;
20499#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020500 case VAR_STRING:
20501 case VAR_FUNC:
20502 if (from->vval.v_string == NULL)
20503 to->vval.v_string = NULL;
20504 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020505 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020506 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020507 if (from->v_type == VAR_FUNC)
20508 func_ref(to->vval.v_string);
20509 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020510 break;
20511 case VAR_LIST:
20512 if (from->vval.v_list == NULL)
20513 to->vval.v_list = NULL;
20514 else
20515 {
20516 to->vval.v_list = from->vval.v_list;
20517 ++to->vval.v_list->lv_refcount;
20518 }
20519 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020520 case VAR_DICT:
20521 if (from->vval.v_dict == NULL)
20522 to->vval.v_dict = NULL;
20523 else
20524 {
20525 to->vval.v_dict = from->vval.v_dict;
20526 ++to->vval.v_dict->dv_refcount;
20527 }
20528 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020529 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020530 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020531 break;
20532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533}
20534
20535/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020536 * Make a copy of an item.
20537 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020538 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20539 * reference to an already copied list/dict can be used.
20540 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020541 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020542 static int
20543item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020544 typval_T *from;
20545 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020546 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020547 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020548{
20549 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020550 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020551
Bram Moolenaar33570922005-01-25 22:26:29 +000020552 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020553 {
20554 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020555 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020556 }
20557 ++recurse;
20558
20559 switch (from->v_type)
20560 {
20561 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020562#ifdef FEAT_FLOAT
20563 case VAR_FLOAT:
20564#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020565 case VAR_STRING:
20566 case VAR_FUNC:
20567 copy_tv(from, to);
20568 break;
20569 case VAR_LIST:
20570 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020571 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020572 if (from->vval.v_list == NULL)
20573 to->vval.v_list = NULL;
20574 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20575 {
20576 /* use the copy made earlier */
20577 to->vval.v_list = from->vval.v_list->lv_copylist;
20578 ++to->vval.v_list->lv_refcount;
20579 }
20580 else
20581 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20582 if (to->vval.v_list == NULL)
20583 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020584 break;
20585 case VAR_DICT:
20586 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020587 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020588 if (from->vval.v_dict == NULL)
20589 to->vval.v_dict = NULL;
20590 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20591 {
20592 /* use the copy made earlier */
20593 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20594 ++to->vval.v_dict->dv_refcount;
20595 }
20596 else
20597 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20598 if (to->vval.v_dict == NULL)
20599 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020600 break;
20601 default:
20602 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020603 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020604 }
20605 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020606 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020607}
20608
20609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 * ":echo expr1 ..." print each argument separated with a space, add a
20611 * newline at the end.
20612 * ":echon expr1 ..." print each argument plain.
20613 */
20614 void
20615ex_echo(eap)
20616 exarg_T *eap;
20617{
20618 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020619 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020620 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 char_u *p;
20622 int needclr = TRUE;
20623 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625
20626 if (eap->skip)
20627 ++emsg_skip;
20628 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20629 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020630 /* If eval1() causes an error message the text from the command may
20631 * still need to be cleared. E.g., "echo 22,44". */
20632 need_clr_eos = needclr;
20633
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020635 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 {
20637 /*
20638 * Report the invalid expression unless the expression evaluation
20639 * has been cancelled due to an aborting error, an interrupt, or an
20640 * exception.
20641 */
20642 if (!aborting())
20643 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020644 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 break;
20646 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020647 need_clr_eos = FALSE;
20648
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 if (!eap->skip)
20650 {
20651 if (atstart)
20652 {
20653 atstart = FALSE;
20654 /* Call msg_start() after eval1(), evaluating the expression
20655 * may cause a message to appear. */
20656 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020657 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020658 /* Mark the saved text as finishing the line, so that what
20659 * follows is displayed on a new line when scrolling back
20660 * at the more prompt. */
20661 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 }
20665 else if (eap->cmdidx == CMD_echo)
20666 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020667 current_copyID += COPYID_INC;
20668 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020669 if (p != NULL)
20670 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020672 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 if (*p != TAB && needclr)
20675 {
20676 /* remove any text still there from the command */
20677 msg_clr_eos();
20678 needclr = FALSE;
20679 }
20680 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 }
20682 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020683 {
20684#ifdef FEAT_MBYTE
20685 if (has_mbyte)
20686 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020687 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020688
20689 (void)msg_outtrans_len_attr(p, i, echo_attr);
20690 p += i - 1;
20691 }
20692 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020694 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020697 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 arg = skipwhite(arg);
20701 }
20702 eap->nextcmd = check_nextcmd(arg);
20703
20704 if (eap->skip)
20705 --emsg_skip;
20706 else
20707 {
20708 /* remove text that may still be there from the command */
20709 if (needclr)
20710 msg_clr_eos();
20711 if (eap->cmdidx == CMD_echo)
20712 msg_end();
20713 }
20714}
20715
20716/*
20717 * ":echohl {name}".
20718 */
20719 void
20720ex_echohl(eap)
20721 exarg_T *eap;
20722{
20723 int id;
20724
20725 id = syn_name2id(eap->arg);
20726 if (id == 0)
20727 echo_attr = 0;
20728 else
20729 echo_attr = syn_id2attr(id);
20730}
20731
20732/*
20733 * ":execute expr1 ..." execute the result of an expression.
20734 * ":echomsg expr1 ..." Print a message
20735 * ":echoerr expr1 ..." Print an error
20736 * Each gets spaces around each argument and a newline at the end for
20737 * echo commands
20738 */
20739 void
20740ex_execute(eap)
20741 exarg_T *eap;
20742{
20743 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020744 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 int ret = OK;
20746 char_u *p;
20747 garray_T ga;
20748 int len;
20749 int save_did_emsg;
20750
20751 ga_init2(&ga, 1, 80);
20752
20753 if (eap->skip)
20754 ++emsg_skip;
20755 while (*arg != NUL && *arg != '|' && *arg != '\n')
20756 {
20757 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020758 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 {
20760 /*
20761 * Report the invalid expression unless the expression evaluation
20762 * has been cancelled due to an aborting error, an interrupt, or an
20763 * exception.
20764 */
20765 if (!aborting())
20766 EMSG2(_(e_invexpr2), p);
20767 ret = FAIL;
20768 break;
20769 }
20770
20771 if (!eap->skip)
20772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020773 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 len = (int)STRLEN(p);
20775 if (ga_grow(&ga, len + 2) == FAIL)
20776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020777 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 ret = FAIL;
20779 break;
20780 }
20781 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 ga.ga_len += len;
20785 }
20786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020787 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 arg = skipwhite(arg);
20789 }
20790
20791 if (ret != FAIL && ga.ga_data != NULL)
20792 {
20793 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020796 out_flush();
20797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 else if (eap->cmdidx == CMD_echoerr)
20799 {
20800 /* We don't want to abort following commands, restore did_emsg. */
20801 save_did_emsg = did_emsg;
20802 EMSG((char_u *)ga.ga_data);
20803 if (!force_abort)
20804 did_emsg = save_did_emsg;
20805 }
20806 else if (eap->cmdidx == CMD_execute)
20807 do_cmdline((char_u *)ga.ga_data,
20808 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20809 }
20810
20811 ga_clear(&ga);
20812
20813 if (eap->skip)
20814 --emsg_skip;
20815
20816 eap->nextcmd = check_nextcmd(arg);
20817}
20818
20819/*
20820 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20821 * "arg" points to the "&" or '+' when called, to "option" when returning.
20822 * Returns NULL when no option name found. Otherwise pointer to the char
20823 * after the option name.
20824 */
20825 static char_u *
20826find_option_end(arg, opt_flags)
20827 char_u **arg;
20828 int *opt_flags;
20829{
20830 char_u *p = *arg;
20831
20832 ++p;
20833 if (*p == 'g' && p[1] == ':')
20834 {
20835 *opt_flags = OPT_GLOBAL;
20836 p += 2;
20837 }
20838 else if (*p == 'l' && p[1] == ':')
20839 {
20840 *opt_flags = OPT_LOCAL;
20841 p += 2;
20842 }
20843 else
20844 *opt_flags = 0;
20845
20846 if (!ASCII_ISALPHA(*p))
20847 return NULL;
20848 *arg = p;
20849
20850 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20851 p += 4; /* termcap option */
20852 else
20853 while (ASCII_ISALPHA(*p))
20854 ++p;
20855 return p;
20856}
20857
20858/*
20859 * ":function"
20860 */
20861 void
20862ex_function(eap)
20863 exarg_T *eap;
20864{
20865 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020866 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 int j;
20868 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 char_u *name = NULL;
20871 char_u *p;
20872 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020873 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 garray_T newargs;
20875 garray_T newlines;
20876 int varargs = FALSE;
20877 int mustend = FALSE;
20878 int flags = 0;
20879 ufunc_T *fp;
20880 int indent;
20881 int nesting;
20882 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020883 dictitem_T *v;
20884 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020885 static int func_nr = 0; /* number for nameless function */
20886 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020887 hashtab_T *ht;
20888 int todo;
20889 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020890 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891
20892 /*
20893 * ":function" without argument: list functions.
20894 */
20895 if (ends_excmd(*eap->arg))
20896 {
20897 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020898 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020899 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020900 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020901 {
20902 if (!HASHITEM_EMPTY(hi))
20903 {
20904 --todo;
20905 fp = HI2UF(hi);
20906 if (!isdigit(*fp->uf_name))
20907 list_func_head(fp, FALSE);
20908 }
20909 }
20910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 eap->nextcmd = check_nextcmd(eap->arg);
20912 return;
20913 }
20914
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020915 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020916 * ":function /pat": list functions matching pattern.
20917 */
20918 if (*eap->arg == '/')
20919 {
20920 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20921 if (!eap->skip)
20922 {
20923 regmatch_T regmatch;
20924
20925 c = *p;
20926 *p = NUL;
20927 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20928 *p = c;
20929 if (regmatch.regprog != NULL)
20930 {
20931 regmatch.rm_ic = p_ic;
20932
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020933 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020934 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20935 {
20936 if (!HASHITEM_EMPTY(hi))
20937 {
20938 --todo;
20939 fp = HI2UF(hi);
20940 if (!isdigit(*fp->uf_name)
20941 && vim_regexec(&regmatch, fp->uf_name, 0))
20942 list_func_head(fp, FALSE);
20943 }
20944 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020945 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020946 }
20947 }
20948 if (*p == '/')
20949 ++p;
20950 eap->nextcmd = check_nextcmd(p);
20951 return;
20952 }
20953
20954 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020955 * Get the function name. There are these situations:
20956 * func normal function name
20957 * "name" == func, "fudi.fd_dict" == NULL
20958 * dict.func new dictionary entry
20959 * "name" == NULL, "fudi.fd_dict" set,
20960 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20961 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020962 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020963 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20964 * dict.func existing dict entry that's not a Funcref
20965 * "name" == NULL, "fudi.fd_dict" set,
20966 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020969 name = trans_function_name(&p, eap->skip, 0, &fudi);
20970 paren = (vim_strchr(p, '(') != NULL);
20971 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 {
20973 /*
20974 * Return on an invalid expression in braces, unless the expression
20975 * evaluation has been cancelled due to an aborting error, an
20976 * interrupt, or an exception.
20977 */
20978 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020979 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020980 if (!eap->skip && fudi.fd_newkey != NULL)
20981 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020982 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 else
20986 eap->skip = TRUE;
20987 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020988
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 /* An error in a function call during evaluation of an expression in magic
20990 * braces should not cause the function not to be defined. */
20991 saved_did_emsg = did_emsg;
20992 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993
20994 /*
20995 * ":function func" with only function name: list function.
20996 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020997 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 {
20999 if (!ends_excmd(*skipwhite(p)))
21000 {
21001 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021002 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 }
21004 eap->nextcmd = check_nextcmd(p);
21005 if (eap->nextcmd != NULL)
21006 *p = NUL;
21007 if (!eap->skip && !got_int)
21008 {
21009 fp = find_func(name);
21010 if (fp != NULL)
21011 {
21012 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021013 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021015 if (FUNCLINE(fp, j) == NULL)
21016 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 msg_putchar('\n');
21018 msg_outnum((long)(j + 1));
21019 if (j < 9)
21020 msg_putchar(' ');
21021 if (j < 99)
21022 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021023 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 out_flush(); /* show a line at a time */
21025 ui_breakcheck();
21026 }
21027 if (!got_int)
21028 {
21029 msg_putchar('\n');
21030 msg_puts((char_u *)" endfunction");
21031 }
21032 }
21033 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021034 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021036 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 }
21038
21039 /*
21040 * ":function name(arg1, arg2)" Define function.
21041 */
21042 p = skipwhite(p);
21043 if (*p != '(')
21044 {
21045 if (!eap->skip)
21046 {
21047 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021048 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 }
21050 /* attempt to continue by skipping some text */
21051 if (vim_strchr(p, '(') != NULL)
21052 p = vim_strchr(p, '(');
21053 }
21054 p = skipwhite(p + 1);
21055
21056 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21057 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21058
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021059 if (!eap->skip)
21060 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021061 /* Check the name of the function. Unless it's a dictionary function
21062 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021063 if (name != NULL)
21064 arg = name;
21065 else
21066 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021067 if (arg != NULL && (fudi.fd_di == NULL
21068 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021069 {
21070 if (*arg == K_SPECIAL)
21071 j = 3;
21072 else
21073 j = 0;
21074 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21075 : eval_isnamec(arg[j])))
21076 ++j;
21077 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021078 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021079 }
21080 }
21081
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 /*
21083 * Isolate the arguments: "arg1, arg2, ...)"
21084 */
21085 while (*p != ')')
21086 {
21087 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21088 {
21089 varargs = TRUE;
21090 p += 3;
21091 mustend = TRUE;
21092 }
21093 else
21094 {
21095 arg = p;
21096 while (ASCII_ISALNUM(*p) || *p == '_')
21097 ++p;
21098 if (arg == p || isdigit(*arg)
21099 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21100 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21101 {
21102 if (!eap->skip)
21103 EMSG2(_("E125: Illegal argument: %s"), arg);
21104 break;
21105 }
21106 if (ga_grow(&newargs, 1) == FAIL)
21107 goto erret;
21108 c = *p;
21109 *p = NUL;
21110 arg = vim_strsave(arg);
21111 if (arg == NULL)
21112 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021113
21114 /* Check for duplicate argument name. */
21115 for (i = 0; i < newargs.ga_len; ++i)
21116 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21117 {
21118 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21119 goto erret;
21120 }
21121
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21123 *p = c;
21124 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 if (*p == ',')
21126 ++p;
21127 else
21128 mustend = TRUE;
21129 }
21130 p = skipwhite(p);
21131 if (mustend && *p != ')')
21132 {
21133 if (!eap->skip)
21134 EMSG2(_(e_invarg2), eap->arg);
21135 break;
21136 }
21137 }
21138 ++p; /* skip the ')' */
21139
Bram Moolenaare9a41262005-01-15 22:18:47 +000021140 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 for (;;)
21142 {
21143 p = skipwhite(p);
21144 if (STRNCMP(p, "range", 5) == 0)
21145 {
21146 flags |= FC_RANGE;
21147 p += 5;
21148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021149 else if (STRNCMP(p, "dict", 4) == 0)
21150 {
21151 flags |= FC_DICT;
21152 p += 4;
21153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 else if (STRNCMP(p, "abort", 5) == 0)
21155 {
21156 flags |= FC_ABORT;
21157 p += 5;
21158 }
21159 else
21160 break;
21161 }
21162
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021163 /* When there is a line break use what follows for the function body.
21164 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21165 if (*p == '\n')
21166 line_arg = p + 1;
21167 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 EMSG(_(e_trailing));
21169
21170 /*
21171 * Read the body of the function, until ":endfunction" is found.
21172 */
21173 if (KeyTyped)
21174 {
21175 /* Check if the function already exists, don't let the user type the
21176 * whole function before telling him it doesn't work! For a script we
21177 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021178 if (!eap->skip && !eap->forceit)
21179 {
21180 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21181 EMSG(_(e_funcdict));
21182 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021183 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021186 if (!eap->skip && did_emsg)
21187 goto erret;
21188
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 msg_putchar('\n'); /* don't overwrite the function name */
21190 cmdline_row = msg_row;
21191 }
21192
21193 indent = 2;
21194 nesting = 0;
21195 for (;;)
21196 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021197 if (KeyTyped)
21198 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021200 sourcing_lnum_off = sourcing_lnum;
21201
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021202 if (line_arg != NULL)
21203 {
21204 /* Use eap->arg, split up in parts by line breaks. */
21205 theline = line_arg;
21206 p = vim_strchr(theline, '\n');
21207 if (p == NULL)
21208 line_arg += STRLEN(line_arg);
21209 else
21210 {
21211 *p = NUL;
21212 line_arg = p + 1;
21213 }
21214 }
21215 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 theline = getcmdline(':', 0L, indent);
21217 else
21218 theline = eap->getline(':', eap->cookie, indent);
21219 if (KeyTyped)
21220 lines_left = Rows - 1;
21221 if (theline == NULL)
21222 {
21223 EMSG(_("E126: Missing :endfunction"));
21224 goto erret;
21225 }
21226
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021227 /* Detect line continuation: sourcing_lnum increased more than one. */
21228 if (sourcing_lnum > sourcing_lnum_off + 1)
21229 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21230 else
21231 sourcing_lnum_off = 0;
21232
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 if (skip_until != NULL)
21234 {
21235 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21236 * don't check for ":endfunc". */
21237 if (STRCMP(theline, skip_until) == 0)
21238 {
21239 vim_free(skip_until);
21240 skip_until = NULL;
21241 }
21242 }
21243 else
21244 {
21245 /* skip ':' and blanks*/
21246 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21247 ;
21248
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021249 /* Check for "endfunction". */
21250 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021252 if (line_arg == NULL)
21253 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 break;
21255 }
21256
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021257 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 * at "end". */
21259 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21260 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021261 else if (STRNCMP(p, "if", 2) == 0
21262 || STRNCMP(p, "wh", 2) == 0
21263 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 || STRNCMP(p, "try", 3) == 0)
21265 indent += 2;
21266
21267 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021268 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021270 if (*p == '!')
21271 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 p += eval_fname_script(p);
21273 if (ASCII_ISALPHA(*p))
21274 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021275 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 if (*skipwhite(p) == '(')
21277 {
21278 ++nesting;
21279 indent += 2;
21280 }
21281 }
21282 }
21283
21284 /* Check for ":append" or ":insert". */
21285 p = skip_range(p, NULL);
21286 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21287 || (p[0] == 'i'
21288 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21289 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21290 skip_until = vim_strsave((char_u *)".");
21291
21292 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21293 arg = skipwhite(skiptowhite(p));
21294 if (arg[0] == '<' && arg[1] =='<'
21295 && ((p[0] == 'p' && p[1] == 'y'
21296 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21297 || (p[0] == 'p' && p[1] == 'e'
21298 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21299 || (p[0] == 't' && p[1] == 'c'
21300 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021301 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21302 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21304 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021305 || (p[0] == 'm' && p[1] == 'z'
21306 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 ))
21308 {
21309 /* ":python <<" continues until a dot, like ":append" */
21310 p = skipwhite(arg + 2);
21311 if (*p == NUL)
21312 skip_until = vim_strsave((char_u *)".");
21313 else
21314 skip_until = vim_strsave(p);
21315 }
21316 }
21317
21318 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021319 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021320 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021321 if (line_arg == NULL)
21322 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021324 }
21325
21326 /* Copy the line to newly allocated memory. get_one_sourceline()
21327 * allocates 250 bytes per line, this saves 80% on average. The cost
21328 * is an extra alloc/free. */
21329 p = vim_strsave(theline);
21330 if (p != NULL)
21331 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021332 if (line_arg == NULL)
21333 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021334 theline = p;
21335 }
21336
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021337 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21338
21339 /* Add NULL lines for continuation lines, so that the line count is
21340 * equal to the index in the growarray. */
21341 while (sourcing_lnum_off-- > 0)
21342 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021343
21344 /* Check for end of eap->arg. */
21345 if (line_arg != NULL && *line_arg == NUL)
21346 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 }
21348
21349 /* Don't define the function when skipping commands or when an error was
21350 * detected. */
21351 if (eap->skip || did_emsg)
21352 goto erret;
21353
21354 /*
21355 * If there are no errors, add the function
21356 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021357 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021358 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021359 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021360 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021361 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021362 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021363 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021364 goto erret;
21365 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021366
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021367 fp = find_func(name);
21368 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 if (!eap->forceit)
21371 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021372 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021373 goto erret;
21374 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021375 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021376 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021377 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021378 name);
21379 goto erret;
21380 }
21381 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021382 ga_clear_strings(&(fp->uf_args));
21383 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021384 vim_free(name);
21385 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 }
21388 else
21389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021390 char numbuf[20];
21391
21392 fp = NULL;
21393 if (fudi.fd_newkey == NULL && !eap->forceit)
21394 {
21395 EMSG(_(e_funcdict));
21396 goto erret;
21397 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021398 if (fudi.fd_di == NULL)
21399 {
21400 /* Can't add a function to a locked dictionary */
21401 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21402 goto erret;
21403 }
21404 /* Can't change an existing function if it is locked */
21405 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21406 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021407
21408 /* Give the function a sequential number. Can only be used with a
21409 * Funcref! */
21410 vim_free(name);
21411 sprintf(numbuf, "%d", ++func_nr);
21412 name = vim_strsave((char_u *)numbuf);
21413 if (name == NULL)
21414 goto erret;
21415 }
21416
21417 if (fp == NULL)
21418 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021419 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021420 {
21421 int slen, plen;
21422 char_u *scriptname;
21423
21424 /* Check that the autoload name matches the script name. */
21425 j = FAIL;
21426 if (sourcing_name != NULL)
21427 {
21428 scriptname = autoload_name(name);
21429 if (scriptname != NULL)
21430 {
21431 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021432 plen = (int)STRLEN(p);
21433 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021434 if (slen > plen && fnamecmp(p,
21435 sourcing_name + slen - plen) == 0)
21436 j = OK;
21437 vim_free(scriptname);
21438 }
21439 }
21440 if (j == FAIL)
21441 {
21442 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21443 goto erret;
21444 }
21445 }
21446
21447 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 if (fp == NULL)
21449 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021450
21451 if (fudi.fd_dict != NULL)
21452 {
21453 if (fudi.fd_di == NULL)
21454 {
21455 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021456 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021457 if (fudi.fd_di == NULL)
21458 {
21459 vim_free(fp);
21460 goto erret;
21461 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021462 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21463 {
21464 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021465 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021466 goto erret;
21467 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021468 }
21469 else
21470 /* overwrite existing dict entry */
21471 clear_tv(&fudi.fd_di->di_tv);
21472 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021473 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021474 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021475 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021476
21477 /* behave like "dict" was used */
21478 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021479 }
21480
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021482 STRCPY(fp->uf_name, name);
21483 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021485 fp->uf_args = newargs;
21486 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021487#ifdef FEAT_PROFILE
21488 fp->uf_tml_count = NULL;
21489 fp->uf_tml_total = NULL;
21490 fp->uf_tml_self = NULL;
21491 fp->uf_profiling = FALSE;
21492 if (prof_def_func())
21493 func_do_profile(fp);
21494#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021495 fp->uf_varargs = varargs;
21496 fp->uf_flags = flags;
21497 fp->uf_calls = 0;
21498 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021499 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500
21501erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 ga_clear_strings(&newargs);
21503 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504ret_free:
21505 vim_free(skip_until);
21506 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509}
21510
21511/*
21512 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021513 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515 * flags:
21516 * TFN_INT: internal function name OK
21517 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 * Advances "pp" to just after the function name (if no error).
21519 */
21520 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021521trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 char_u **pp;
21523 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021524 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021527 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 char_u *start;
21529 char_u *end;
21530 int lead;
21531 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021534
21535 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021536 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021538
21539 /* Check for hard coded <SNR>: already translated function ID (from a user
21540 * command). */
21541 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21542 && (*pp)[2] == (int)KE_SNR)
21543 {
21544 *pp += 3;
21545 len = get_id_len(pp) + 3;
21546 return vim_strnsave(start, len);
21547 }
21548
21549 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21550 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021552 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021554
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021555 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21556 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021557 if (end == start)
21558 {
21559 if (!skip)
21560 EMSG(_("E129: Function name required"));
21561 goto theend;
21562 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021563 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021564 {
21565 /*
21566 * Report an invalid expression in braces, unless the expression
21567 * evaluation has been cancelled due to an aborting error, an
21568 * interrupt, or an exception.
21569 */
21570 if (!aborting())
21571 {
21572 if (end != NULL)
21573 EMSG2(_(e_invarg2), start);
21574 }
21575 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021576 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021577 goto theend;
21578 }
21579
21580 if (lv.ll_tv != NULL)
21581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021582 if (fdp != NULL)
21583 {
21584 fdp->fd_dict = lv.ll_dict;
21585 fdp->fd_newkey = lv.ll_newkey;
21586 lv.ll_newkey = NULL;
21587 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021588 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021589 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21590 {
21591 name = vim_strsave(lv.ll_tv->vval.v_string);
21592 *pp = end;
21593 }
21594 else
21595 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021596 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21597 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021598 EMSG(_(e_funcref));
21599 else
21600 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021601 name = NULL;
21602 }
21603 goto theend;
21604 }
21605
21606 if (lv.ll_name == NULL)
21607 {
21608 /* Error found, but continue after the function name. */
21609 *pp = end;
21610 goto theend;
21611 }
21612
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021613 /* Check if the name is a Funcref. If so, use the value. */
21614 if (lv.ll_exp_name != NULL)
21615 {
21616 len = (int)STRLEN(lv.ll_exp_name);
21617 name = deref_func_name(lv.ll_exp_name, &len);
21618 if (name == lv.ll_exp_name)
21619 name = NULL;
21620 }
21621 else
21622 {
21623 len = (int)(end - *pp);
21624 name = deref_func_name(*pp, &len);
21625 if (name == *pp)
21626 name = NULL;
21627 }
21628 if (name != NULL)
21629 {
21630 name = vim_strsave(name);
21631 *pp = end;
21632 goto theend;
21633 }
21634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021635 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021636 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021637 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021638 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21639 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21640 {
21641 /* When there was "s:" already or the name expanded to get a
21642 * leading "s:" then remove it. */
21643 lv.ll_name += 2;
21644 len -= 2;
21645 lead = 2;
21646 }
21647 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021648 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021649 {
21650 if (lead == 2) /* skip over "s:" */
21651 lv.ll_name += 2;
21652 len = (int)(end - lv.ll_name);
21653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021654
21655 /*
21656 * Copy the function name to allocated memory.
21657 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21658 * Accept <SNR>123_name() outside a script.
21659 */
21660 if (skip)
21661 lead = 0; /* do nothing */
21662 else if (lead > 0)
21663 {
21664 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021665 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21666 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021667 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021668 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021669 if (current_SID <= 0)
21670 {
21671 EMSG(_(e_usingsid));
21672 goto theend;
21673 }
21674 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21675 lead += (int)STRLEN(sid_buf);
21676 }
21677 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021678 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021679 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021680 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021681 goto theend;
21682 }
21683 name = alloc((unsigned)(len + lead + 1));
21684 if (name != NULL)
21685 {
21686 if (lead > 0)
21687 {
21688 name[0] = K_SPECIAL;
21689 name[1] = KS_EXTRA;
21690 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021691 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021692 STRCPY(name + 3, sid_buf);
21693 }
21694 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21695 name[len + lead] = NUL;
21696 }
21697 *pp = end;
21698
21699theend:
21700 clear_lval(&lv);
21701 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702}
21703
21704/*
21705 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21706 * Return 2 if "p" starts with "s:".
21707 * Return 0 otherwise.
21708 */
21709 static int
21710eval_fname_script(p)
21711 char_u *p;
21712{
21713 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21714 || STRNICMP(p + 1, "SNR>", 4) == 0))
21715 return 5;
21716 if (p[0] == 's' && p[1] == ':')
21717 return 2;
21718 return 0;
21719}
21720
21721/*
21722 * Return TRUE if "p" starts with "<SID>" or "s:".
21723 * Only works if eval_fname_script() returned non-zero for "p"!
21724 */
21725 static int
21726eval_fname_sid(p)
21727 char_u *p;
21728{
21729 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21730}
21731
21732/*
21733 * List the head of the function: "name(arg1, arg2)".
21734 */
21735 static void
21736list_func_head(fp, indent)
21737 ufunc_T *fp;
21738 int indent;
21739{
21740 int j;
21741
21742 msg_start();
21743 if (indent)
21744 MSG_PUTS(" ");
21745 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021746 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 {
21748 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021749 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 }
21751 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021752 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021754 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 {
21756 if (j)
21757 MSG_PUTS(", ");
21758 msg_puts(FUNCARG(fp, j));
21759 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021760 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 {
21762 if (j)
21763 MSG_PUTS(", ");
21764 MSG_PUTS("...");
21765 }
21766 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021767 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021768 if (p_verbose > 0)
21769 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770}
21771
21772/*
21773 * Find a function by name, return pointer to it in ufuncs.
21774 * Return NULL for unknown function.
21775 */
21776 static ufunc_T *
21777find_func(name)
21778 char_u *name;
21779{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021780 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021782 hi = hash_find(&func_hashtab, name);
21783 if (!HASHITEM_EMPTY(hi))
21784 return HI2UF(hi);
21785 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786}
21787
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021788#if defined(EXITFREE) || defined(PROTO)
21789 void
21790free_all_functions()
21791{
21792 hashitem_T *hi;
21793
21794 /* Need to start all over every time, because func_free() may change the
21795 * hash table. */
21796 while (func_hashtab.ht_used > 0)
21797 for (hi = func_hashtab.ht_array; ; ++hi)
21798 if (!HASHITEM_EMPTY(hi))
21799 {
21800 func_free(HI2UF(hi));
21801 break;
21802 }
21803}
21804#endif
21805
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806/*
21807 * Return TRUE if a function "name" exists.
21808 */
21809 static int
21810function_exists(name)
21811 char_u *name;
21812{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021813 char_u *nm = name;
21814 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021815 int n = FALSE;
21816
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021817 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021818 nm = skipwhite(nm);
21819
21820 /* Only accept "funcname", "funcname ", "funcname (..." and
21821 * "funcname(...", not "funcname!...". */
21822 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021823 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021824 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021825 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021826 else
21827 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021828 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021829 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021830 return n;
21831}
21832
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021833/*
21834 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021835 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021836 */
21837 static int
21838builtin_function(name)
21839 char_u *name;
21840{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021841 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21842 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021843}
21844
Bram Moolenaar05159a02005-02-26 23:04:13 +000021845#if defined(FEAT_PROFILE) || defined(PROTO)
21846/*
21847 * Start profiling function "fp".
21848 */
21849 static void
21850func_do_profile(fp)
21851 ufunc_T *fp;
21852{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021853 int len = fp->uf_lines.ga_len;
21854
21855 if (len == 0)
21856 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021857 fp->uf_tm_count = 0;
21858 profile_zero(&fp->uf_tm_self);
21859 profile_zero(&fp->uf_tm_total);
21860 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021861 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021862 if (fp->uf_tml_total == NULL)
21863 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021864 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021865 if (fp->uf_tml_self == NULL)
21866 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021867 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021868 fp->uf_tml_idx = -1;
21869 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21870 || fp->uf_tml_self == NULL)
21871 return; /* out of memory */
21872
21873 fp->uf_profiling = TRUE;
21874}
21875
21876/*
21877 * Dump the profiling results for all functions in file "fd".
21878 */
21879 void
21880func_dump_profile(fd)
21881 FILE *fd;
21882{
21883 hashitem_T *hi;
21884 int todo;
21885 ufunc_T *fp;
21886 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021887 ufunc_T **sorttab;
21888 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021889
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021890 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021891 if (todo == 0)
21892 return; /* nothing to dump */
21893
Bram Moolenaar73830342005-02-28 22:48:19 +000021894 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21895
Bram Moolenaar05159a02005-02-26 23:04:13 +000021896 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21897 {
21898 if (!HASHITEM_EMPTY(hi))
21899 {
21900 --todo;
21901 fp = HI2UF(hi);
21902 if (fp->uf_profiling)
21903 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021904 if (sorttab != NULL)
21905 sorttab[st_len++] = fp;
21906
Bram Moolenaar05159a02005-02-26 23:04:13 +000021907 if (fp->uf_name[0] == K_SPECIAL)
21908 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21909 else
21910 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21911 if (fp->uf_tm_count == 1)
21912 fprintf(fd, "Called 1 time\n");
21913 else
21914 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21915 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21916 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21917 fprintf(fd, "\n");
21918 fprintf(fd, "count total (s) self (s)\n");
21919
21920 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21921 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021922 if (FUNCLINE(fp, i) == NULL)
21923 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021924 prof_func_line(fd, fp->uf_tml_count[i],
21925 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021926 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21927 }
21928 fprintf(fd, "\n");
21929 }
21930 }
21931 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021932
21933 if (sorttab != NULL && st_len > 0)
21934 {
21935 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21936 prof_total_cmp);
21937 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21938 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21939 prof_self_cmp);
21940 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21941 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021942
21943 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021944}
Bram Moolenaar73830342005-02-28 22:48:19 +000021945
21946 static void
21947prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21948 FILE *fd;
21949 ufunc_T **sorttab;
21950 int st_len;
21951 char *title;
21952 int prefer_self; /* when equal print only self time */
21953{
21954 int i;
21955 ufunc_T *fp;
21956
21957 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21958 fprintf(fd, "count total (s) self (s) function\n");
21959 for (i = 0; i < 20 && i < st_len; ++i)
21960 {
21961 fp = sorttab[i];
21962 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21963 prefer_self);
21964 if (fp->uf_name[0] == K_SPECIAL)
21965 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21966 else
21967 fprintf(fd, " %s()\n", fp->uf_name);
21968 }
21969 fprintf(fd, "\n");
21970}
21971
21972/*
21973 * Print the count and times for one function or function line.
21974 */
21975 static void
21976prof_func_line(fd, count, total, self, prefer_self)
21977 FILE *fd;
21978 int count;
21979 proftime_T *total;
21980 proftime_T *self;
21981 int prefer_self; /* when equal print only self time */
21982{
21983 if (count > 0)
21984 {
21985 fprintf(fd, "%5d ", count);
21986 if (prefer_self && profile_equal(total, self))
21987 fprintf(fd, " ");
21988 else
21989 fprintf(fd, "%s ", profile_msg(total));
21990 if (!prefer_self && profile_equal(total, self))
21991 fprintf(fd, " ");
21992 else
21993 fprintf(fd, "%s ", profile_msg(self));
21994 }
21995 else
21996 fprintf(fd, " ");
21997}
21998
21999/*
22000 * Compare function for total time sorting.
22001 */
22002 static int
22003#ifdef __BORLANDC__
22004_RTLENTRYF
22005#endif
22006prof_total_cmp(s1, s2)
22007 const void *s1;
22008 const void *s2;
22009{
22010 ufunc_T *p1, *p2;
22011
22012 p1 = *(ufunc_T **)s1;
22013 p2 = *(ufunc_T **)s2;
22014 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22015}
22016
22017/*
22018 * Compare function for self time sorting.
22019 */
22020 static int
22021#ifdef __BORLANDC__
22022_RTLENTRYF
22023#endif
22024prof_self_cmp(s1, s2)
22025 const void *s1;
22026 const void *s2;
22027{
22028 ufunc_T *p1, *p2;
22029
22030 p1 = *(ufunc_T **)s1;
22031 p2 = *(ufunc_T **)s2;
22032 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22033}
22034
Bram Moolenaar05159a02005-02-26 23:04:13 +000022035#endif
22036
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022037/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022038 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022039 * Return TRUE if a package was loaded.
22040 */
22041 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022042script_autoload(name, reload)
22043 char_u *name;
22044 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022045{
22046 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022047 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022048 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022049 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022050
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022051 /* Return quickly when autoload disabled. */
22052 if (no_autoload)
22053 return FALSE;
22054
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022055 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022056 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022057 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022058 return FALSE;
22059
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022060 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022061
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022062 /* Find the name in the list of previously loaded package names. Skip
22063 * "autoload/", it's always the same. */
22064 for (i = 0; i < ga_loaded.ga_len; ++i)
22065 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22066 break;
22067 if (!reload && i < ga_loaded.ga_len)
22068 ret = FALSE; /* was loaded already */
22069 else
22070 {
22071 /* Remember the name if it wasn't loaded already. */
22072 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22073 {
22074 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22075 tofree = NULL;
22076 }
22077
22078 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022079 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022080 ret = TRUE;
22081 }
22082
22083 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022084 return ret;
22085}
22086
22087/*
22088 * Return the autoload script name for a function or variable name.
22089 * Returns NULL when out of memory.
22090 */
22091 static char_u *
22092autoload_name(name)
22093 char_u *name;
22094{
22095 char_u *p;
22096 char_u *scriptname;
22097
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022098 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022099 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22100 if (scriptname == NULL)
22101 return FALSE;
22102 STRCPY(scriptname, "autoload/");
22103 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022104 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022105 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022106 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022107 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022108 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022109}
22110
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22112
22113/*
22114 * Function given to ExpandGeneric() to obtain the list of user defined
22115 * function names.
22116 */
22117 char_u *
22118get_user_func_name(xp, idx)
22119 expand_T *xp;
22120 int idx;
22121{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022122 static long_u done;
22123 static hashitem_T *hi;
22124 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125
22126 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022128 done = 0;
22129 hi = func_hashtab.ht_array;
22130 }
22131 if (done < func_hashtab.ht_used)
22132 {
22133 if (done++ > 0)
22134 ++hi;
22135 while (HASHITEM_EMPTY(hi))
22136 ++hi;
22137 fp = HI2UF(hi);
22138
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022139 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022140 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022141
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022142 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22143 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144
22145 cat_func_name(IObuff, fp);
22146 if (xp->xp_context != EXPAND_USER_FUNC)
22147 {
22148 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022149 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 STRCAT(IObuff, ")");
22151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 return IObuff;
22153 }
22154 return NULL;
22155}
22156
22157#endif /* FEAT_CMDL_COMPL */
22158
22159/*
22160 * Copy the function name of "fp" to buffer "buf".
22161 * "buf" must be able to hold the function name plus three bytes.
22162 * Takes care of script-local function names.
22163 */
22164 static void
22165cat_func_name(buf, fp)
22166 char_u *buf;
22167 ufunc_T *fp;
22168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022169 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 {
22171 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022172 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 }
22174 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022175 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176}
22177
22178/*
22179 * ":delfunction {name}"
22180 */
22181 void
22182ex_delfunction(eap)
22183 exarg_T *eap;
22184{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022185 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 char_u *p;
22187 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189
22190 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022191 name = trans_function_name(&p, eap->skip, 0, &fudi);
22192 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022194 {
22195 if (fudi.fd_dict != NULL && !eap->skip)
22196 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 if (!ends_excmd(*skipwhite(p)))
22200 {
22201 vim_free(name);
22202 EMSG(_(e_trailing));
22203 return;
22204 }
22205 eap->nextcmd = check_nextcmd(p);
22206 if (eap->nextcmd != NULL)
22207 *p = NUL;
22208
22209 if (!eap->skip)
22210 fp = find_func(name);
22211 vim_free(name);
22212
22213 if (!eap->skip)
22214 {
22215 if (fp == NULL)
22216 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022217 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 return;
22219 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022220 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 {
22222 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22223 return;
22224 }
22225
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022226 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022228 /* Delete the dict item that refers to the function, it will
22229 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022230 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022232 else
22233 func_free(fp);
22234 }
22235}
22236
22237/*
22238 * Free a function and remove it from the list of functions.
22239 */
22240 static void
22241func_free(fp)
22242 ufunc_T *fp;
22243{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022244 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022245
22246 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022247 ga_clear_strings(&(fp->uf_args));
22248 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022249#ifdef FEAT_PROFILE
22250 vim_free(fp->uf_tml_count);
22251 vim_free(fp->uf_tml_total);
22252 vim_free(fp->uf_tml_self);
22253#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022255 /* remove the function from the function hashtable */
22256 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22257 if (HASHITEM_EMPTY(hi))
22258 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022259 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022260 hash_remove(&func_hashtab, hi);
22261
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022262 vim_free(fp);
22263}
22264
22265/*
22266 * Unreference a Function: decrement the reference count and free it when it
22267 * becomes zero. Only for numbered functions.
22268 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022269 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022270func_unref(name)
22271 char_u *name;
22272{
22273 ufunc_T *fp;
22274
22275 if (name != NULL && isdigit(*name))
22276 {
22277 fp = find_func(name);
22278 if (fp == NULL)
22279 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022280 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022281 {
22282 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022283 * when "uf_calls" becomes zero. */
22284 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022285 func_free(fp);
22286 }
22287 }
22288}
22289
22290/*
22291 * Count a reference to a Function.
22292 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022293 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022294func_ref(name)
22295 char_u *name;
22296{
22297 ufunc_T *fp;
22298
22299 if (name != NULL && isdigit(*name))
22300 {
22301 fp = find_func(name);
22302 if (fp == NULL)
22303 EMSG2(_(e_intern2), "func_ref()");
22304 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022305 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 }
22307}
22308
22309/*
22310 * Call a user function.
22311 */
22312 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022313call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 ufunc_T *fp; /* pointer to function */
22315 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022316 typval_T *argvars; /* arguments */
22317 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 linenr_T firstline; /* first line of range */
22319 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022320 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321{
Bram Moolenaar33570922005-01-25 22:26:29 +000022322 char_u *save_sourcing_name;
22323 linenr_T save_sourcing_lnum;
22324 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022325 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 int save_did_emsg;
22327 static int depth = 0;
22328 dictitem_T *v;
22329 int fixvar_idx = 0; /* index in fixvar[] */
22330 int i;
22331 int ai;
22332 char_u numbuf[NUMBUFLEN];
22333 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022334#ifdef FEAT_PROFILE
22335 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022336 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338
22339 /* If depth of calling is getting too high, don't execute the function */
22340 if (depth >= p_mfd)
22341 {
22342 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022343 rettv->v_type = VAR_NUMBER;
22344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 return;
22346 }
22347 ++depth;
22348
22349 line_breakcheck(); /* check for CTRL-C hit */
22350
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022351 fc = (funccall_T *)alloc(sizeof(funccall_T));
22352 fc->caller = current_funccal;
22353 current_funccal = fc;
22354 fc->func = fp;
22355 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022356 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022357 fc->linenr = 0;
22358 fc->returned = FALSE;
22359 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022361 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22362 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022365 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22367 * each argument variable and saves a lot of time.
22368 */
22369 /*
22370 * Init l: variables.
22371 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022372 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022373 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022374 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022375 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22376 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022377 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022378 name = v->di_key;
22379 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022380 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022381 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022382 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022383 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022384 v->di_tv.vval.v_dict = selfdict;
22385 ++selfdict->dv_refcount;
22386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022387
Bram Moolenaar33570922005-01-25 22:26:29 +000022388 /*
22389 * Init a: variables.
22390 * Set a:0 to "argcount".
22391 * Set a:000 to a list with room for the "..." arguments.
22392 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022393 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022394 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022395 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022396 /* Use "name" to avoid a warning from some compiler that checks the
22397 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022398 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022399 name = v->di_key;
22400 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022401 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022402 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022404 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022405 v->di_tv.vval.v_list = &fc->l_varlist;
22406 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22407 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22408 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022409
22410 /*
22411 * Set a:firstline to "firstline" and a:lastline to "lastline".
22412 * Set a:name to named arguments.
22413 * Set a:N to the "..." arguments.
22414 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022415 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022417 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022418 (varnumber_T)lastline);
22419 for (i = 0; i < argcount; ++i)
22420 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 if (ai < 0)
22423 /* named argument a:name */
22424 name = FUNCARG(fp, i);
22425 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022426 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 /* "..." argument a:1, a:2, etc. */
22428 sprintf((char *)numbuf, "%d", ai + 1);
22429 name = numbuf;
22430 }
22431 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22432 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022433 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22435 }
22436 else
22437 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022438 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22439 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022440 if (v == NULL)
22441 break;
22442 v->di_flags = DI_FLAGS_RO;
22443 }
22444 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022445 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022446
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022447 /* Note: the values are copied directly to avoid alloc/free.
22448 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022450 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022451
22452 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22453 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022454 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22455 fc->l_listitems[ai].li_tv = argvars[i];
22456 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022457 }
22458 }
22459
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 /* Don't redraw while executing the function. */
22461 ++RedrawingDisabled;
22462 save_sourcing_name = sourcing_name;
22463 save_sourcing_lnum = sourcing_lnum;
22464 sourcing_lnum = 1;
22465 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022466 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 if (sourcing_name != NULL)
22468 {
22469 if (save_sourcing_name != NULL
22470 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22471 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22472 else
22473 STRCPY(sourcing_name, "function ");
22474 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22475
22476 if (p_verbose >= 12)
22477 {
22478 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022479 verbose_enter_scroll();
22480
Bram Moolenaar555b2802005-05-19 21:08:39 +000022481 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 if (p_verbose >= 14)
22483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022485 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022486 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022487 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488
22489 msg_puts((char_u *)"(");
22490 for (i = 0; i < argcount; ++i)
22491 {
22492 if (i > 0)
22493 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022494 if (argvars[i].v_type == VAR_NUMBER)
22495 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 else
22497 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022498 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22499 if (s != NULL)
22500 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022501 if (vim_strsize(s) > MSG_BUF_CLEN)
22502 {
22503 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22504 s = buf;
22505 }
22506 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022507 vim_free(tofree);
22508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 }
22510 }
22511 msg_puts((char_u *)")");
22512 }
22513 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022514
22515 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 --no_wait_return;
22517 }
22518 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022519#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022520 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022521 {
22522 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22523 func_do_profile(fp);
22524 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022525 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022526 {
22527 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022528 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022529 profile_zero(&fp->uf_tm_children);
22530 }
22531 script_prof_save(&wait_start);
22532 }
22533#endif
22534
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022536 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 save_did_emsg = did_emsg;
22538 did_emsg = FALSE;
22539
22540 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022541 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22543
22544 --RedrawingDisabled;
22545
22546 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022547 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022549 clear_tv(rettv);
22550 rettv->v_type = VAR_NUMBER;
22551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 }
22553
Bram Moolenaar05159a02005-02-26 23:04:13 +000022554#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022555 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022556 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022557 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022558 profile_end(&call_start);
22559 profile_sub_wait(&wait_start, &call_start);
22560 profile_add(&fp->uf_tm_total, &call_start);
22561 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022562 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022563 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022564 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22565 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022566 }
22567 }
22568#endif
22569
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 /* when being verbose, mention the return value */
22571 if (p_verbose >= 12)
22572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022574 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022577 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022578 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022579 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022580 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022583 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022584 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022585 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022586 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022587
Bram Moolenaar555b2802005-05-19 21:08:39 +000022588 /* The value may be very long. Skip the middle part, so that we
22589 * have some idea how it starts and ends. smsg() would always
22590 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022591 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022592 if (s != NULL)
22593 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022594 if (vim_strsize(s) > MSG_BUF_CLEN)
22595 {
22596 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22597 s = buf;
22598 }
22599 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022600 vim_free(tofree);
22601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 }
22603 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022604
22605 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 --no_wait_return;
22607 }
22608
22609 vim_free(sourcing_name);
22610 sourcing_name = save_sourcing_name;
22611 sourcing_lnum = save_sourcing_lnum;
22612 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022613#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022614 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022615 script_prof_restore(&wait_start);
22616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617
22618 if (p_verbose >= 12 && sourcing_name != NULL)
22619 {
22620 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022621 verbose_enter_scroll();
22622
Bram Moolenaar555b2802005-05-19 21:08:39 +000022623 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022625
22626 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 --no_wait_return;
22628 }
22629
22630 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022631 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022633
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022634 /* If the a:000 list and the l: and a: dicts are not referenced we can
22635 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022636 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22637 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22638 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22639 {
22640 free_funccal(fc, FALSE);
22641 }
22642 else
22643 {
22644 hashitem_T *hi;
22645 listitem_T *li;
22646 int todo;
22647
22648 /* "fc" is still in use. This can happen when returning "a:000" or
22649 * assigning "l:" to a global variable.
22650 * Link "fc" in the list for garbage collection later. */
22651 fc->caller = previous_funccal;
22652 previous_funccal = fc;
22653
22654 /* Make a copy of the a: variables, since we didn't do that above. */
22655 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22656 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22657 {
22658 if (!HASHITEM_EMPTY(hi))
22659 {
22660 --todo;
22661 v = HI2DI(hi);
22662 copy_tv(&v->di_tv, &v->di_tv);
22663 }
22664 }
22665
22666 /* Make a copy of the a:000 items, since we didn't do that above. */
22667 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22668 copy_tv(&li->li_tv, &li->li_tv);
22669 }
22670}
22671
22672/*
22673 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022674 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022675 */
22676 static int
22677can_free_funccal(fc, copyID)
22678 funccall_T *fc;
22679 int copyID;
22680{
22681 return (fc->l_varlist.lv_copyID != copyID
22682 && fc->l_vars.dv_copyID != copyID
22683 && fc->l_avars.dv_copyID != copyID);
22684}
22685
22686/*
22687 * Free "fc" and what it contains.
22688 */
22689 static void
22690free_funccal(fc, free_val)
22691 funccall_T *fc;
22692 int free_val; /* a: vars were allocated */
22693{
22694 listitem_T *li;
22695
22696 /* The a: variables typevals may not have been allocated, only free the
22697 * allocated variables. */
22698 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22699
22700 /* free all l: variables */
22701 vars_clear(&fc->l_vars.dv_hashtab);
22702
22703 /* Free the a:000 variables if they were allocated. */
22704 if (free_val)
22705 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22706 clear_tv(&li->li_tv);
22707
22708 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709}
22710
22711/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 * Add a number variable "name" to dict "dp" with value "nr".
22713 */
22714 static void
22715add_nr_var(dp, v, name, nr)
22716 dict_T *dp;
22717 dictitem_T *v;
22718 char *name;
22719 varnumber_T nr;
22720{
22721 STRCPY(v->di_key, name);
22722 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22723 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22724 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022725 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 v->di_tv.vval.v_number = nr;
22727}
22728
22729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 * ":return [expr]"
22731 */
22732 void
22733ex_return(eap)
22734 exarg_T *eap;
22735{
22736 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022737 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 int returning = FALSE;
22739
22740 if (current_funccal == NULL)
22741 {
22742 EMSG(_("E133: :return not inside a function"));
22743 return;
22744 }
22745
22746 if (eap->skip)
22747 ++emsg_skip;
22748
22749 eap->nextcmd = NULL;
22750 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022751 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 {
22753 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022754 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 }
22758 /* It's safer to return also on error. */
22759 else if (!eap->skip)
22760 {
22761 /*
22762 * Return unless the expression evaluation has been cancelled due to an
22763 * aborting error, an interrupt, or an exception.
22764 */
22765 if (!aborting())
22766 returning = do_return(eap, FALSE, TRUE, NULL);
22767 }
22768
22769 /* When skipping or the return gets pending, advance to the next command
22770 * in this line (!returning). Otherwise, ignore the rest of the line.
22771 * Following lines will be ignored by get_func_line(). */
22772 if (returning)
22773 eap->nextcmd = NULL;
22774 else if (eap->nextcmd == NULL) /* no argument */
22775 eap->nextcmd = check_nextcmd(arg);
22776
22777 if (eap->skip)
22778 --emsg_skip;
22779}
22780
22781/*
22782 * Return from a function. Possibly makes the return pending. Also called
22783 * for a pending return at the ":endtry" or after returning from an extra
22784 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022786 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022787 * FALSE when the return gets pending.
22788 */
22789 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022790do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 exarg_T *eap;
22792 int reanimate;
22793 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022794 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795{
22796 int idx;
22797 struct condstack *cstack = eap->cstack;
22798
22799 if (reanimate)
22800 /* Undo the return. */
22801 current_funccal->returned = FALSE;
22802
22803 /*
22804 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22805 * not in its finally clause (which then is to be executed next) is found.
22806 * In this case, make the ":return" pending for execution at the ":endtry".
22807 * Otherwise, return normally.
22808 */
22809 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22810 if (idx >= 0)
22811 {
22812 cstack->cs_pending[idx] = CSTP_RETURN;
22813
22814 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022815 /* A pending return again gets pending. "rettv" points to an
22816 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022818 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 else
22820 {
22821 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022822 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022824 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022826 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 {
22828 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022829 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 else
22832 EMSG(_(e_outofmem));
22833 }
22834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022835 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836
22837 if (reanimate)
22838 {
22839 /* The pending return value could be overwritten by a ":return"
22840 * without argument in a finally clause; reset the default
22841 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022842 current_funccal->rettv->v_type = VAR_NUMBER;
22843 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 }
22845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022846 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 }
22848 else
22849 {
22850 current_funccal->returned = TRUE;
22851
22852 /* If the return is carried out now, store the return value. For
22853 * a return immediately after reanimation, the value is already
22854 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022855 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022857 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022860 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 }
22862 }
22863
22864 return idx < 0;
22865}
22866
22867/*
22868 * Free the variable with a pending return value.
22869 */
22870 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022871discard_pending_return(rettv)
22872 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873{
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875}
22876
22877/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022878 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 * is an allocated string. Used by report_pending() for verbose messages.
22880 */
22881 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022882get_return_cmd(rettv)
22883 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022885 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022886 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022887 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022889 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022890 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022891 if (s == NULL)
22892 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022893
22894 STRCPY(IObuff, ":return ");
22895 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22896 if (STRLEN(s) + 8 >= IOSIZE)
22897 STRCPY(IObuff + IOSIZE - 4, "...");
22898 vim_free(tofree);
22899 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900}
22901
22902/*
22903 * Get next function line.
22904 * Called by do_cmdline() to get the next line.
22905 * Returns allocated string, or NULL for end of function.
22906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 char_u *
22908get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022909 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022911 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912{
Bram Moolenaar33570922005-01-25 22:26:29 +000022913 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022914 ufunc_T *fp = fcp->func;
22915 char_u *retval;
22916 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917
22918 /* If breakpoints have been added/deleted need to check for it. */
22919 if (fcp->dbg_tick != debug_tick)
22920 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022921 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 sourcing_lnum);
22923 fcp->dbg_tick = debug_tick;
22924 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022925#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022927 func_line_end(cookie);
22928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929
Bram Moolenaar05159a02005-02-26 23:04:13 +000022930 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022931 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22932 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 retval = NULL;
22934 else
22935 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022936 /* Skip NULL lines (continuation lines). */
22937 while (fcp->linenr < gap->ga_len
22938 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22939 ++fcp->linenr;
22940 if (fcp->linenr >= gap->ga_len)
22941 retval = NULL;
22942 else
22943 {
22944 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22945 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022946#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022947 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022948 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022949#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 }
22952
22953 /* Did we encounter a breakpoint? */
22954 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22955 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022956 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022958 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 sourcing_lnum);
22960 fcp->dbg_tick = debug_tick;
22961 }
22962
22963 return retval;
22964}
22965
Bram Moolenaar05159a02005-02-26 23:04:13 +000022966#if defined(FEAT_PROFILE) || defined(PROTO)
22967/*
22968 * Called when starting to read a function line.
22969 * "sourcing_lnum" must be correct!
22970 * When skipping lines it may not actually be executed, but we won't find out
22971 * until later and we need to store the time now.
22972 */
22973 void
22974func_line_start(cookie)
22975 void *cookie;
22976{
22977 funccall_T *fcp = (funccall_T *)cookie;
22978 ufunc_T *fp = fcp->func;
22979
22980 if (fp->uf_profiling && sourcing_lnum >= 1
22981 && sourcing_lnum <= fp->uf_lines.ga_len)
22982 {
22983 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022984 /* Skip continuation lines. */
22985 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22986 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022987 fp->uf_tml_execed = FALSE;
22988 profile_start(&fp->uf_tml_start);
22989 profile_zero(&fp->uf_tml_children);
22990 profile_get_wait(&fp->uf_tml_wait);
22991 }
22992}
22993
22994/*
22995 * Called when actually executing a function line.
22996 */
22997 void
22998func_line_exec(cookie)
22999 void *cookie;
23000{
23001 funccall_T *fcp = (funccall_T *)cookie;
23002 ufunc_T *fp = fcp->func;
23003
23004 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23005 fp->uf_tml_execed = TRUE;
23006}
23007
23008/*
23009 * Called when done with a function line.
23010 */
23011 void
23012func_line_end(cookie)
23013 void *cookie;
23014{
23015 funccall_T *fcp = (funccall_T *)cookie;
23016 ufunc_T *fp = fcp->func;
23017
23018 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23019 {
23020 if (fp->uf_tml_execed)
23021 {
23022 ++fp->uf_tml_count[fp->uf_tml_idx];
23023 profile_end(&fp->uf_tml_start);
23024 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023025 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023026 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23027 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023028 }
23029 fp->uf_tml_idx = -1;
23030 }
23031}
23032#endif
23033
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034/*
23035 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023036 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 */
23038 int
23039func_has_ended(cookie)
23040 void *cookie;
23041{
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043
23044 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23045 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023046 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 || fcp->returned);
23048}
23049
23050/*
23051 * return TRUE if cookie indicates a function which "abort"s on errors.
23052 */
23053 int
23054func_has_abort(cookie)
23055 void *cookie;
23056{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023057 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058}
23059
23060#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23061typedef enum
23062{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023063 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23064 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23065 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066} var_flavour_T;
23067
23068static var_flavour_T var_flavour __ARGS((char_u *varname));
23069
23070 static var_flavour_T
23071var_flavour(varname)
23072 char_u *varname;
23073{
23074 char_u *p = varname;
23075
23076 if (ASCII_ISUPPER(*p))
23077 {
23078 while (*(++p))
23079 if (ASCII_ISLOWER(*p))
23080 return VAR_FLAVOUR_SESSION;
23081 return VAR_FLAVOUR_VIMINFO;
23082 }
23083 else
23084 return VAR_FLAVOUR_DEFAULT;
23085}
23086#endif
23087
23088#if defined(FEAT_VIMINFO) || defined(PROTO)
23089/*
23090 * Restore global vars that start with a capital from the viminfo file
23091 */
23092 int
23093read_viminfo_varlist(virp, writing)
23094 vir_T *virp;
23095 int writing;
23096{
23097 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023098 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023099 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100
23101 if (!writing && (find_viminfo_parameter('!') != NULL))
23102 {
23103 tab = vim_strchr(virp->vir_line + 1, '\t');
23104 if (tab != NULL)
23105 {
23106 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023107 switch (*tab)
23108 {
23109 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023110#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023111 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023112#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023113 case 'D': type = VAR_DICT; break;
23114 case 'L': type = VAR_LIST; break;
23115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116
23117 tab = vim_strchr(tab, '\t');
23118 if (tab != NULL)
23119 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023120 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023121 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023122 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023124#ifdef FEAT_FLOAT
23125 else if (type == VAR_FLOAT)
23126 (void)string2float(tab + 1, &tv.vval.v_float);
23127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023129 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023130 if (type == VAR_DICT || type == VAR_LIST)
23131 {
23132 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23133
23134 if (etv == NULL)
23135 /* Failed to parse back the dict or list, use it as a
23136 * string. */
23137 tv.v_type = VAR_STRING;
23138 else
23139 {
23140 vim_free(tv.vval.v_string);
23141 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023142 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023143 }
23144 }
23145
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023146 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023147
23148 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023149 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023150 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23151 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 }
23153 }
23154 }
23155
23156 return viminfo_readline(virp);
23157}
23158
23159/*
23160 * Write global vars that start with a capital to the viminfo file
23161 */
23162 void
23163write_viminfo_varlist(fp)
23164 FILE *fp;
23165{
Bram Moolenaar33570922005-01-25 22:26:29 +000023166 hashitem_T *hi;
23167 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023168 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023169 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023170 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023171 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023172 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173
23174 if (find_viminfo_parameter('!') == NULL)
23175 return;
23176
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023177 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023179 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023180 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023184 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 this_var = HI2DI(hi);
23186 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023188 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023189 {
23190 case VAR_STRING: s = "STR"; break;
23191 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023192#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023193 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023194#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023195 case VAR_DICT: s = "DIC"; break;
23196 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023197 default: continue;
23198 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023199 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023200 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023201 if (p != NULL)
23202 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023203 vim_free(tofree);
23204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 }
23206 }
23207}
23208#endif
23209
23210#if defined(FEAT_SESSION) || defined(PROTO)
23211 int
23212store_session_globals(fd)
23213 FILE *fd;
23214{
Bram Moolenaar33570922005-01-25 22:26:29 +000023215 hashitem_T *hi;
23216 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023217 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 char_u *p, *t;
23219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023220 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023223 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023225 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023226 this_var = HI2DI(hi);
23227 if ((this_var->di_tv.v_type == VAR_NUMBER
23228 || this_var->di_tv.v_type == VAR_STRING)
23229 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023230 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023231 /* Escape special characters with a backslash. Turn a LF and
23232 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023234 (char_u *)"\\\"\n\r");
23235 if (p == NULL) /* out of memory */
23236 break;
23237 for (t = p; *t != NUL; ++t)
23238 if (*t == '\n')
23239 *t = 'n';
23240 else if (*t == '\r')
23241 *t = 'r';
23242 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 this_var->di_key,
23244 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23245 : ' ',
23246 p,
23247 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23248 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023249 || put_eol(fd) == FAIL)
23250 {
23251 vim_free(p);
23252 return FAIL;
23253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 vim_free(p);
23255 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023256#ifdef FEAT_FLOAT
23257 else if (this_var->di_tv.v_type == VAR_FLOAT
23258 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23259 {
23260 float_T f = this_var->di_tv.vval.v_float;
23261 int sign = ' ';
23262
23263 if (f < 0)
23264 {
23265 f = -f;
23266 sign = '-';
23267 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023268 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023269 this_var->di_key, sign, f) < 0)
23270 || put_eol(fd) == FAIL)
23271 return FAIL;
23272 }
23273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 }
23275 }
23276 return OK;
23277}
23278#endif
23279
Bram Moolenaar661b1822005-07-28 22:36:45 +000023280/*
23281 * Display script name where an item was last set.
23282 * Should only be invoked when 'verbose' is non-zero.
23283 */
23284 void
23285last_set_msg(scriptID)
23286 scid_T scriptID;
23287{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023288 char_u *p;
23289
Bram Moolenaar661b1822005-07-28 22:36:45 +000023290 if (scriptID != 0)
23291 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023292 p = home_replace_save(NULL, get_scriptname(scriptID));
23293 if (p != NULL)
23294 {
23295 verbose_enter();
23296 MSG_PUTS(_("\n\tLast set from "));
23297 MSG_PUTS(p);
23298 vim_free(p);
23299 verbose_leave();
23300 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023301 }
23302}
23303
Bram Moolenaard812df62008-11-09 12:46:09 +000023304/*
23305 * List v:oldfiles in a nice way.
23306 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023307 void
23308ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023309 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023310{
23311 list_T *l = vimvars[VV_OLDFILES].vv_list;
23312 listitem_T *li;
23313 int nr = 0;
23314
23315 if (l == NULL)
23316 msg((char_u *)_("No old files"));
23317 else
23318 {
23319 msg_start();
23320 msg_scroll = TRUE;
23321 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23322 {
23323 msg_outnum((long)++nr);
23324 MSG_PUTS(": ");
23325 msg_outtrans(get_tv_string(&li->li_tv));
23326 msg_putchar('\n');
23327 out_flush(); /* output one line at a time */
23328 ui_breakcheck();
23329 }
23330 /* Assume "got_int" was set to truncate the listing. */
23331 got_int = FALSE;
23332
23333#ifdef FEAT_BROWSE_CMD
23334 if (cmdmod.browse)
23335 {
23336 quit_more = FALSE;
23337 nr = prompt_for_number(FALSE);
23338 msg_starthere();
23339 if (nr > 0)
23340 {
23341 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23342 (long)nr);
23343
23344 if (p != NULL)
23345 {
23346 p = expand_env_save(p);
23347 eap->arg = p;
23348 eap->cmdidx = CMD_edit;
23349 cmdmod.browse = FALSE;
23350 do_exedit(eap, NULL);
23351 vim_free(p);
23352 }
23353 }
23354 }
23355#endif
23356 }
23357}
23358
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359#endif /* FEAT_EVAL */
23360
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023362#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363
23364#ifdef WIN3264
23365/*
23366 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23367 */
23368static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23369static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23370static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23371
23372/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023373 * Get the short path (8.3) for the filename in "fnamep".
23374 * Only works for a valid file name.
23375 * When the path gets longer "fnamep" is changed and the allocated buffer
23376 * is put in "bufp".
23377 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23378 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 */
23380 static int
23381get_short_pathname(fnamep, bufp, fnamelen)
23382 char_u **fnamep;
23383 char_u **bufp;
23384 int *fnamelen;
23385{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023386 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 char_u *newbuf;
23388
23389 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 l = GetShortPathName(*fnamep, *fnamep, len);
23391 if (l > len - 1)
23392 {
23393 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023394 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 newbuf = vim_strnsave(*fnamep, l);
23396 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023397 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398
23399 vim_free(*bufp);
23400 *fnamep = *bufp = newbuf;
23401
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023402 /* Really should always succeed, as the buffer is big enough. */
23403 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 }
23405
23406 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023407 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408}
23409
23410/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023411 * Get the short path (8.3) for the filename in "fname". The converted
23412 * path is returned in "bufp".
23413 *
23414 * Some of the directories specified in "fname" may not exist. This function
23415 * will shorten the existing directories at the beginning of the path and then
23416 * append the remaining non-existing path.
23417 *
23418 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023419 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023420 * bufp - Pointer to an allocated buffer for the filename.
23421 * fnamelen - Length of the filename pointed to by fname
23422 *
23423 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 */
23425 static int
23426shortpath_for_invalid_fname(fname, bufp, fnamelen)
23427 char_u **fname;
23428 char_u **bufp;
23429 int *fnamelen;
23430{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023431 char_u *short_fname, *save_fname, *pbuf_unused;
23432 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023434 int old_len, len;
23435 int new_len, sfx_len;
23436 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437
23438 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023439 old_len = *fnamelen;
23440 save_fname = vim_strnsave(*fname, old_len);
23441 pbuf_unused = NULL;
23442 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023444 endp = save_fname + old_len - 1; /* Find the end of the copy */
23445 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023447 /*
23448 * Try shortening the supplied path till it succeeds by removing one
23449 * directory at a time from the tail of the path.
23450 */
23451 len = 0;
23452 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023454 /* go back one path-separator */
23455 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23456 --endp;
23457 if (endp <= save_fname)
23458 break; /* processed the complete path */
23459
23460 /*
23461 * Replace the path separator with a NUL and try to shorten the
23462 * resulting path.
23463 */
23464 ch = *endp;
23465 *endp = 0;
23466 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023467 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023468 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23469 {
23470 retval = FAIL;
23471 goto theend;
23472 }
23473 *endp = ch; /* preserve the string */
23474
23475 if (len > 0)
23476 break; /* successfully shortened the path */
23477
23478 /* failed to shorten the path. Skip the path separator */
23479 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 }
23481
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023482 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023484 /*
23485 * Succeeded in shortening the path. Now concatenate the shortened
23486 * path with the remaining path at the tail.
23487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023489 /* Compute the length of the new path. */
23490 sfx_len = (int)(save_endp - endp) + 1;
23491 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023493 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023495 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023497 /* There is not enough space in the currently allocated string,
23498 * copy it to a buffer big enough. */
23499 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023501 {
23502 retval = FAIL;
23503 goto theend;
23504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 }
23506 else
23507 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023508 /* Transfer short_fname to the main buffer (it's big enough),
23509 * unless get_short_pathname() did its work in-place. */
23510 *fname = *bufp = save_fname;
23511 if (short_fname != save_fname)
23512 vim_strncpy(save_fname, short_fname, len);
23513 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023515
23516 /* concat the not-shortened part of the path */
23517 vim_strncpy(*fname + len, endp, sfx_len);
23518 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023520
23521theend:
23522 vim_free(pbuf_unused);
23523 vim_free(save_fname);
23524
23525 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526}
23527
23528/*
23529 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023530 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 */
23532 static int
23533shortpath_for_partial(fnamep, bufp, fnamelen)
23534 char_u **fnamep;
23535 char_u **bufp;
23536 int *fnamelen;
23537{
23538 int sepcount, len, tflen;
23539 char_u *p;
23540 char_u *pbuf, *tfname;
23541 int hasTilde;
23542
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023543 /* Count up the path separators from the RHS.. so we know which part
23544 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023546 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 if (vim_ispathsep(*p))
23548 ++sepcount;
23549
23550 /* Need full path first (use expand_env() to remove a "~/") */
23551 hasTilde = (**fnamep == '~');
23552 if (hasTilde)
23553 pbuf = tfname = expand_env_save(*fnamep);
23554 else
23555 pbuf = tfname = FullName_save(*fnamep, FALSE);
23556
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023557 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023559 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23560 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561
23562 if (len == 0)
23563 {
23564 /* Don't have a valid filename, so shorten the rest of the
23565 * path if we can. This CAN give us invalid 8.3 filenames, but
23566 * there's not a lot of point in guessing what it might be.
23567 */
23568 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023569 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23570 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 }
23572
23573 /* Count the paths backward to find the beginning of the desired string. */
23574 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023575 {
23576#ifdef FEAT_MBYTE
23577 if (has_mbyte)
23578 p -= mb_head_off(tfname, p);
23579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 if (vim_ispathsep(*p))
23581 {
23582 if (sepcount == 0 || (hasTilde && sepcount == 1))
23583 break;
23584 else
23585 sepcount --;
23586 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588 if (hasTilde)
23589 {
23590 --p;
23591 if (p >= tfname)
23592 *p = '~';
23593 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023594 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 }
23596 else
23597 ++p;
23598
23599 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23600 vim_free(*bufp);
23601 *fnamelen = (int)STRLEN(p);
23602 *bufp = pbuf;
23603 *fnamep = p;
23604
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023605 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606}
23607#endif /* WIN3264 */
23608
23609/*
23610 * Adjust a filename, according to a string of modifiers.
23611 * *fnamep must be NUL terminated when called. When returning, the length is
23612 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023613 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 * When there is an error, *fnamep is set to NULL.
23615 */
23616 int
23617modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23618 char_u *src; /* string with modifiers */
23619 int *usedlen; /* characters after src that are used */
23620 char_u **fnamep; /* file name so far */
23621 char_u **bufp; /* buffer for allocated file name or NULL */
23622 int *fnamelen; /* length of fnamep */
23623{
23624 int valid = 0;
23625 char_u *tail;
23626 char_u *s, *p, *pbuf;
23627 char_u dirname[MAXPATHL];
23628 int c;
23629 int has_fullname = 0;
23630#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023631 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 int has_shortname = 0;
23633#endif
23634
23635repeat:
23636 /* ":p" - full path/file_name */
23637 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23638 {
23639 has_fullname = 1;
23640
23641 valid |= VALID_PATH;
23642 *usedlen += 2;
23643
23644 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23645 if ((*fnamep)[0] == '~'
23646#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23647 && ((*fnamep)[1] == '/'
23648# ifdef BACKSLASH_IN_FILENAME
23649 || (*fnamep)[1] == '\\'
23650# endif
23651 || (*fnamep)[1] == NUL)
23652
23653#endif
23654 )
23655 {
23656 *fnamep = expand_env_save(*fnamep);
23657 vim_free(*bufp); /* free any allocated file name */
23658 *bufp = *fnamep;
23659 if (*fnamep == NULL)
23660 return -1;
23661 }
23662
23663 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023664 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 {
23666 if (vim_ispathsep(*p)
23667 && p[1] == '.'
23668 && (p[2] == NUL
23669 || vim_ispathsep(p[2])
23670 || (p[2] == '.'
23671 && (p[3] == NUL || vim_ispathsep(p[3])))))
23672 break;
23673 }
23674
23675 /* FullName_save() is slow, don't use it when not needed. */
23676 if (*p != NUL || !vim_isAbsName(*fnamep))
23677 {
23678 *fnamep = FullName_save(*fnamep, *p != NUL);
23679 vim_free(*bufp); /* free any allocated file name */
23680 *bufp = *fnamep;
23681 if (*fnamep == NULL)
23682 return -1;
23683 }
23684
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023685#ifdef WIN3264
23686# if _WIN32_WINNT >= 0x0500
23687 if (vim_strchr(*fnamep, '~') != NULL)
23688 {
23689 /* Expand 8.3 filename to full path. Needed to make sure the same
23690 * file does not have two different names.
23691 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23692 p = alloc(_MAX_PATH + 1);
23693 if (p != NULL)
23694 {
23695 if (GetLongPathName(*fnamep, p, MAXPATHL))
23696 {
23697 vim_free(*bufp);
23698 *bufp = *fnamep = p;
23699 }
23700 else
23701 vim_free(p);
23702 }
23703 }
23704# endif
23705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 /* Append a path separator to a directory. */
23707 if (mch_isdir(*fnamep))
23708 {
23709 /* Make room for one or two extra characters. */
23710 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23711 vim_free(*bufp); /* free any allocated file name */
23712 *bufp = *fnamep;
23713 if (*fnamep == NULL)
23714 return -1;
23715 add_pathsep(*fnamep);
23716 }
23717 }
23718
23719 /* ":." - path relative to the current directory */
23720 /* ":~" - path relative to the home directory */
23721 /* ":8" - shortname path - postponed till after */
23722 while (src[*usedlen] == ':'
23723 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23724 {
23725 *usedlen += 2;
23726 if (c == '8')
23727 {
23728#ifdef WIN3264
23729 has_shortname = 1; /* Postpone this. */
23730#endif
23731 continue;
23732 }
23733 pbuf = NULL;
23734 /* Need full path first (use expand_env() to remove a "~/") */
23735 if (!has_fullname)
23736 {
23737 if (c == '.' && **fnamep == '~')
23738 p = pbuf = expand_env_save(*fnamep);
23739 else
23740 p = pbuf = FullName_save(*fnamep, FALSE);
23741 }
23742 else
23743 p = *fnamep;
23744
23745 has_fullname = 0;
23746
23747 if (p != NULL)
23748 {
23749 if (c == '.')
23750 {
23751 mch_dirname(dirname, MAXPATHL);
23752 s = shorten_fname(p, dirname);
23753 if (s != NULL)
23754 {
23755 *fnamep = s;
23756 if (pbuf != NULL)
23757 {
23758 vim_free(*bufp); /* free any allocated file name */
23759 *bufp = pbuf;
23760 pbuf = NULL;
23761 }
23762 }
23763 }
23764 else
23765 {
23766 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23767 /* Only replace it when it starts with '~' */
23768 if (*dirname == '~')
23769 {
23770 s = vim_strsave(dirname);
23771 if (s != NULL)
23772 {
23773 *fnamep = s;
23774 vim_free(*bufp);
23775 *bufp = s;
23776 }
23777 }
23778 }
23779 vim_free(pbuf);
23780 }
23781 }
23782
23783 tail = gettail(*fnamep);
23784 *fnamelen = (int)STRLEN(*fnamep);
23785
23786 /* ":h" - head, remove "/file_name", can be repeated */
23787 /* Don't remove the first "/" or "c:\" */
23788 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23789 {
23790 valid |= VALID_HEAD;
23791 *usedlen += 2;
23792 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023793 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023794 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 *fnamelen = (int)(tail - *fnamep);
23796#ifdef VMS
23797 if (*fnamelen > 0)
23798 *fnamelen += 1; /* the path separator is part of the path */
23799#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023800 if (*fnamelen == 0)
23801 {
23802 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23803 p = vim_strsave((char_u *)".");
23804 if (p == NULL)
23805 return -1;
23806 vim_free(*bufp);
23807 *bufp = *fnamep = tail = p;
23808 *fnamelen = 1;
23809 }
23810 else
23811 {
23812 while (tail > s && !after_pathsep(s, tail))
23813 mb_ptr_back(*fnamep, tail);
23814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 }
23816
23817 /* ":8" - shortname */
23818 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23819 {
23820 *usedlen += 2;
23821#ifdef WIN3264
23822 has_shortname = 1;
23823#endif
23824 }
23825
23826#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023827 /*
23828 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 */
23830 if (has_shortname)
23831 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023832 /* Copy the string if it is shortened by :h and when it wasn't copied
23833 * yet, because we are going to change it in place. Avoids changing
23834 * the buffer name for "%:8". */
23835 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 {
23837 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023838 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 return -1;
23840 vim_free(*bufp);
23841 *bufp = *fnamep = p;
23842 }
23843
23844 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023845 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 if (!has_fullname && !vim_isAbsName(*fnamep))
23847 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023848 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 return -1;
23850 }
23851 else
23852 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023853 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854
Bram Moolenaardc935552011-08-17 15:23:23 +020023855 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023857 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 return -1;
23859
23860 if (l == 0)
23861 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023862 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023864 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 return -1;
23866 }
23867 *fnamelen = l;
23868 }
23869 }
23870#endif /* WIN3264 */
23871
23872 /* ":t" - tail, just the basename */
23873 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23874 {
23875 *usedlen += 2;
23876 *fnamelen -= (int)(tail - *fnamep);
23877 *fnamep = tail;
23878 }
23879
23880 /* ":e" - extension, can be repeated */
23881 /* ":r" - root, without extension, can be repeated */
23882 while (src[*usedlen] == ':'
23883 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23884 {
23885 /* find a '.' in the tail:
23886 * - for second :e: before the current fname
23887 * - otherwise: The last '.'
23888 */
23889 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23890 s = *fnamep - 2;
23891 else
23892 s = *fnamep + *fnamelen - 1;
23893 for ( ; s > tail; --s)
23894 if (s[0] == '.')
23895 break;
23896 if (src[*usedlen + 1] == 'e') /* :e */
23897 {
23898 if (s > tail)
23899 {
23900 *fnamelen += (int)(*fnamep - (s + 1));
23901 *fnamep = s + 1;
23902#ifdef VMS
23903 /* cut version from the extension */
23904 s = *fnamep + *fnamelen - 1;
23905 for ( ; s > *fnamep; --s)
23906 if (s[0] == ';')
23907 break;
23908 if (s > *fnamep)
23909 *fnamelen = s - *fnamep;
23910#endif
23911 }
23912 else if (*fnamep <= tail)
23913 *fnamelen = 0;
23914 }
23915 else /* :r */
23916 {
23917 if (s > tail) /* remove one extension */
23918 *fnamelen = (int)(s - *fnamep);
23919 }
23920 *usedlen += 2;
23921 }
23922
23923 /* ":s?pat?foo?" - substitute */
23924 /* ":gs?pat?foo?" - global substitute */
23925 if (src[*usedlen] == ':'
23926 && (src[*usedlen + 1] == 's'
23927 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23928 {
23929 char_u *str;
23930 char_u *pat;
23931 char_u *sub;
23932 int sep;
23933 char_u *flags;
23934 int didit = FALSE;
23935
23936 flags = (char_u *)"";
23937 s = src + *usedlen + 2;
23938 if (src[*usedlen + 1] == 'g')
23939 {
23940 flags = (char_u *)"g";
23941 ++s;
23942 }
23943
23944 sep = *s++;
23945 if (sep)
23946 {
23947 /* find end of pattern */
23948 p = vim_strchr(s, sep);
23949 if (p != NULL)
23950 {
23951 pat = vim_strnsave(s, (int)(p - s));
23952 if (pat != NULL)
23953 {
23954 s = p + 1;
23955 /* find end of substitution */
23956 p = vim_strchr(s, sep);
23957 if (p != NULL)
23958 {
23959 sub = vim_strnsave(s, (int)(p - s));
23960 str = vim_strnsave(*fnamep, *fnamelen);
23961 if (sub != NULL && str != NULL)
23962 {
23963 *usedlen = (int)(p + 1 - src);
23964 s = do_string_sub(str, pat, sub, flags);
23965 if (s != NULL)
23966 {
23967 *fnamep = s;
23968 *fnamelen = (int)STRLEN(s);
23969 vim_free(*bufp);
23970 *bufp = s;
23971 didit = TRUE;
23972 }
23973 }
23974 vim_free(sub);
23975 vim_free(str);
23976 }
23977 vim_free(pat);
23978 }
23979 }
23980 /* after using ":s", repeat all the modifiers */
23981 if (didit)
23982 goto repeat;
23983 }
23984 }
23985
23986 return valid;
23987}
23988
23989/*
23990 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23991 * "flags" can be "g" to do a global substitute.
23992 * Returns an allocated string, NULL for error.
23993 */
23994 char_u *
23995do_string_sub(str, pat, sub, flags)
23996 char_u *str;
23997 char_u *pat;
23998 char_u *sub;
23999 char_u *flags;
24000{
24001 int sublen;
24002 regmatch_T regmatch;
24003 int i;
24004 int do_all;
24005 char_u *tail;
24006 garray_T ga;
24007 char_u *ret;
24008 char_u *save_cpo;
24009
24010 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24011 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024012 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013
24014 ga_init2(&ga, 1, 200);
24015
24016 do_all = (flags[0] == 'g');
24017
24018 regmatch.rm_ic = p_ic;
24019 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24020 if (regmatch.regprog != NULL)
24021 {
24022 tail = str;
24023 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24024 {
24025 /*
24026 * Get some space for a temporary buffer to do the substitution
24027 * into. It will contain:
24028 * - The text up to where the match is.
24029 * - The substituted text.
24030 * - The text after the match.
24031 */
24032 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24033 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24034 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24035 {
24036 ga_clear(&ga);
24037 break;
24038 }
24039
24040 /* copy the text up to where the match is */
24041 i = (int)(regmatch.startp[0] - tail);
24042 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24043 /* add the substituted text */
24044 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24045 + ga.ga_len + i, TRUE, TRUE, FALSE);
24046 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 /* avoid getting stuck on a match with an empty string */
24048 if (tail == regmatch.endp[0])
24049 {
24050 if (*tail == NUL)
24051 break;
24052 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24053 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 }
24055 else
24056 {
24057 tail = regmatch.endp[0];
24058 if (*tail == NUL)
24059 break;
24060 }
24061 if (!do_all)
24062 break;
24063 }
24064
24065 if (ga.ga_data != NULL)
24066 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24067
24068 vim_free(regmatch.regprog);
24069 }
24070
24071 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24072 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024073 if (p_cpo == empty_option)
24074 p_cpo = save_cpo;
24075 else
24076 /* Darn, evaluating {sub} expression changed the value. */
24077 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078
24079 return ret;
24080}
24081
24082#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */