blob: 4d45ae2574091caf8c6585e2ebe562a5a7e3212b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static listitem_T *listitem_alloc __ARGS((void));
428static void listitem_free __ARGS((listitem_T *item));
429static void listitem_remove __ARGS((list_T *l, listitem_T *item));
430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000435static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000438static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
440static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
441static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100445static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000452static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
454static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000455static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
460static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
463static int string2float __ARGS((char_u *text, float_T *value));
464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
466static int find_internal_func __ARGS((char_u *name));
467static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
468static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200469static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000470static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000471static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
474static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100478static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100607static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000621#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200622static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000623static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
624#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200625#ifdef FEAT_LUA
626static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100649static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000675static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000677static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000684static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000685static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000686static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000687static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200689static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000690static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000775static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000787static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
827static void func_unref __ARGS((char_u *name));
828static void func_ref __ARGS((char_u *name));
829static 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 +0000830static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
831static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
834static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000835static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000836static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000837static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200839
840#ifdef EBCDIC
841static int compare_func_name __ARGS((const void *s1, const void *s2));
842static void sortFunctions __ARGS(());
843#endif
844
845
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000846/* Character used as separated in autoload function/variable names. */
847#define AUTOLOAD_CHAR '#'
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
858 init_var_dict(&globvardict, &globvars_var);
859 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200883 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200884
885#ifdef EBCDIC
886 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100887 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200888 */
889 sortFunctions();
890#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000891}
892
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000893#if defined(EXITFREE) || defined(PROTO)
894 void
895eval_clear()
896{
897 int i;
898 struct vimvar *p;
899
900 for (i = 0; i < VV_LEN; ++i)
901 {
902 p = &vimvars[i];
903 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000905 vim_free(p->vv_str);
906 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000907 }
908 else if (p->vv_di.di_tv.v_type == VAR_LIST)
909 {
910 list_unref(p->vv_list);
911 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913 }
914 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000915 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000916 hash_clear(&compat_hashtab);
917
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200919 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000920
921 /* global variables */
922 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000923
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000924 /* autoloaded script names */
925 ga_clear_strings(&ga_loaded);
926
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 /* script-local variables */
928 for (i = 1; i <= ga_scripts.ga_len; ++i)
929 {
930 vars_clear(&SCRIPT_VARS(i));
931 vim_free(SCRIPT_SV(i));
932 }
933 ga_clear(&ga_scripts);
934
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000935 /* unreferenced lists and dicts */
936 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000937
938 /* functions */
939 free_all_functions();
940 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000941}
942#endif
943
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 * Return the name of the executed function.
946 */
947 char_u *
948func_name(cookie)
949 void *cookie;
950{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000951 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954/*
955 * Return the address holding the next breakpoint line for a funccall cookie.
956 */
957 linenr_T *
958func_breakpoint(cookie)
959 void *cookie;
960{
Bram Moolenaar33570922005-01-25 22:26:29 +0000961 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Return the address holding the debug tick for a funccall cookie.
966 */
967 int *
968func_dbg_tick(cookie)
969 void *cookie;
970{
Bram Moolenaar33570922005-01-25 22:26:29 +0000971 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973
974/*
975 * Return the nesting level for a funccall cookie.
976 */
977 int
978func_level(cookie)
979 void *cookie;
980{
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000985funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000987/* pointer to list of previously used funccal, still around because some
988 * item in it is still being used. */
989funccall_T *previous_funccal = NULL;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
992 * Return TRUE when a function was ended by a ":return" command.
993 */
994 int
995current_func_returned()
996{
997 return current_funccal->returned;
998}
999
1000
1001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Set an internal variable to a string value. Creates the variable if it does
1003 * not already exist.
1004 */
1005 void
1006set_internal_string_var(name, value)
1007 char_u *name;
1008 char_u *value;
1009{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001011 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 val = vim_strsave(value);
1014 if (val != NULL)
1015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001019 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 }
1023}
1024
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001026static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027static char_u *redir_endp = NULL;
1028static char_u *redir_varname = NULL;
1029
1030/*
1031 * Start recording command output to a variable
1032 * Returns OK if successfully completed the setup. FAIL otherwise.
1033 */
1034 int
1035var_redir_start(name, append)
1036 char_u *name;
1037 int append; /* append to an existing variable */
1038{
1039 int save_emsg;
1040 int err;
1041 typval_T tv;
1042
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001043 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001044 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 {
1046 EMSG(_(e_invarg));
1047 return FAIL;
1048 }
1049
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001050 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 redir_varname = vim_strsave(name);
1052 if (redir_varname == NULL)
1053 return FAIL;
1054
1055 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1056 if (redir_lval == NULL)
1057 {
1058 var_redir_stop();
1059 return FAIL;
1060 }
1061
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001062 /* The output is stored in growarray "redir_ga" until redirection ends. */
1063 ga_init2(&redir_ga, (int)sizeof(char), 500);
1064
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001066 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1067 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1069 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001070 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 if (redir_endp != NULL && *redir_endp != NUL)
1072 /* Trailing characters are present after the variable name */
1073 EMSG(_(e_trailing));
1074 else
1075 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001076 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 var_redir_stop();
1078 return FAIL;
1079 }
1080
1081 /* check if we can write to the variable: set it to or append an empty
1082 * string */
1083 save_emsg = did_emsg;
1084 did_emsg = FALSE;
1085 tv.v_type = VAR_STRING;
1086 tv.vval.v_string = (char_u *)"";
1087 if (append)
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1089 else
1090 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001091 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001093 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 if (err)
1095 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001096 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 var_redir_stop();
1098 return FAIL;
1099 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100
1101 return OK;
1102}
1103
1104/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 * Append "value[value_len]" to the variable set by var_redir_start().
1106 * The actual appending is postponed until redirection ends, because the value
1107 * appended may in fact be the string we write to, changing it may cause freed
1108 * memory to be used:
1109 * :redir => foo
1110 * :let foo
1111 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112 */
1113 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
1120 if (redir_lval == NULL)
1121 return;
1122
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 {
1130 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001131 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 }
1133 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135}
1136
1137/*
1138 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 */
1141 void
1142var_redir_stop()
1143{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001144 typval_T tv;
1145
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 if (redir_lval != NULL)
1147 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 /* If there was no error: assign the text to the variable. */
1149 if (redir_endp != NULL)
1150 {
1151 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1152 tv.v_type = VAR_STRING;
1153 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001154 /* Call get_lval() again, if it's inside a Dict or List it may
1155 * have changed. */
1156 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1157 FALSE, FALSE, FALSE, FNE_CHECK_START);
1158 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1159 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1160 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001162
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 /* free the collected output */
1164 vim_free(redir_ga.ga_data);
1165 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 vim_free(redir_lval);
1168 redir_lval = NULL;
1169 }
1170 vim_free(redir_varname);
1171 redir_varname = NULL;
1172}
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174# if defined(FEAT_MBYTE) || defined(PROTO)
1175 int
1176eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1177 char_u *enc_from;
1178 char_u *enc_to;
1179 char_u *fname_from;
1180 char_u *fname_to;
1181{
1182 int err = FALSE;
1183
1184 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1185 set_vim_var_string(VV_CC_TO, enc_to, -1);
1186 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1187 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1188 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1189 err = TRUE;
1190 set_vim_var_string(VV_CC_FROM, NULL, -1);
1191 set_vim_var_string(VV_CC_TO, NULL, -1);
1192 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1193 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1194
1195 if (err)
1196 return FAIL;
1197 return OK;
1198}
1199# endif
1200
1201# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1202 int
1203eval_printexpr(fname, args)
1204 char_u *fname;
1205 char_u *args;
1206{
1207 int err = FALSE;
1208
1209 set_vim_var_string(VV_FNAME_IN, fname, -1);
1210 set_vim_var_string(VV_CMDARG, args, -1);
1211 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1212 err = TRUE;
1213 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1214 set_vim_var_string(VV_CMDARG, NULL, -1);
1215
1216 if (err)
1217 {
1218 mch_remove(fname);
1219 return FAIL;
1220 }
1221 return OK;
1222}
1223# endif
1224
1225# if defined(FEAT_DIFF) || defined(PROTO)
1226 void
1227eval_diff(origfile, newfile, outfile)
1228 char_u *origfile;
1229 char_u *newfile;
1230 char_u *outfile;
1231{
1232 int err = FALSE;
1233
1234 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1235 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1236 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1237 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1238 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1239 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1240 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1241}
1242
1243 void
1244eval_patch(origfile, difffile, outfile)
1245 char_u *origfile;
1246 char_u *difffile;
1247 char_u *outfile;
1248{
1249 int err;
1250
1251 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1252 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1253 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1254 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1255 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258}
1259# endif
1260
1261/*
1262 * Top level evaluation function, returning a boolean.
1263 * Sets "error" to TRUE if there was an error.
1264 * Return TRUE or FALSE.
1265 */
1266 int
1267eval_to_bool(arg, error, nextcmd, skip)
1268 char_u *arg;
1269 int *error;
1270 char_u **nextcmd;
1271 int skip; /* only parse, don't execute */
1272{
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 int retval = FALSE;
1275
1276 if (skip)
1277 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 else
1281 {
1282 *error = FALSE;
1283 if (!skip)
1284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001285 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001286 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 }
1289 if (skip)
1290 --emsg_skip;
1291
1292 return retval;
1293}
1294
1295/*
1296 * Top level evaluation function, returning a string. If "skip" is TRUE,
1297 * only parsing to "nextcmd" is done, without reporting errors. Return
1298 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1299 */
1300 char_u *
1301eval_to_string_skip(arg, nextcmd, skip)
1302 char_u *arg;
1303 char_u **nextcmd;
1304 int skip; /* only parse, don't execute */
1305{
Bram Moolenaar33570922005-01-25 22:26:29 +00001306 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 char_u *retval;
1308
1309 if (skip)
1310 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 retval = NULL;
1313 else
1314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 retval = vim_strsave(get_tv_string(&tv));
1316 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 if (skip)
1319 --emsg_skip;
1320
1321 return retval;
1322}
1323
1324/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325 * Skip over an expression at "*pp".
1326 * Return FAIL for an error, OK otherwise.
1327 */
1328 int
1329skip_expr(pp)
1330 char_u **pp;
1331{
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001333
1334 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336}
1337
1338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 * When "convert" is TRUE convert a List into a sequence of lines and convert
1341 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 * Return pointer to allocated memory, or NULL for failure.
1343 */
1344 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *arg;
1347 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
Bram Moolenaar33570922005-01-25 22:26:29 +00001350 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001352 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 retval = NULL;
1359 else
1360 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001361 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 {
1363 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001366 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001367 if (tv.vval.v_list->lv_len > 0)
1368 ga_append(&ga, NL);
1369 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 ga_append(&ga, NUL);
1371 retval = (char_u *)ga.ga_data;
1372 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373#ifdef FEAT_FLOAT
1374 else if (convert && tv.v_type == VAR_FLOAT)
1375 {
1376 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1377 retval = vim_strsave(numbuf);
1378 }
1379#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001380 else
1381 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384
1385 return retval;
1386}
1387
1388/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389 * Call eval_to_string() without using current local variables and using
1390 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 */
1392 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 char_u *arg;
1395 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 char_u *retval;
1399 void *save_funccalp;
1400
1401 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001402 if (use_sandbox)
1403 ++sandbox;
1404 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001405 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 --sandbox;
1408 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 restore_funccal(save_funccalp);
1410 return retval;
1411}
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413/*
1414 * Top level evaluation function, returning a number.
1415 * Evaluates "expr" silently.
1416 * Returns -1 for an error.
1417 */
1418 int
1419eval_to_number(expr)
1420 char_u *expr;
1421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001424 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 ++emsg_off;
1427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 retval = -1;
1430 else
1431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001432 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 --emsg_off;
1436
1437 return retval;
1438}
1439
Bram Moolenaara40058a2005-07-11 22:42:07 +00001440/*
1441 * Prepare v: variable "idx" to be used.
1442 * Save the current typeval in "save_tv".
1443 * When not used yet add the variable to the v: hashtable.
1444 */
1445 static void
1446prepare_vimvar(idx, save_tv)
1447 int idx;
1448 typval_T *save_tv;
1449{
1450 *save_tv = vimvars[idx].vv_tv;
1451 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1452 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1453}
1454
1455/*
1456 * Restore v: variable "idx" to typeval "save_tv".
1457 * When no longer defined, remove the variable from the v: hashtable.
1458 */
1459 static void
1460restore_vimvar(idx, save_tv)
1461 int idx;
1462 typval_T *save_tv;
1463{
1464 hashitem_T *hi;
1465
Bram Moolenaara40058a2005-07-11 22:42:07 +00001466 vimvars[idx].vv_tv = *save_tv;
1467 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1468 {
1469 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1470 if (HASHITEM_EMPTY(hi))
1471 EMSG2(_(e_intern2), "restore_vimvar()");
1472 else
1473 hash_remove(&vimvarht, hi);
1474 }
1475}
1476
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001477#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001478/*
1479 * Evaluate an expression to a list with suggestions.
1480 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001481 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482 */
1483 list_T *
1484eval_spell_expr(badword, expr)
1485 char_u *badword;
1486 char_u *expr;
1487{
1488 typval_T save_val;
1489 typval_T rettv;
1490 list_T *list = NULL;
1491 char_u *p = skipwhite(expr);
1492
1493 /* Set "v:val" to the bad word. */
1494 prepare_vimvar(VV_VAL, &save_val);
1495 vimvars[VV_VAL].vv_type = VAR_STRING;
1496 vimvars[VV_VAL].vv_str = badword;
1497 if (p_verbose == 0)
1498 ++emsg_off;
1499
1500 if (eval1(&p, &rettv, TRUE) == OK)
1501 {
1502 if (rettv.v_type != VAR_LIST)
1503 clear_tv(&rettv);
1504 else
1505 list = rettv.vval.v_list;
1506 }
1507
1508 if (p_verbose == 0)
1509 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510 restore_vimvar(VV_VAL, &save_val);
1511
1512 return list;
1513}
1514
1515/*
1516 * "list" is supposed to contain two items: a word and a number. Return the
1517 * word in "pp" and the number as the return value.
1518 * Return -1 if anything isn't right.
1519 * Used to get the good word and score from the eval_spell_expr() result.
1520 */
1521 int
1522get_spellword(list, pp)
1523 list_T *list;
1524 char_u **pp;
1525{
1526 listitem_T *li;
1527
1528 li = list->lv_first;
1529 if (li == NULL)
1530 return -1;
1531 *pp = get_tv_string(&li->li_tv);
1532
1533 li = li->li_next;
1534 if (li == NULL)
1535 return -1;
1536 return get_tv_number(&li->li_tv);
1537}
1538#endif
1539
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001541 * Top level evaluation function.
1542 * Returns an allocated typval_T with the result.
1543 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 */
1545 typval_T *
1546eval_expr(arg, nextcmd)
1547 char_u *arg;
1548 char_u **nextcmd;
1549{
1550 typval_T *tv;
1551
1552 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001553 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001554 {
1555 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001556 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001557 }
1558
1559 return tv;
1560}
1561
1562
Bram Moolenaar4f688582007-07-24 12:34:30 +00001563#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1564 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 * Uses argv[argc] for the function arguments. Only Number and String
1568 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001571 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 char_u *func;
1574 int argc;
1575 char_u **argv;
1576 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaar33570922005-01-25 22:26:29 +00001579 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 long n;
1581 int len;
1582 int i;
1583 int doesrange;
1584 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001587 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 for (i = 0; i < argc; i++)
1592 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 /* Pass a NULL or empty argument as an empty string */
1594 if (argv[i] == NULL || *argv[i] == NUL)
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_STRING;
1597 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001598 continue;
1599 }
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1603 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
1654 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1655 return NULL;
1656
1657 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 return retval;
1660}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001661# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662
Bram Moolenaar4f688582007-07-24 12:34:30 +00001663# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001664/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001665 * Call vimL function "func" and return the result as a number.
1666 * Returns -1 when calling the function fails.
1667 * Uses argv[argc] for the function arguments.
1668 */
1669 long
1670call_func_retnr(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
1677 long retval;
1678
1679 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1680 return -1;
1681
1682 retval = get_tv_number_chk(&rettv, NULL);
1683 clear_tv(&rettv);
1684 return retval;
1685}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001686# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001687
1688/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001689 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001691 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 */
1693 void *
1694call_func_retlist(func, argc, argv, safe)
1695 char_u *func;
1696 int argc;
1697 char_u **argv;
1698 int safe; /* use the sandbox */
1699{
1700 typval_T rettv;
1701
1702 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1703 return NULL;
1704
1705 if (rettv.v_type != VAR_LIST)
1706 {
1707 clear_tv(&rettv);
1708 return NULL;
1709 }
1710
1711 return rettv.vval.v_list;
1712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#endif
1714
Bram Moolenaar4f688582007-07-24 12:34:30 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716/*
1717 * Save the current function call pointer, and set it to NULL.
1718 * Used when executing autocommands and for ":source".
1719 */
1720 void *
1721save_funccal()
1722{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 current_funccal = NULL;
1726 return (void *)fc;
1727}
1728
1729 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001730restore_funccal(vfc)
1731 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733 funccall_T *fc = (funccall_T *)vfc;
1734
1735 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736}
1737
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738#if defined(FEAT_PROFILE) || defined(PROTO)
1739/*
1740 * Prepare profiling for entering a child or something else that is not
1741 * counted for the script/function itself.
1742 * Should always be called in pair with prof_child_exit().
1743 */
1744 void
1745prof_child_enter(tm)
1746 proftime_T *tm; /* place to store waittime */
1747{
1748 funccall_T *fc = current_funccal;
1749
1750 if (fc != NULL && fc->func->uf_profiling)
1751 profile_start(&fc->prof_child);
1752 script_prof_save(tm);
1753}
1754
1755/*
1756 * Take care of time spent in a child.
1757 * Should always be called after prof_child_enter().
1758 */
1759 void
1760prof_child_exit(tm)
1761 proftime_T *tm; /* where waittime was stored */
1762{
1763 funccall_T *fc = current_funccal;
1764
1765 if (fc != NULL && fc->func->uf_profiling)
1766 {
1767 profile_end(&fc->prof_child);
1768 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1769 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1770 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1771 }
1772 script_prof_restore(tm);
1773}
1774#endif
1775
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#ifdef FEAT_FOLDING
1778/*
1779 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1780 * it in "*cp". Doesn't give error messages.
1781 */
1782 int
1783eval_foldexpr(arg, cp)
1784 char_u *arg;
1785 int *cp;
1786{
Bram Moolenaar33570922005-01-25 22:26:29 +00001787 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 int retval;
1789 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001790 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1791 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001794 if (use_sandbox)
1795 ++sandbox;
1796 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 retval = 0;
1800 else
1801 {
1802 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 if (tv.v_type == VAR_NUMBER)
1804 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001805 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 retval = 0;
1807 else
1808 {
1809 /* If the result is a string, check if there is a non-digit before
1810 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (!VIM_ISDIGIT(*s) && *s != '-')
1813 *cp = *s++;
1814 retval = atol((char *)s);
1815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
1818 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001819 if (use_sandbox)
1820 --sandbox;
1821 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 return retval;
1824}
1825#endif
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 * ":let" list all variable values
1829 * ":let var1 var2" list variable values
1830 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 * ":let var += expr" assignment command.
1832 * ":let var -= expr" assignment command.
1833 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 void
1837ex_let(eap)
1838 exarg_T *eap;
1839{
1840 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 int var_count = 0;
1845 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001847 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001848 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 argend = skip_var_list(arg, &var_count, &semicolon);
1851 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001853 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1854 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001855 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001858 /*
1859 * ":let" without "=": list variables
1860 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 if (*arg == '[')
1862 EMSG(_(e_invarg));
1863 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_glob_vars(&first);
1870 list_buf_vars(&first);
1871 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001872#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001873 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001874#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 list_script_vars(&first);
1876 list_func_vars(&first);
1877 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 eap->nextcmd = check_nextcmd(arg);
1880 }
1881 else
1882 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 op[0] = '=';
1884 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001885 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 {
1887 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1888 op[0] = expr[-1]; /* +=, -= or .= */
1889 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (eap->skip)
1893 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 {
1897 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 --emsg_skip;
1900 }
1901 else if (i != FAIL)
1902 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 }
1908}
1909
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910/*
1911 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1912 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 * When "nextchars" is not NULL it points to a string with characters that
1914 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1915 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 * Returns OK or FAIL;
1917 */
1918 static int
1919ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1920 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 int copy; /* copy values from "tv", don't move */
1923 int semicolon; /* from skip_var_list() */
1924 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926{
1927 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001928 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 listitem_T *item;
1931 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932
1933 if (*arg != '[')
1934 {
1935 /*
1936 * ":let var = expr" or ":for var in list"
1937 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 return FAIL;
1940 return OK;
1941 }
1942
1943 /*
1944 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1945 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001946 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 {
1948 EMSG(_(e_listreq));
1949 return FAIL;
1950 }
1951
1952 i = list_len(l);
1953 if (semicolon == 0 && var_count < i)
1954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001955 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 }
1958 if (var_count - semicolon > i)
1959 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001960 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 return FAIL;
1962 }
1963
1964 item = l->lv_first;
1965 while (*arg != ']')
1966 {
1967 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 item = item->li_next;
1970 if (arg == NULL)
1971 return FAIL;
1972
1973 arg = skipwhite(arg);
1974 if (*arg == ';')
1975 {
1976 /* Put the rest of the list (may be empty) in the var after ';'.
1977 * Create a new list for this. */
1978 l = list_alloc();
1979 if (l == NULL)
1980 return FAIL;
1981 while (item != NULL)
1982 {
1983 list_append_tv(l, &item->li_tv);
1984 item = item->li_next;
1985 }
1986
1987 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001988 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 ltv.vval.v_list = l;
1990 l->lv_refcount = 1;
1991
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1993 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 clear_tv(&ltv);
1995 if (arg == NULL)
1996 return FAIL;
1997 break;
1998 }
1999 else if (*arg != ',' && *arg != ']')
2000 {
2001 EMSG2(_(e_intern2), "ex_let_vars()");
2002 return FAIL;
2003 }
2004 }
2005
2006 return OK;
2007}
2008
2009/*
2010 * Skip over assignable variable "var" or list of variables "[var, var]".
2011 * Used for ":let varvar = expr" and ":for varvar in expr".
2012 * For "[var, var]" increment "*var_count" for each variable.
2013 * for "[var, var; var]" set "semicolon".
2014 * Return NULL for an error.
2015 */
2016 static char_u *
2017skip_var_list(arg, var_count, semicolon)
2018 char_u *arg;
2019 int *var_count;
2020 int *semicolon;
2021{
2022 char_u *p, *s;
2023
2024 if (*arg == '[')
2025 {
2026 /* "[var, var]": find the matching ']'. */
2027 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002028 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 {
2030 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2031 s = skip_var_one(p);
2032 if (s == p)
2033 {
2034 EMSG2(_(e_invarg2), p);
2035 return NULL;
2036 }
2037 ++*var_count;
2038
2039 p = skipwhite(s);
2040 if (*p == ']')
2041 break;
2042 else if (*p == ';')
2043 {
2044 if (*semicolon == 1)
2045 {
2046 EMSG(_("Double ; in list of variables"));
2047 return NULL;
2048 }
2049 *semicolon = 1;
2050 }
2051 else if (*p != ',')
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 }
2057 return p + 1;
2058 }
2059 else
2060 return skip_var_one(arg);
2061}
2062
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002064 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002065 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002067 static char_u *
2068skip_var_one(arg)
2069 char_u *arg;
2070{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002071 if (*arg == '@' && arg[1] != NUL)
2072 return arg + 2;
2073 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2074 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075}
2076
Bram Moolenaara7043832005-01-21 11:56:39 +00002077/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 * List variables for hashtab "ht" with prefix "prefix".
2079 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087{
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 hashitem_T *hi;
2089 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090 int todo;
2091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002092 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2094 {
2095 if (!HASHITEM_EMPTY(hi))
2096 {
2097 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002098 di = HI2DI(hi);
2099 if (empty || di->di_tv.v_type != VAR_STRING
2100 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 }
2103 }
2104}
2105
2106/*
2107 * List global variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_glob_vars(first)
2111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114}
2115
2116/*
2117 * List buffer variables.
2118 */
2119 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120list_buf_vars(first)
2121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 char_u numbuf[NUMBUFLEN];
2124
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2126 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002127
2128 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2130 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131}
2132
2133/*
2134 * List window variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_win_vars(first)
2138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2141 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142}
2143
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144#ifdef FEAT_WINDOWS
2145/*
2146 * List tab page variables.
2147 */
2148 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149list_tab_vars(first)
2150 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002151{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2153 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154}
2155#endif
2156
Bram Moolenaara7043832005-01-21 11:56:39 +00002157/*
2158 * List Vim variables.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_vim_vars(first)
2162 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165}
2166
2167/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168 * List script-local variables, if there is a script.
2169 */
2170 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171list_script_vars(first)
2172 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173{
2174 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2176 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177}
2178
2179/*
2180 * List function variables, if there is a function.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_func_vars(first)
2184 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185{
2186 if (current_funccal != NULL)
2187 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002189}
2190
2191/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 * List variables in "arg".
2193 */
2194 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 exarg_T *eap;
2197 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199{
2200 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 char_u *name_start;
2204 char_u *arg_subsc;
2205 char_u *tofree;
2206 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207
2208 while (!ends_excmd(*arg) && !got_int)
2209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002212 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2214 {
2215 emsg_severe = TRUE;
2216 EMSG(_(e_trailing));
2217 break;
2218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* get_name_len() takes care of expanding curly braces */
2223 name_start = name = arg;
2224 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2225 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 /* This is mainly to keep test 49 working: when expanding
2228 * curly braces fails overrule the exception error message. */
2229 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 emsg_severe = TRUE;
2232 EMSG2(_(e_invarg2), arg);
2233 break;
2234 }
2235 error = TRUE;
2236 }
2237 else
2238 {
2239 if (tofree != NULL)
2240 name = tofree;
2241 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 else
2244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 /* handle d.key, l[idx], f(expr) */
2246 arg_subsc = arg;
2247 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 'g': list_glob_vars(first); break;
2256 case 'b': list_buf_vars(first); break;
2257 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002258#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002259 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002260#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002261 case 'v': list_vim_vars(first); break;
2262 case 's': list_script_vars(first); break;
2263 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 default:
2265 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 }
2268 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 {
2270 char_u numbuf[NUMBUFLEN];
2271 char_u *tf;
2272 int c;
2273 char_u *s;
2274
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002275 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 c = *arg;
2277 *arg = NUL;
2278 list_one_var_a((char_u *)"",
2279 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 tv.v_type,
2281 s == NULL ? (char_u *)"" : s,
2282 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 *arg = c;
2284 vim_free(tf);
2285 }
2286 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
2289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290
2291 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
2296
2297 return arg;
2298}
2299
2300/*
2301 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2302 * Returns a pointer to the char just after the var name.
2303 * Returns NULL if there is an error.
2304 */
2305 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002308 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002310 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312{
2313 int c1;
2314 char_u *name;
2315 char_u *p;
2316 char_u *arg_end = NULL;
2317 int len;
2318 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320
2321 /*
2322 * ":let $VAR = expr": Set environment variable.
2323 */
2324 if (*arg == '$')
2325 {
2326 /* Find the end of the name. */
2327 ++arg;
2328 name = arg;
2329 len = get_env_len(&arg);
2330 if (len == 0)
2331 EMSG2(_(e_invarg2), name - 1);
2332 else
2333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 if (op != NULL && (*op == '+' || *op == '-'))
2335 EMSG2(_(e_letwrong), op);
2336 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002337 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002339 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 {
2341 c1 = name[len];
2342 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 p = get_tv_string_chk(tv);
2344 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 {
2346 int mustfree = FALSE;
2347 char_u *s = vim_getenv(name, &mustfree);
2348
2349 if (s != NULL)
2350 {
2351 p = tofree = concat_str(s, p);
2352 if (mustfree)
2353 vim_free(s);
2354 }
2355 }
2356 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002357 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 if (STRICMP(name, "HOME") == 0)
2360 init_homedir();
2361 else if (didset_vim && STRICMP(name, "VIM") == 0)
2362 didset_vim = FALSE;
2363 else if (didset_vimruntime
2364 && STRICMP(name, "VIMRUNTIME") == 0)
2365 didset_vimruntime = FALSE;
2366 arg_end = arg;
2367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002368 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 }
2371 }
2372 }
2373
2374 /*
2375 * ":let &option = expr": Set option value.
2376 * ":let &l:option = expr": Set local option value.
2377 * ":let &g:option = expr": Set global option value.
2378 */
2379 else if (*arg == '&')
2380 {
2381 /* Find the end of the name. */
2382 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 if (p == NULL || (endchars != NULL
2384 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 EMSG(_(e_letunexp));
2386 else
2387 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 long n;
2389 int opt_type;
2390 long numval;
2391 char_u *stringval = NULL;
2392 char_u *s;
2393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 c1 = *p;
2395 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396
2397 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 s = get_tv_string_chk(tv); /* != NULL if number or string */
2399 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 {
2401 opt_type = get_option_value(arg, &numval,
2402 &stringval, opt_flags);
2403 if ((opt_type == 1 && *op == '.')
2404 || (opt_type == 0 && *op != '.'))
2405 EMSG2(_(e_letwrong), op);
2406 else
2407 {
2408 if (opt_type == 1) /* number */
2409 {
2410 if (*op == '+')
2411 n = numval + n;
2412 else
2413 n = numval - n;
2414 }
2415 else if (opt_type == 0 && stringval != NULL) /* string */
2416 {
2417 s = concat_str(stringval, s);
2418 vim_free(stringval);
2419 stringval = s;
2420 }
2421 }
2422 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 if (s != NULL)
2424 {
2425 set_option_value(arg, n, s, opt_flags);
2426 arg_end = p;
2427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 }
2431 }
2432
2433 /*
2434 * ":let @r = expr": Set register contents.
2435 */
2436 else if (*arg == '@')
2437 {
2438 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 if (op != NULL && (*op == '+' || *op == '-'))
2440 EMSG2(_(e_letwrong), op);
2441 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002442 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 EMSG(_(e_letunexp));
2444 else
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 char_u *s;
2448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 p = get_tv_string_chk(tv);
2450 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002452 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 if (s != NULL)
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_free(s);
2457 }
2458 }
2459 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002460 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 arg_end = arg + 1;
2463 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
2466 }
2467
2468 /*
2469 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002474 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002476 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2480 EMSG(_(e_letunexp));
2481 else
2482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 arg_end = p;
2485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 }
2489
2490 else
2491 EMSG2(_(e_invarg2), arg);
2492
2493 return arg_end;
2494}
2495
2496/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2498 */
2499 static int
2500check_changedtick(arg)
2501 char_u *arg;
2502{
2503 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2504 {
2505 EMSG2(_(e_readonlyvar), arg);
2506 return TRUE;
2507 }
2508 return FALSE;
2509}
2510
2511/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 * Get an lval: variable, Dict item or List item that can be assigned a value
2513 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2514 * "name.key", "name.key[expr]" etc.
2515 * Indexing only works if "name" is an existing List or Dictionary.
2516 * "name" points to the start of the name.
2517 * If "rettv" is not NULL it points to the value to be assigned.
2518 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2519 * wrong; must end in space or cmd separator.
2520 *
2521 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002522 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 */
2525 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002526get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 typval_T *rettv;
2529 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 int unlet;
2531 int skip;
2532 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002533 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 char_u *expr_start, *expr_end;
2537 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 dictitem_T *v;
2539 typval_T var1;
2540 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549
2550 if (skip)
2551 {
2552 /* When skipping just find the end of the name. */
2553 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 }
2556
2557 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002558 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (expr_start != NULL)
2560 {
2561 /* Don't expand the name when we already know there is an error. */
2562 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2563 && *p != '[' && *p != '.')
2564 {
2565 EMSG(_(e_trailing));
2566 return NULL;
2567 }
2568
2569 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2570 if (lp->ll_exp_name == NULL)
2571 {
2572 /* Report an invalid expression in braces, unless the
2573 * expression evaluation has been cancelled due to an
2574 * aborting error, an interrupt, or an exception. */
2575 if (!aborting() && !quiet)
2576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002577 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 EMSG2(_(e_invarg2), name);
2579 return NULL;
2580 }
2581 }
2582 lp->ll_name = lp->ll_exp_name;
2583 }
2584 else
2585 lp->ll_name = name;
2586
2587 /* Without [idx] or .key we are done. */
2588 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2589 return p;
2590
2591 cc = *p;
2592 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002593 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (v == NULL && !quiet)
2595 EMSG2(_(e_undefvar), lp->ll_name);
2596 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 if (v == NULL)
2598 return NULL;
2599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 /*
2601 * Loop until no more [idx] or .key is following.
2602 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002603 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2607 && !(lp->ll_tv->v_type == VAR_DICT
2608 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (!quiet)
2611 EMSG(_("E689: Can only index a List or Dictionary"));
2612 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (!quiet)
2617 EMSG(_("E708: [:] must come last"));
2618 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 len = -1;
2622 if (*p == '.')
2623 {
2624 key = p + 1;
2625 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2626 ;
2627 if (len == 0)
2628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!quiet)
2630 EMSG(_(e_emptykey));
2631 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 }
2633 p = key + len;
2634 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 else
2636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 if (*p == ':')
2640 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 else
2642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 empty1 = FALSE;
2644 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002646 if (get_tv_string_chk(&var1) == NULL)
2647 {
2648 /* not a number or string */
2649 clear_tv(&var1);
2650 return NULL;
2651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 }
2653
2654 /* Optionally get the second index [ :expr]. */
2655 if (*p == ':')
2656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (rettv != NULL && (rettv->v_type != VAR_LIST
2666 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
2669 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (!empty1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 }
2674 p = skipwhite(p + 1);
2675 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 else
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (!empty1)
2683 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (get_tv_string_chk(&var2) == NULL)
2687 {
2688 /* not a number or string */
2689 if (!empty1)
2690 clear_tv(&var1);
2691 clear_tv(&var2);
2692 return NULL;
2693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
2703 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (!empty1)
2705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710
2711 /* Skip to past ']'. */
2712 ++p;
2713 }
2714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
2717 if (len == -1)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002720 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (*key == NUL)
2722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (!quiet)
2724 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
2728 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002729 lp->ll_list = NULL;
2730 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002731 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002732
2733 /* When assigning to g: check that a function and variable name is
2734 * valid. */
2735 if (rettv != NULL && lp->ll_dict == &globvardict)
2736 {
2737 if (rettv->v_type == VAR_FUNC
2738 && var_check_func_name(key, lp->ll_di == NULL))
2739 return NULL;
2740 if (!valid_varname(key))
2741 return NULL;
2742 }
2743
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 /* Can't add "v:" variable. */
2747 if (lp->ll_dict == &vimvardict)
2748 {
2749 EMSG2(_(e_illvar), name);
2750 return NULL;
2751 }
2752
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002753 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002757 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (len == -1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
2762 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (len == -1)
2767 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 p = NULL;
2770 break;
2771 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 /* existing variable, need to check if it can be changed */
2773 else if (var_check_ro(lp->ll_di->di_flags, name))
2774 return NULL;
2775
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (len == -1)
2777 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
2780 else
2781 {
2782 /*
2783 * Get the number and item for the only or first index of the List.
2784 */
2785 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 else
2788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002789 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 clear_tv(&var1);
2791 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002792 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_list = lp->ll_tv->vval.v_list;
2794 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2795 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002797 if (lp->ll_n1 < 0)
2798 {
2799 lp->ll_n1 = 0;
2800 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2801 }
2802 }
2803 if (lp->ll_li == NULL)
2804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002807 if (!quiet)
2808 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811
2812 /*
2813 * May need to find the item or absolute index for the second
2814 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 * When no index given: "lp->ll_empty2" is TRUE.
2816 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002826 {
2827 if (!quiet)
2828 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002830 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2835 if (lp->ll_n1 < 0)
2836 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2837 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 {
2839 if (!quiet)
2840 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002842 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002847 }
2848
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 return p;
2850}
2851
2852/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 */
2855 static void
2856clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858{
2859 vim_free(lp->ll_exp_name);
2860 vim_free(lp->ll_newkey);
2861}
2862
2863/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002864 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002866 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 */
2868 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002874 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875{
2876 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 listitem_T *ri;
2878 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879
2880 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 cc = *endp;
2885 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002886 if (op != NULL && *op != '=')
2887 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002890 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002891 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002892 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 {
2894 if (tv_op(&tv, rettv, op) == OK)
2895 set_var(lp->ll_name, &tv, FALSE);
2896 clear_tv(&tv);
2897 }
2898 }
2899 else
2900 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002904 else if (tv_check_lock(lp->ll_newkey == NULL
2905 ? lp->ll_tv->v_lock
2906 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2907 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 else if (lp->ll_range)
2909 {
2910 /*
2911 * Assign the List values to the list items.
2912 */
2913 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 if (op != NULL && *op != '=')
2916 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2917 else
2918 {
2919 clear_tv(&lp->ll_li->li_tv);
2920 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2921 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 ri = ri->li_next;
2923 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2924 break;
2925 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002928 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 ri = NULL;
2931 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002932 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 lp->ll_li = lp->ll_li->li_next;
2935 ++lp->ll_n1;
2936 }
2937 if (ri != NULL)
2938 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 else if (lp->ll_empty2
2940 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 : lp->ll_n1 != lp->ll_n2)
2942 EMSG(_("E711: List value has not enough items"));
2943 }
2944 else
2945 {
2946 /*
2947 * Assign to a List or Dictionary item.
2948 */
2949 if (lp->ll_newkey != NULL)
2950 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 if (op != NULL && *op != '=')
2952 {
2953 EMSG2(_(e_letwrong), op);
2954 return;
2955 }
2956
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002958 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 if (di == NULL)
2960 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002961 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2962 {
2963 vim_free(di);
2964 return;
2965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002967 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 else if (op != NULL && *op != '=')
2969 {
2970 tv_op(lp->ll_tv, rettv, op);
2971 return;
2972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002975
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 /*
2977 * Assign the value to the variable or list item.
2978 */
2979 if (copy)
2980 copy_tv(rettv, lp->ll_tv);
2981 else
2982 {
2983 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002984 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002986 }
2987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002988}
2989
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002990/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2992 * Returns OK or FAIL.
2993 */
2994 static int
2995tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002996 typval_T *tv1;
2997 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 char_u *op;
2999{
3000 long n;
3001 char_u numbuf[NUMBUFLEN];
3002 char_u *s;
3003
3004 /* Can't do anything with a Funcref or a Dict on the right. */
3005 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3006 {
3007 switch (tv1->v_type)
3008 {
3009 case VAR_DICT:
3010 case VAR_FUNC:
3011 break;
3012
3013 case VAR_LIST:
3014 if (*op != '+' || tv2->v_type != VAR_LIST)
3015 break;
3016 /* List += List */
3017 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3018 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3019 return OK;
3020
3021 case VAR_NUMBER:
3022 case VAR_STRING:
3023 if (tv2->v_type == VAR_LIST)
3024 break;
3025 if (*op == '+' || *op == '-')
3026 {
3027 /* nr += nr or nr -= nr*/
3028 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029#ifdef FEAT_FLOAT
3030 if (tv2->v_type == VAR_FLOAT)
3031 {
3032 float_T f = n;
3033
3034 if (*op == '+')
3035 f += tv2->vval.v_float;
3036 else
3037 f -= tv2->vval.v_float;
3038 clear_tv(tv1);
3039 tv1->v_type = VAR_FLOAT;
3040 tv1->vval.v_float = f;
3041 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043#endif
3044 {
3045 if (*op == '+')
3046 n += get_tv_number(tv2);
3047 else
3048 n -= get_tv_number(tv2);
3049 clear_tv(tv1);
3050 tv1->v_type = VAR_NUMBER;
3051 tv1->vval.v_number = n;
3052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 }
3054 else
3055 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056 if (tv2->v_type == VAR_FLOAT)
3057 break;
3058
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 /* str .= str */
3060 s = get_tv_string(tv1);
3061 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3062 clear_tv(tv1);
3063 tv1->v_type = VAR_STRING;
3064 tv1->vval.v_string = s;
3065 }
3066 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067
3068#ifdef FEAT_FLOAT
3069 case VAR_FLOAT:
3070 {
3071 float_T f;
3072
3073 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3074 && tv2->v_type != VAR_NUMBER
3075 && tv2->v_type != VAR_STRING))
3076 break;
3077 if (tv2->v_type == VAR_FLOAT)
3078 f = tv2->vval.v_float;
3079 else
3080 f = get_tv_number(tv2);
3081 if (*op == '+')
3082 tv1->vval.v_float += f;
3083 else
3084 tv1->vval.v_float -= f;
3085 }
3086 return OK;
3087#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 }
3089 }
3090
3091 EMSG2(_(e_letwrong), op);
3092 return FAIL;
3093}
3094
3095/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 * Add a watcher to a list.
3097 */
3098 static void
3099list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003100 list_T *l;
3101 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003102{
3103 lw->lw_next = l->lv_watch;
3104 l->lv_watch = lw;
3105}
3106
3107/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003108 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109 * No warning when it isn't found...
3110 */
3111 static void
3112list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 list_T *l;
3114 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115{
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117
3118 lwp = &l->lv_watch;
3119 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3120 {
3121 if (lw == lwrem)
3122 {
3123 *lwp = lw->lw_next;
3124 break;
3125 }
3126 lwp = &lw->lw_next;
3127 }
3128}
3129
3130/*
3131 * Just before removing an item from a list: advance watchers to the next
3132 * item.
3133 */
3134 static void
3135list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003136 list_T *l;
3137 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138{
Bram Moolenaar33570922005-01-25 22:26:29 +00003139 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140
3141 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3142 if (lw->lw_item == item)
3143 lw->lw_item = item->li_next;
3144}
3145
3146/*
3147 * Evaluate the expression used in a ":for var in expr" command.
3148 * "arg" points to "var".
3149 * Set "*errp" to TRUE for an error, FALSE otherwise;
3150 * Return a pointer that holds the info. Null when there is an error.
3151 */
3152 void *
3153eval_for_line(arg, errp, nextcmdp, skip)
3154 char_u *arg;
3155 int *errp;
3156 char_u **nextcmdp;
3157 int skip;
3158{
Bram Moolenaar33570922005-01-25 22:26:29 +00003159 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 typval_T tv;
3162 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003163
3164 *errp = TRUE; /* default: there is an error */
3165
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167 if (fi == NULL)
3168 return NULL;
3169
3170 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3171 if (expr == NULL)
3172 return fi;
3173
3174 expr = skipwhite(expr);
3175 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3176 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003177 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 return fi;
3179 }
3180
3181 if (skip)
3182 ++emsg_skip;
3183 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3184 {
3185 *errp = FALSE;
3186 if (!skip)
3187 {
3188 l = tv.vval.v_list;
3189 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003190 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003192 clear_tv(&tv);
3193 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 else
3195 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003196 /* No need to increment the refcount, it's already set for the
3197 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 fi->fi_list = l;
3199 list_add_watch(l, &fi->fi_lw);
3200 fi->fi_lw.lw_item = l->lv_first;
3201 }
3202 }
3203 }
3204 if (skip)
3205 --emsg_skip;
3206
3207 return fi;
3208}
3209
3210/*
3211 * Use the first item in a ":for" list. Advance to the next.
3212 * Assign the values to the variable (list). "arg" points to the first one.
3213 * Return TRUE when a valid item was found, FALSE when at end of list or
3214 * something wrong.
3215 */
3216 int
3217next_for_item(fi_void, arg)
3218 void *fi_void;
3219 char_u *arg;
3220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003223 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224
3225 item = fi->fi_lw.lw_item;
3226 if (item == NULL)
3227 result = FALSE;
3228 else
3229 {
3230 fi->fi_lw.lw_item = item->li_next;
3231 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3232 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3233 }
3234 return result;
3235}
3236
3237/*
3238 * Free the structure used to store info used by ":for".
3239 */
3240 void
3241free_for_info(fi_void)
3242 void *fi_void;
3243{
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003246 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003249 list_unref(fi->fi_list);
3250 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 vim_free(fi);
3252}
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3255
3256 void
3257set_context_for_expression(xp, arg, cmdidx)
3258 expand_T *xp;
3259 char_u *arg;
3260 cmdidx_T cmdidx;
3261{
3262 int got_eq = FALSE;
3263 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 if (cmdidx == CMD_let)
3267 {
3268 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003269 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 {
3271 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003272 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 {
3274 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 if (vim_iswhite(*p))
3277 break;
3278 }
3279 return;
3280 }
3281 }
3282 else
3283 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3284 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 while ((xp->xp_pattern = vim_strpbrk(arg,
3286 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3287 {
3288 c = *xp->xp_pattern;
3289 if (c == '&')
3290 {
3291 c = xp->xp_pattern[1];
3292 if (c == '&')
3293 {
3294 ++xp->xp_pattern;
3295 xp->xp_context = cmdidx != CMD_let || got_eq
3296 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3297 }
3298 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003301 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3302 xp->xp_pattern += 2;
3303
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 else if (c == '$')
3307 {
3308 /* environment variable */
3309 xp->xp_context = EXPAND_ENV_VARS;
3310 }
3311 else if (c == '=')
3312 {
3313 got_eq = TRUE;
3314 xp->xp_context = EXPAND_EXPRESSION;
3315 }
3316 else if (c == '<'
3317 && xp->xp_context == EXPAND_FUNCTIONS
3318 && vim_strchr(xp->xp_pattern, '(') == NULL)
3319 {
3320 /* Function name can start with "<SNR>" */
3321 break;
3322 }
3323 else if (cmdidx != CMD_let || got_eq)
3324 {
3325 if (c == '"') /* string */
3326 {
3327 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3328 if (c == '\\' && xp->xp_pattern[1] != NUL)
3329 ++xp->xp_pattern;
3330 xp->xp_context = EXPAND_NOTHING;
3331 }
3332 else if (c == '\'') /* literal string */
3333 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003334 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3336 /* skip */ ;
3337 xp->xp_context = EXPAND_NOTHING;
3338 }
3339 else if (c == '|')
3340 {
3341 if (xp->xp_pattern[1] == '|')
3342 {
3343 ++xp->xp_pattern;
3344 xp->xp_context = EXPAND_EXPRESSION;
3345 }
3346 else
3347 xp->xp_context = EXPAND_COMMANDS;
3348 }
3349 else
3350 xp->xp_context = EXPAND_EXPRESSION;
3351 }
3352 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353 /* Doesn't look like something valid, expand as an expression
3354 * anyway. */
3355 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 arg = xp->xp_pattern;
3357 if (*arg != NUL)
3358 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3359 /* skip */ ;
3360 }
3361 xp->xp_pattern = arg;
3362}
3363
3364#endif /* FEAT_CMDL_COMPL */
3365
3366/*
3367 * ":1,25call func(arg1, arg2)" function call.
3368 */
3369 void
3370ex_call(eap)
3371 exarg_T *eap;
3372{
3373 char_u *arg = eap->arg;
3374 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003376 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 linenr_T lnum;
3380 int doesrange;
3381 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003382 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003384 if (eap->skip)
3385 {
3386 /* trans_function_name() doesn't work well when skipping, use eval0()
3387 * instead to skip to any following command, e.g. for:
3388 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003389 ++emsg_skip;
3390 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3391 clear_tv(&rettv);
3392 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003393 return;
3394 }
3395
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003396 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003397 if (fudi.fd_newkey != NULL)
3398 {
3399 /* Still need to give an error message for missing key. */
3400 EMSG2(_(e_dictkey), fudi.fd_newkey);
3401 vim_free(fudi.fd_newkey);
3402 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003403 if (tofree == NULL)
3404 return;
3405
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003406 /* Increase refcount on dictionary, it could get deleted when evaluating
3407 * the arguments. */
3408 if (fudi.fd_dict != NULL)
3409 ++fudi.fd_dict->dv_refcount;
3410
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003411 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003412 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003413 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar532c7802005-01-27 14:44:31 +00003415 /* Skip white space to allow ":call func ()". Not good, but required for
3416 * backward compatibility. */
3417 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 if (*startarg != '(')
3421 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003422 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 goto end;
3424 }
3425
3426 /*
3427 * When skipping, evaluate the function once, to find the end of the
3428 * arguments.
3429 * When the function takes a range, this is discovered after the first
3430 * call, and the loop is broken.
3431 */
3432 if (eap->skip)
3433 {
3434 ++emsg_skip;
3435 lnum = eap->line2; /* do it once, also with an invalid range */
3436 }
3437 else
3438 lnum = eap->line1;
3439 for ( ; lnum <= eap->line2; ++lnum)
3440 {
3441 if (!eap->skip && eap->addr_count > 0)
3442 {
3443 curwin->w_cursor.lnum = lnum;
3444 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003445#ifdef FEAT_VIRTUALEDIT
3446 curwin->w_cursor.coladd = 0;
3447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003450 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003451 eap->line1, eap->line2, &doesrange,
3452 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 failed = TRUE;
3455 break;
3456 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003457
3458 /* Handle a function returning a Funcref, Dictionary or List. */
3459 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3460 {
3461 failed = TRUE;
3462 break;
3463 }
3464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (doesrange || eap->skip)
3467 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003470 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003472 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (aborting())
3474 break;
3475 }
3476 if (eap->skip)
3477 --emsg_skip;
3478
3479 if (!failed)
3480 {
3481 /* Check for trailing illegal characters and a following command. */
3482 if (!ends_excmd(*arg))
3483 {
3484 emsg_severe = TRUE;
3485 EMSG(_(e_trailing));
3486 }
3487 else
3488 eap->nextcmd = check_nextcmd(arg);
3489 }
3490
3491end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003492 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003493 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494}
3495
3496/*
3497 * ":unlet[!] var1 ... " command.
3498 */
3499 void
3500ex_unlet(eap)
3501 exarg_T *eap;
3502{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003503 ex_unletlock(eap, eap->arg, 0);
3504}
3505
3506/*
3507 * ":lockvar" and ":unlockvar" commands
3508 */
3509 void
3510ex_lockvar(eap)
3511 exarg_T *eap;
3512{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003514 int deep = 2;
3515
3516 if (eap->forceit)
3517 deep = -1;
3518 else if (vim_isdigit(*arg))
3519 {
3520 deep = getdigits(&arg);
3521 arg = skipwhite(arg);
3522 }
3523
3524 ex_unletlock(eap, arg, deep);
3525}
3526
3527/*
3528 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3529 */
3530 static void
3531ex_unletlock(eap, argstart, deep)
3532 exarg_T *eap;
3533 char_u *argstart;
3534 int deep;
3535{
3536 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003539 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 do
3542 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003543 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003544 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3545 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 if (lv.ll_name == NULL)
3547 error = TRUE; /* error but continue parsing */
3548 if (name_end == NULL || (!vim_iswhite(*name_end)
3549 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 if (name_end != NULL)
3552 {
3553 emsg_severe = TRUE;
3554 EMSG(_(e_trailing));
3555 }
3556 if (!(eap->skip || error))
3557 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 break;
3559 }
3560
3561 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003562 {
3563 if (eap->cmdidx == CMD_unlet)
3564 {
3565 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3566 error = TRUE;
3567 }
3568 else
3569 {
3570 if (do_lock_var(&lv, name_end, deep,
3571 eap->cmdidx == CMD_lockvar) == FAIL)
3572 error = TRUE;
3573 }
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003576 if (!eap->skip)
3577 clear_lval(&lv);
3578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 arg = skipwhite(name_end);
3580 } while (!ends_excmd(*arg));
3581
3582 eap->nextcmd = check_nextcmd(arg);
3583}
3584
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003587 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 int forceit;
3590{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 int ret = OK;
3592 int cc;
3593
3594 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 cc = *name_end;
3597 *name_end = NUL;
3598
3599 /* Normal name or expanded name. */
3600 if (check_changedtick(lp->ll_name))
3601 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003602 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003606 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3607 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 else if (lp->ll_range)
3609 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003610 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611
3612 /* Delete a range of List items. */
3613 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3614 {
3615 li = lp->ll_li->li_next;
3616 listitem_remove(lp->ll_list, lp->ll_li);
3617 lp->ll_li = li;
3618 ++lp->ll_n1;
3619 }
3620 }
3621 else
3622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 /* unlet a List item. */
3625 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003628 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 }
3630
3631 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632}
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634/*
3635 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 */
3638 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
Bram Moolenaar33570922005-01-25 22:26:29 +00003643 hashtab_T *ht;
3644 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003645 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003646 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 ht = find_var_ht(name, &varname);
3649 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003651 hi = hash_find(ht, varname);
3652 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003653 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003654 di = HI2DI(hi);
3655 if (var_check_fixed(di->di_flags, name)
3656 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 return FAIL;
3658 delete_var(ht, hi);
3659 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 if (forceit)
3663 return OK;
3664 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return FAIL;
3666}
3667
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668/*
3669 * Lock or unlock variable indicated by "lp".
3670 * "deep" is the levels to go (-1 for unlimited);
3671 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3672 */
3673 static int
3674do_lock_var(lp, name_end, deep, lock)
3675 lval_T *lp;
3676 char_u *name_end;
3677 int deep;
3678 int lock;
3679{
3680 int ret = OK;
3681 int cc;
3682 dictitem_T *di;
3683
3684 if (deep == 0) /* nothing to do */
3685 return OK;
3686
3687 if (lp->ll_tv == NULL)
3688 {
3689 cc = *name_end;
3690 *name_end = NUL;
3691
3692 /* Normal name or expanded name. */
3693 if (check_changedtick(lp->ll_name))
3694 ret = FAIL;
3695 else
3696 {
3697 di = find_var(lp->ll_name, NULL);
3698 if (di == NULL)
3699 ret = FAIL;
3700 else
3701 {
3702 if (lock)
3703 di->di_flags |= DI_FLAGS_LOCK;
3704 else
3705 di->di_flags &= ~DI_FLAGS_LOCK;
3706 item_lock(&di->di_tv, deep, lock);
3707 }
3708 }
3709 *name_end = cc;
3710 }
3711 else if (lp->ll_range)
3712 {
3713 listitem_T *li = lp->ll_li;
3714
3715 /* (un)lock a range of List items. */
3716 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3717 {
3718 item_lock(&li->li_tv, deep, lock);
3719 li = li->li_next;
3720 ++lp->ll_n1;
3721 }
3722 }
3723 else if (lp->ll_list != NULL)
3724 /* (un)lock a List item. */
3725 item_lock(&lp->ll_li->li_tv, deep, lock);
3726 else
3727 /* un(lock) a Dictionary item. */
3728 item_lock(&lp->ll_di->di_tv, deep, lock);
3729
3730 return ret;
3731}
3732
3733/*
3734 * Lock or unlock an item. "deep" is nr of levels to go.
3735 */
3736 static void
3737item_lock(tv, deep, lock)
3738 typval_T *tv;
3739 int deep;
3740 int lock;
3741{
3742 static int recurse = 0;
3743 list_T *l;
3744 listitem_T *li;
3745 dict_T *d;
3746 hashitem_T *hi;
3747 int todo;
3748
3749 if (recurse >= DICT_MAXNEST)
3750 {
3751 EMSG(_("E743: variable nested too deep for (un)lock"));
3752 return;
3753 }
3754 if (deep == 0)
3755 return;
3756 ++recurse;
3757
3758 /* lock/unlock the item itself */
3759 if (lock)
3760 tv->v_lock |= VAR_LOCKED;
3761 else
3762 tv->v_lock &= ~VAR_LOCKED;
3763
3764 switch (tv->v_type)
3765 {
3766 case VAR_LIST:
3767 if ((l = tv->vval.v_list) != NULL)
3768 {
3769 if (lock)
3770 l->lv_lock |= VAR_LOCKED;
3771 else
3772 l->lv_lock &= ~VAR_LOCKED;
3773 if (deep < 0 || deep > 1)
3774 /* recursive: lock/unlock the items the List contains */
3775 for (li = l->lv_first; li != NULL; li = li->li_next)
3776 item_lock(&li->li_tv, deep - 1, lock);
3777 }
3778 break;
3779 case VAR_DICT:
3780 if ((d = tv->vval.v_dict) != NULL)
3781 {
3782 if (lock)
3783 d->dv_lock |= VAR_LOCKED;
3784 else
3785 d->dv_lock &= ~VAR_LOCKED;
3786 if (deep < 0 || deep > 1)
3787 {
3788 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003789 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3791 {
3792 if (!HASHITEM_EMPTY(hi))
3793 {
3794 --todo;
3795 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3796 }
3797 }
3798 }
3799 }
3800 }
3801 --recurse;
3802}
3803
Bram Moolenaara40058a2005-07-11 22:42:07 +00003804/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003805 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3806 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003807 */
3808 static int
3809tv_islocked(tv)
3810 typval_T *tv;
3811{
3812 return (tv->v_lock & VAR_LOCKED)
3813 || (tv->v_type == VAR_LIST
3814 && tv->vval.v_list != NULL
3815 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3816 || (tv->v_type == VAR_DICT
3817 && tv->vval.v_dict != NULL
3818 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3819}
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3822/*
3823 * Delete all "menutrans_" variables.
3824 */
3825 void
3826del_menutrans_vars()
3827{
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003829 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003832 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003834 {
3835 if (!HASHITEM_EMPTY(hi))
3836 {
3837 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3839 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003840 }
3841 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843}
3844#endif
3845
3846#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3847
3848/*
3849 * Local string buffer for the next two functions to store a variable name
3850 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3851 * get_user_var_name().
3852 */
3853
3854static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3855
3856static char_u *varnamebuf = NULL;
3857static int varnamebuflen = 0;
3858
3859/*
3860 * Function to concatenate a prefix and a variable name.
3861 */
3862 static char_u *
3863cat_prefix_varname(prefix, name)
3864 int prefix;
3865 char_u *name;
3866{
3867 int len;
3868
3869 len = (int)STRLEN(name) + 3;
3870 if (len > varnamebuflen)
3871 {
3872 vim_free(varnamebuf);
3873 len += 10; /* some additional space */
3874 varnamebuf = alloc(len);
3875 if (varnamebuf == NULL)
3876 {
3877 varnamebuflen = 0;
3878 return NULL;
3879 }
3880 varnamebuflen = len;
3881 }
3882 *varnamebuf = prefix;
3883 varnamebuf[1] = ':';
3884 STRCPY(varnamebuf + 2, name);
3885 return varnamebuf;
3886}
3887
3888/*
3889 * Function given to ExpandGeneric() to obtain the list of user defined
3890 * (global/buffer/window/built-in) variable names.
3891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 char_u *
3893get_user_var_name(xp, idx)
3894 expand_T *xp;
3895 int idx;
3896{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003897 static long_u gdone;
3898 static long_u bdone;
3899 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003900#ifdef FEAT_WINDOWS
3901 static long_u tdone;
3902#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003903 static int vidx;
3904 static hashitem_T *hi;
3905 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003909 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910#ifdef FEAT_WINDOWS
3911 tdone = 0;
3912#endif
3913 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003914
3915 /* Global variables */
3916 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003920 else
3921 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003922 while (HASHITEM_EMPTY(hi))
3923 ++hi;
3924 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3925 return cat_prefix_varname('g', hi->hi_key);
3926 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003928
3929 /* b: variables */
3930 ht = &curbuf->b_vars.dv_hashtab;
3931 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hi = ht->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 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return (char_u *)"b:changedtick";
3945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003946
3947 /* w: variables */
3948 ht = &curwin->w_vars.dv_hashtab;
3949 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003951 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003953 else
3954 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 while (HASHITEM_EMPTY(hi))
3956 ++hi;
3957 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003960#ifdef FEAT_WINDOWS
3961 /* t: variables */
3962 ht = &curtab->tp_vars.dv_hashtab;
3963 if (tdone < ht->ht_used)
3964 {
3965 if (tdone++ == 0)
3966 hi = ht->ht_array;
3967 else
3968 ++hi;
3969 while (HASHITEM_EMPTY(hi))
3970 ++hi;
3971 return cat_prefix_varname('t', hi->hi_key);
3972 }
3973#endif
3974
Bram Moolenaar33570922005-01-25 22:26:29 +00003975 /* v: variables */
3976 if (vidx < VV_LEN)
3977 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 vim_free(varnamebuf);
3980 varnamebuf = NULL;
3981 varnamebuflen = 0;
3982 return NULL;
3983}
3984
3985#endif /* FEAT_CMDL_COMPL */
3986
3987/*
3988 * types for expressions.
3989 */
3990typedef enum
3991{
3992 TYPE_UNKNOWN = 0
3993 , TYPE_EQUAL /* == */
3994 , TYPE_NEQUAL /* != */
3995 , TYPE_GREATER /* > */
3996 , TYPE_GEQUAL /* >= */
3997 , TYPE_SMALLER /* < */
3998 , TYPE_SEQUAL /* <= */
3999 , TYPE_MATCH /* =~ */
4000 , TYPE_NOMATCH /* !~ */
4001} exptype_T;
4002
4003/*
4004 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4007 */
4008
4009/*
4010 * Handle zero level expression.
4011 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004013 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * Return OK or FAIL.
4015 */
4016 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 char_u **nextcmd;
4021 int evaluate;
4022{
4023 int ret;
4024 char_u *p;
4025
4026 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (ret == FAIL || !ends_excmd(*p))
4029 {
4030 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 /*
4033 * Report the invalid expression unless the expression evaluation has
4034 * been cancelled due to an aborting error, an interrupt, or an
4035 * exception.
4036 */
4037 if (!aborting())
4038 EMSG2(_(e_invexpr2), arg);
4039 ret = FAIL;
4040 }
4041 if (nextcmd != NULL)
4042 *nextcmd = check_nextcmd(p);
4043
4044 return ret;
4045}
4046
4047/*
4048 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004049 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 *
4051 * "arg" must point to the first non-white of the expression.
4052 * "arg" is advanced to the next non-white after the recognized expression.
4053 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004054 * Note: "rettv.v_lock" is not set.
4055 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 * Return OK or FAIL.
4057 */
4058 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 int evaluate;
4063{
4064 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 /*
4068 * Get the first variable.
4069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return FAIL;
4072
4073 if ((*arg)[0] == '?')
4074 {
4075 result = FALSE;
4076 if (evaluate)
4077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 int error = FALSE;
4079
4080 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 if (error)
4084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086
4087 /*
4088 * Get the second variable.
4089 */
4090 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093
4094 /*
4095 * Check for the ":".
4096 */
4097 if ((*arg)[0] != ':')
4098 {
4099 EMSG(_("E109: Missing ':' after '?'"));
4100 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103 }
4104
4105 /*
4106 * Get the third variable.
4107 */
4108 *arg = skipwhite(*arg + 1);
4109 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4110 {
4111 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114 }
4115 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118
4119 return OK;
4120}
4121
4122/*
4123 * Handle first level expression:
4124 * expr2 || expr2 || expr2 logical OR
4125 *
4126 * "arg" must point to the first non-white of the expression.
4127 * "arg" is advanced to the next non-white after the recognized expression.
4128 *
4129 * Return OK or FAIL.
4130 */
4131 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 int evaluate;
4136{
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 long result;
4139 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004140 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /*
4143 * Get the first variable.
4144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return FAIL;
4147
4148 /*
4149 * Repeat until there is no following "||".
4150 */
4151 first = TRUE;
4152 result = FALSE;
4153 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4154 {
4155 if (evaluate && first)
4156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 if (error)
4161 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 first = FALSE;
4163 }
4164
4165 /*
4166 * Get the second variable.
4167 */
4168 *arg = skipwhite(*arg + 2);
4169 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4170 return FAIL;
4171
4172 /*
4173 * Compute the result.
4174 */
4175 if (evaluate && !result)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (error)
4181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 if (evaluate)
4184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 rettv->v_type = VAR_NUMBER;
4186 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 }
4189
4190 return OK;
4191}
4192
4193/*
4194 * Handle second level expression:
4195 * expr3 && expr3 && expr3 logical AND
4196 *
4197 * "arg" must point to the first non-white of the expression.
4198 * "arg" is advanced to the next non-white after the recognized expression.
4199 *
4200 * Return OK or FAIL.
4201 */
4202 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int evaluate;
4207{
Bram Moolenaar33570922005-01-25 22:26:29 +00004208 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 long result;
4210 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 /*
4214 * Get the first variable.
4215 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218
4219 /*
4220 * Repeat until there is no following "&&".
4221 */
4222 first = TRUE;
4223 result = TRUE;
4224 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4225 {
4226 if (evaluate && first)
4227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004228 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 if (error)
4232 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 first = FALSE;
4234 }
4235
4236 /*
4237 * Get the second variable.
4238 */
4239 *arg = skipwhite(*arg + 2);
4240 if (eval4(arg, &var2, evaluate && result) == FAIL)
4241 return FAIL;
4242
4243 /*
4244 * Compute the result.
4245 */
4246 if (evaluate && result)
4247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (error)
4252 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 if (evaluate)
4255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 rettv->v_type = VAR_NUMBER;
4257 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 }
4260
4261 return OK;
4262}
4263
4264/*
4265 * Handle third level expression:
4266 * var1 == var2
4267 * var1 =~ var2
4268 * var1 != var2
4269 * var1 !~ var2
4270 * var1 > var2
4271 * var1 >= var2
4272 * var1 < var2
4273 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004274 * var1 is var2
4275 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 *
4277 * "arg" must point to the first non-white of the expression.
4278 * "arg" is advanced to the next non-white after the recognized expression.
4279 *
4280 * Return OK or FAIL.
4281 */
4282 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 int evaluate;
4287{
Bram Moolenaar33570922005-01-25 22:26:29 +00004288 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 char_u *p;
4290 int i;
4291 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004292 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int len = 2;
4294 long n1, n2;
4295 char_u *s1, *s2;
4296 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4297 regmatch_T regmatch;
4298 int ic;
4299 char_u *save_cpo;
4300
4301 /*
4302 * Get the first variable.
4303 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FAIL;
4306
4307 p = *arg;
4308 switch (p[0])
4309 {
4310 case '=': if (p[1] == '=')
4311 type = TYPE_EQUAL;
4312 else if (p[1] == '~')
4313 type = TYPE_MATCH;
4314 break;
4315 case '!': if (p[1] == '=')
4316 type = TYPE_NEQUAL;
4317 else if (p[1] == '~')
4318 type = TYPE_NOMATCH;
4319 break;
4320 case '>': if (p[1] != '=')
4321 {
4322 type = TYPE_GREATER;
4323 len = 1;
4324 }
4325 else
4326 type = TYPE_GEQUAL;
4327 break;
4328 case '<': if (p[1] != '=')
4329 {
4330 type = TYPE_SMALLER;
4331 len = 1;
4332 }
4333 else
4334 type = TYPE_SEQUAL;
4335 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004336 case 'i': if (p[1] == 's')
4337 {
4338 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4339 len = 5;
4340 if (!vim_isIDc(p[len]))
4341 {
4342 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4343 type_is = TRUE;
4344 }
4345 }
4346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348
4349 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004350 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 */
4352 if (type != TYPE_UNKNOWN)
4353 {
4354 /* extra question mark appended: ignore case */
4355 if (p[len] == '?')
4356 {
4357 ic = TRUE;
4358 ++len;
4359 }
4360 /* extra '#' appended: match case */
4361 else if (p[len] == '#')
4362 {
4363 ic = FALSE;
4364 ++len;
4365 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004366 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 else
4368 ic = p_ic;
4369
4370 /*
4371 * Get the second variable.
4372 */
4373 *arg = skipwhite(p + len);
4374 if (eval5(arg, &var2, evaluate) == FAIL)
4375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 return FAIL;
4378 }
4379
4380 if (evaluate)
4381 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 if (type_is && rettv->v_type != var2.v_type)
4383 {
4384 /* For "is" a different type always means FALSE, for "notis"
4385 * it means TRUE. */
4386 n1 = (type == TYPE_NEQUAL);
4387 }
4388 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4389 {
4390 if (type_is)
4391 {
4392 n1 = (rettv->v_type == var2.v_type
4393 && rettv->vval.v_list == var2.vval.v_list);
4394 if (type == TYPE_NEQUAL)
4395 n1 = !n1;
4396 }
4397 else if (rettv->v_type != var2.v_type
4398 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4399 {
4400 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004401 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004403 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 clear_tv(rettv);
4405 clear_tv(&var2);
4406 return FAIL;
4407 }
4408 else
4409 {
4410 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004411 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4412 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 if (type == TYPE_NEQUAL)
4414 n1 = !n1;
4415 }
4416 }
4417
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004418 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4419 {
4420 if (type_is)
4421 {
4422 n1 = (rettv->v_type == var2.v_type
4423 && rettv->vval.v_dict == var2.vval.v_dict);
4424 if (type == TYPE_NEQUAL)
4425 n1 = !n1;
4426 }
4427 else if (rettv->v_type != var2.v_type
4428 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4429 {
4430 if (rettv->v_type != var2.v_type)
4431 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4432 else
4433 EMSG(_("E736: Invalid operation for Dictionary"));
4434 clear_tv(rettv);
4435 clear_tv(&var2);
4436 return FAIL;
4437 }
4438 else
4439 {
4440 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004441 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4442 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004443 if (type == TYPE_NEQUAL)
4444 n1 = !n1;
4445 }
4446 }
4447
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4449 {
4450 if (rettv->v_type != var2.v_type
4451 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4452 {
4453 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004454 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004456 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004457 clear_tv(rettv);
4458 clear_tv(&var2);
4459 return FAIL;
4460 }
4461 else
4462 {
4463 /* Compare two Funcrefs for being equal or unequal. */
4464 if (rettv->vval.v_string == NULL
4465 || var2.vval.v_string == NULL)
4466 n1 = FALSE;
4467 else
4468 n1 = STRCMP(rettv->vval.v_string,
4469 var2.vval.v_string) == 0;
4470 if (type == TYPE_NEQUAL)
4471 n1 = !n1;
4472 }
4473 }
4474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004475#ifdef FEAT_FLOAT
4476 /*
4477 * If one of the two variables is a float, compare as a float.
4478 * When using "=~" or "!~", always compare as string.
4479 */
4480 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4481 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4482 {
4483 float_T f1, f2;
4484
4485 if (rettv->v_type == VAR_FLOAT)
4486 f1 = rettv->vval.v_float;
4487 else
4488 f1 = get_tv_number(rettv);
4489 if (var2.v_type == VAR_FLOAT)
4490 f2 = var2.vval.v_float;
4491 else
4492 f2 = get_tv_number(&var2);
4493 n1 = FALSE;
4494 switch (type)
4495 {
4496 case TYPE_EQUAL: n1 = (f1 == f2); break;
4497 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4498 case TYPE_GREATER: n1 = (f1 > f2); break;
4499 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4500 case TYPE_SMALLER: n1 = (f1 < f2); break;
4501 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4502 case TYPE_UNKNOWN:
4503 case TYPE_MATCH:
4504 case TYPE_NOMATCH: break; /* avoid gcc warning */
4505 }
4506 }
4507#endif
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /*
4510 * If one of the two variables is a number, compare as a number.
4511 * When using "=~" or "!~", always compare as string.
4512 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004516 n1 = get_tv_number(rettv);
4517 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 switch (type)
4519 {
4520 case TYPE_EQUAL: n1 = (n1 == n2); break;
4521 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4522 case TYPE_GREATER: n1 = (n1 > n2); break;
4523 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4524 case TYPE_SMALLER: n1 = (n1 < n2); break;
4525 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4526 case TYPE_UNKNOWN:
4527 case TYPE_MATCH:
4528 case TYPE_NOMATCH: break; /* avoid gcc warning */
4529 }
4530 }
4531 else
4532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533 s1 = get_tv_string_buf(rettv, buf1);
4534 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4536 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4537 else
4538 i = 0;
4539 n1 = FALSE;
4540 switch (type)
4541 {
4542 case TYPE_EQUAL: n1 = (i == 0); break;
4543 case TYPE_NEQUAL: n1 = (i != 0); break;
4544 case TYPE_GREATER: n1 = (i > 0); break;
4545 case TYPE_GEQUAL: n1 = (i >= 0); break;
4546 case TYPE_SMALLER: n1 = (i < 0); break;
4547 case TYPE_SEQUAL: n1 = (i <= 0); break;
4548
4549 case TYPE_MATCH:
4550 case TYPE_NOMATCH:
4551 /* avoid 'l' flag in 'cpoptions' */
4552 save_cpo = p_cpo;
4553 p_cpo = (char_u *)"";
4554 regmatch.regprog = vim_regcomp(s2,
4555 RE_MAGIC + RE_STRING);
4556 regmatch.rm_ic = ic;
4557 if (regmatch.regprog != NULL)
4558 {
4559 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4560 vim_free(regmatch.regprog);
4561 if (type == TYPE_NOMATCH)
4562 n1 = !n1;
4563 }
4564 p_cpo = save_cpo;
4565 break;
4566
4567 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4568 }
4569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 clear_tv(rettv);
4571 clear_tv(&var2);
4572 rettv->v_type = VAR_NUMBER;
4573 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575 }
4576
4577 return OK;
4578}
4579
4580/*
4581 * Handle fourth level expression:
4582 * + number addition
4583 * - number subtraction
4584 * . string concatenation
4585 *
4586 * "arg" must point to the first non-white of the expression.
4587 * "arg" is advanced to the next non-white after the recognized expression.
4588 *
4589 * Return OK or FAIL.
4590 */
4591 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 int evaluate;
4596{
Bram Moolenaar33570922005-01-25 22:26:29 +00004597 typval_T var2;
4598 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 int op;
4600 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004601#ifdef FEAT_FLOAT
4602 float_T f1 = 0, f2 = 0;
4603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 char_u *s1, *s2;
4605 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4606 char_u *p;
4607
4608 /*
4609 * Get the first variable.
4610 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004611 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 return FAIL;
4613
4614 /*
4615 * Repeat computing, until no '+', '-' or '.' is following.
4616 */
4617 for (;;)
4618 {
4619 op = **arg;
4620 if (op != '+' && op != '-' && op != '.')
4621 break;
4622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623 if ((op != '+' || rettv->v_type != VAR_LIST)
4624#ifdef FEAT_FLOAT
4625 && (op == '.' || rettv->v_type != VAR_FLOAT)
4626#endif
4627 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 {
4629 /* For "list + ...", an illegal use of the first operand as
4630 * a number cannot be determined before evaluating the 2nd
4631 * operand: if this is also a list, all is ok.
4632 * For "something . ...", "something - ..." or "non-list + ...",
4633 * we know that the first operand needs to be a string or number
4634 * without evaluating the 2nd operand. So check before to avoid
4635 * side effects after an error. */
4636 if (evaluate && get_tv_string_chk(rettv) == NULL)
4637 {
4638 clear_tv(rettv);
4639 return FAIL;
4640 }
4641 }
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /*
4644 * Get the second variable.
4645 */
4646 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004647 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return FAIL;
4651 }
4652
4653 if (evaluate)
4654 {
4655 /*
4656 * Compute the result.
4657 */
4658 if (op == '.')
4659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004660 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4661 s2 = get_tv_string_buf_chk(&var2, buf2);
4662 if (s2 == NULL) /* type error ? */
4663 {
4664 clear_tv(rettv);
4665 clear_tv(&var2);
4666 return FAIL;
4667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004668 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
4670 rettv->v_type = VAR_STRING;
4671 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004673 else if (op == '+' && rettv->v_type == VAR_LIST
4674 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004675 {
4676 /* concatenate Lists */
4677 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4678 &var3) == FAIL)
4679 {
4680 clear_tv(rettv);
4681 clear_tv(&var2);
4682 return FAIL;
4683 }
4684 clear_tv(rettv);
4685 *rettv = var3;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 else
4688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 int error = FALSE;
4690
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#ifdef FEAT_FLOAT
4692 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004693 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694 f1 = rettv->vval.v_float;
4695 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697 else
4698#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004700 n1 = get_tv_number_chk(rettv, &error);
4701 if (error)
4702 {
4703 /* This can only happen for "list + non-list". For
4704 * "non-list + ..." or "something - ...", we returned
4705 * before evaluating the 2nd operand. */
4706 clear_tv(rettv);
4707 return FAIL;
4708 }
4709#ifdef FEAT_FLOAT
4710 if (var2.v_type == VAR_FLOAT)
4711 f1 = n1;
4712#endif
4713 }
4714#ifdef FEAT_FLOAT
4715 if (var2.v_type == VAR_FLOAT)
4716 {
4717 f2 = var2.vval.v_float;
4718 n2 = 0;
4719 }
4720 else
4721#endif
4722 {
4723 n2 = get_tv_number_chk(&var2, &error);
4724 if (error)
4725 {
4726 clear_tv(rettv);
4727 clear_tv(&var2);
4728 return FAIL;
4729 }
4730#ifdef FEAT_FLOAT
4731 if (rettv->v_type == VAR_FLOAT)
4732 f2 = n2;
4733#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004736
4737#ifdef FEAT_FLOAT
4738 /* If there is a float on either side the result is a float. */
4739 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4740 {
4741 if (op == '+')
4742 f1 = f1 + f2;
4743 else
4744 f1 = f1 - f2;
4745 rettv->v_type = VAR_FLOAT;
4746 rettv->vval.v_float = f1;
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749#endif
4750 {
4751 if (op == '+')
4752 n1 = n1 + n2;
4753 else
4754 n1 = n1 - n2;
4755 rettv->v_type = VAR_NUMBER;
4756 rettv->vval.v_number = n1;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762 return OK;
4763}
4764
4765/*
4766 * Handle fifth level expression:
4767 * * number multiplication
4768 * / number division
4769 * % number modulo
4770 *
4771 * "arg" must point to the first non-white of the expression.
4772 * "arg" is advanced to the next non-white after the recognized expression.
4773 *
4774 * Return OK or FAIL.
4775 */
4776 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004777eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004781 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
Bram Moolenaar33570922005-01-25 22:26:29 +00004783 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 int op;
4785 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 int use_float = FALSE;
4788 float_T f1 = 0, f2;
4789#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 /*
4793 * Get the first variable.
4794 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004795 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return FAIL;
4797
4798 /*
4799 * Repeat computing, until no '*', '/' or '%' is following.
4800 */
4801 for (;;)
4802 {
4803 op = **arg;
4804 if (op != '*' && op != '/' && op != '%')
4805 break;
4806
4807 if (evaluate)
4808 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809#ifdef FEAT_FLOAT
4810 if (rettv->v_type == VAR_FLOAT)
4811 {
4812 f1 = rettv->vval.v_float;
4813 use_float = TRUE;
4814 n1 = 0;
4815 }
4816 else
4817#endif
4818 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 if (error)
4821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823 else
4824 n1 = 0;
4825
4826 /*
4827 * Get the second variable.
4828 */
4829 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004830 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 return FAIL;
4832
4833 if (evaluate)
4834 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835#ifdef FEAT_FLOAT
4836 if (var2.v_type == VAR_FLOAT)
4837 {
4838 if (!use_float)
4839 {
4840 f1 = n1;
4841 use_float = TRUE;
4842 }
4843 f2 = var2.vval.v_float;
4844 n2 = 0;
4845 }
4846 else
4847#endif
4848 {
4849 n2 = get_tv_number_chk(&var2, &error);
4850 clear_tv(&var2);
4851 if (error)
4852 return FAIL;
4853#ifdef FEAT_FLOAT
4854 if (use_float)
4855 f2 = n2;
4856#endif
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 /*
4860 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863#ifdef FEAT_FLOAT
4864 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 if (op == '*')
4867 f1 = f1 * f2;
4868 else if (op == '/')
4869 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004870# ifdef VMS
4871 /* VMS crashes on divide by zero, work around it */
4872 if (f2 == 0.0)
4873 {
4874 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004875 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004876 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004877 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004878 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004879 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004880 }
4881 else
4882 f1 = f1 / f2;
4883# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884 /* We rely on the floating point library to handle divide
4885 * by zero to result in "inf" and not a crash. */
4886 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004891 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 return FAIL;
4893 }
4894 rettv->v_type = VAR_FLOAT;
4895 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900 if (op == '*')
4901 n1 = n1 * n2;
4902 else if (op == '/')
4903 {
4904 if (n2 == 0) /* give an error message? */
4905 {
4906 if (n1 == 0)
4907 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4908 else if (n1 < 0)
4909 n1 = -0x7fffffffL;
4910 else
4911 n1 = 0x7fffffffL;
4912 }
4913 else
4914 n1 = n1 / n2;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917 {
4918 if (n2 == 0) /* give an error message? */
4919 n1 = 0;
4920 else
4921 n1 = n1 % n2;
4922 }
4923 rettv->v_type = VAR_NUMBER;
4924 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 }
4928
4929 return OK;
4930}
4931
4932/*
4933 * Handle sixth level expression:
4934 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004935 * "string" string constant
4936 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 * &option-name option value
4938 * @r register contents
4939 * identifier variable value
4940 * function() function call
4941 * $VAR environment variable
4942 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004943 * [expr, expr] List
4944 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 *
4946 * Also handle:
4947 * ! in front logical NOT
4948 * - in front unary minus
4949 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 * trailing [] subscript in String or List
4951 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 *
4953 * "arg" must point to the first non-white of the expression.
4954 * "arg" is advanced to the next non-white after the recognized expression.
4955 *
4956 * Return OK or FAIL.
4957 */
4958 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004959eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004963 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 long n;
4966 int len;
4967 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 char_u *start_leader, *end_leader;
4969 int ret = OK;
4970 char_u *alias;
4971
4972 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 /*
4979 * Skip '!' and '-' characters. They are handled later.
4980 */
4981 start_leader = *arg;
4982 while (**arg == '!' || **arg == '-' || **arg == '+')
4983 *arg = skipwhite(*arg + 1);
4984 end_leader = *arg;
4985
4986 switch (**arg)
4987 {
4988 /*
4989 * Number constant.
4990 */
4991 case '0':
4992 case '1':
4993 case '2':
4994 case '3':
4995 case '4':
4996 case '5':
4997 case '6':
4998 case '7':
4999 case '8':
5000 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 {
5002#ifdef FEAT_FLOAT
5003 char_u *p = skipdigits(*arg + 1);
5004 int get_float = FALSE;
5005
5006 /* We accept a float when the format matches
5007 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005008 * strict to avoid backwards compatibility problems.
5009 * Don't look for a float after the "." operator, so that
5010 * ":let vers = 1.2.3" doesn't fail. */
5011 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 get_float = TRUE;
5014 p = skipdigits(p + 2);
5015 if (*p == 'e' || *p == 'E')
5016 {
5017 ++p;
5018 if (*p == '-' || *p == '+')
5019 ++p;
5020 if (!vim_isdigit(*p))
5021 get_float = FALSE;
5022 else
5023 p = skipdigits(p + 1);
5024 }
5025 if (ASCII_ISALPHA(*p) || *p == '.')
5026 get_float = FALSE;
5027 }
5028 if (get_float)
5029 {
5030 float_T f;
5031
5032 *arg += string2float(*arg, &f);
5033 if (evaluate)
5034 {
5035 rettv->v_type = VAR_FLOAT;
5036 rettv->vval.v_float = f;
5037 }
5038 }
5039 else
5040#endif
5041 {
5042 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5043 *arg += len;
5044 if (evaluate)
5045 {
5046 rettv->v_type = VAR_NUMBER;
5047 rettv->vval.v_number = n;
5048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 /*
5054 * String constant: "string".
5055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058
5059 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005063 break;
5064
5065 /*
5066 * List: [expr, expr]
5067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 break;
5070
5071 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 * Dictionary: {key: val, key: val}
5073 */
5074 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5075 break;
5076
5077 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005078 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005080 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 break;
5082
5083 /*
5084 * Environment variable: $VAR.
5085 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 break;
5088
5089 /*
5090 * Register contents: @r.
5091 */
5092 case '@': ++*arg;
5093 if (evaluate)
5094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005096 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 if (**arg != NUL)
5099 ++*arg;
5100 break;
5101
5102 /*
5103 * nested expression: (expression).
5104 */
5105 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (**arg == ')')
5108 ++*arg;
5109 else if (ret == OK)
5110 {
5111 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 ret = FAIL;
5114 }
5115 break;
5116
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120
5121 if (ret == NOTDONE)
5122 {
5123 /*
5124 * Must be a variable or function name.
5125 * Can also be a curly-braces kind of name: {expr}.
5126 */
5127 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005128 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 if (alias != NULL)
5130 s = alias;
5131
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005132 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 ret = FAIL;
5134 else
5135 {
5136 if (**arg == '(') /* recursive! */
5137 {
5138 /* If "s" is the name of a variable of type VAR_FUNC
5139 * use its contents. */
5140 s = deref_func_name(s, &len);
5141
5142 /* Invoke the function. */
5143 ret = get_func_tv(s, len, rettv, arg,
5144 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005145 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 /* Stop the expression evaluation when immediately
5147 * aborting on error, or when an interrupt occurred or
5148 * an exception was thrown but not caught. */
5149 if (aborting())
5150 {
5151 if (ret == OK)
5152 clear_tv(rettv);
5153 ret = FAIL;
5154 }
5155 }
5156 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005158 else
5159 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005161 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 }
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 *arg = skipwhite(*arg);
5165
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005166 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5167 * expr(expr). */
5168 if (ret == OK)
5169 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 /*
5172 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5173 */
5174 if (ret == OK && evaluate && end_leader > start_leader)
5175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005177 int val = 0;
5178#ifdef FEAT_FLOAT
5179 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005180
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005181 if (rettv->v_type == VAR_FLOAT)
5182 f = rettv->vval.v_float;
5183 else
5184#endif
5185 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005186 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005188 clear_tv(rettv);
5189 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005191 else
5192 {
5193 while (end_leader > start_leader)
5194 {
5195 --end_leader;
5196 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 {
5198#ifdef FEAT_FLOAT
5199 if (rettv->v_type == VAR_FLOAT)
5200 f = !f;
5201 else
5202#endif
5203 val = !val;
5204 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005206 {
5207#ifdef FEAT_FLOAT
5208 if (rettv->v_type == VAR_FLOAT)
5209 f = -f;
5210 else
5211#endif
5212 val = -val;
5213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215#ifdef FEAT_FLOAT
5216 if (rettv->v_type == VAR_FLOAT)
5217 {
5218 clear_tv(rettv);
5219 rettv->vval.v_float = f;
5220 }
5221 else
5222#endif
5223 {
5224 clear_tv(rettv);
5225 rettv->v_type = VAR_NUMBER;
5226 rettv->vval.v_number = val;
5227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230
5231 return ret;
5232}
5233
5234/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005235 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5236 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5238 */
5239 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245{
5246 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005247 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 long len = -1;
5250 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005254 if (rettv->v_type == VAR_FUNC
5255#ifdef FEAT_FLOAT
5256 || rettv->v_type == VAR_FLOAT
5257#endif
5258 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 if (verbose)
5261 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 return FAIL;
5263 }
5264
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 /*
5268 * dict.name
5269 */
5270 key = *arg + 1;
5271 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5272 ;
5273 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 }
5277 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 /*
5280 * something[idx]
5281 *
5282 * Get the (first) variable from inside the [].
5283 */
5284 *arg = skipwhite(*arg + 1);
5285 if (**arg == ':')
5286 empty1 = TRUE;
5287 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5288 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5290 {
5291 /* not a number or string */
5292 clear_tv(&var1);
5293 return FAIL;
5294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295
5296 /*
5297 * Get the second variable from inside the [:].
5298 */
5299 if (**arg == ':')
5300 {
5301 range = TRUE;
5302 *arg = skipwhite(*arg + 1);
5303 if (**arg == ']')
5304 empty2 = TRUE;
5305 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 if (!empty1)
5308 clear_tv(&var1);
5309 return FAIL;
5310 }
5311 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5312 {
5313 /* not a number or string */
5314 if (!empty1)
5315 clear_tv(&var1);
5316 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 return FAIL;
5318 }
5319 }
5320
5321 /* Check for the ']'. */
5322 if (**arg != ']')
5323 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 if (verbose)
5325 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 clear_tv(&var1);
5327 if (range)
5328 clear_tv(&var2);
5329 return FAIL;
5330 }
5331 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 }
5333
5334 if (evaluate)
5335 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336 n1 = 0;
5337 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005339 n1 = get_tv_number(&var1);
5340 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 }
5342 if (range)
5343 {
5344 if (empty2)
5345 n2 = -1;
5346 else
5347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 n2 = get_tv_number(&var2);
5349 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 }
5351 }
5352
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
5355 case VAR_NUMBER:
5356 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005357 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 len = (long)STRLEN(s);
5359 if (range)
5360 {
5361 /* The resulting variable is a substring. If the indexes
5362 * are out of range the result is empty. */
5363 if (n1 < 0)
5364 {
5365 n1 = len + n1;
5366 if (n1 < 0)
5367 n1 = 0;
5368 }
5369 if (n2 < 0)
5370 n2 = len + n2;
5371 else if (n2 >= len)
5372 n2 = len;
5373 if (n1 >= len || n2 < 0 || n1 > n2)
5374 s = NULL;
5375 else
5376 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5377 }
5378 else
5379 {
5380 /* The resulting variable is a string of a single
5381 * character. If the index is too big or negative the
5382 * result is empty. */
5383 if (n1 >= len || n1 < 0)
5384 s = NULL;
5385 else
5386 s = vim_strnsave(s + n1, 1);
5387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 clear_tv(rettv);
5389 rettv->v_type = VAR_STRING;
5390 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 break;
5392
5393 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 if (n1 < 0)
5396 n1 = len + n1;
5397 if (!empty1 && (n1 < 0 || n1 >= len))
5398 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005399 /* For a range we allow invalid values and return an empty
5400 * list. A list index out of range is an error. */
5401 if (!range)
5402 {
5403 if (verbose)
5404 EMSGN(_(e_listidx), n1);
5405 return FAIL;
5406 }
5407 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 }
5409 if (range)
5410 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 list_T *l;
5412 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413
5414 if (n2 < 0)
5415 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005416 else if (n2 >= len)
5417 n2 = len - 1;
5418 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005419 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 l = list_alloc();
5421 if (l == NULL)
5422 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005423 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 n1 <= n2; ++n1)
5425 {
5426 if (list_append_tv(l, &item->li_tv) == FAIL)
5427 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005428 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 return FAIL;
5430 }
5431 item = item->li_next;
5432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 clear_tv(rettv);
5434 rettv->v_type = VAR_LIST;
5435 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005436 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438 else
5439 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005440 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 clear_tv(rettv);
5442 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 }
5444 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445
5446 case VAR_DICT:
5447 if (range)
5448 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005449 if (verbose)
5450 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005451 if (len == -1)
5452 clear_tv(&var1);
5453 return FAIL;
5454 }
5455 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457
5458 if (len == -1)
5459 {
5460 key = get_tv_string(&var1);
5461 if (*key == NUL)
5462 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005463 if (verbose)
5464 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 clear_tv(&var1);
5466 return FAIL;
5467 }
5468 }
5469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005470 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005471
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005472 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005473 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474 if (len == -1)
5475 clear_tv(&var1);
5476 if (item == NULL)
5477 return FAIL;
5478
5479 copy_tv(&item->di_tv, &var1);
5480 clear_tv(rettv);
5481 *rettv = var1;
5482 }
5483 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485 }
5486
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 return OK;
5488}
5489
5490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 * Get an option value.
5492 * "arg" points to the '&' or '+' before the option name.
5493 * "arg" is advanced to character after the option name.
5494 * Return OK or FAIL.
5495 */
5496 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int evaluate;
5501{
5502 char_u *option_end;
5503 long numval;
5504 char_u *stringval;
5505 int opt_type;
5506 int c;
5507 int working = (**arg == '+'); /* has("+option") */
5508 int ret = OK;
5509 int opt_flags;
5510
5511 /*
5512 * Isolate the option name and find its value.
5513 */
5514 option_end = find_option_end(arg, &opt_flags);
5515 if (option_end == NULL)
5516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 EMSG2(_("E112: Option name missing: %s"), *arg);
5519 return FAIL;
5520 }
5521
5522 if (!evaluate)
5523 {
5524 *arg = option_end;
5525 return OK;
5526 }
5527
5528 c = *option_end;
5529 *option_end = NUL;
5530 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 if (opt_type == -3) /* invalid name */
5534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 EMSG2(_("E113: Unknown option: %s"), *arg);
5537 ret = FAIL;
5538 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
5541 if (opt_type == -2) /* hidden string option */
5542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 rettv->v_type = VAR_STRING;
5544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 else if (opt_type == -1) /* hidden number option */
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv->v_type = VAR_NUMBER;
5549 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551 else if (opt_type == 1) /* number option */
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 rettv->v_type = VAR_NUMBER;
5554 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 else /* string option */
5557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 rettv->v_type = VAR_STRING;
5559 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 }
5562 else if (working && (opt_type == -2 || opt_type == -1))
5563 ret = FAIL;
5564
5565 *option_end = c; /* put back for error messages */
5566 *arg = option_end;
5567
5568 return ret;
5569}
5570
5571/*
5572 * Allocate a variable for a string constant.
5573 * Return OK or FAIL.
5574 */
5575 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 int evaluate;
5580{
5581 char_u *p;
5582 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 int extra = 0;
5584
5585 /*
5586 * Find the end of the string, skipping backslashed characters.
5587 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005588 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
5590 if (*p == '\\' && p[1] != NUL)
5591 {
5592 ++p;
5593 /* A "\<x>" form occupies at least 4 characters, and produces up
5594 * to 6 characters: reserve space for 2 extra */
5595 if (*p == '<')
5596 extra += 2;
5597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599
5600 if (*p != '"')
5601 {
5602 EMSG2(_("E114: Missing quote: %s"), *arg);
5603 return FAIL;
5604 }
5605
5606 /* If only parsing, set *arg and return here */
5607 if (!evaluate)
5608 {
5609 *arg = p + 1;
5610 return OK;
5611 }
5612
5613 /*
5614 * Copy the string into allocated memory, handling backslashed
5615 * characters.
5616 */
5617 name = alloc((unsigned)(p - *arg + extra));
5618 if (name == NULL)
5619 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 rettv->v_type = VAR_STRING;
5621 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
5625 if (*p == '\\')
5626 {
5627 switch (*++p)
5628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 case 'b': *name++ = BS; ++p; break;
5630 case 'e': *name++ = ESC; ++p; break;
5631 case 'f': *name++ = FF; ++p; break;
5632 case 'n': *name++ = NL; ++p; break;
5633 case 'r': *name++ = CAR; ++p; break;
5634 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636 case 'X': /* hex: "\x1", "\x12" */
5637 case 'x':
5638 case 'u': /* Unicode: "\u0023" */
5639 case 'U':
5640 if (vim_isxdigit(p[1]))
5641 {
5642 int n, nr;
5643 int c = toupper(*p);
5644
5645 if (c == 'X')
5646 n = 2;
5647 else
5648 n = 4;
5649 nr = 0;
5650 while (--n >= 0 && vim_isxdigit(p[1]))
5651 {
5652 ++p;
5653 nr = (nr << 4) + hex2nr(*p);
5654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656#ifdef FEAT_MBYTE
5657 /* For "\u" store the number according to
5658 * 'encoding'. */
5659 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
5662#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 break;
5666
5667 /* octal: "\1", "\12", "\123" */
5668 case '0':
5669 case '1':
5670 case '2':
5671 case '3':
5672 case '4':
5673 case '5':
5674 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 case '7': *name = *p++ - '0';
5676 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 *name = (*name << 3) + *p++ - '0';
5679 if (*p >= '0' && *p <= '7')
5680 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 break;
5684
5685 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 if (extra != 0)
5688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691 }
5692 /* FALLTHROUGH */
5693
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 break;
5696 }
5697 }
5698 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 *arg = p + 1;
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 return OK;
5706}
5707
5708/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 * Return OK or FAIL.
5711 */
5712 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 int evaluate;
5717{
5718 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 int reduce = 0;
5721
5722 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 */
5725 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730 break;
5731 ++reduce;
5732 ++p;
5733 }
5734 }
5735
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 return FAIL;
5740 }
5741
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 if (!evaluate)
5744 {
5745 *arg = p + 1;
5746 return OK;
5747 }
5748
5749 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 */
5752 str = alloc((unsigned)((p - *arg) - reduce));
5753 if (str == NULL)
5754 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 rettv->v_type = VAR_STRING;
5756 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 break;
5764 ++p;
5765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005769 *arg = p + 1;
5770
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 return OK;
5772}
5773
5774/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 * Allocate a variable for a List and fill it from "*arg".
5776 * Return OK or FAIL.
5777 */
5778 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005779get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 int evaluate;
5783{
Bram Moolenaar33570922005-01-25 22:26:29 +00005784 list_T *l = NULL;
5785 typval_T tv;
5786 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787
5788 if (evaluate)
5789 {
5790 l = list_alloc();
5791 if (l == NULL)
5792 return FAIL;
5793 }
5794
5795 *arg = skipwhite(*arg + 1);
5796 while (**arg != ']' && **arg != NUL)
5797 {
5798 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5799 goto failret;
5800 if (evaluate)
5801 {
5802 item = listitem_alloc();
5803 if (item != NULL)
5804 {
5805 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005806 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 list_append(l, item);
5808 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005809 else
5810 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 }
5812
5813 if (**arg == ']')
5814 break;
5815 if (**arg != ',')
5816 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 goto failret;
5819 }
5820 *arg = skipwhite(*arg + 1);
5821 }
5822
5823 if (**arg != ']')
5824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826failret:
5827 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005828 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 return FAIL;
5830 }
5831
5832 *arg = skipwhite(*arg + 1);
5833 if (evaluate)
5834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005835 rettv->v_type = VAR_LIST;
5836 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 ++l->lv_refcount;
5838 }
5839
5840 return OK;
5841}
5842
5843/*
5844 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005845 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005847 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848list_alloc()
5849{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005850 list_T *l;
5851
5852 l = (list_T *)alloc_clear(sizeof(list_T));
5853 if (l != NULL)
5854 {
5855 /* Prepend the list to the list of lists for garbage collection. */
5856 if (first_list != NULL)
5857 first_list->lv_used_prev = l;
5858 l->lv_used_prev = NULL;
5859 l->lv_used_next = first_list;
5860 first_list = l;
5861 }
5862 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863}
5864
5865/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005866 * Allocate an empty list for a return value.
5867 * Returns OK or FAIL.
5868 */
5869 static int
5870rettv_list_alloc(rettv)
5871 typval_T *rettv;
5872{
5873 list_T *l = list_alloc();
5874
5875 if (l == NULL)
5876 return FAIL;
5877
5878 rettv->vval.v_list = l;
5879 rettv->v_type = VAR_LIST;
5880 ++l->lv_refcount;
5881 return OK;
5882}
5883
5884/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 * Unreference a list: decrement the reference count and free it when it
5886 * becomes zero.
5887 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005888 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005892 if (l != NULL && --l->lv_refcount <= 0)
5893 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894}
5895
5896/*
5897 * Free a list, including all items it points to.
5898 * Ignores the reference count.
5899 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005900 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005901list_free(l, recurse)
5902 list_T *l;
5903 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904{
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005907 /* Remove the list from the list of lists for garbage collection. */
5908 if (l->lv_used_prev == NULL)
5909 first_list = l->lv_used_next;
5910 else
5911 l->lv_used_prev->lv_used_next = l->lv_used_next;
5912 if (l->lv_used_next != NULL)
5913 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5914
Bram Moolenaard9fba312005-06-26 22:34:35 +00005915 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005917 /* Remove the item before deleting it. */
5918 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005919 if (recurse || (item->li_tv.v_type != VAR_LIST
5920 && item->li_tv.v_type != VAR_DICT))
5921 clear_tv(&item->li_tv);
5922 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 }
5924 vim_free(l);
5925}
5926
5927/*
5928 * Allocate a list item.
5929 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931listitem_alloc()
5932{
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934}
5935
5936/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005937 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 */
5939 static void
5940listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005943 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 vim_free(item);
5945}
5946
5947/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005948 * Remove a list item from a List and free it. Also clears the value.
5949 */
5950 static void
5951listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 list_T *l;
5953 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005954{
5955 list_remove(l, item, item);
5956 listitem_free(item);
5957}
5958
5959/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 * Get the number of items in a list.
5961 */
5962 static long
5963list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 if (l == NULL)
5967 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005968 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969}
5970
5971/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972 * Return TRUE when two lists have exactly the same values.
5973 */
5974 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005975list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l1;
5977 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005979 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005980{
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005983 if (l1 == NULL || l2 == NULL)
5984 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005985 if (l1 == l2)
5986 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005987 if (list_len(l1) != list_len(l2))
5988 return FALSE;
5989
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990 for (item1 = l1->lv_first, item2 = l2->lv_first;
5991 item1 != NULL && item2 != NULL;
5992 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005993 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 return FALSE;
5995 return item1 == NULL && item2 == NULL;
5996}
5997
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005998#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5999 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006000/*
6001 * Return the dictitem that an entry in a hashtable points to.
6002 */
6003 dictitem_T *
6004dict_lookup(hi)
6005 hashitem_T *hi;
6006{
6007 return HI2DI(hi);
6008}
6009#endif
6010
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006012 * Return TRUE when two dictionaries have exactly the same key/values.
6013 */
6014 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 dict_T *d1;
6017 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006019 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006020{
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 hashitem_T *hi;
6022 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006023 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006024
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006025 if (d1 == NULL || d2 == NULL)
6026 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006027 if (d1 == d2)
6028 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 if (dict_len(d1) != dict_len(d2))
6030 return FALSE;
6031
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006032 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006035 if (!HASHITEM_EMPTY(hi))
6036 {
6037 item2 = dict_find(d2, hi->hi_key, -1);
6038 if (item2 == NULL)
6039 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006041 return FALSE;
6042 --todo;
6043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044 }
6045 return TRUE;
6046}
6047
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006048static int tv_equal_recurse_limit;
6049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006050/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006051 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006052 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006053 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054 */
6055 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 typval_T *tv1;
6058 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006059 int ic; /* ignore case */
6060 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061{
6062 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006063 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006065 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006066
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006067 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006068 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006069
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006070 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 * recursiveness to a limit. We guess they are equal then.
6072 * A fixed limit has the problem of still taking an awful long time.
6073 * Reduce the limit every time running into it. That should work fine for
6074 * deeply linked structures that are not recursively linked and catch
6075 * recursiveness quickly. */
6076 if (!recursive)
6077 tv_equal_recurse_limit = 1000;
6078 if (recursive_cnt >= tv_equal_recurse_limit)
6079 {
6080 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006081 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083
6084 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006086 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087 ++recursive_cnt;
6088 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6089 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006090 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006091
6092 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093 ++recursive_cnt;
6094 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6095 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006096 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006097
6098 case VAR_FUNC:
6099 return (tv1->vval.v_string != NULL
6100 && tv2->vval.v_string != NULL
6101 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6102
6103 case VAR_NUMBER:
6104 return tv1->vval.v_number == tv2->vval.v_number;
6105
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006106#ifdef FEAT_FLOAT
6107 case VAR_FLOAT:
6108 return tv1->vval.v_float == tv2->vval.v_float;
6109#endif
6110
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111 case VAR_STRING:
6112 s1 = get_tv_string_buf(tv1, buf1);
6113 s2 = get_tv_string_buf(tv2, buf2);
6114 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006116
6117 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 return TRUE;
6119}
6120
6121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 * Locate item with index "n" in list "l" and return it.
6123 * A negative index is counted from the end; -1 is the last item.
6124 * Returns NULL when "n" is out of range.
6125 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 long n;
6130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006132 long idx;
6133
6134 if (l == NULL)
6135 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006136
6137 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006139 n = l->lv_len + n;
6140
6141 /* Check for index out of range. */
6142 if (n < 0 || n >= l->lv_len)
6143 return NULL;
6144
6145 /* When there is a cached index may start search from there. */
6146 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006148 if (n < l->lv_idx / 2)
6149 {
6150 /* closest to the start of the list */
6151 item = l->lv_first;
6152 idx = 0;
6153 }
6154 else if (n > (l->lv_idx + l->lv_len) / 2)
6155 {
6156 /* closest to the end of the list */
6157 item = l->lv_last;
6158 idx = l->lv_len - 1;
6159 }
6160 else
6161 {
6162 /* closest to the cached index */
6163 item = l->lv_idx_item;
6164 idx = l->lv_idx;
6165 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 }
6167 else
6168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006169 if (n < l->lv_len / 2)
6170 {
6171 /* closest to the start of the list */
6172 item = l->lv_first;
6173 idx = 0;
6174 }
6175 else
6176 {
6177 /* closest to the end of the list */
6178 item = l->lv_last;
6179 idx = l->lv_len - 1;
6180 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182
6183 while (n > idx)
6184 {
6185 /* search forward */
6186 item = item->li_next;
6187 ++idx;
6188 }
6189 while (n < idx)
6190 {
6191 /* search backward */
6192 item = item->li_prev;
6193 --idx;
6194 }
6195
6196 /* cache the used index */
6197 l->lv_idx = idx;
6198 l->lv_idx_item = item;
6199
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 return item;
6201}
6202
6203/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006204 * Get list item "l[idx]" as a number.
6205 */
6206 static long
6207list_find_nr(l, idx, errorp)
6208 list_T *l;
6209 long idx;
6210 int *errorp; /* set to TRUE when something wrong */
6211{
6212 listitem_T *li;
6213
6214 li = list_find(l, idx);
6215 if (li == NULL)
6216 {
6217 if (errorp != NULL)
6218 *errorp = TRUE;
6219 return -1L;
6220 }
6221 return get_tv_number_chk(&li->li_tv, errorp);
6222}
6223
6224/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006225 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6226 */
6227 char_u *
6228list_find_str(l, idx)
6229 list_T *l;
6230 long idx;
6231{
6232 listitem_T *li;
6233
6234 li = list_find(l, idx - 1);
6235 if (li == NULL)
6236 {
6237 EMSGN(_(e_listidx), idx);
6238 return NULL;
6239 }
6240 return get_tv_string(&li->li_tv);
6241}
6242
6243/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006244 * Locate "item" list "l" and return its index.
6245 * Returns -1 when "item" is not in the list.
6246 */
6247 static long
6248list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 list_T *l;
6250 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006251{
6252 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006254
6255 if (l == NULL)
6256 return -1;
6257 idx = 0;
6258 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6259 ++idx;
6260 if (li == NULL)
6261 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006262 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006263}
6264
6265/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 * Append item "item" to the end of list "l".
6267 */
6268 static void
6269list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 list_T *l;
6271 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272{
6273 if (l->lv_last == NULL)
6274 {
6275 /* empty list */
6276 l->lv_first = item;
6277 l->lv_last = item;
6278 item->li_prev = NULL;
6279 }
6280 else
6281 {
6282 l->lv_last->li_next = item;
6283 item->li_prev = l->lv_last;
6284 l->lv_last = item;
6285 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006286 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 item->li_next = NULL;
6288}
6289
6290/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006292 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006294 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 list_T *l;
6297 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006299 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300
Bram Moolenaar05159a02005-02-26 23:04:13 +00006301 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006303 copy_tv(tv, &li->li_tv);
6304 list_append(l, li);
6305 return OK;
6306}
6307
6308/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006309 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006310 * Return FAIL when out of memory.
6311 */
6312 int
6313list_append_dict(list, dict)
6314 list_T *list;
6315 dict_T *dict;
6316{
6317 listitem_T *li = listitem_alloc();
6318
6319 if (li == NULL)
6320 return FAIL;
6321 li->li_tv.v_type = VAR_DICT;
6322 li->li_tv.v_lock = 0;
6323 li->li_tv.vval.v_dict = dict;
6324 list_append(list, li);
6325 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return OK;
6327}
6328
6329/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006330 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006331 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006332 * Returns FAIL when out of memory.
6333 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006334 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006335list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006336 list_T *l;
6337 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006338 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006339{
6340 listitem_T *li = listitem_alloc();
6341
6342 if (li == NULL)
6343 return FAIL;
6344 list_append(l, li);
6345 li->li_tv.v_type = VAR_STRING;
6346 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006347 if (str == NULL)
6348 li->li_tv.vval.v_string = NULL;
6349 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006350 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 return FAIL;
6352 return OK;
6353}
6354
6355/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006356 * Append "n" to list "l".
6357 * Returns FAIL when out of memory.
6358 */
6359 static int
6360list_append_number(l, n)
6361 list_T *l;
6362 varnumber_T n;
6363{
6364 listitem_T *li;
6365
6366 li = listitem_alloc();
6367 if (li == NULL)
6368 return FAIL;
6369 li->li_tv.v_type = VAR_NUMBER;
6370 li->li_tv.v_lock = 0;
6371 li->li_tv.vval.v_number = n;
6372 list_append(l, li);
6373 return OK;
6374}
6375
6376/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006378 * If "item" is NULL append at the end.
6379 * Return FAIL when out of memory.
6380 */
6381 static int
6382list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 list_T *l;
6384 typval_T *tv;
6385 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006386{
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006388
6389 if (ni == NULL)
6390 return FAIL;
6391 copy_tv(tv, &ni->li_tv);
6392 if (item == NULL)
6393 /* Append new item at end of list. */
6394 list_append(l, ni);
6395 else
6396 {
6397 /* Insert new item before existing item. */
6398 ni->li_prev = item->li_prev;
6399 ni->li_next = item;
6400 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403 ++l->lv_idx;
6404 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006408 l->lv_idx_item = NULL;
6409 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006411 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 }
6413 return OK;
6414}
6415
6416/*
6417 * Extend "l1" with "l2".
6418 * If "bef" is NULL append at the end, otherwise insert before this item.
6419 * Returns FAIL when out of memory.
6420 */
6421 static int
6422list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 list_T *l1;
6424 list_T *l2;
6425 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426{
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006428 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006430 /* We also quit the loop when we have inserted the original item count of
6431 * the list, avoid a hang when we extend a list with itself. */
6432 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6434 return FAIL;
6435 return OK;
6436}
6437
6438/*
6439 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6440 * Return FAIL when out of memory.
6441 */
6442 static int
6443list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 list_T *l1;
6445 list_T *l2;
6446 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447{
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006450 if (l1 == NULL || l2 == NULL)
6451 return FAIL;
6452
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006454 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 if (l == NULL)
6456 return FAIL;
6457 tv->v_type = VAR_LIST;
6458 tv->vval.v_list = l;
6459
6460 /* append all items from the second list */
6461 return list_extend(l, l2, NULL);
6462}
6463
6464/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 * Returns NULL when out of memory.
6469 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475{
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 list_T *copy;
6477 listitem_T *item;
6478 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479
6480 if (orig == NULL)
6481 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482
6483 copy = list_alloc();
6484 if (copy != NULL)
6485 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (copyID != 0)
6487 {
6488 /* Do this before adding the items, because one of the items may
6489 * refer back to this list. */
6490 orig->lv_copyID = copyID;
6491 orig->lv_copylist = copy;
6492 }
6493 for (item = orig->lv_first; item != NULL && !got_int;
6494 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 {
6496 ni = listitem_alloc();
6497 if (ni == NULL)
6498 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 {
6501 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6502 {
6503 vim_free(ni);
6504 break;
6505 }
6506 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 list_append(copy, ni);
6510 }
6511 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 if (item != NULL)
6513 {
6514 list_unref(copy);
6515 copy = NULL;
6516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 }
6518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 return copy;
6520}
6521
6522/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006524 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006527list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 list_T *l;
6529 listitem_T *item;
6530 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531{
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 /* notify watchers */
6535 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 list_fix_watch(l, ip);
6539 if (ip == item2)
6540 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542
6543 if (item2->li_next == NULL)
6544 l->lv_last = item->li_prev;
6545 else
6546 item2->li_next->li_prev = item->li_prev;
6547 if (item->li_prev == NULL)
6548 l->lv_first = item2->li_next;
6549 else
6550 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006551 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552}
6553
6554/*
6555 * Return an allocated string with the string representation of a list.
6556 * May return NULL.
6557 */
6558 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006559list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006561 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562{
6563 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564
6565 if (tv->vval.v_list == NULL)
6566 return NULL;
6567 ga_init2(&ga, (int)sizeof(char), 80);
6568 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006569 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006570 {
6571 vim_free(ga.ga_data);
6572 return NULL;
6573 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 ga_append(&ga, ']');
6575 ga_append(&ga, NUL);
6576 return (char_u *)ga.ga_data;
6577}
6578
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006579typedef struct join_S {
6580 char_u *s;
6581 char_u *tofree;
6582} join_T;
6583
6584 static int
6585list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6586 garray_T *gap; /* to store the result in */
6587 list_T *l;
6588 char_u *sep;
6589 int echo_style;
6590 int copyID;
6591 garray_T *join_gap; /* to keep each list item string */
6592{
6593 int i;
6594 join_T *p;
6595 int len;
6596 int sumlen = 0;
6597 int first = TRUE;
6598 char_u *tofree;
6599 char_u numbuf[NUMBUFLEN];
6600 listitem_T *item;
6601 char_u *s;
6602
6603 /* Stringify each item in the list. */
6604 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6605 {
6606 if (echo_style)
6607 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6608 else
6609 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6610 if (s == NULL)
6611 return FAIL;
6612
6613 len = (int)STRLEN(s);
6614 sumlen += len;
6615
6616 ga_grow(join_gap, 1);
6617 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6618 if (tofree != NULL || s != numbuf)
6619 {
6620 p->s = s;
6621 p->tofree = tofree;
6622 }
6623 else
6624 {
6625 p->s = vim_strnsave(s, len);
6626 p->tofree = p->s;
6627 }
6628
6629 line_breakcheck();
6630 }
6631
6632 /* Allocate result buffer with its total size, avoid re-allocation and
6633 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6634 if (join_gap->ga_len >= 2)
6635 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6636 if (ga_grow(gap, sumlen + 2) == FAIL)
6637 return FAIL;
6638
6639 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6640 {
6641 if (first)
6642 first = FALSE;
6643 else
6644 ga_concat(gap, sep);
6645 p = ((join_T *)join_gap->ga_data) + i;
6646
6647 if (p->s != NULL)
6648 ga_concat(gap, p->s);
6649 line_breakcheck();
6650 }
6651
6652 return OK;
6653}
6654
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006656 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006657 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006658 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006659 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006660 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006661list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006662 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006664 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006665 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006666 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006667{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006668 garray_T join_ga;
6669 int retval;
6670 join_T *p;
6671 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006673 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6674 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6675
6676 /* Dispose each item in join_ga. */
6677 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006679 p = (join_T *)join_ga.ga_data;
6680 for (i = 0; i < join_ga.ga_len; ++i)
6681 {
6682 vim_free(p->tofree);
6683 ++p;
6684 }
6685 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687
6688 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689}
6690
6691/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006692 * Garbage collection for lists and dictionaries.
6693 *
6694 * We use reference counts to be able to free most items right away when they
6695 * are no longer used. But for composite items it's possible that it becomes
6696 * unused while the reference count is > 0: When there is a recursive
6697 * reference. Example:
6698 * :let l = [1, 2, 3]
6699 * :let d = {9: l}
6700 * :let l[1] = d
6701 *
6702 * Since this is quite unusual we handle this with garbage collection: every
6703 * once in a while find out which lists and dicts are not referenced from any
6704 * variable.
6705 *
6706 * Here is a good reference text about garbage collection (refers to Python
6707 * but it applies to all reference-counting mechanisms):
6708 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006709 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006710
6711/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 * Do garbage collection for lists and dicts.
6713 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006714 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006715 int
6716garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006717{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006718 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006719 buf_T *buf;
6720 win_T *wp;
6721 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006722 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006723 int did_free;
6724 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006725#ifdef FEAT_WINDOWS
6726 tabpage_T *tp;
6727#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006729 /* Only do this once. */
6730 want_garbage_collect = FALSE;
6731 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006732 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006733
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006734 /* We advance by two because we add one for items referenced through
6735 * previous_funccal. */
6736 current_copyID += COPYID_INC;
6737 copyID = current_copyID;
6738
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 /*
6740 * 1. Go through all accessible variables and mark all lists and dicts
6741 * with copyID.
6742 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006743
6744 /* Don't free variables in the previous_funccal list unless they are only
6745 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006746 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6748 {
6749 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6750 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6751 }
6752
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753 /* script-local variables */
6754 for (i = 1; i <= ga_scripts.ga_len; ++i)
6755 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6756
6757 /* buffer-local variables */
6758 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6759 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6760
6761 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006762 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6764
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006765#ifdef FEAT_WINDOWS
6766 /* tabpage-local variables */
6767 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6768 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6769#endif
6770
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 /* global variables */
6772 set_ref_in_ht(&globvarht, copyID);
6773
6774 /* function-local variables */
6775 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6776 {
6777 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6778 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6779 }
6780
Bram Moolenaard812df62008-11-09 12:46:09 +00006781 /* v: vars */
6782 set_ref_in_ht(&vimvarht, copyID);
6783
Bram Moolenaar1dced572012-04-05 16:54:08 +02006784#ifdef FEAT_LUA
6785 set_ref_in_lua(copyID);
6786#endif
6787
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006788 /*
6789 * 2. Free lists and dictionaries that are not referenced.
6790 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006791 did_free = free_unref_items(copyID);
6792
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006793 /*
6794 * 3. Check if any funccal can be freed now.
6795 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006796 for (pfc = &previous_funccal; *pfc != NULL; )
6797 {
6798 if (can_free_funccal(*pfc, copyID))
6799 {
6800 fc = *pfc;
6801 *pfc = fc->caller;
6802 free_funccal(fc, TRUE);
6803 did_free = TRUE;
6804 did_free_funccal = TRUE;
6805 }
6806 else
6807 pfc = &(*pfc)->caller;
6808 }
6809 if (did_free_funccal)
6810 /* When a funccal was freed some more items might be garbage
6811 * collected, so run again. */
6812 (void)garbage_collect();
6813
6814 return did_free;
6815}
6816
6817/*
6818 * Free lists and dictionaries that are no longer referenced.
6819 */
6820 static int
6821free_unref_items(copyID)
6822 int copyID;
6823{
6824 dict_T *dd;
6825 list_T *ll;
6826 int did_free = FALSE;
6827
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006829 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 */
6831 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006834 /* Free the Dictionary and ordinary items it contains, but don't
6835 * recurse into Lists and Dictionaries, they will be in the list
6836 * of dicts or list of lists. */
6837 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 did_free = TRUE;
6839
6840 /* restart, next dict may also have been freed */
6841 dd = first_dict;
6842 }
6843 else
6844 dd = dd->dv_used_next;
6845
6846 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 * Go through the list of lists and free items without the copyID.
6848 * But don't free a list that has a watcher (used in a for loop), these
6849 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 */
6851 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6853 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006855 /* Free the List and ordinary items it contains, but don't recurse
6856 * into Lists and Dictionaries, they will be in the list of dicts
6857 * or list of lists. */
6858 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 did_free = TRUE;
6860
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006861 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 ll = first_list;
6863 }
6864 else
6865 ll = ll->lv_used_next;
6866
6867 return did_free;
6868}
6869
6870/*
6871 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6872 */
6873 static void
6874set_ref_in_ht(ht, copyID)
6875 hashtab_T *ht;
6876 int copyID;
6877{
6878 int todo;
6879 hashitem_T *hi;
6880
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006881 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 for (hi = ht->ht_array; todo > 0; ++hi)
6883 if (!HASHITEM_EMPTY(hi))
6884 {
6885 --todo;
6886 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6887 }
6888}
6889
6890/*
6891 * Mark all lists and dicts referenced through list "l" with "copyID".
6892 */
6893 static void
6894set_ref_in_list(l, copyID)
6895 list_T *l;
6896 int copyID;
6897{
6898 listitem_T *li;
6899
6900 for (li = l->lv_first; li != NULL; li = li->li_next)
6901 set_ref_in_item(&li->li_tv, copyID);
6902}
6903
6904/*
6905 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6906 */
6907 static void
6908set_ref_in_item(tv, copyID)
6909 typval_T *tv;
6910 int copyID;
6911{
6912 dict_T *dd;
6913 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006914
6915 switch (tv->v_type)
6916 {
6917 case VAR_DICT:
6918 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006919 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006920 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 /* Didn't see this dict yet. */
6922 dd->dv_copyID = copyID;
6923 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006926
6927 case VAR_LIST:
6928 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006929 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006930 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 /* Didn't see this list yet. */
6932 ll->lv_copyID = copyID;
6933 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006934 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006936 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006938}
6939
6940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006941 * Allocate an empty header for a dictionary.
6942 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006943 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944dict_alloc()
6945{
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006947
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006950 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006951 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 if (first_dict != NULL)
6953 first_dict->dv_used_prev = d;
6954 d->dv_used_next = first_dict;
6955 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006956 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006959 d->dv_lock = 0;
6960 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006961 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006962 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964}
6965
6966/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006967 * Allocate an empty dict for a return value.
6968 * Returns OK or FAIL.
6969 */
6970 static int
6971rettv_dict_alloc(rettv)
6972 typval_T *rettv;
6973{
6974 dict_T *d = dict_alloc();
6975
6976 if (d == NULL)
6977 return FAIL;
6978
6979 rettv->vval.v_dict = d;
6980 rettv->v_type = VAR_DICT;
6981 ++d->dv_refcount;
6982 return OK;
6983}
6984
6985
6986/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006987 * Unreference a Dictionary: decrement the reference count and free it when it
6988 * becomes zero.
6989 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006990 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006991dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006992 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006993{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006994 if (d != NULL && --d->dv_refcount <= 0)
6995 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996}
6997
6998/*
6999 * Free a Dictionary, including all items it contains.
7000 * Ignores the reference count.
7001 */
7002 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007003dict_free(d, recurse)
7004 dict_T *d;
7005 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007007 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007009 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 /* Remove the dict from the list of dicts for garbage collection. */
7012 if (d->dv_used_prev == NULL)
7013 first_dict = d->dv_used_next;
7014 else
7015 d->dv_used_prev->dv_used_next = d->dv_used_next;
7016 if (d->dv_used_next != NULL)
7017 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7018
7019 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007020 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007021 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007024 if (!HASHITEM_EMPTY(hi))
7025 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007026 /* Remove the item before deleting it, just in case there is
7027 * something recursive causing trouble. */
7028 di = HI2DI(hi);
7029 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007030 if (recurse || (di->di_tv.v_type != VAR_LIST
7031 && di->di_tv.v_type != VAR_DICT))
7032 clear_tv(&di->di_tv);
7033 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007034 --todo;
7035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038 vim_free(d);
7039}
7040
7041/*
7042 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007043 * The "key" is copied to the new item.
7044 * Note that the value of the item "di_tv" still needs to be initialized!
7045 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007046 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007047 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048dictitem_alloc(key)
7049 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050{
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007053 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007057 di->di_flags = 0;
7058 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060}
7061
7062/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007063 * Make a copy of a Dictionary item.
7064 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007065 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007066dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007068{
Bram Moolenaar33570922005-01-25 22:26:29 +00007069 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007070
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007071 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7072 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 if (di != NULL)
7074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007076 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007077 copy_tv(&org->di_tv, &di->di_tv);
7078 }
7079 return di;
7080}
7081
7082/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 * Remove item "item" from Dictionary "dict" and free it.
7084 */
7085 static void
7086dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007087 dict_T *dict;
7088 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089{
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 if (HASHITEM_EMPTY(hi))
7094 EMSG2(_(e_intern2), "dictitem_remove()");
7095 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097 dictitem_free(item);
7098}
7099
7100/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 * Free a dict item. Also clears the value.
7102 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007103 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 clear_tv(&item->di_tv);
7108 vim_free(item);
7109}
7110
7111/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7113 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007114 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115 * Returns NULL when out of memory.
7116 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007118dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007120 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007121 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007122{
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 dict_T *copy;
7124 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007127
7128 if (orig == NULL)
7129 return NULL;
7130
7131 copy = dict_alloc();
7132 if (copy != NULL)
7133 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007134 if (copyID != 0)
7135 {
7136 orig->dv_copyID = copyID;
7137 orig->dv_copydict = copy;
7138 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007139 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007140 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007142 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007144 --todo;
7145
7146 di = dictitem_alloc(hi->hi_key);
7147 if (di == NULL)
7148 break;
7149 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007150 {
7151 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7152 copyID) == FAIL)
7153 {
7154 vim_free(di);
7155 break;
7156 }
7157 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007158 else
7159 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7160 if (dict_add(copy, di) == FAIL)
7161 {
7162 dictitem_free(di);
7163 break;
7164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007165 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167
Bram Moolenaare9a41262005-01-15 22:18:47 +00007168 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 if (todo > 0)
7170 {
7171 dict_unref(copy);
7172 copy = NULL;
7173 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007174 }
7175
7176 return copy;
7177}
7178
7179/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007181 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007183 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 dict_T *d;
7186 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187{
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189}
7190
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007192 * Add a number or string entry to dictionary "d".
7193 * When "str" is NULL use number "nr", otherwise use "str".
7194 * Returns FAIL when out of memory and when key already exists.
7195 */
7196 int
7197dict_add_nr_str(d, key, nr, str)
7198 dict_T *d;
7199 char *key;
7200 long nr;
7201 char_u *str;
7202{
7203 dictitem_T *item;
7204
7205 item = dictitem_alloc((char_u *)key);
7206 if (item == NULL)
7207 return FAIL;
7208 item->di_tv.v_lock = 0;
7209 if (str == NULL)
7210 {
7211 item->di_tv.v_type = VAR_NUMBER;
7212 item->di_tv.vval.v_number = nr;
7213 }
7214 else
7215 {
7216 item->di_tv.v_type = VAR_STRING;
7217 item->di_tv.vval.v_string = vim_strsave(str);
7218 }
7219 if (dict_add(d, item) == FAIL)
7220 {
7221 dictitem_free(item);
7222 return FAIL;
7223 }
7224 return OK;
7225}
7226
7227/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007228 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007229 * Returns FAIL when out of memory and when key already exists.
7230 */
7231 int
7232dict_add_list(d, key, list)
7233 dict_T *d;
7234 char *key;
7235 list_T *list;
7236{
7237 dictitem_T *item;
7238
7239 item = dictitem_alloc((char_u *)key);
7240 if (item == NULL)
7241 return FAIL;
7242 item->di_tv.v_lock = 0;
7243 item->di_tv.v_type = VAR_LIST;
7244 item->di_tv.vval.v_list = list;
7245 if (dict_add(d, item) == FAIL)
7246 {
7247 dictitem_free(item);
7248 return FAIL;
7249 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007250 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007251 return OK;
7252}
7253
7254/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007255 * Get the number of items in a Dictionary.
7256 */
7257 static long
7258dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 if (d == NULL)
7262 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007263 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007264}
7265
7266/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 * Find item "key[len]" in Dictionary "d".
7268 * If "len" is negative use strlen(key).
7269 * Returns NULL when not found.
7270 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007271 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 char_u *key;
7275 int len;
7276{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007277#define AKEYLEN 200
7278 char_u buf[AKEYLEN];
7279 char_u *akey;
7280 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 if (len < 0)
7284 akey = key;
7285 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 tofree = akey = vim_strnsave(key, len);
7288 if (akey == NULL)
7289 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 else
7292 {
7293 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007294 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 akey = buf;
7296 }
7297
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 vim_free(tofree);
7300 if (HASHITEM_EMPTY(hi))
7301 return NULL;
7302 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303}
7304
7305/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007306 * Get a string item from a dictionary.
7307 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007308 * Returns NULL if the entry doesn't exist or out of memory.
7309 */
7310 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007311get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007312 dict_T *d;
7313 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007314 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007315{
7316 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007317 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007318
7319 di = dict_find(d, key, -1);
7320 if (di == NULL)
7321 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007322 s = get_tv_string(&di->di_tv);
7323 if (save && s != NULL)
7324 s = vim_strsave(s);
7325 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007326}
7327
7328/*
7329 * Get a number item from a dictionary.
7330 * Returns 0 if the entry doesn't exist or out of memory.
7331 */
7332 long
7333get_dict_number(d, key)
7334 dict_T *d;
7335 char_u *key;
7336{
7337 dictitem_T *di;
7338
7339 di = dict_find(d, key, -1);
7340 if (di == NULL)
7341 return 0;
7342 return get_tv_number(&di->di_tv);
7343}
7344
7345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 * Return an allocated string with the string representation of a Dictionary.
7347 * May return NULL.
7348 */
7349 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007350dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007352 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353{
7354 garray_T ga;
7355 int first = TRUE;
7356 char_u *tofree;
7357 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 return NULL;
7365 ga_init2(&ga, (int)sizeof(char), 80);
7366 ga_append(&ga, '{');
7367
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007368 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 --todo;
7374
7375 if (first)
7376 first = FALSE;
7377 else
7378 ga_concat(&ga, (char_u *)", ");
7379
7380 tofree = string_quote(hi->hi_key, FALSE);
7381 if (tofree != NULL)
7382 {
7383 ga_concat(&ga, tofree);
7384 vim_free(tofree);
7385 }
7386 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 if (s != NULL)
7389 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (s == NULL)
7392 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 if (todo > 0)
7396 {
7397 vim_free(ga.ga_data);
7398 return NULL;
7399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400
7401 ga_append(&ga, '}');
7402 ga_append(&ga, NUL);
7403 return (char_u *)ga.ga_data;
7404}
7405
7406/*
7407 * Allocate a variable for a Dictionary and fill it from "*arg".
7408 * Return OK or FAIL. Returns NOTDONE for {expr}.
7409 */
7410 static int
7411get_dict_tv(arg, rettv, evaluate)
7412 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 int evaluate;
7415{
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 dict_T *d = NULL;
7417 typval_T tvkey;
7418 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007419 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007422 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423
7424 /*
7425 * First check if it's not a curly-braces thing: {expr}.
7426 * Must do this without evaluating, otherwise a function may be called
7427 * twice. Unfortunately this means we need to call eval1() twice for the
7428 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 if (*start != '}')
7432 {
7433 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7434 return FAIL;
7435 if (*start == '}')
7436 return NOTDONE;
7437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438
7439 if (evaluate)
7440 {
7441 d = dict_alloc();
7442 if (d == NULL)
7443 return FAIL;
7444 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445 tvkey.v_type = VAR_UNKNOWN;
7446 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447
7448 *arg = skipwhite(*arg + 1);
7449 while (**arg != '}' && **arg != NUL)
7450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007451 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 goto failret;
7453 if (**arg != ':')
7454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007455 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007456 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 goto failret;
7458 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007459 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007461 key = get_tv_string_buf_chk(&tvkey, buf);
7462 if (key == NULL || *key == NUL)
7463 {
7464 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7465 if (key != NULL)
7466 EMSG(_(e_emptykey));
7467 clear_tv(&tvkey);
7468 goto failret;
7469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471
7472 *arg = skipwhite(*arg + 1);
7473 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7474 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007475 if (evaluate)
7476 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 goto failret;
7478 }
7479 if (evaluate)
7480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 if (item != NULL)
7483 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007484 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 clear_tv(&tv);
7487 goto failret;
7488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489 item = dictitem_alloc(key);
7490 clear_tv(&tvkey);
7491 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007494 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 if (dict_add(d, item) == FAIL)
7496 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 }
7498 }
7499
7500 if (**arg == '}')
7501 break;
7502 if (**arg != ',')
7503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007504 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 goto failret;
7506 }
7507 *arg = skipwhite(*arg + 1);
7508 }
7509
7510 if (**arg != '}')
7511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007512 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513failret:
7514 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007515 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 return FAIL;
7517 }
7518
7519 *arg = skipwhite(*arg + 1);
7520 if (evaluate)
7521 {
7522 rettv->v_type = VAR_DICT;
7523 rettv->vval.v_dict = d;
7524 ++d->dv_refcount;
7525 }
7526
7527 return OK;
7528}
7529
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007531 * Return a string with the string representation of a variable.
7532 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007533 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007535 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007536 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007537 */
7538 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007539echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007541 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007542 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007543 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007545 static int recurse = 0;
7546 char_u *r = NULL;
7547
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007550 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007551 *tofree = NULL;
7552 return NULL;
7553 }
7554 ++recurse;
7555
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 switch (tv->v_type)
7557 {
7558 case VAR_FUNC:
7559 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560 r = tv->vval.v_string;
7561 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007564 if (tv->vval.v_list == NULL)
7565 {
7566 *tofree = NULL;
7567 r = NULL;
7568 }
7569 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7570 {
7571 *tofree = NULL;
7572 r = (char_u *)"[...]";
7573 }
7574 else
7575 {
7576 tv->vval.v_list->lv_copyID = copyID;
7577 *tofree = list2string(tv, copyID);
7578 r = *tofree;
7579 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007581
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583 if (tv->vval.v_dict == NULL)
7584 {
7585 *tofree = NULL;
7586 r = NULL;
7587 }
7588 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7589 {
7590 *tofree = NULL;
7591 r = (char_u *)"{...}";
7592 }
7593 else
7594 {
7595 tv->vval.v_dict->dv_copyID = copyID;
7596 *tofree = dict2string(tv, copyID);
7597 r = *tofree;
7598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007600
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 case VAR_STRING:
7602 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007603 *tofree = NULL;
7604 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007607#ifdef FEAT_FLOAT
7608 case VAR_FLOAT:
7609 *tofree = NULL;
7610 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7611 r = numbuf;
7612 break;
7613#endif
7614
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007615 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007616 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619
7620 --recurse;
7621 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007622}
7623
7624/*
7625 * Return a string with the string representation of a variable.
7626 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7627 * "numbuf" is used for a number.
7628 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007629 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007630 */
7631 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007634 char_u **tofree;
7635 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007636 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007637{
7638 switch (tv->v_type)
7639 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007640 case VAR_FUNC:
7641 *tofree = string_quote(tv->vval.v_string, TRUE);
7642 return *tofree;
7643 case VAR_STRING:
7644 *tofree = string_quote(tv->vval.v_string, FALSE);
7645 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007646#ifdef FEAT_FLOAT
7647 case VAR_FLOAT:
7648 *tofree = NULL;
7649 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7650 return numbuf;
7651#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007652 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007659 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660}
7661
7662/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 * Return string "str" in ' quotes, doubling ' characters.
7664 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666 */
7667 static char_u *
7668string_quote(str, function)
7669 char_u *str;
7670 int function;
7671{
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673 char_u *p, *r, *s;
7674
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 len = (function ? 13 : 3);
7676 if (str != NULL)
7677 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007678 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 for (p = str; *p != NUL; mb_ptr_adv(p))
7680 if (*p == '\'')
7681 ++len;
7682 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 s = r = alloc(len);
7684 if (r != NULL)
7685 {
7686 if (function)
7687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 r += 10;
7690 }
7691 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 if (str != NULL)
7694 for (p = str; *p != NUL; )
7695 {
7696 if (*p == '\'')
7697 *r++ = '\'';
7698 MB_COPY_CHAR(p, r);
7699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 if (function)
7702 *r++ = ')';
7703 *r++ = NUL;
7704 }
7705 return s;
7706}
7707
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007708#ifdef FEAT_FLOAT
7709/*
7710 * Convert the string "text" to a floating point number.
7711 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7712 * this always uses a decimal point.
7713 * Returns the length of the text that was consumed.
7714 */
7715 static int
7716string2float(text, value)
7717 char_u *text;
7718 float_T *value; /* result stored here */
7719{
7720 char *s = (char *)text;
7721 float_T f;
7722
7723 f = strtod(s, &s);
7724 *value = f;
7725 return (int)((char_u *)s - text);
7726}
7727#endif
7728
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 * Get the value of an environment variable.
7731 * "arg" is pointing to the '$'. It is advanced to after the name.
7732 * If the environment variable was not set, silently assume it is empty.
7733 * Always return OK.
7734 */
7735 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 int evaluate;
7740{
7741 char_u *string = NULL;
7742 int len;
7743 int cc;
7744 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007745 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
7747 ++*arg;
7748 name = *arg;
7749 len = get_env_len(arg);
7750 if (evaluate)
7751 {
7752 if (len != 0)
7753 {
7754 cc = name[len];
7755 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007756 /* first try vim_getenv(), fast for normal environment vars */
7757 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007759 {
7760 if (!mustfree)
7761 string = vim_strsave(string);
7762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 else
7764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007765 if (mustfree)
7766 vim_free(string);
7767
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 /* next try expanding things like $VIM and ${HOME} */
7769 string = expand_env_save(name - 1);
7770 if (string != NULL && *string == '$')
7771 {
7772 vim_free(string);
7773 string = NULL;
7774 }
7775 }
7776 name[len] = cc;
7777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 rettv->v_type = VAR_STRING;
7779 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781
7782 return OK;
7783}
7784
7785/*
7786 * Array with names and number of arguments of all internal functions
7787 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7788 */
7789static struct fst
7790{
7791 char *f_name; /* function name */
7792 char f_min_argc; /* minimal number of arguments */
7793 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007795 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796} functions[] =
7797{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007798#ifdef FEAT_FLOAT
7799 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007800 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007801#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007802 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007803 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {"append", 2, 2, f_append},
7805 {"argc", 0, 0, f_argc},
7806 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007807 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007808#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007809 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007810 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007811 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007814 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"bufexists", 1, 1, f_bufexists},
7816 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7817 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7818 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7819 {"buflisted", 1, 1, f_buflisted},
7820 {"bufloaded", 1, 1, f_bufloaded},
7821 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007822 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"bufwinnr", 1, 1, f_bufwinnr},
7824 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007825 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#ifdef FEAT_FLOAT
7828 {"ceil", 1, 1, f_ceil},
7829#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007830 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"char2nr", 1, 1, f_char2nr},
7832 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007835#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007836 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007837 {"complete_add", 1, 1, f_complete_add},
7838 {"complete_check", 0, 0, f_complete_check},
7839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#ifdef FEAT_FLOAT
7843 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007844 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007846 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007848 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007849 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"delete", 1, 1, f_delete},
7851 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007852 {"diff_filler", 1, 1, f_diff_filler},
7853 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007854 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007856 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"eventhandler", 0, 0, f_eventhandler},
7858 {"executable", 1, 1, f_executable},
7859 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007860#ifdef FEAT_FLOAT
7861 {"exp", 1, 1, f_exp},
7862#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007863 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007864 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007865 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7867 {"filereadable", 1, 1, f_filereadable},
7868 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007870 {"finddir", 1, 3, f_finddir},
7871 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
7873 {"float2nr", 1, 1, f_float2nr},
7874 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007875 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007877 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"fnamemodify", 2, 2, f_fnamemodify},
7879 {"foldclosed", 1, 1, f_foldclosed},
7880 {"foldclosedend", 1, 1, f_foldclosedend},
7881 {"foldlevel", 1, 1, f_foldlevel},
7882 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007883 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007886 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007887 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007888 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"getbufvar", 2, 2, f_getbufvar},
7890 {"getchar", 0, 1, f_getchar},
7891 {"getcharmod", 0, 0, f_getcharmod},
7892 {"getcmdline", 0, 0, f_getcmdline},
7893 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007894 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007896 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007897 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"getfsize", 1, 1, f_getfsize},
7899 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007900 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007901 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007902 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007903 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007904 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007905 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007906 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007907 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007909 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007910 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"getwinposx", 0, 0, f_getwinposx},
7912 {"getwinposy", 0, 0, f_getwinposy},
7913 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007914 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007915 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007918 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007919 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7921 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7922 {"histadd", 2, 2, f_histadd},
7923 {"histdel", 1, 2, f_histdel},
7924 {"histget", 1, 2, f_histget},
7925 {"histnr", 1, 1, f_histnr},
7926 {"hlID", 1, 1, f_hlID},
7927 {"hlexists", 1, 1, f_hlexists},
7928 {"hostname", 0, 0, f_hostname},
7929 {"iconv", 3, 3, f_iconv},
7930 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007932 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007934 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"inputrestore", 0, 0, f_inputrestore},
7936 {"inputsave", 0, 0, f_inputsave},
7937 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007939 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007941 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007946 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {"libcall", 3, 3, f_libcall},
7948 {"libcallnr", 3, 3, f_libcallnr},
7949 {"line", 1, 1, f_line},
7950 {"line2byte", 1, 1, f_line2byte},
7951 {"lispindent", 1, 1, f_lispindent},
7952 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007953#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007954 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007955 {"log10", 1, 1, f_log10},
7956#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007957#ifdef FEAT_LUA
7958 {"luaeval", 1, 2, f_luaeval},
7959#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007961 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007962 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007963 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007964 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007965 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007966 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007967 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007968 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007969 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007970 {"max", 1, 1, f_max},
7971 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007972#ifdef vim_mkdir
7973 {"mkdir", 1, 3, f_mkdir},
7974#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007976#ifdef FEAT_MZSCHEME
7977 {"mzeval", 1, 1, f_mzeval},
7978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"nextnonblank", 1, 1, f_nextnonblank},
7980 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007981 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007982 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
7984 {"pow", 2, 2, f_pow},
7985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007987 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007988 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007990 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007991 {"reltime", 0, 2, f_reltime},
7992 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"remote_expr", 2, 3, f_remote_expr},
7994 {"remote_foreground", 1, 1, f_remote_foreground},
7995 {"remote_peek", 1, 2, f_remote_peek},
7996 {"remote_read", 1, 1, f_remote_read},
7997 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008000 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008002 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003#ifdef FEAT_FLOAT
8004 {"round", 1, 1, f_round},
8005#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00008006 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008007 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008008 {"searchpair", 3, 7, f_searchpair},
8009 {"searchpairpos", 3, 7, f_searchpairpos},
8010 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"server2client", 2, 2, f_server2client},
8012 {"serverlist", 0, 0, f_serverlist},
8013 {"setbufvar", 3, 3, f_setbufvar},
8014 {"setcmdpos", 1, 1, f_setcmdpos},
8015 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008016 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008017 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008018 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008019 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008021 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008022 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008024 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
8027 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008028 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008029#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008030 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008031 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008032 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008033 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008034 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035#ifdef FEAT_FLOAT
8036 {"sqrt", 1, 1, f_sqrt},
8037 {"str2float", 1, 1, f_str2float},
8038#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008039 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008040 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008041 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042#ifdef HAVE_STRFTIME
8043 {"strftime", 1, 2, f_strftime},
8044#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008046 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"strlen", 1, 1, f_strlen},
8048 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008049 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008051 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"submatch", 1, 1, f_submatch},
8053 {"substitute", 4, 4, f_substitute},
8054 {"synID", 3, 3, f_synID},
8055 {"synIDattr", 2, 3, f_synIDattr},
8056 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008057 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008058 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008059 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008060 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008061 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008062 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008063 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008064 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008065#ifdef FEAT_FLOAT
8066 {"tan", 1, 1, f_tan},
8067 {"tanh", 1, 1, f_tanh},
8068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008070 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"tolower", 1, 1, f_tolower},
8072 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008073 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008074#ifdef FEAT_FLOAT
8075 {"trunc", 1, 1, f_trunc},
8076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008078 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008079 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008080 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"virtcol", 1, 1, f_virtcol},
8082 {"visualmode", 0, 1, f_visualmode},
8083 {"winbufnr", 1, 1, f_winbufnr},
8084 {"wincol", 0, 0, f_wincol},
8085 {"winheight", 1, 1, f_winheight},
8086 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008087 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008089 {"winrestview", 1, 1, f_winrestview},
8090 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008092 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008093 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094};
8095
8096#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8097
8098/*
8099 * Function given to ExpandGeneric() to obtain the list of internal
8100 * or user defined function names.
8101 */
8102 char_u *
8103get_function_name(xp, idx)
8104 expand_T *xp;
8105 int idx;
8106{
8107 static int intidx = -1;
8108 char_u *name;
8109
8110 if (idx == 0)
8111 intidx = -1;
8112 if (intidx < 0)
8113 {
8114 name = get_user_func_name(xp, idx);
8115 if (name != NULL)
8116 return name;
8117 }
8118 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8119 {
8120 STRCPY(IObuff, functions[intidx].f_name);
8121 STRCAT(IObuff, "(");
8122 if (functions[intidx].f_max_argc == 0)
8123 STRCAT(IObuff, ")");
8124 return IObuff;
8125 }
8126
8127 return NULL;
8128}
8129
8130/*
8131 * Function given to ExpandGeneric() to obtain the list of internal or
8132 * user defined variable or function names.
8133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 char_u *
8135get_expr_name(xp, idx)
8136 expand_T *xp;
8137 int idx;
8138{
8139 static int intidx = -1;
8140 char_u *name;
8141
8142 if (idx == 0)
8143 intidx = -1;
8144 if (intidx < 0)
8145 {
8146 name = get_function_name(xp, idx);
8147 if (name != NULL)
8148 return name;
8149 }
8150 return get_user_var_name(xp, ++intidx);
8151}
8152
8153#endif /* FEAT_CMDL_COMPL */
8154
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008155#if defined(EBCDIC) || defined(PROTO)
8156/*
8157 * Compare struct fst by function name.
8158 */
8159 static int
8160compare_func_name(s1, s2)
8161 const void *s1;
8162 const void *s2;
8163{
8164 struct fst *p1 = (struct fst *)s1;
8165 struct fst *p2 = (struct fst *)s2;
8166
8167 return STRCMP(p1->f_name, p2->f_name);
8168}
8169
8170/*
8171 * Sort the function table by function name.
8172 * The sorting of the table above is ASCII dependant.
8173 * On machines using EBCDIC we have to sort it.
8174 */
8175 static void
8176sortFunctions()
8177{
8178 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8179
8180 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8181}
8182#endif
8183
8184
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185/*
8186 * Find internal function in table above.
8187 * Return index, or -1 if not found
8188 */
8189 static int
8190find_internal_func(name)
8191 char_u *name; /* name of the function */
8192{
8193 int first = 0;
8194 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8195 int cmp;
8196 int x;
8197
8198 /*
8199 * Find the function name in the table. Binary search.
8200 */
8201 while (first <= last)
8202 {
8203 x = first + ((unsigned)(last - first) >> 1);
8204 cmp = STRCMP(name, functions[x].f_name);
8205 if (cmp < 0)
8206 last = x - 1;
8207 else if (cmp > 0)
8208 first = x + 1;
8209 else
8210 return x;
8211 }
8212 return -1;
8213}
8214
8215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8217 * name it contains, otherwise return "name".
8218 */
8219 static char_u *
8220deref_func_name(name, lenp)
8221 char_u *name;
8222 int *lenp;
8223{
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 int cc;
8226
8227 cc = name[*lenp];
8228 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008229 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008230 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008233 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 {
8235 *lenp = 0;
8236 return (char_u *)""; /* just in case */
8237 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008238 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 }
8241
8242 return name;
8243}
8244
8245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 * Allocate a variable for the result of a function.
8247 * Return OK or FAIL.
8248 */
8249 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008250get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8251 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 char_u *name; /* name of the function */
8253 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 char_u **arg; /* argument, pointing to the '(' */
8256 linenr_T firstline; /* first line of range */
8257 linenr_T lastline; /* last line of range */
8258 int *doesrange; /* return: function handled range */
8259 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 char_u *argp;
8263 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008264 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 int argcount = 0; /* number of arguments found */
8266
8267 /*
8268 * Get the arguments.
8269 */
8270 argp = *arg;
8271 while (argcount < MAX_FUNC_ARGS)
8272 {
8273 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8274 if (*argp == ')' || *argp == ',' || *argp == NUL)
8275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8277 {
8278 ret = FAIL;
8279 break;
8280 }
8281 ++argcount;
8282 if (*argp != ',')
8283 break;
8284 }
8285 if (*argp == ')')
8286 ++argp;
8287 else
8288 ret = FAIL;
8289
8290 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 {
8295 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008296 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008298 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
8301 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303
8304 *arg = skipwhite(argp);
8305 return ret;
8306}
8307
8308
8309/*
8310 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008311 * Return OK when the function can't be called, FAIL otherwise.
8312 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 */
8314 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008315call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008316 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008317 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008321 typval_T *argvars; /* vars for arguments, must have "argcount"
8322 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 linenr_T firstline; /* first line of range */
8324 linenr_T lastline; /* last line of range */
8325 int *doesrange; /* return: function handled range */
8326 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328{
8329 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#define ERROR_UNKNOWN 0
8331#define ERROR_TOOMANY 1
8332#define ERROR_TOOFEW 2
8333#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334#define ERROR_DICT 4
8335#define ERROR_NONE 5
8336#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 int error = ERROR_NONE;
8338 int i;
8339 int llen;
8340 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341#define FLEN_FIXED 40
8342 char_u fname_buf[FLEN_FIXED + 1];
8343 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008344 char_u *name;
8345
8346 /* Make a copy of the name, if it comes from a funcref variable it could
8347 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008348 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008349 if (name == NULL)
8350 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
8352 /*
8353 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8354 * Change <SNR>123_name() to K_SNR 123_name().
8355 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 llen = eval_fname_script(name);
8358 if (llen > 0)
8359 {
8360 fname_buf[0] = K_SPECIAL;
8361 fname_buf[1] = KS_EXTRA;
8362 fname_buf[2] = (int)KE_SNR;
8363 i = 3;
8364 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8365 {
8366 if (current_SID <= 0)
8367 error = ERROR_SCRIPT;
8368 else
8369 {
8370 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8371 i = (int)STRLEN(fname_buf);
8372 }
8373 }
8374 if (i + STRLEN(name + llen) < FLEN_FIXED)
8375 {
8376 STRCPY(fname_buf + i, name + llen);
8377 fname = fname_buf;
8378 }
8379 else
8380 {
8381 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8382 if (fname == NULL)
8383 error = ERROR_OTHER;
8384 else
8385 {
8386 mch_memmove(fname, fname_buf, (size_t)i);
8387 STRCPY(fname + i, name + llen);
8388 }
8389 }
8390 }
8391 else
8392 fname = name;
8393
8394 *doesrange = FALSE;
8395
8396
8397 /* execute the function if no errors detected and executing */
8398 if (evaluate && error == ERROR_NONE)
8399 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008400 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8401 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 error = ERROR_UNKNOWN;
8403
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008404 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
8406 /*
8407 * User defined function.
8408 */
8409 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008412 /* Trigger FuncUndefined event, may load the function. */
8413 if (fp == NULL
8414 && apply_autocmds(EVENT_FUNCUNDEFINED,
8415 fname, fname, TRUE, NULL)
8416 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008418 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 fp = find_func(fname);
8420 }
8421#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008422 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008423 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008424 {
8425 /* loaded a package, search for the function again */
8426 fp = find_func(fname);
8427 }
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (fp != NULL)
8430 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008431 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008433 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008435 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008437 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008438 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 else
8440 {
8441 /*
8442 * Call the user function.
8443 * Save and restore search patterns, script variables and
8444 * redo buffer.
8445 */
8446 save_search_patterns();
8447 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008448 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008451 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8452 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8453 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008454 /* Function was unreferenced while being used, free it
8455 * now. */
8456 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 restoreRedobuff();
8458 restore_search_patterns();
8459 error = ERROR_NONE;
8460 }
8461 }
8462 }
8463 else
8464 {
8465 /*
8466 * Find the function name in the table, call its implementation.
8467 */
8468 i = find_internal_func(fname);
8469 if (i >= 0)
8470 {
8471 if (argcount < functions[i].f_min_argc)
8472 error = ERROR_TOOFEW;
8473 else if (argcount > functions[i].f_max_argc)
8474 error = ERROR_TOOMANY;
8475 else
8476 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 error = ERROR_NONE;
8480 }
8481 }
8482 }
8483 /*
8484 * The function call (or "FuncUndefined" autocommand sequence) might
8485 * have been aborted by an error, an interrupt, or an explicitly thrown
8486 * exception that has not been caught so far. This situation can be
8487 * tested for by calling aborting(). For an error in an internal
8488 * function or for the "E132" error in call_user_func(), however, the
8489 * throw point at which the "force_abort" flag (temporarily reset by
8490 * emsg()) is normally updated has not been reached yet. We need to
8491 * update that flag first to make aborting() reliable.
8492 */
8493 update_force_abort();
8494 }
8495 if (error == ERROR_NONE)
8496 ret = OK;
8497
8498 /*
8499 * Report an error unless the argument evaluation or function call has been
8500 * cancelled due to an aborting error, an interrupt, or an exception.
8501 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008502 if (!aborting())
8503 {
8504 switch (error)
8505 {
8506 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008507 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008508 break;
8509 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008510 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008511 break;
8512 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008513 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008514 name);
8515 break;
8516 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008517 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008518 name);
8519 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008520 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008521 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 name);
8523 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008524 }
8525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (fname != name && fname != fname_buf)
8528 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008529 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530
8531 return ret;
8532}
8533
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008534/*
8535 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008536 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008537 */
8538 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008539emsg_funcname(ermsg, name)
8540 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008541 char_u *name;
8542{
8543 char_u *p;
8544
8545 if (*name == K_SPECIAL)
8546 p = concat_str((char_u *)"<SNR>", name + 3);
8547 else
8548 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008549 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008550 if (p != name)
8551 vim_free(p);
8552}
8553
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008554/*
8555 * Return TRUE for a non-zero Number and a non-empty String.
8556 */
8557 static int
8558non_zero_arg(argvars)
8559 typval_T *argvars;
8560{
8561 return ((argvars[0].v_type == VAR_NUMBER
8562 && argvars[0].vval.v_number != 0)
8563 || (argvars[0].v_type == VAR_STRING
8564 && argvars[0].vval.v_string != NULL
8565 && *argvars[0].vval.v_string != NUL));
8566}
8567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568/*********************************************
8569 * Implementation of the built-in functions
8570 */
8571
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008572#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008573static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8574
8575/*
8576 * Get the float value of "argvars[0]" into "f".
8577 * Returns FAIL when the argument is not a Number or Float.
8578 */
8579 static int
8580get_float_arg(argvars, f)
8581 typval_T *argvars;
8582 float_T *f;
8583{
8584 if (argvars[0].v_type == VAR_FLOAT)
8585 {
8586 *f = argvars[0].vval.v_float;
8587 return OK;
8588 }
8589 if (argvars[0].v_type == VAR_NUMBER)
8590 {
8591 *f = (float_T)argvars[0].vval.v_number;
8592 return OK;
8593 }
8594 EMSG(_("E808: Number or Float required"));
8595 return FAIL;
8596}
8597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008598/*
8599 * "abs(expr)" function
8600 */
8601 static void
8602f_abs(argvars, rettv)
8603 typval_T *argvars;
8604 typval_T *rettv;
8605{
8606 if (argvars[0].v_type == VAR_FLOAT)
8607 {
8608 rettv->v_type = VAR_FLOAT;
8609 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8610 }
8611 else
8612 {
8613 varnumber_T n;
8614 int error = FALSE;
8615
8616 n = get_tv_number_chk(&argvars[0], &error);
8617 if (error)
8618 rettv->vval.v_number = -1;
8619 else if (n > 0)
8620 rettv->vval.v_number = n;
8621 else
8622 rettv->vval.v_number = -n;
8623 }
8624}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625
8626/*
8627 * "acos()" function
8628 */
8629 static void
8630f_acos(argvars, rettv)
8631 typval_T *argvars;
8632 typval_T *rettv;
8633{
8634 float_T f;
8635
8636 rettv->v_type = VAR_FLOAT;
8637 if (get_float_arg(argvars, &f) == OK)
8638 rettv->vval.v_float = acos(f);
8639 else
8640 rettv->vval.v_float = 0.0;
8641}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008642#endif
8643
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008645 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 */
8647 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008648f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 typval_T *argvars;
8650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651{
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008657 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008658 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008659 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 }
8662 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008663 EMSG(_(e_listreq));
8664}
8665
8666/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008667 * "and(expr, expr)" function
8668 */
8669 static void
8670f_and(argvars, rettv)
8671 typval_T *argvars;
8672 typval_T *rettv;
8673{
8674 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8675 & get_tv_number_chk(&argvars[1], NULL);
8676}
8677
8678/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008679 * "append(lnum, string/list)" function
8680 */
8681 static void
8682f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *argvars;
8684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008685{
8686 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008688 list_T *l = NULL;
8689 listitem_T *li = NULL;
8690 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008691 long added = 0;
8692
Bram Moolenaar0d660222005-01-07 21:51:51 +00008693 lnum = get_tv_lnum(argvars);
8694 if (lnum >= 0
8695 && lnum <= curbuf->b_ml.ml_line_count
8696 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008697 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008699 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700 l = argvars[1].vval.v_list;
8701 if (l == NULL)
8702 return;
8703 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008704 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008705 for (;;)
8706 {
8707 if (l == NULL)
8708 tv = &argvars[1]; /* append a string */
8709 else if (li == NULL)
8710 break; /* end of list */
8711 else
8712 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008713 line = get_tv_string_chk(tv);
8714 if (line == NULL) /* type error */
8715 {
8716 rettv->vval.v_number = 1; /* Failed */
8717 break;
8718 }
8719 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 ++added;
8721 if (l == NULL)
8722 break;
8723 li = li->li_next;
8724 }
8725
8726 appended_lines_mark(lnum, added);
8727 if (curwin->w_cursor.lnum > lnum)
8728 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 else
8731 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732}
8733
8734/*
8735 * "argc()" function
8736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008739 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744
8745/*
8746 * "argidx()" function
8747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
8755
8756/*
8757 * "argv(nr)" function
8758 */
8759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008761 typval_T *argvars;
8762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 int idx;
8765
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008766 if (argvars[0].v_type != VAR_UNKNOWN)
8767 {
8768 idx = get_tv_number_chk(&argvars[0], NULL);
8769 if (idx >= 0 && idx < ARGCOUNT)
8770 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8771 else
8772 rettv->vval.v_string = NULL;
8773 rettv->v_type = VAR_STRING;
8774 }
8775 else if (rettv_list_alloc(rettv) == OK)
8776 for (idx = 0; idx < ARGCOUNT; ++idx)
8777 list_append_string(rettv->vval.v_list,
8778 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008782/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008783 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008784 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008785 static void
8786f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008787 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008788 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008789{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008790 float_T f;
8791
8792 rettv->v_type = VAR_FLOAT;
8793 if (get_float_arg(argvars, &f) == OK)
8794 rettv->vval.v_float = asin(f);
8795 else
8796 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008797}
8798
8799/*
8800 * "atan()" function
8801 */
8802 static void
8803f_atan(argvars, rettv)
8804 typval_T *argvars;
8805 typval_T *rettv;
8806{
8807 float_T f;
8808
8809 rettv->v_type = VAR_FLOAT;
8810 if (get_float_arg(argvars, &f) == OK)
8811 rettv->vval.v_float = atan(f);
8812 else
8813 rettv->vval.v_float = 0.0;
8814}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008815
8816/*
8817 * "atan2()" function
8818 */
8819 static void
8820f_atan2(argvars, rettv)
8821 typval_T *argvars;
8822 typval_T *rettv;
8823{
8824 float_T fx, fy;
8825
8826 rettv->v_type = VAR_FLOAT;
8827 if (get_float_arg(argvars, &fx) == OK
8828 && get_float_arg(&argvars[1], &fy) == OK)
8829 rettv->vval.v_float = atan2(fx, fy);
8830 else
8831 rettv->vval.v_float = 0.0;
8832}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008833#endif
8834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835/*
8836 * "browse(save, title, initdir, default)" function
8837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008840 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843#ifdef FEAT_BROWSE
8844 int save;
8845 char_u *title;
8846 char_u *initdir;
8847 char_u *defname;
8848 char_u buf[NUMBUFLEN];
8849 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008850 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 save = get_tv_number_chk(&argvars[0], &error);
8853 title = get_tv_string_chk(&argvars[1]);
8854 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8855 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008857 if (error || title == NULL || initdir == NULL || defname == NULL)
8858 rettv->vval.v_string = NULL;
8859 else
8860 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008861 do_browse(save ? BROWSE_SAVE : 0,
8862 title, defname, NULL, initdir, NULL, curbuf);
8863#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008867}
8868
8869/*
8870 * "browsedir(title, initdir)" function
8871 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008875 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008876{
8877#ifdef FEAT_BROWSE
8878 char_u *title;
8879 char_u *initdir;
8880 char_u buf[NUMBUFLEN];
8881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 title = get_tv_string_chk(&argvars[0]);
8883 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 if (title == NULL || initdir == NULL)
8886 rettv->vval.v_string = NULL;
8887 else
8888 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008889 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894}
8895
Bram Moolenaar33570922005-01-25 22:26:29 +00008896static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898/*
8899 * Find a buffer by number or exact name.
8900 */
8901 static buf_T *
8902find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008907 if (avar->v_type == VAR_NUMBER)
8908 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008909 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008911 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008912 if (buf == NULL)
8913 {
8914 /* No full path name match, try a match with a URL or a "nofile"
8915 * buffer, these don't use the full path. */
8916 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8917 if (buf->b_fname != NULL
8918 && (path_with_url(buf->b_fname)
8919#ifdef FEAT_QUICKFIX
8920 || bt_nofile(buf)
8921#endif
8922 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008923 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008924 break;
8925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 }
8927 return buf;
8928}
8929
8930/*
8931 * "bufexists(expr)" function
8932 */
8933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *argvars;
8936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939}
8940
8941/*
8942 * "buflisted(expr)" function
8943 */
8944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *argvars;
8947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
8949 buf_T *buf;
8950
8951 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953}
8954
8955/*
8956 * "bufloaded(expr)" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 buf_T *buf;
8964
8965 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967}
8968
Bram Moolenaar33570922005-01-25 22:26:29 +00008969static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971/*
8972 * Get buffer by number or pattern.
8973 */
8974 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 int save_magic;
8980 char_u *save_cpo;
8981 buf_T *buf;
8982
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 if (tv->v_type == VAR_NUMBER)
8984 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008985 if (tv->v_type != VAR_STRING)
8986 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 if (name == NULL || *name == NUL)
8988 return curbuf;
8989 if (name[0] == '$' && name[1] == NUL)
8990 return lastbuf;
8991
8992 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8993 save_magic = p_magic;
8994 p_magic = TRUE;
8995 save_cpo = p_cpo;
8996 p_cpo = (char_u *)"";
8997
8998 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8999 TRUE, FALSE));
9000
9001 p_magic = save_magic;
9002 p_cpo = save_cpo;
9003
9004 /* If not found, try expanding the name, like done for bufexists(). */
9005 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 return buf;
9009}
9010
9011/*
9012 * "bufname(expr)" function
9013 */
9014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009016 typval_T *argvars;
9017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
9019 buf_T *buf;
9020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 buf = get_buf_tv(&argvars[0]);
9024 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 --emsg_off;
9030}
9031
9032/*
9033 * "bufnr(expr)" function
9034 */
9035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009041 int error = FALSE;
9042 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009047 --emsg_off;
9048
9049 /* If the buffer isn't found and the second argument is not zero create a
9050 * new buffer. */
9051 if (buf == NULL
9052 && argvars[1].v_type != VAR_UNKNOWN
9053 && get_tv_number_chk(&argvars[1], &error) != 0
9054 && !error
9055 && (name = get_tv_string_chk(&argvars[0])) != NULL
9056 && !error)
9057 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9058
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
9065/*
9066 * "bufwinnr(nr)" function
9067 */
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 typval_T *argvars;
9071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073#ifdef FEAT_WINDOWS
9074 win_T *wp;
9075 int winnr = 0;
9076#endif
9077 buf_T *buf;
9078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082#ifdef FEAT_WINDOWS
9083 for (wp = firstwin; wp; wp = wp->w_next)
9084 {
9085 ++winnr;
9086 if (wp->w_buffer == buf)
9087 break;
9088 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092#endif
9093 --emsg_off;
9094}
9095
9096/*
9097 * "byte2line(byte)" function
9098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009101 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103{
9104#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106#else
9107 long boff = 0;
9108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 (linenr_T)0, &boff);
9115#endif
9116}
9117
9118/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009119 * "byteidx()" function
9120 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009125{
9126#ifdef FEAT_MBYTE
9127 char_u *t;
9128#endif
9129 char_u *str;
9130 long idx;
9131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 str = get_tv_string_chk(&argvars[0]);
9133 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009136 return;
9137
9138#ifdef FEAT_MBYTE
9139 t = str;
9140 for ( ; idx > 0; idx--)
9141 {
9142 if (*t == NUL) /* EOL reached */
9143 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009144 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009145 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009146 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009147#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009148 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009150#endif
9151}
9152
9153/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009154 * "call(func, arglist)" function
9155 */
9156 static void
9157f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009158 typval_T *argvars;
9159 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009160{
9161 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009162 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009163 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009165 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009167
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009168 if (argvars[1].v_type != VAR_LIST)
9169 {
9170 EMSG(_(e_listreq));
9171 return;
9172 }
9173 if (argvars[1].vval.v_list == NULL)
9174 return;
9175
9176 if (argvars[0].v_type == VAR_FUNC)
9177 func = argvars[0].vval.v_string;
9178 else
9179 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 if (*func == NUL)
9181 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009182
Bram Moolenaare9a41262005-01-15 22:18:47 +00009183 if (argvars[2].v_type != VAR_UNKNOWN)
9184 {
9185 if (argvars[2].v_type != VAR_DICT)
9186 {
9187 EMSG(_(e_dictreq));
9188 return;
9189 }
9190 selfdict = argvars[2].vval.v_dict;
9191 }
9192
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009193 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9194 item = item->li_next)
9195 {
9196 if (argc == MAX_FUNC_ARGS)
9197 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009198 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009199 break;
9200 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009201 /* Make a copy of each argument. This is needed to be able to set
9202 * v_lock to VAR_FIXED in the copy without changing the original list.
9203 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009204 copy_tv(&item->li_tv, &argv[argc++]);
9205 }
9206
9207 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009208 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009209 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9210 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009211
9212 /* Free the arguments. */
9213 while (argc > 0)
9214 clear_tv(&argv[--argc]);
9215}
9216
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009217#ifdef FEAT_FLOAT
9218/*
9219 * "ceil({float})" function
9220 */
9221 static void
9222f_ceil(argvars, rettv)
9223 typval_T *argvars;
9224 typval_T *rettv;
9225{
9226 float_T f;
9227
9228 rettv->v_type = VAR_FLOAT;
9229 if (get_float_arg(argvars, &f) == OK)
9230 rettv->vval.v_float = ceil(f);
9231 else
9232 rettv->vval.v_float = 0.0;
9233}
9234#endif
9235
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009236/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009237 * "changenr()" function
9238 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009239 static void
9240f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009241 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009242 typval_T *rettv;
9243{
9244 rettv->vval.v_number = curbuf->b_u_seq_cur;
9245}
9246
9247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 * "char2nr(string)" function
9249 */
9250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009252 typval_T *argvars;
9253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255#ifdef FEAT_MBYTE
9256 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 else
9259#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261}
9262
9263/*
9264 * "cindent(lnum)" function
9265 */
9266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009268 typval_T *argvars;
9269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
9271#ifdef FEAT_CINDENT
9272 pos_T pos;
9273 linenr_T lnum;
9274
9275 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9278 {
9279 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 curwin->w_cursor = pos;
9282 }
9283 else
9284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009285 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286}
9287
9288/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009289 * "clearmatches()" function
9290 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009291 static void
9292f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009293 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009294 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009295{
9296#ifdef FEAT_SEARCH_EXTRA
9297 clear_matches(curwin);
9298#endif
9299}
9300
9301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * "col(string)" function
9303 */
9304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 typval_T *argvars;
9307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 colnr_T col = 0;
9310 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009311 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009313 fp = var2fpos(&argvars[0], FALSE, &fnum);
9314 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 {
9316 if (fp->col == MAXCOL)
9317 {
9318 /* '> can be MAXCOL, get the length of the line then */
9319 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009320 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 else
9322 col = MAXCOL;
9323 }
9324 else
9325 {
9326 col = fp->col + 1;
9327#ifdef FEAT_VIRTUALEDIT
9328 /* col(".") when the cursor is on the NUL at the end of the line
9329 * because of "coladd" can be seen as an extra column. */
9330 if (virtual_active() && fp == &curwin->w_cursor)
9331 {
9332 char_u *p = ml_get_cursor();
9333
9334 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9335 curwin->w_virtcol - curwin->w_cursor.coladd))
9336 {
9337# ifdef FEAT_MBYTE
9338 int l;
9339
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009340 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 col += l;
9342# else
9343 if (*p != NUL && p[1] == NUL)
9344 ++col;
9345# endif
9346 }
9347 }
9348#endif
9349 }
9350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352}
9353
Bram Moolenaar572cb562005-08-05 21:35:02 +00009354#if defined(FEAT_INS_EXPAND)
9355/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009356 * "complete()" function
9357 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009358 static void
9359f_complete(argvars, rettv)
9360 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009361 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009362{
9363 int startcol;
9364
9365 if ((State & INSERT) == 0)
9366 {
9367 EMSG(_("E785: complete() can only be used in Insert mode"));
9368 return;
9369 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009370
9371 /* Check for undo allowed here, because if something was already inserted
9372 * the line was already saved for undo and this check isn't done. */
9373 if (!undo_allowed())
9374 return;
9375
Bram Moolenaarade00832006-03-10 21:46:58 +00009376 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9377 {
9378 EMSG(_(e_invarg));
9379 return;
9380 }
9381
9382 startcol = get_tv_number_chk(&argvars[0], NULL);
9383 if (startcol <= 0)
9384 return;
9385
9386 set_completion(startcol - 1, argvars[1].vval.v_list);
9387}
9388
9389/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009390 * "complete_add()" function
9391 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009392 static void
9393f_complete_add(argvars, rettv)
9394 typval_T *argvars;
9395 typval_T *rettv;
9396{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009397 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009398}
9399
9400/*
9401 * "complete_check()" function
9402 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009403 static void
9404f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009405 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009406 typval_T *rettv;
9407{
9408 int saved = RedrawingDisabled;
9409
9410 RedrawingDisabled = 0;
9411 ins_compl_check_keys(0);
9412 rettv->vval.v_number = compl_interrupted;
9413 RedrawingDisabled = saved;
9414}
9415#endif
9416
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417/*
9418 * "confirm(message, buttons[, default [, type]])" function
9419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009422 typval_T *argvars UNUSED;
9423 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424{
9425#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9426 char_u *message;
9427 char_u *buttons = NULL;
9428 char_u buf[NUMBUFLEN];
9429 char_u buf2[NUMBUFLEN];
9430 int def = 1;
9431 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009432 char_u *typestr;
9433 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 message = get_tv_string_chk(&argvars[0]);
9436 if (message == NULL)
9437 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009438 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9441 if (buttons == NULL)
9442 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009443 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009446 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009448 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9449 if (typestr == NULL)
9450 error = TRUE;
9451 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 switch (TOUPPER_ASC(*typestr))
9454 {
9455 case 'E': type = VIM_ERROR; break;
9456 case 'Q': type = VIM_QUESTION; break;
9457 case 'I': type = VIM_INFO; break;
9458 case 'W': type = VIM_WARNING; break;
9459 case 'G': type = VIM_GENERIC; break;
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 }
9462 }
9463 }
9464 }
9465
9466 if (buttons == NULL || *buttons == NUL)
9467 buttons = (char_u *)_("&Ok");
9468
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009469 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009471 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472#endif
9473}
9474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009475/*
9476 * "copy()" function
9477 */
9478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 typval_T *argvars;
9481 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009482{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009483 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009486#ifdef FEAT_FLOAT
9487/*
9488 * "cos()" function
9489 */
9490 static void
9491f_cos(argvars, rettv)
9492 typval_T *argvars;
9493 typval_T *rettv;
9494{
9495 float_T f;
9496
9497 rettv->v_type = VAR_FLOAT;
9498 if (get_float_arg(argvars, &f) == OK)
9499 rettv->vval.v_float = cos(f);
9500 else
9501 rettv->vval.v_float = 0.0;
9502}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009503
9504/*
9505 * "cosh()" function
9506 */
9507 static void
9508f_cosh(argvars, rettv)
9509 typval_T *argvars;
9510 typval_T *rettv;
9511{
9512 float_T f;
9513
9514 rettv->v_type = VAR_FLOAT;
9515 if (get_float_arg(argvars, &f) == OK)
9516 rettv->vval.v_float = cosh(f);
9517 else
9518 rettv->vval.v_float = 0.0;
9519}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009520#endif
9521
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009523 * "count()" function
9524 */
9525 static void
9526f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 typval_T *argvars;
9528 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009529{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009530 long n = 0;
9531 int ic = FALSE;
9532
Bram Moolenaare9a41262005-01-15 22:18:47 +00009533 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009535 listitem_T *li;
9536 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009537 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009538
Bram Moolenaare9a41262005-01-15 22:18:47 +00009539 if ((l = argvars[0].vval.v_list) != NULL)
9540 {
9541 li = l->lv_first;
9542 if (argvars[2].v_type != VAR_UNKNOWN)
9543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 int error = FALSE;
9545
9546 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009547 if (argvars[3].v_type != VAR_UNKNOWN)
9548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 idx = get_tv_number_chk(&argvars[3], &error);
9550 if (!error)
9551 {
9552 li = list_find(l, idx);
9553 if (li == NULL)
9554 EMSGN(_(e_listidx), idx);
9555 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009556 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 if (error)
9558 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009559 }
9560
9561 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009562 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009563 ++n;
9564 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009565 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009566 else if (argvars[0].v_type == VAR_DICT)
9567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 int todo;
9569 dict_T *d;
9570 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009571
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009572 if ((d = argvars[0].vval.v_dict) != NULL)
9573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 int error = FALSE;
9575
Bram Moolenaare9a41262005-01-15 22:18:47 +00009576 if (argvars[2].v_type != VAR_UNKNOWN)
9577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009578 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009579 if (argvars[3].v_type != VAR_UNKNOWN)
9580 EMSG(_(e_invarg));
9581 }
9582
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009583 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009584 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009585 {
9586 if (!HASHITEM_EMPTY(hi))
9587 {
9588 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009589 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009590 ++n;
9591 }
9592 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009593 }
9594 }
9595 else
9596 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009597 rettv->vval.v_number = n;
9598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9602 *
9603 * Checks the existence of a cscope connection.
9604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009607 typval_T *argvars UNUSED;
9608 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610#ifdef FEAT_CSCOPE
9611 int num = 0;
9612 char_u *dbpath = NULL;
9613 char_u *prepend = NULL;
9614 char_u buf[NUMBUFLEN];
9615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009616 if (argvars[0].v_type != VAR_UNKNOWN
9617 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 num = (int)get_tv_number(&argvars[0]);
9620 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 }
9624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626#endif
9627}
9628
9629/*
9630 * "cursor(lnum, col)" function
9631 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009632 * Moves the cursor to the specified line and column.
9633 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 typval_T *argvars;
9638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
9640 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009641#ifdef FEAT_VIRTUALEDIT
9642 long coladd = 0;
9643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009645 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009646 if (argvars[1].v_type == VAR_UNKNOWN)
9647 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009648 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009649
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009650 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009651 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009652 line = pos.lnum;
9653 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009654#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009655 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009656#endif
9657 }
9658 else
9659 {
9660 line = get_tv_lnum(argvars);
9661 col = get_tv_number_chk(&argvars[1], NULL);
9662#ifdef FEAT_VIRTUALEDIT
9663 if (argvars[2].v_type != VAR_UNKNOWN)
9664 coladd = get_tv_number_chk(&argvars[2], NULL);
9665#endif
9666 }
9667 if (line < 0 || col < 0
9668#ifdef FEAT_VIRTUALEDIT
9669 || coladd < 0
9670#endif
9671 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (line > 0)
9674 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 if (col > 0)
9676 curwin->w_cursor.col = col - 1;
9677#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009678 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#endif
9680
9681 /* Make sure the cursor is in a valid position. */
9682 check_cursor();
9683#ifdef FEAT_MBYTE
9684 /* Correct cursor for multi-byte character. */
9685 if (has_mbyte)
9686 mb_adjust_cursor();
9687#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009688
9689 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009690 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691}
9692
9693/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009694 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 */
9696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 typval_T *argvars;
9699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009701 int noref = 0;
9702
9703 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009705 if (noref < 0 || noref > 1)
9706 EMSG(_(e_invarg));
9707 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009708 {
9709 current_copyID += COPYID_INC;
9710 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
9715 * "delete()" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
9728/*
9729 * "did_filetype()" function
9730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009733 typval_T *argvars UNUSED;
9734 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735{
9736#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#endif
9739}
9740
9741/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009742 * "diff_filler()" function
9743 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009746 typval_T *argvars UNUSED;
9747 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009748{
9749#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009751#endif
9752}
9753
9754/*
9755 * "diff_hlID()" function
9756 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009759 typval_T *argvars UNUSED;
9760 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009761{
9762#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009764 static linenr_T prev_lnum = 0;
9765 static int changedtick = 0;
9766 static int fnum = 0;
9767 static int change_start = 0;
9768 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009769 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009770 int filler_lines;
9771 int col;
9772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 if (lnum < 0) /* ignore type error in {lnum} arg */
9774 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009775 if (lnum != prev_lnum
9776 || changedtick != curbuf->b_changedtick
9777 || fnum != curbuf->b_fnum)
9778 {
9779 /* New line, buffer, change: need to get the values. */
9780 filler_lines = diff_check(curwin, lnum);
9781 if (filler_lines < 0)
9782 {
9783 if (filler_lines == -1)
9784 {
9785 change_start = MAXCOL;
9786 change_end = -1;
9787 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9788 hlID = HLF_ADD; /* added line */
9789 else
9790 hlID = HLF_CHD; /* changed line */
9791 }
9792 else
9793 hlID = HLF_ADD; /* added line */
9794 }
9795 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009796 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009797 prev_lnum = lnum;
9798 changedtick = curbuf->b_changedtick;
9799 fnum = curbuf->b_fnum;
9800 }
9801
9802 if (hlID == HLF_CHD || hlID == HLF_TXD)
9803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009805 if (col >= change_start && col <= change_end)
9806 hlID = HLF_TXD; /* changed text */
9807 else
9808 hlID = HLF_CHD; /* changed line */
9809 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009810 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009811#endif
9812}
9813
9814/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009815 * "empty({expr})" function
9816 */
9817 static void
9818f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 typval_T *argvars;
9820 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009821{
9822 int n;
9823
9824 switch (argvars[0].v_type)
9825 {
9826 case VAR_STRING:
9827 case VAR_FUNC:
9828 n = argvars[0].vval.v_string == NULL
9829 || *argvars[0].vval.v_string == NUL;
9830 break;
9831 case VAR_NUMBER:
9832 n = argvars[0].vval.v_number == 0;
9833 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009834#ifdef FEAT_FLOAT
9835 case VAR_FLOAT:
9836 n = argvars[0].vval.v_float == 0.0;
9837 break;
9838#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009839 case VAR_LIST:
9840 n = argvars[0].vval.v_list == NULL
9841 || argvars[0].vval.v_list->lv_first == NULL;
9842 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009843 case VAR_DICT:
9844 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009845 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009846 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009847 default:
9848 EMSG2(_(e_intern2), "f_empty()");
9849 n = 0;
9850 }
9851
9852 rettv->vval.v_number = n;
9853}
9854
9855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 * "escape({string}, {chars})" function
9857 */
9858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 typval_T *argvars;
9861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 char_u buf[NUMBUFLEN];
9864
Bram Moolenaar758711c2005-02-02 23:11:38 +00009865 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9866 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868}
9869
9870/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009871 * "eval()" function
9872 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009873 static void
9874f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 typval_T *argvars;
9876 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009877{
9878 char_u *s;
9879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 s = get_tv_string_chk(&argvars[0]);
9881 if (s != NULL)
9882 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9885 {
9886 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009887 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009889 else if (*s != NUL)
9890 EMSG(_(e_trailing));
9891}
9892
9893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 * "eventhandler()" function
9895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
9905 * "executable()" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913}
9914
9915/*
9916 * "exists()" function
9917 */
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T *argvars;
9921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 char_u *p;
9924 char_u *name;
9925 int n = FALSE;
9926 int len = 0;
9927
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009928 no_autoload = TRUE;
9929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 if (*p == '$') /* environment variable */
9932 {
9933 /* first try "normal" environment variables (fast) */
9934 if (mch_getenv(p + 1) != NULL)
9935 n = TRUE;
9936 else
9937 {
9938 /* try expanding things like $VIM and ${HOME} */
9939 p = expand_env_save(p);
9940 if (p != NULL && *p != '$')
9941 n = TRUE;
9942 vim_free(p);
9943 }
9944 }
9945 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009947 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009948 if (*skipwhite(p) != NUL)
9949 n = FALSE; /* trailing garbage */
9950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 else if (*p == '*') /* internal or user defined function */
9952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009953 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 }
9955 else if (*p == ':')
9956 {
9957 n = cmd_exists(p + 1);
9958 }
9959 else if (*p == '#')
9960 {
9961#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009962 if (p[1] == '#')
9963 n = autocmd_supported(p + 2);
9964 else
9965 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966#endif
9967 }
9968 else /* internal variable */
9969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009970 char_u *tofree;
9971 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009973 /* get_name_len() takes care of expanding curly braces */
9974 name = p;
9975 len = get_name_len(&p, &tofree, TRUE, FALSE);
9976 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009978 if (tofree != NULL)
9979 name = tofree;
9980 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9981 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009983 /* handle d.key, l[idx], f(expr) */
9984 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9985 if (n)
9986 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 }
9988 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009989 if (*p != NUL)
9990 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009992 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009996
9997 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998}
9999
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010000#ifdef FEAT_FLOAT
10001/*
10002 * "exp()" function
10003 */
10004 static void
10005f_exp(argvars, rettv)
10006 typval_T *argvars;
10007 typval_T *rettv;
10008{
10009 float_T f;
10010
10011 rettv->v_type = VAR_FLOAT;
10012 if (get_float_arg(argvars, &f) == OK)
10013 rettv->vval.v_float = exp(f);
10014 else
10015 rettv->vval.v_float = 0.0;
10016}
10017#endif
10018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019/*
10020 * "expand()" function
10021 */
10022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 char_u *s;
10028 int len;
10029 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010030 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010033 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010036 if (argvars[1].v_type != VAR_UNKNOWN
10037 && argvars[2].v_type != VAR_UNKNOWN
10038 && get_tv_number_chk(&argvars[2], &error)
10039 && !error)
10040 {
10041 rettv->v_type = VAR_LIST;
10042 rettv->vval.v_list = NULL;
10043 }
10044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (*s == '%' || *s == '#' || *s == '<')
10047 {
10048 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010049 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010051 if (rettv->v_type == VAR_LIST)
10052 {
10053 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10054 list_append_string(rettv->vval.v_list, result, -1);
10055 else
10056 vim_free(result);
10057 }
10058 else
10059 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061 else
10062 {
10063 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010064 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010065 if (argvars[1].v_type != VAR_UNKNOWN
10066 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010067 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 if (!error)
10069 {
10070 ExpandInit(&xpc);
10071 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010072 if (p_wic)
10073 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010074 if (rettv->v_type == VAR_STRING)
10075 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10076 options, WILD_ALL);
10077 else if (rettv_list_alloc(rettv) != FAIL)
10078 {
10079 int i;
10080
10081 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10082 for (i = 0; i < xpc.xp_numfiles; i++)
10083 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10084 ExpandCleanup(&xpc);
10085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 }
10087 else
10088 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 }
10090}
10091
10092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010093 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010094 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010095 */
10096 static void
10097f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010098 typval_T *argvars;
10099 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010100{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010101 char *arg_errmsg = N_("extend() argument");
10102
Bram Moolenaare9a41262005-01-15 22:18:47 +000010103 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010104 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010105 list_T *l1, *l2;
10106 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010107 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010109
Bram Moolenaare9a41262005-01-15 22:18:47 +000010110 l1 = argvars[0].vval.v_list;
10111 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010112 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010113 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010114 {
10115 if (argvars[2].v_type != VAR_UNKNOWN)
10116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 before = get_tv_number_chk(&argvars[2], &error);
10118 if (error)
10119 return; /* type error; errmsg already given */
10120
Bram Moolenaar758711c2005-02-02 23:11:38 +000010121 if (before == l1->lv_len)
10122 item = NULL;
10123 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010124 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010125 item = list_find(l1, before);
10126 if (item == NULL)
10127 {
10128 EMSGN(_(e_listidx), before);
10129 return;
10130 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010131 }
10132 }
10133 else
10134 item = NULL;
10135 list_extend(l1, l2, item);
10136
Bram Moolenaare9a41262005-01-15 22:18:47 +000010137 copy_tv(&argvars[0], rettv);
10138 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010139 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010140 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010142 dict_T *d1, *d2;
10143 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010144 char_u *action;
10145 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010146 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010147 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010148
10149 d1 = argvars[0].vval.v_dict;
10150 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010151 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010152 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 {
10154 /* Check the third argument. */
10155 if (argvars[2].v_type != VAR_UNKNOWN)
10156 {
10157 static char *(av[]) = {"keep", "force", "error"};
10158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 action = get_tv_string_chk(&argvars[2]);
10160 if (action == NULL)
10161 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 for (i = 0; i < 3; ++i)
10163 if (STRCMP(action, av[i]) == 0)
10164 break;
10165 if (i == 3)
10166 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010167 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 return;
10169 }
10170 }
10171 else
10172 action = (char_u *)"force";
10173
10174 /* Go over all entries in the second dict and add them to the
10175 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010176 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010177 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010179 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010181 --todo;
10182 di1 = dict_find(d1, hi2->hi_key, -1);
10183 if (di1 == NULL)
10184 {
10185 di1 = dictitem_copy(HI2DI(hi2));
10186 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10187 dictitem_free(di1);
10188 }
10189 else if (*action == 'e')
10190 {
10191 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10192 break;
10193 }
10194 else if (*action == 'f')
10195 {
10196 clear_tv(&di1->di_tv);
10197 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 }
10200 }
10201
Bram Moolenaare9a41262005-01-15 22:18:47 +000010202 copy_tv(&argvars[0], rettv);
10203 }
10204 }
10205 else
10206 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010207}
10208
10209/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010210 * "feedkeys()" function
10211 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010212 static void
10213f_feedkeys(argvars, rettv)
10214 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010215 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010216{
10217 int remap = TRUE;
10218 char_u *keys, *flags;
10219 char_u nbuf[NUMBUFLEN];
10220 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010221 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010222
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010223 /* This is not allowed in the sandbox. If the commands would still be
10224 * executed in the sandbox it would be OK, but it probably happens later,
10225 * when "sandbox" is no longer set. */
10226 if (check_secure())
10227 return;
10228
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010229 keys = get_tv_string(&argvars[0]);
10230 if (*keys != NUL)
10231 {
10232 if (argvars[1].v_type != VAR_UNKNOWN)
10233 {
10234 flags = get_tv_string_buf(&argvars[1], nbuf);
10235 for ( ; *flags != NUL; ++flags)
10236 {
10237 switch (*flags)
10238 {
10239 case 'n': remap = FALSE; break;
10240 case 'm': remap = TRUE; break;
10241 case 't': typed = TRUE; break;
10242 }
10243 }
10244 }
10245
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010246 /* Need to escape K_SPECIAL and CSI before putting the string in the
10247 * typeahead buffer. */
10248 keys_esc = vim_strsave_escape_csi(keys);
10249 if (keys_esc != NULL)
10250 {
10251 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010252 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010253 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010254 if (vgetc_busy)
10255 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010256 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010257 }
10258}
10259
10260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 * "filereadable()" function
10262 */
10263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010265 typval_T *argvars;
10266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010268 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 char_u *p;
10270 int n;
10271
Bram Moolenaarc236c162008-07-13 17:41:49 +000010272#ifndef O_NONBLOCK
10273# define O_NONBLOCK 0
10274#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010276 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10277 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 {
10279 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010280 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 }
10282 else
10283 n = FALSE;
10284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287
10288/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010289 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 * rights to write into.
10291 */
10292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010294 typval_T *argvars;
10295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010297 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298}
10299
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010300static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010301
10302 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010303findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010304 typval_T *argvars;
10305 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010306 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010307{
10308#ifdef FEAT_SEARCHPATH
10309 char_u *fname;
10310 char_u *fresult = NULL;
10311 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10312 char_u *p;
10313 char_u pathbuf[NUMBUFLEN];
10314 int count = 1;
10315 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010316 int error = FALSE;
10317#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010318
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010319 rettv->vval.v_string = NULL;
10320 rettv->v_type = VAR_STRING;
10321
10322#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10328 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010329 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010330 else
10331 {
10332 if (*p != NUL)
10333 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010335 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010336 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010338 }
10339
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010340 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10341 error = TRUE;
10342
10343 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 do
10346 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010347 if (rettv->v_type == VAR_STRING)
10348 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010349 fresult = find_file_in_path_option(first ? fname : NULL,
10350 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010351 0, first, path,
10352 find_what,
10353 curbuf->b_ffname,
10354 find_what == FINDFILE_DIR
10355 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010357
10358 if (fresult != NULL && rettv->v_type == VAR_LIST)
10359 list_append_string(rettv->vval.v_list, fresult, -1);
10360
10361 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010362 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010363
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010364 if (rettv->v_type == VAR_STRING)
10365 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010366#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010367}
10368
Bram Moolenaar33570922005-01-25 22:26:29 +000010369static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10370static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010371
10372/*
10373 * Implementation of map() and filter().
10374 */
10375 static void
10376filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010379 int map;
10380{
10381 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010382 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010383 listitem_T *li, *nli;
10384 list_T *l = NULL;
10385 dictitem_T *di;
10386 hashtab_T *ht;
10387 hashitem_T *hi;
10388 dict_T *d = NULL;
10389 typval_T save_val;
10390 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010392 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010393 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10394 char *arg_errmsg = (map ? N_("map() argument")
10395 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010396 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010397 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010398
Bram Moolenaare9a41262005-01-15 22:18:47 +000010399 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010400 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010401 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010402 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010403 return;
10404 }
10405 else if (argvars[0].v_type == VAR_DICT)
10406 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010407 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010408 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010409 return;
10410 }
10411 else
10412 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010413 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 return;
10415 }
10416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 expr = get_tv_string_buf_chk(&argvars[1], buf);
10418 /* On type errors, the preceding call has already displayed an error
10419 * message. Avoid a misleading error message for an empty string that
10420 * was not passed as argument. */
10421 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 prepare_vimvar(VV_VAL, &save_val);
10424 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010425
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010426 /* We reset "did_emsg" to be able to detect whether an error
10427 * occurred during evaluation of the expression. */
10428 save_did_emsg = did_emsg;
10429 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010430
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010431 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 vimvars[VV_KEY].vv_type = VAR_STRING;
10435
10436 ht = &d->dv_hashtab;
10437 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010438 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 if (!HASHITEM_EMPTY(hi))
10442 {
10443 --todo;
10444 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010445 if (tv_check_lock(di->di_tv.v_lock,
10446 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 break;
10448 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010449 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010450 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 break;
10452 if (!map && rem)
10453 dictitem_remove(d, di);
10454 clear_tv(&vimvars[VV_KEY].vv_tv);
10455 }
10456 }
10457 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 }
10459 else
10460 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010461 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 for (li = l->lv_first; li != NULL; li = nli)
10464 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010465 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010466 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010468 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010469 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010470 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010471 break;
10472 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010474 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010475 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010476 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010477
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010478 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010480
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010481 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010483
10484 copy_tv(&argvars[0], rettv);
10485}
10486
10487 static int
10488filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010490 char_u *expr;
10491 int map;
10492 int *remp;
10493{
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010496 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010497
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010499 s = expr;
10500 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010501 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010502 if (*s != NUL) /* check for trailing chars after expr */
10503 {
10504 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010505 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010506 }
10507 if (map)
10508 {
10509 /* map(): replace the list item value */
10510 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010511 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 *tv = rettv;
10513 }
10514 else
10515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 int error = FALSE;
10517
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 /* On type error, nothing has been removed; return FAIL to stop the
10522 * loop. The error message was given by get_tv_number_chk(). */
10523 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010524 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010525 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010526 retval = OK;
10527theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010529 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010530}
10531
10532/*
10533 * "filter()" function
10534 */
10535 static void
10536f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010539{
10540 filter_map(argvars, rettv, FALSE);
10541}
10542
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010544 * "finddir({fname}[, {path}[, {count}]])" function
10545 */
10546 static void
10547f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010548 typval_T *argvars;
10549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010551 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010552}
10553
10554/*
10555 * "findfile({fname}[, {path}[, {count}]])" function
10556 */
10557 static void
10558f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010562 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010563}
10564
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010565#ifdef FEAT_FLOAT
10566/*
10567 * "float2nr({float})" function
10568 */
10569 static void
10570f_float2nr(argvars, rettv)
10571 typval_T *argvars;
10572 typval_T *rettv;
10573{
10574 float_T f;
10575
10576 if (get_float_arg(argvars, &f) == OK)
10577 {
10578 if (f < -0x7fffffff)
10579 rettv->vval.v_number = -0x7fffffff;
10580 else if (f > 0x7fffffff)
10581 rettv->vval.v_number = 0x7fffffff;
10582 else
10583 rettv->vval.v_number = (varnumber_T)f;
10584 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010585}
10586
10587/*
10588 * "floor({float})" function
10589 */
10590 static void
10591f_floor(argvars, rettv)
10592 typval_T *argvars;
10593 typval_T *rettv;
10594{
10595 float_T f;
10596
10597 rettv->v_type = VAR_FLOAT;
10598 if (get_float_arg(argvars, &f) == OK)
10599 rettv->vval.v_float = floor(f);
10600 else
10601 rettv->vval.v_float = 0.0;
10602}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010603
10604/*
10605 * "fmod()" function
10606 */
10607 static void
10608f_fmod(argvars, rettv)
10609 typval_T *argvars;
10610 typval_T *rettv;
10611{
10612 float_T fx, fy;
10613
10614 rettv->v_type = VAR_FLOAT;
10615 if (get_float_arg(argvars, &fx) == OK
10616 && get_float_arg(&argvars[1], &fy) == OK)
10617 rettv->vval.v_float = fmod(fx, fy);
10618 else
10619 rettv->vval.v_float = 0.0;
10620}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010621#endif
10622
Bram Moolenaar0d660222005-01-07 21:51:51 +000010623/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010624 * "fnameescape({string})" function
10625 */
10626 static void
10627f_fnameescape(argvars, rettv)
10628 typval_T *argvars;
10629 typval_T *rettv;
10630{
10631 rettv->vval.v_string = vim_strsave_fnameescape(
10632 get_tv_string(&argvars[0]), FALSE);
10633 rettv->v_type = VAR_STRING;
10634}
10635
10636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 * "fnamemodify({fname}, {mods})" function
10638 */
10639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
10644 char_u *fname;
10645 char_u *mods;
10646 int usedlen = 0;
10647 int len;
10648 char_u *fbuf = NULL;
10649 char_u buf[NUMBUFLEN];
10650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010651 fname = get_tv_string_chk(&argvars[0]);
10652 mods = get_tv_string_buf_chk(&argvars[1], buf);
10653 if (fname == NULL || mods == NULL)
10654 fname = NULL;
10655 else
10656 {
10657 len = (int)STRLEN(fname);
10658 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 vim_free(fbuf);
10667}
10668
Bram Moolenaar33570922005-01-25 22:26:29 +000010669static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670
10671/*
10672 * "foldclosed()" function
10673 */
10674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 int end;
10679{
10680#ifdef FEAT_FOLDING
10681 linenr_T lnum;
10682 linenr_T first, last;
10683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10686 {
10687 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10688 {
10689 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 return;
10694 }
10695 }
10696#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698}
10699
10700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010701 * "foldclosed()" function
10702 */
10703 static void
10704f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010705 typval_T *argvars;
10706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707{
10708 foldclosed_both(argvars, rettv, FALSE);
10709}
10710
10711/*
10712 * "foldclosedend()" function
10713 */
10714 static void
10715f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 typval_T *argvars;
10717 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010718{
10719 foldclosed_both(argvars, rettv, TRUE);
10720}
10721
10722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 * "foldlevel()" function
10724 */
10725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010727 typval_T *argvars;
10728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729{
10730#ifdef FEAT_FOLDING
10731 linenr_T lnum;
10732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737}
10738
10739/*
10740 * "foldtext()" function
10741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010744 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
10747#ifdef FEAT_FOLDING
10748 linenr_T lnum;
10749 char_u *s;
10750 char_u *r;
10751 int len;
10752 char *txt;
10753#endif
10754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->v_type = VAR_STRING;
10756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10759 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10760 <= curbuf->b_ml.ml_line_count
10761 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 {
10763 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010764 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10765 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 {
10767 if (!linewhite(lnum))
10768 break;
10769 ++lnum;
10770 }
10771
10772 /* Find interesting text in this line. */
10773 s = skipwhite(ml_get(lnum));
10774 /* skip C comment-start */
10775 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010778 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010780 {
10781 s = skipwhite(ml_get(lnum + 1));
10782 if (*s == '*')
10783 s = skipwhite(s + 1);
10784 }
10785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 txt = _("+-%s%3ld lines: ");
10787 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 + 20 /* for %3ld */
10790 + STRLEN(s))); /* concatenated */
10791 if (r != NULL)
10792 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10794 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10795 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 len = (int)STRLEN(r);
10797 STRCAT(r, s);
10798 /* remove 'foldmarker' and 'commentstring' */
10799 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 }
10802 }
10803#endif
10804}
10805
10806/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010807 * "foldtextresult(lnum)" function
10808 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010811 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010812 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010813{
10814#ifdef FEAT_FOLDING
10815 linenr_T lnum;
10816 char_u *text;
10817 char_u buf[51];
10818 foldinfo_T foldinfo;
10819 int fold_count;
10820#endif
10821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822 rettv->v_type = VAR_STRING;
10823 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010824#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 /* treat illegal types and illegal string values for {lnum} the same */
10827 if (lnum < 0)
10828 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010829 fold_count = foldedCount(curwin, lnum, &foldinfo);
10830 if (fold_count > 0)
10831 {
10832 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10833 &foldinfo, buf);
10834 if (text == buf)
10835 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010837 }
10838#endif
10839}
10840
10841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 * "foreground()" function
10843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010846 typval_T *argvars UNUSED;
10847 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#ifdef FEAT_GUI
10850 if (gui.in_use)
10851 gui_mch_set_foreground();
10852#else
10853# ifdef WIN32
10854 win32_set_foreground();
10855# endif
10856#endif
10857}
10858
10859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010860 * "function()" function
10861 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010864 typval_T *argvars;
10865 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010866{
10867 char_u *s;
10868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010870 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010872 /* Don't check an autoload name for existence here. */
10873 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010874 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 else
10876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 rettv->vval.v_string = vim_strsave(s);
10878 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010879 }
10880}
10881
10882/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010883 * "garbagecollect()" function
10884 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010885 static void
10886f_garbagecollect(argvars, rettv)
10887 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010888 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010889{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010890 /* This is postponed until we are back at the toplevel, because we may be
10891 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10892 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010893
10894 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10895 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010896}
10897
10898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010899 * "get()" function
10900 */
10901 static void
10902f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010905{
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 listitem_T *li;
10907 list_T *l;
10908 dictitem_T *di;
10909 dict_T *d;
10910 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010911
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 int error = FALSE;
10917
10918 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10919 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010921 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010922 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923 else if (argvars[0].v_type == VAR_DICT)
10924 {
10925 if ((d = argvars[0].vval.v_dict) != NULL)
10926 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010927 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010928 if (di != NULL)
10929 tv = &di->di_tv;
10930 }
10931 }
10932 else
10933 EMSG2(_(e_listdictarg), "get()");
10934
10935 if (tv == NULL)
10936 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010937 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010938 copy_tv(&argvars[2], rettv);
10939 }
10940 else
10941 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010942}
10943
Bram Moolenaar342337a2005-07-21 21:11:17 +000010944static 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 +000010945
10946/*
10947 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010948 * Return a range (from start to end) of lines in rettv from the specified
10949 * buffer.
10950 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010951 */
10952 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010953get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010954 buf_T *buf;
10955 linenr_T start;
10956 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010957 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010958 typval_T *rettv;
10959{
10960 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010961
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010962 if (retlist && rettv_list_alloc(rettv) == FAIL)
10963 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010964
10965 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10966 return;
10967
10968 if (!retlist)
10969 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010970 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10971 p = ml_get_buf(buf, start, FALSE);
10972 else
10973 p = (char_u *)"";
10974
10975 rettv->v_type = VAR_STRING;
10976 rettv->vval.v_string = vim_strsave(p);
10977 }
10978 else
10979 {
10980 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010981 return;
10982
10983 if (start < 1)
10984 start = 1;
10985 if (end > buf->b_ml.ml_line_count)
10986 end = buf->b_ml.ml_line_count;
10987 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010988 if (list_append_string(rettv->vval.v_list,
10989 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010990 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010991 }
10992}
10993
10994/*
10995 * "getbufline()" function
10996 */
10997 static void
10998f_getbufline(argvars, rettv)
10999 typval_T *argvars;
11000 typval_T *rettv;
11001{
11002 linenr_T lnum;
11003 linenr_T end;
11004 buf_T *buf;
11005
11006 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11007 ++emsg_off;
11008 buf = get_buf_tv(&argvars[0]);
11009 --emsg_off;
11010
Bram Moolenaar661b1822005-07-28 22:36:45 +000011011 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011012 if (argvars[2].v_type == VAR_UNKNOWN)
11013 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011014 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011015 end = get_tv_lnum_buf(&argvars[2], buf);
11016
Bram Moolenaar342337a2005-07-21 21:11:17 +000011017 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011018}
11019
Bram Moolenaar0d660222005-01-07 21:51:51 +000011020/*
11021 * "getbufvar()" function
11022 */
11023 static void
11024f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011025 typval_T *argvars;
11026 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011027{
11028 buf_T *buf;
11029 buf_T *save_curbuf;
11030 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11034 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011035 ++emsg_off;
11036 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011037
11038 rettv->v_type = VAR_STRING;
11039 rettv->vval.v_string = NULL;
11040
11041 if (buf != NULL && varname != NULL)
11042 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011043 /* set curbuf to be our buf, temporarily */
11044 save_curbuf = curbuf;
11045 curbuf = buf;
11046
Bram Moolenaar0d660222005-01-07 21:51:51 +000011047 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011048 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011049 else if (STRCMP(varname, "changedtick") == 0)
11050 {
11051 rettv->v_type = VAR_NUMBER;
11052 rettv->vval.v_number = curbuf->b_changedtick;
11053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011054 else
11055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 if (*varname == NUL)
11057 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11058 * scope prefix before the NUL byte is required by
11059 * find_var_in_ht(). */
11060 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011061 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011062 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011063 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011065 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011066
11067 /* restore previous notion of curbuf */
11068 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011069 }
11070
11071 --emsg_off;
11072}
11073
11074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 * "getchar()" function
11076 */
11077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 typval_T *argvars;
11080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081{
11082 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011085 /* Position the cursor. Needed after a message that ends in a space. */
11086 windgoto(msg_row, msg_col);
11087
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 ++no_mapping;
11089 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011090 for (;;)
11091 {
11092 if (argvars[0].v_type == VAR_UNKNOWN)
11093 /* getchar(): blocking wait. */
11094 n = safe_vgetc();
11095 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11096 /* getchar(1): only check if char avail */
11097 n = vpeekc();
11098 else if (error || vpeekc() == NUL)
11099 /* illegal argument or getchar(0) and no char avail: return zero */
11100 n = 0;
11101 else
11102 /* getchar(0) and char avail: return char */
11103 n = safe_vgetc();
11104 if (n == K_IGNORE)
11105 continue;
11106 break;
11107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 --no_mapping;
11109 --allow_keys;
11110
Bram Moolenaar219b8702006-11-01 14:32:36 +000011111 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11112 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11113 vimvars[VV_MOUSE_COL].vv_nr = 0;
11114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 if (IS_SPECIAL(n) || mod_mask != 0)
11117 {
11118 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11119 int i = 0;
11120
11121 /* Turn a special key into three bytes, plus modifier. */
11122 if (mod_mask != 0)
11123 {
11124 temp[i++] = K_SPECIAL;
11125 temp[i++] = KS_MODIFIER;
11126 temp[i++] = mod_mask;
11127 }
11128 if (IS_SPECIAL(n))
11129 {
11130 temp[i++] = K_SPECIAL;
11131 temp[i++] = K_SECOND(n);
11132 temp[i++] = K_THIRD(n);
11133 }
11134#ifdef FEAT_MBYTE
11135 else if (has_mbyte)
11136 i += (*mb_char2bytes)(n, temp + i);
11137#endif
11138 else
11139 temp[i++] = n;
11140 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 rettv->v_type = VAR_STRING;
11142 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011143
11144#ifdef FEAT_MOUSE
11145 if (n == K_LEFTMOUSE
11146 || n == K_LEFTMOUSE_NM
11147 || n == K_LEFTDRAG
11148 || n == K_LEFTRELEASE
11149 || n == K_LEFTRELEASE_NM
11150 || n == K_MIDDLEMOUSE
11151 || n == K_MIDDLEDRAG
11152 || n == K_MIDDLERELEASE
11153 || n == K_RIGHTMOUSE
11154 || n == K_RIGHTDRAG
11155 || n == K_RIGHTRELEASE
11156 || n == K_X1MOUSE
11157 || n == K_X1DRAG
11158 || n == K_X1RELEASE
11159 || n == K_X2MOUSE
11160 || n == K_X2DRAG
11161 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011162 || n == K_MOUSELEFT
11163 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011164 || n == K_MOUSEDOWN
11165 || n == K_MOUSEUP)
11166 {
11167 int row = mouse_row;
11168 int col = mouse_col;
11169 win_T *win;
11170 linenr_T lnum;
11171# ifdef FEAT_WINDOWS
11172 win_T *wp;
11173# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011174 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011175
11176 if (row >= 0 && col >= 0)
11177 {
11178 /* Find the window at the mouse coordinates and compute the
11179 * text position. */
11180 win = mouse_find_win(&row, &col);
11181 (void)mouse_comp_pos(win, &row, &col, &lnum);
11182# ifdef FEAT_WINDOWS
11183 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011184 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011185# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011186 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011187 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11188 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11189 }
11190 }
11191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 }
11193}
11194
11195/*
11196 * "getcharmod()" function
11197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011200 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204}
11205
11206/*
11207 * "getcmdline()" function
11208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011211 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 rettv->v_type = VAR_STRING;
11215 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216}
11217
11218/*
11219 * "getcmdpos()" function
11220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011223 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011230 * "getcmdtype()" function
11231 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011232 static void
11233f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011234 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011235 typval_T *rettv;
11236{
11237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = alloc(2);
11239 if (rettv->vval.v_string != NULL)
11240 {
11241 rettv->vval.v_string[0] = get_cmdline_type();
11242 rettv->vval.v_string[1] = NUL;
11243 }
11244}
11245
11246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 * "getcwd()" function
11248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011251 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011254 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011257 rettv->vval.v_string = NULL;
11258 cwd = alloc(MAXPATHL);
11259 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011261 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11262 {
11263 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011265 if (rettv->vval.v_string != NULL)
11266 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011268 }
11269 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 }
11271}
11272
11273/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011274 * "getfontname()" function
11275 */
11276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011278 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011279 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->v_type = VAR_STRING;
11282 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011283#ifdef FEAT_GUI
11284 if (gui.in_use)
11285 {
11286 GuiFont font;
11287 char_u *name = NULL;
11288
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011289 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011290 {
11291 /* Get the "Normal" font. Either the name saved by
11292 * hl_set_font_name() or from the font ID. */
11293 font = gui.norm_font;
11294 name = hl_get_font_name();
11295 }
11296 else
11297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011299 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11300 return;
11301 font = gui_mch_get_font(name, FALSE);
11302 if (font == NOFONT)
11303 return; /* Invalid font name, return empty string. */
11304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011306 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011307 gui_mch_free_font(font);
11308 }
11309#endif
11310}
11311
11312/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011313 * "getfperm({fname})" function
11314 */
11315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 typval_T *argvars;
11318 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011319{
11320 char_u *fname;
11321 struct stat st;
11322 char_u *perm = NULL;
11323 char_u flags[] = "rwx";
11324 int i;
11325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011329 if (mch_stat((char *)fname, &st) >= 0)
11330 {
11331 perm = vim_strsave((char_u *)"---------");
11332 if (perm != NULL)
11333 {
11334 for (i = 0; i < 9; i++)
11335 {
11336 if (st.st_mode & (1 << (8 - i)))
11337 perm[i] = flags[i % 3];
11338 }
11339 }
11340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011342}
11343
11344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 * "getfsize({fname})" function
11346 */
11347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 typval_T *argvars;
11350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351{
11352 char_u *fname;
11353 struct stat st;
11354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358
11359 if (mch_stat((char *)fname, &st) >= 0)
11360 {
11361 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011366
11367 /* non-perfect check for overflow */
11368 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11369 rettv->vval.v_number = -2;
11370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 }
11372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374}
11375
11376/*
11377 * "getftime({fname})" function
11378 */
11379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011381 typval_T *argvars;
11382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
11384 char_u *fname;
11385 struct stat st;
11386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388
11389 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393}
11394
11395/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011396 * "getftype({fname})" function
11397 */
11398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *argvars;
11401 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011402{
11403 char_u *fname;
11404 struct stat st;
11405 char_u *type = NULL;
11406 char *t;
11407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011411 if (mch_lstat((char *)fname, &st) >= 0)
11412 {
11413#ifdef S_ISREG
11414 if (S_ISREG(st.st_mode))
11415 t = "file";
11416 else if (S_ISDIR(st.st_mode))
11417 t = "dir";
11418# ifdef S_ISLNK
11419 else if (S_ISLNK(st.st_mode))
11420 t = "link";
11421# endif
11422# ifdef S_ISBLK
11423 else if (S_ISBLK(st.st_mode))
11424 t = "bdev";
11425# endif
11426# ifdef S_ISCHR
11427 else if (S_ISCHR(st.st_mode))
11428 t = "cdev";
11429# endif
11430# ifdef S_ISFIFO
11431 else if (S_ISFIFO(st.st_mode))
11432 t = "fifo";
11433# endif
11434# ifdef S_ISSOCK
11435 else if (S_ISSOCK(st.st_mode))
11436 t = "fifo";
11437# endif
11438 else
11439 t = "other";
11440#else
11441# ifdef S_IFMT
11442 switch (st.st_mode & S_IFMT)
11443 {
11444 case S_IFREG: t = "file"; break;
11445 case S_IFDIR: t = "dir"; break;
11446# ifdef S_IFLNK
11447 case S_IFLNK: t = "link"; break;
11448# endif
11449# ifdef S_IFBLK
11450 case S_IFBLK: t = "bdev"; break;
11451# endif
11452# ifdef S_IFCHR
11453 case S_IFCHR: t = "cdev"; break;
11454# endif
11455# ifdef S_IFIFO
11456 case S_IFIFO: t = "fifo"; break;
11457# endif
11458# ifdef S_IFSOCK
11459 case S_IFSOCK: t = "socket"; break;
11460# endif
11461 default: t = "other";
11462 }
11463# else
11464 if (mch_isdir(fname))
11465 t = "dir";
11466 else
11467 t = "file";
11468# endif
11469#endif
11470 type = vim_strsave((char_u *)t);
11471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011473}
11474
11475/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011476 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011477 */
11478 static void
11479f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011480 typval_T *argvars;
11481 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011482{
11483 linenr_T lnum;
11484 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011485 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011486
11487 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011488 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011489 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011490 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011491 retlist = FALSE;
11492 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011494 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011496 retlist = TRUE;
11497 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011498
Bram Moolenaar342337a2005-07-21 21:11:17 +000011499 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500}
11501
11502/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011503 * "getmatches()" function
11504 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011505 static void
11506f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011507 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011508 typval_T *rettv;
11509{
11510#ifdef FEAT_SEARCH_EXTRA
11511 dict_T *dict;
11512 matchitem_T *cur = curwin->w_match_head;
11513
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011514 if (rettv_list_alloc(rettv) == OK)
11515 {
11516 while (cur != NULL)
11517 {
11518 dict = dict_alloc();
11519 if (dict == NULL)
11520 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011521 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11522 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11523 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11524 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11525 list_append_dict(rettv->vval.v_list, dict);
11526 cur = cur->next;
11527 }
11528 }
11529#endif
11530}
11531
11532/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011533 * "getpid()" function
11534 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011535 static void
11536f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011537 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011538 typval_T *rettv;
11539{
11540 rettv->vval.v_number = mch_get_pid();
11541}
11542
11543/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011544 * "getpos(string)" function
11545 */
11546 static void
11547f_getpos(argvars, rettv)
11548 typval_T *argvars;
11549 typval_T *rettv;
11550{
11551 pos_T *fp;
11552 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011553 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011554
11555 if (rettv_list_alloc(rettv) == OK)
11556 {
11557 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011558 fp = var2fpos(&argvars[0], TRUE, &fnum);
11559 if (fnum != -1)
11560 list_append_number(l, (varnumber_T)fnum);
11561 else
11562 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011563 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11564 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011565 list_append_number(l, (fp != NULL)
11566 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011567 : (varnumber_T)0);
11568 list_append_number(l,
11569#ifdef FEAT_VIRTUALEDIT
11570 (fp != NULL) ? (varnumber_T)fp->coladd :
11571#endif
11572 (varnumber_T)0);
11573 }
11574 else
11575 rettv->vval.v_number = FALSE;
11576}
11577
11578/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011579 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011580 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011581 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011582f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011583 typval_T *argvars UNUSED;
11584 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011585{
11586#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011587 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011588#endif
11589
Bram Moolenaar2641f772005-03-25 21:58:17 +000011590#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011591 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011592 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011593 wp = NULL;
11594 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11595 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011596 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011597 if (wp == NULL)
11598 return;
11599 }
11600
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011601 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011602 }
11603#endif
11604}
11605
11606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 * "getreg()" function
11608 */
11609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 typval_T *argvars;
11612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613{
11614 char_u *strregname;
11615 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011616 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011617 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011619 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 strregname = get_tv_string_chk(&argvars[0]);
11622 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011623 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011627 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 regname = (strregname == NULL ? '"' : *strregname);
11629 if (regname == 0)
11630 regname = '"';
11631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 rettv->vval.v_string = error ? NULL :
11634 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635}
11636
11637/*
11638 * "getregtype()" function
11639 */
11640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 typval_T *argvars;
11643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644{
11645 char_u *strregname;
11646 int regname;
11647 char_u buf[NUMBUFLEN + 2];
11648 long reglen = 0;
11649
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011650 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 {
11652 strregname = get_tv_string_chk(&argvars[0]);
11653 if (strregname == NULL) /* type error; errmsg already given */
11654 {
11655 rettv->v_type = VAR_STRING;
11656 rettv->vval.v_string = NULL;
11657 return;
11658 }
11659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
11661 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011662 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663
11664 regname = (strregname == NULL ? '"' : *strregname);
11665 if (regname == 0)
11666 regname = '"';
11667
11668 buf[0] = NUL;
11669 buf[1] = NUL;
11670 switch (get_reg_type(regname, &reglen))
11671 {
11672 case MLINE: buf[0] = 'V'; break;
11673 case MCHAR: buf[0] = 'v'; break;
11674#ifdef FEAT_VISUAL
11675 case MBLOCK:
11676 buf[0] = Ctrl_V;
11677 sprintf((char *)buf + 1, "%ld", reglen + 1);
11678 break;
11679#endif
11680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->v_type = VAR_STRING;
11682 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683}
11684
11685/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011686 * "gettabvar()" function
11687 */
11688 static void
11689f_gettabvar(argvars, rettv)
11690 typval_T *argvars;
11691 typval_T *rettv;
11692{
11693 tabpage_T *tp;
11694 dictitem_T *v;
11695 char_u *varname;
11696
11697 rettv->v_type = VAR_STRING;
11698 rettv->vval.v_string = NULL;
11699
11700 varname = get_tv_string_chk(&argvars[1]);
11701 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11702 if (tp != NULL && varname != NULL)
11703 {
11704 /* look up the variable */
11705 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11706 if (v != NULL)
11707 copy_tv(&v->di_tv, rettv);
11708 }
11709}
11710
11711/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011712 * "gettabwinvar()" function
11713 */
11714 static void
11715f_gettabwinvar(argvars, rettv)
11716 typval_T *argvars;
11717 typval_T *rettv;
11718{
11719 getwinvar(argvars, rettv, 1);
11720}
11721
11722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 * "getwinposx()" function
11724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011727 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731#ifdef FEAT_GUI
11732 if (gui.in_use)
11733 {
11734 int x, y;
11735
11736 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 }
11739#endif
11740}
11741
11742/*
11743 * "getwinposy()" function
11744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011746f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751#ifdef FEAT_GUI
11752 if (gui.in_use)
11753 {
11754 int x, y;
11755
11756 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 }
11759#endif
11760}
11761
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011762/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011763 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011764 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011765 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011766find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011767 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011768 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011769{
11770#ifdef FEAT_WINDOWS
11771 win_T *wp;
11772#endif
11773 int nr;
11774
11775 nr = get_tv_number_chk(vp, NULL);
11776
11777#ifdef FEAT_WINDOWS
11778 if (nr < 0)
11779 return NULL;
11780 if (nr == 0)
11781 return curwin;
11782
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011783 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11784 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011785 if (--nr <= 0)
11786 break;
11787 return wp;
11788#else
11789 if (nr == 0 || nr == 1)
11790 return curwin;
11791 return NULL;
11792#endif
11793}
11794
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795/*
11796 * "getwinvar()" function
11797 */
11798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011800 typval_T *argvars;
11801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011803 getwinvar(argvars, rettv, 0);
11804}
11805
11806/*
11807 * getwinvar() and gettabwinvar()
11808 */
11809 static void
11810getwinvar(argvars, rettv, off)
11811 typval_T *argvars;
11812 typval_T *rettv;
11813 int off; /* 1 for gettabwinvar() */
11814{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 win_T *win, *oldcurwin;
11816 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011817 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011818 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011820#ifdef FEAT_WINDOWS
11821 if (off == 1)
11822 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11823 else
11824 tp = curtab;
11825#endif
11826 win = find_win_by_nr(&argvars[off], tp);
11827 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
11831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832
11833 if (win != NULL && varname != NULL)
11834 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011835 /* Set curwin to be our win, temporarily. Also set curbuf, so
11836 * that we can get buffer-local options. */
11837 oldcurwin = curwin;
11838 curwin = win;
11839 curbuf = win->w_buffer;
11840
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 else
11844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 if (*varname == NUL)
11846 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11847 * scope prefix before the NUL byte is required by
11848 * find_var_in_ht(). */
11849 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011851 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011853 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011855
11856 /* restore previous notion of curwin */
11857 curwin = oldcurwin;
11858 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 }
11860
11861 --emsg_off;
11862}
11863
11864/*
11865 * "glob()" function
11866 */
11867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011869 typval_T *argvars;
11870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011872 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011874 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011876 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011877 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011879 if (argvars[1].v_type != VAR_UNKNOWN)
11880 {
11881 if (get_tv_number_chk(&argvars[1], &error))
11882 options |= WILD_KEEP_ALL;
11883 if (argvars[2].v_type != VAR_UNKNOWN
11884 && get_tv_number_chk(&argvars[2], &error))
11885 {
11886 rettv->v_type = VAR_LIST;
11887 rettv->vval.v_list = NULL;
11888 }
11889 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011890 if (!error)
11891 {
11892 ExpandInit(&xpc);
11893 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011894 if (p_wic)
11895 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011896 if (rettv->v_type == VAR_STRING)
11897 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011898 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011899 else if (rettv_list_alloc(rettv) != FAIL)
11900 {
11901 int i;
11902
11903 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11904 NULL, options, WILD_ALL_KEEP);
11905 for (i = 0; i < xpc.xp_numfiles; i++)
11906 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11907
11908 ExpandCleanup(&xpc);
11909 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011910 }
11911 else
11912 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913}
11914
11915/*
11916 * "globpath()" function
11917 */
11918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011920 typval_T *argvars;
11921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011923 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011925 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011928 /* When the optional second argument is non-zero, don't remove matches
11929 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11930 if (argvars[2].v_type != VAR_UNKNOWN
11931 && get_tv_number_chk(&argvars[2], &error))
11932 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011934 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011935 rettv->vval.v_string = NULL;
11936 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011937 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11938 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939}
11940
11941/*
11942 * "has()" function
11943 */
11944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 typval_T *argvars;
11947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948{
11949 int i;
11950 char_u *name;
11951 int n = FALSE;
11952 static char *(has_list[]) =
11953 {
11954#ifdef AMIGA
11955 "amiga",
11956# ifdef FEAT_ARP
11957 "arp",
11958# endif
11959#endif
11960#ifdef __BEOS__
11961 "beos",
11962#endif
11963#ifdef MSDOS
11964# ifdef DJGPP
11965 "dos32",
11966# else
11967 "dos16",
11968# endif
11969#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011970#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 "mac",
11972#endif
11973#if defined(MACOS_X_UNIX)
11974 "macunix",
11975#endif
11976#ifdef OS2
11977 "os2",
11978#endif
11979#ifdef __QNX__
11980 "qnx",
11981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982#ifdef UNIX
11983 "unix",
11984#endif
11985#ifdef VMS
11986 "vms",
11987#endif
11988#ifdef WIN16
11989 "win16",
11990#endif
11991#ifdef WIN32
11992 "win32",
11993#endif
11994#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11995 "win32unix",
11996#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011997#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 "win64",
11999#endif
12000#ifdef EBCDIC
12001 "ebcdic",
12002#endif
12003#ifndef CASE_INSENSITIVE_FILENAME
12004 "fname_case",
12005#endif
12006#ifdef FEAT_ARABIC
12007 "arabic",
12008#endif
12009#ifdef FEAT_AUTOCMD
12010 "autocmd",
12011#endif
12012#ifdef FEAT_BEVAL
12013 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012014# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12015 "balloon_multiline",
12016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017#endif
12018#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12019 "builtin_terms",
12020# ifdef ALL_BUILTIN_TCAPS
12021 "all_builtin_terms",
12022# endif
12023#endif
12024#ifdef FEAT_BYTEOFF
12025 "byte_offset",
12026#endif
12027#ifdef FEAT_CINDENT
12028 "cindent",
12029#endif
12030#ifdef FEAT_CLIENTSERVER
12031 "clientserver",
12032#endif
12033#ifdef FEAT_CLIPBOARD
12034 "clipboard",
12035#endif
12036#ifdef FEAT_CMDL_COMPL
12037 "cmdline_compl",
12038#endif
12039#ifdef FEAT_CMDHIST
12040 "cmdline_hist",
12041#endif
12042#ifdef FEAT_COMMENTS
12043 "comments",
12044#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012045#ifdef FEAT_CONCEAL
12046 "conceal",
12047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048#ifdef FEAT_CRYPT
12049 "cryptv",
12050#endif
12051#ifdef FEAT_CSCOPE
12052 "cscope",
12053#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012054#ifdef FEAT_CURSORBIND
12055 "cursorbind",
12056#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012057#ifdef CURSOR_SHAPE
12058 "cursorshape",
12059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060#ifdef DEBUG
12061 "debug",
12062#endif
12063#ifdef FEAT_CON_DIALOG
12064 "dialog_con",
12065#endif
12066#ifdef FEAT_GUI_DIALOG
12067 "dialog_gui",
12068#endif
12069#ifdef FEAT_DIFF
12070 "diff",
12071#endif
12072#ifdef FEAT_DIGRAPHS
12073 "digraphs",
12074#endif
12075#ifdef FEAT_DND
12076 "dnd",
12077#endif
12078#ifdef FEAT_EMACS_TAGS
12079 "emacs_tags",
12080#endif
12081 "eval", /* always present, of course! */
12082#ifdef FEAT_EX_EXTRA
12083 "ex_extra",
12084#endif
12085#ifdef FEAT_SEARCH_EXTRA
12086 "extra_search",
12087#endif
12088#ifdef FEAT_FKMAP
12089 "farsi",
12090#endif
12091#ifdef FEAT_SEARCHPATH
12092 "file_in_path",
12093#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012094#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012095 "filterpipe",
12096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097#ifdef FEAT_FIND_ID
12098 "find_in_path",
12099#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012100#ifdef FEAT_FLOAT
12101 "float",
12102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#ifdef FEAT_FOLDING
12104 "folding",
12105#endif
12106#ifdef FEAT_FOOTER
12107 "footer",
12108#endif
12109#if !defined(USE_SYSTEM) && defined(UNIX)
12110 "fork",
12111#endif
12112#ifdef FEAT_GETTEXT
12113 "gettext",
12114#endif
12115#ifdef FEAT_GUI
12116 "gui",
12117#endif
12118#ifdef FEAT_GUI_ATHENA
12119# ifdef FEAT_GUI_NEXTAW
12120 "gui_neXtaw",
12121# else
12122 "gui_athena",
12123# endif
12124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125#ifdef FEAT_GUI_GTK
12126 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012129#ifdef FEAT_GUI_GNOME
12130 "gui_gnome",
12131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132#ifdef FEAT_GUI_MAC
12133 "gui_mac",
12134#endif
12135#ifdef FEAT_GUI_MOTIF
12136 "gui_motif",
12137#endif
12138#ifdef FEAT_GUI_PHOTON
12139 "gui_photon",
12140#endif
12141#ifdef FEAT_GUI_W16
12142 "gui_win16",
12143#endif
12144#ifdef FEAT_GUI_W32
12145 "gui_win32",
12146#endif
12147#ifdef FEAT_HANGULIN
12148 "hangul_input",
12149#endif
12150#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12151 "iconv",
12152#endif
12153#ifdef FEAT_INS_EXPAND
12154 "insert_expand",
12155#endif
12156#ifdef FEAT_JUMPLIST
12157 "jumplist",
12158#endif
12159#ifdef FEAT_KEYMAP
12160 "keymap",
12161#endif
12162#ifdef FEAT_LANGMAP
12163 "langmap",
12164#endif
12165#ifdef FEAT_LIBCALL
12166 "libcall",
12167#endif
12168#ifdef FEAT_LINEBREAK
12169 "linebreak",
12170#endif
12171#ifdef FEAT_LISP
12172 "lispindent",
12173#endif
12174#ifdef FEAT_LISTCMDS
12175 "listcmds",
12176#endif
12177#ifdef FEAT_LOCALMAP
12178 "localmap",
12179#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012180#ifdef FEAT_LUA
12181# ifndef DYNAMIC_LUA
12182 "lua",
12183# endif
12184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185#ifdef FEAT_MENU
12186 "menu",
12187#endif
12188#ifdef FEAT_SESSION
12189 "mksession",
12190#endif
12191#ifdef FEAT_MODIFY_FNAME
12192 "modify_fname",
12193#endif
12194#ifdef FEAT_MOUSE
12195 "mouse",
12196#endif
12197#ifdef FEAT_MOUSESHAPE
12198 "mouseshape",
12199#endif
12200#if defined(UNIX) || defined(VMS)
12201# ifdef FEAT_MOUSE_DEC
12202 "mouse_dec",
12203# endif
12204# ifdef FEAT_MOUSE_GPM
12205 "mouse_gpm",
12206# endif
12207# ifdef FEAT_MOUSE_JSB
12208 "mouse_jsbterm",
12209# endif
12210# ifdef FEAT_MOUSE_NET
12211 "mouse_netterm",
12212# endif
12213# ifdef FEAT_MOUSE_PTERM
12214 "mouse_pterm",
12215# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012216# ifdef FEAT_SYSMOUSE
12217 "mouse_sysmouse",
12218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219# ifdef FEAT_MOUSE_XTERM
12220 "mouse_xterm",
12221# endif
12222#endif
12223#ifdef FEAT_MBYTE
12224 "multi_byte",
12225#endif
12226#ifdef FEAT_MBYTE_IME
12227 "multi_byte_ime",
12228#endif
12229#ifdef FEAT_MULTI_LANG
12230 "multi_lang",
12231#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012232#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012233#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012234 "mzscheme",
12235#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237#ifdef FEAT_OLE
12238 "ole",
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef FEAT_PATH_EXTRA
12241 "path_extra",
12242#endif
12243#ifdef FEAT_PERL
12244#ifndef DYNAMIC_PERL
12245 "perl",
12246#endif
12247#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012248#ifdef FEAT_PERSISTENT_UNDO
12249 "persistent_undo",
12250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251#ifdef FEAT_PYTHON
12252#ifndef DYNAMIC_PYTHON
12253 "python",
12254#endif
12255#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012256#ifdef FEAT_PYTHON3
12257#ifndef DYNAMIC_PYTHON3
12258 "python3",
12259#endif
12260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261#ifdef FEAT_POSTSCRIPT
12262 "postscript",
12263#endif
12264#ifdef FEAT_PRINTER
12265 "printer",
12266#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012267#ifdef FEAT_PROFILE
12268 "profile",
12269#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012270#ifdef FEAT_RELTIME
12271 "reltime",
12272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273#ifdef FEAT_QUICKFIX
12274 "quickfix",
12275#endif
12276#ifdef FEAT_RIGHTLEFT
12277 "rightleft",
12278#endif
12279#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12280 "ruby",
12281#endif
12282#ifdef FEAT_SCROLLBIND
12283 "scrollbind",
12284#endif
12285#ifdef FEAT_CMDL_INFO
12286 "showcmd",
12287 "cmdline_info",
12288#endif
12289#ifdef FEAT_SIGNS
12290 "signs",
12291#endif
12292#ifdef FEAT_SMARTINDENT
12293 "smartindent",
12294#endif
12295#ifdef FEAT_SNIFF
12296 "sniff",
12297#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012298#ifdef STARTUPTIME
12299 "startuptime",
12300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301#ifdef FEAT_STL_OPT
12302 "statusline",
12303#endif
12304#ifdef FEAT_SUN_WORKSHOP
12305 "sun_workshop",
12306#endif
12307#ifdef FEAT_NETBEANS_INTG
12308 "netbeans_intg",
12309#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012310#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012311 "spell",
12312#endif
12313#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 "syntax",
12315#endif
12316#if defined(USE_SYSTEM) || !defined(UNIX)
12317 "system",
12318#endif
12319#ifdef FEAT_TAG_BINS
12320 "tag_binary",
12321#endif
12322#ifdef FEAT_TAG_OLDSTATIC
12323 "tag_old_static",
12324#endif
12325#ifdef FEAT_TAG_ANYWHITE
12326 "tag_any_white",
12327#endif
12328#ifdef FEAT_TCL
12329# ifndef DYNAMIC_TCL
12330 "tcl",
12331# endif
12332#endif
12333#ifdef TERMINFO
12334 "terminfo",
12335#endif
12336#ifdef FEAT_TERMRESPONSE
12337 "termresponse",
12338#endif
12339#ifdef FEAT_TEXTOBJ
12340 "textobjects",
12341#endif
12342#ifdef HAVE_TGETENT
12343 "tgetent",
12344#endif
12345#ifdef FEAT_TITLE
12346 "title",
12347#endif
12348#ifdef FEAT_TOOLBAR
12349 "toolbar",
12350#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012351#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12352 "unnamedplus",
12353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#ifdef FEAT_USR_CMDS
12355 "user-commands", /* was accidentally included in 5.4 */
12356 "user_commands",
12357#endif
12358#ifdef FEAT_VIMINFO
12359 "viminfo",
12360#endif
12361#ifdef FEAT_VERTSPLIT
12362 "vertsplit",
12363#endif
12364#ifdef FEAT_VIRTUALEDIT
12365 "virtualedit",
12366#endif
12367#ifdef FEAT_VISUAL
12368 "visual",
12369#endif
12370#ifdef FEAT_VISUALEXTRA
12371 "visualextra",
12372#endif
12373#ifdef FEAT_VREPLACE
12374 "vreplace",
12375#endif
12376#ifdef FEAT_WILDIGN
12377 "wildignore",
12378#endif
12379#ifdef FEAT_WILDMENU
12380 "wildmenu",
12381#endif
12382#ifdef FEAT_WINDOWS
12383 "windows",
12384#endif
12385#ifdef FEAT_WAK
12386 "winaltkeys",
12387#endif
12388#ifdef FEAT_WRITEBACKUP
12389 "writebackup",
12390#endif
12391#ifdef FEAT_XIM
12392 "xim",
12393#endif
12394#ifdef FEAT_XFONTSET
12395 "xfontset",
12396#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012397#ifdef FEAT_XPM_W32
12398 "xpm_w32",
12399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#ifdef USE_XSMP
12401 "xsmp",
12402#endif
12403#ifdef USE_XSMP_INTERACT
12404 "xsmp_interact",
12405#endif
12406#ifdef FEAT_XCLIPBOARD
12407 "xterm_clipboard",
12408#endif
12409#ifdef FEAT_XTERM_SAVE
12410 "xterm_save",
12411#endif
12412#if defined(UNIX) && defined(FEAT_X11)
12413 "X11",
12414#endif
12415 NULL
12416 };
12417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 for (i = 0; has_list[i] != NULL; ++i)
12420 if (STRICMP(name, has_list[i]) == 0)
12421 {
12422 n = TRUE;
12423 break;
12424 }
12425
12426 if (n == FALSE)
12427 {
12428 if (STRNICMP(name, "patch", 5) == 0)
12429 n = has_patch(atoi((char *)name + 5));
12430 else if (STRICMP(name, "vim_starting") == 0)
12431 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012432#ifdef FEAT_MBYTE
12433 else if (STRICMP(name, "multi_byte_encoding") == 0)
12434 n = has_mbyte;
12435#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012436#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12437 else if (STRICMP(name, "balloon_multiline") == 0)
12438 n = multiline_balloon_available();
12439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440#ifdef DYNAMIC_TCL
12441 else if (STRICMP(name, "tcl") == 0)
12442 n = tcl_enabled(FALSE);
12443#endif
12444#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12445 else if (STRICMP(name, "iconv") == 0)
12446 n = iconv_enabled(FALSE);
12447#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012448#ifdef DYNAMIC_LUA
12449 else if (STRICMP(name, "lua") == 0)
12450 n = lua_enabled(FALSE);
12451#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012452#ifdef DYNAMIC_MZSCHEME
12453 else if (STRICMP(name, "mzscheme") == 0)
12454 n = mzscheme_enabled(FALSE);
12455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456#ifdef DYNAMIC_RUBY
12457 else if (STRICMP(name, "ruby") == 0)
12458 n = ruby_enabled(FALSE);
12459#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012460#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461#ifdef DYNAMIC_PYTHON
12462 else if (STRICMP(name, "python") == 0)
12463 n = python_enabled(FALSE);
12464#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012465#endif
12466#ifdef FEAT_PYTHON3
12467#ifdef DYNAMIC_PYTHON3
12468 else if (STRICMP(name, "python3") == 0)
12469 n = python3_enabled(FALSE);
12470#endif
12471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472#ifdef DYNAMIC_PERL
12473 else if (STRICMP(name, "perl") == 0)
12474 n = perl_enabled(FALSE);
12475#endif
12476#ifdef FEAT_GUI
12477 else if (STRICMP(name, "gui_running") == 0)
12478 n = (gui.in_use || gui.starting);
12479# ifdef FEAT_GUI_W32
12480 else if (STRICMP(name, "gui_win32s") == 0)
12481 n = gui_is_win32s();
12482# endif
12483# ifdef FEAT_BROWSE
12484 else if (STRICMP(name, "browse") == 0)
12485 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12486# endif
12487#endif
12488#ifdef FEAT_SYN_HL
12489 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012490 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491#endif
12492#if defined(WIN3264)
12493 else if (STRICMP(name, "win95") == 0)
12494 n = mch_windows95();
12495#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012496#ifdef FEAT_NETBEANS_INTG
12497 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012498 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 }
12501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503}
12504
12505/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012506 * "has_key()" function
12507 */
12508 static void
12509f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 typval_T *argvars;
12511 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012512{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012513 if (argvars[0].v_type != VAR_DICT)
12514 {
12515 EMSG(_(e_dictreq));
12516 return;
12517 }
12518 if (argvars[0].vval.v_dict == NULL)
12519 return;
12520
12521 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012522 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012523}
12524
12525/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012526 * "haslocaldir()" function
12527 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012528 static void
12529f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012530 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012531 typval_T *rettv;
12532{
12533 rettv->vval.v_number = (curwin->w_localdir != NULL);
12534}
12535
12536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 * "hasmapto()" function
12538 */
12539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012541 typval_T *argvars;
12542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543{
12544 char_u *name;
12545 char_u *mode;
12546 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012547 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012550 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 mode = (char_u *)"nvo";
12552 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012555 if (argvars[2].v_type != VAR_UNKNOWN)
12556 abbr = get_tv_number(&argvars[2]);
12557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558
Bram Moolenaar2c932302006-03-18 21:42:09 +000012559 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563}
12564
12565/*
12566 * "histadd()" function
12567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012570 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572{
12573#ifdef FEAT_CMDHIST
12574 int histype;
12575 char_u *str;
12576 char_u buf[NUMBUFLEN];
12577#endif
12578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 if (check_restricted() || check_secure())
12581 return;
12582#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012583 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12584 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 if (histype >= 0)
12586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 if (*str != NUL)
12589 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012590 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593 return;
12594 }
12595 }
12596#endif
12597}
12598
12599/*
12600 * "histdel()" function
12601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012604 typval_T *argvars UNUSED;
12605 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606{
12607#ifdef FEAT_CMDHIST
12608 int n;
12609 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012610 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12613 if (str == NULL)
12614 n = 0;
12615 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012617 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012618 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012620 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 else
12623 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012624 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012625 get_tv_string_buf(&argvars[1], buf));
12626 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627#endif
12628}
12629
12630/*
12631 * "histget()" function
12632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637{
12638#ifdef FEAT_CMDHIST
12639 int type;
12640 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12644 if (str == NULL)
12645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012647 {
12648 type = get_histtype(str);
12649 if (argvars[1].v_type == VAR_UNKNOWN)
12650 idx = get_history_idx(type);
12651 else
12652 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12653 /* -1 on type error */
12654 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660}
12661
12662/*
12663 * "histnr()" function
12664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669{
12670 int i;
12671
12672#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 char_u *history = get_tv_string_chk(&argvars[0]);
12674
12675 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 if (i >= HIST_CMD && i < HIST_COUNT)
12677 i = get_history_idx(i);
12678 else
12679#endif
12680 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682}
12683
12684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 * "highlightID(name)" function
12686 */
12687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012689 typval_T *argvars;
12690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693}
12694
12695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012696 * "highlight_exists()" function
12697 */
12698 static void
12699f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 typval_T *argvars;
12701 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012702{
12703 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12704}
12705
12706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 * "hostname()" function
12708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012711 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
12714 char_u hostname[256];
12715
12716 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 rettv->v_type = VAR_STRING;
12718 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719}
12720
12721/*
12722 * iconv() function
12723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728{
12729#ifdef FEAT_MBYTE
12730 char_u buf1[NUMBUFLEN];
12731 char_u buf2[NUMBUFLEN];
12732 char_u *from, *to, *str;
12733 vimconv_T vimconv;
12734#endif
12735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 rettv->v_type = VAR_STRING;
12737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
12739#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 str = get_tv_string(&argvars[0]);
12741 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12742 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 vimconv.vc_type = CONV_NONE;
12744 convert_setup(&vimconv, from, to);
12745
12746 /* If the encodings are equal, no conversion needed. */
12747 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
12752 convert_setup(&vimconv, NULL, NULL);
12753 vim_free(from);
12754 vim_free(to);
12755#endif
12756}
12757
12758/*
12759 * "indent()" function
12760 */
12761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766 linenr_T lnum;
12767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773}
12774
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012775/*
12776 * "index()" function
12777 */
12778 static void
12779f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012780 typval_T *argvars;
12781 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012782{
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 list_T *l;
12784 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012785 long idx = 0;
12786 int ic = FALSE;
12787
12788 rettv->vval.v_number = -1;
12789 if (argvars[0].v_type != VAR_LIST)
12790 {
12791 EMSG(_(e_listreq));
12792 return;
12793 }
12794 l = argvars[0].vval.v_list;
12795 if (l != NULL)
12796 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012797 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800 int error = FALSE;
12801
Bram Moolenaar758711c2005-02-02 23:11:38 +000012802 /* Start at specified item. Use the cached index that list_find()
12803 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012805 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012806 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 ic = get_tv_number_chk(&argvars[3], &error);
12808 if (error)
12809 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012810 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012811
Bram Moolenaar758711c2005-02-02 23:11:38 +000012812 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012813 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012814 {
12815 rettv->vval.v_number = idx;
12816 break;
12817 }
12818 }
12819}
12820
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821static int inputsecret_flag = 0;
12822
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012823static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12824
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012826 * This function is used by f_input() and f_inputdialog() functions. The third
12827 * argument to f_input() specifies the type of completion to use at the
12828 * prompt. The third argument to f_inputdialog() specifies the value to return
12829 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 */
12831 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012832get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012833 typval_T *argvars;
12834 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012835 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 char_u *p = NULL;
12839 int c;
12840 char_u buf[NUMBUFLEN];
12841 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012842 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012843 int xp_type = EXPAND_NOTHING;
12844 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
12849#ifdef NO_CONSOLE_INPUT
12850 /* While starting up, there is no place to enter text. */
12851 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853#endif
12854
12855 cmd_silent = FALSE; /* Want to see the prompt. */
12856 if (prompt != NULL)
12857 {
12858 /* Only the part of the message after the last NL is considered as
12859 * prompt for the command line */
12860 p = vim_strrchr(prompt, '\n');
12861 if (p == NULL)
12862 p = prompt;
12863 else
12864 {
12865 ++p;
12866 c = *p;
12867 *p = NUL;
12868 msg_start();
12869 msg_clr_eos();
12870 msg_puts_attr(prompt, echo_attr);
12871 msg_didout = FALSE;
12872 msg_starthere();
12873 *p = c;
12874 }
12875 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012877 if (argvars[1].v_type != VAR_UNKNOWN)
12878 {
12879 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12880 if (defstr != NULL)
12881 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012883 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012884 {
12885 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012886 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012887 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012888
Bram Moolenaar4463f292005-09-25 22:20:24 +000012889 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012890
Bram Moolenaar4463f292005-09-25 22:20:24 +000012891 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12892 if (xp_name == NULL)
12893 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012894
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012895 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012896
Bram Moolenaar4463f292005-09-25 22:20:24 +000012897 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12898 &xp_arg) == FAIL)
12899 return;
12900 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012901 }
12902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 if (defstr != NULL)
12904 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012905 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12906 xp_type, xp_arg);
12907
12908 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012910 /* since the user typed this, no need to wait for return */
12911 need_wait_return = FALSE;
12912 msg_didout = FALSE;
12913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 cmd_silent = cmd_silent_save;
12915}
12916
12917/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012918 * "input()" function
12919 * Also handles inputsecret() when inputsecret is set.
12920 */
12921 static void
12922f_input(argvars, rettv)
12923 typval_T *argvars;
12924 typval_T *rettv;
12925{
12926 get_user_input(argvars, rettv, FALSE);
12927}
12928
12929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930 * "inputdialog()" function
12931 */
12932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012934 typval_T *argvars;
12935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936{
12937#if defined(FEAT_GUI_TEXTDIALOG)
12938 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12939 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12940 {
12941 char_u *message;
12942 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012943 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012945 message = get_tv_string_chk(&argvars[0]);
12946 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012947 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012948 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 else
12950 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012951 if (message != NULL && defstr != NULL
12952 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012953 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 else
12956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012957 if (message != NULL && defstr != NULL
12958 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012959 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 rettv->vval.v_string = vim_strsave(
12961 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 }
12967 else
12968#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012969 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970}
12971
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012972/*
12973 * "inputlist()" function
12974 */
12975 static void
12976f_inputlist(argvars, rettv)
12977 typval_T *argvars;
12978 typval_T *rettv;
12979{
12980 listitem_T *li;
12981 int selected;
12982 int mouse_used;
12983
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012984#ifdef NO_CONSOLE_INPUT
12985 /* While starting up, there is no place to enter text. */
12986 if (no_console_input())
12987 return;
12988#endif
12989 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12990 {
12991 EMSG2(_(e_listarg), "inputlist()");
12992 return;
12993 }
12994
12995 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012996 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012997 lines_left = Rows; /* avoid more prompt */
12998 msg_scroll = TRUE;
12999 msg_clr_eos();
13000
13001 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13002 {
13003 msg_puts(get_tv_string(&li->li_tv));
13004 msg_putchar('\n');
13005 }
13006
13007 /* Ask for choice. */
13008 selected = prompt_for_number(&mouse_used);
13009 if (mouse_used)
13010 selected -= lines_left;
13011
13012 rettv->vval.v_number = selected;
13013}
13014
13015
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13017
13018/*
13019 * "inputrestore()" function
13020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013023 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
13026 if (ga_userinput.ga_len > 0)
13027 {
13028 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13030 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013031 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 }
13033 else if (p_verbose > 1)
13034 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013035 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 }
13038}
13039
13040/*
13041 * "inputsave()" function
13042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013045 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013048 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 if (ga_grow(&ga_userinput, 1) == OK)
13050 {
13051 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13052 + ga_userinput.ga_len);
13053 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013054 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 }
13056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058}
13059
13060/*
13061 * "inputsecret()" function
13062 */
13063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 typval_T *argvars;
13066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067{
13068 ++cmdline_star;
13069 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 --cmdline_star;
13072 --inputsecret_flag;
13073}
13074
13075/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013076 * "insert()" function
13077 */
13078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013080 typval_T *argvars;
13081 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013082{
13083 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 listitem_T *item;
13085 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013087
13088 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013089 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013090 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013091 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013092 {
13093 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 before = get_tv_number_chk(&argvars[2], &error);
13095 if (error)
13096 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013097
Bram Moolenaar758711c2005-02-02 23:11:38 +000013098 if (before == l->lv_len)
13099 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013100 else
13101 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013102 item = list_find(l, before);
13103 if (item == NULL)
13104 {
13105 EMSGN(_(e_listidx), before);
13106 l = NULL;
13107 }
13108 }
13109 if (l != NULL)
13110 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013111 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013112 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013113 }
13114 }
13115}
13116
13117/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013118 * "invert(expr)" function
13119 */
13120 static void
13121f_invert(argvars, rettv)
13122 typval_T *argvars;
13123 typval_T *rettv;
13124{
13125 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13126}
13127
13128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 * "isdirectory()" function
13130 */
13131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 typval_T *argvars;
13134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137}
13138
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013139/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013140 * "islocked()" function
13141 */
13142 static void
13143f_islocked(argvars, rettv)
13144 typval_T *argvars;
13145 typval_T *rettv;
13146{
13147 lval_T lv;
13148 char_u *end;
13149 dictitem_T *di;
13150
13151 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013152 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13153 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013154 if (end != NULL && lv.ll_name != NULL)
13155 {
13156 if (*end != NUL)
13157 EMSG(_(e_trailing));
13158 else
13159 {
13160 if (lv.ll_tv == NULL)
13161 {
13162 if (check_changedtick(lv.ll_name))
13163 rettv->vval.v_number = 1; /* always locked */
13164 else
13165 {
13166 di = find_var(lv.ll_name, NULL);
13167 if (di != NULL)
13168 {
13169 /* Consider a variable locked when:
13170 * 1. the variable itself is locked
13171 * 2. the value of the variable is locked.
13172 * 3. the List or Dict value is locked.
13173 */
13174 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13175 || tv_islocked(&di->di_tv));
13176 }
13177 }
13178 }
13179 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013180 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013181 else if (lv.ll_newkey != NULL)
13182 EMSG2(_(e_dictkey), lv.ll_newkey);
13183 else if (lv.ll_list != NULL)
13184 /* List item. */
13185 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13186 else
13187 /* Dictionary item. */
13188 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13189 }
13190 }
13191
13192 clear_lval(&lv);
13193}
13194
Bram Moolenaar33570922005-01-25 22:26:29 +000013195static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013196
13197/*
13198 * Turn a dict into a list:
13199 * "what" == 0: list of keys
13200 * "what" == 1: list of values
13201 * "what" == 2: list of items
13202 */
13203 static void
13204dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013205 typval_T *argvars;
13206 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013207 int what;
13208{
Bram Moolenaar33570922005-01-25 22:26:29 +000013209 list_T *l2;
13210 dictitem_T *di;
13211 hashitem_T *hi;
13212 listitem_T *li;
13213 listitem_T *li2;
13214 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013215 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013216
Bram Moolenaar8c711452005-01-14 21:53:12 +000013217 if (argvars[0].v_type != VAR_DICT)
13218 {
13219 EMSG(_(e_dictreq));
13220 return;
13221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013222 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013223 return;
13224
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013225 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013226 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013227
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013228 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013230 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013231 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013233 --todo;
13234 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013235
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013236 li = listitem_alloc();
13237 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013238 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013239 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013240
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013241 if (what == 0)
13242 {
13243 /* keys() */
13244 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013245 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013246 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13247 }
13248 else if (what == 1)
13249 {
13250 /* values() */
13251 copy_tv(&di->di_tv, &li->li_tv);
13252 }
13253 else
13254 {
13255 /* items() */
13256 l2 = list_alloc();
13257 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013258 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013259 li->li_tv.vval.v_list = l2;
13260 if (l2 == NULL)
13261 break;
13262 ++l2->lv_refcount;
13263
13264 li2 = listitem_alloc();
13265 if (li2 == NULL)
13266 break;
13267 list_append(l2, li2);
13268 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013269 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013270 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13271
13272 li2 = listitem_alloc();
13273 if (li2 == NULL)
13274 break;
13275 list_append(l2, li2);
13276 copy_tv(&di->di_tv, &li2->li_tv);
13277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013278 }
13279 }
13280}
13281
13282/*
13283 * "items(dict)" function
13284 */
13285 static void
13286f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013287 typval_T *argvars;
13288 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013289{
13290 dict_list(argvars, rettv, 2);
13291}
13292
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013294 * "join()" function
13295 */
13296 static void
13297f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 typval_T *argvars;
13299 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013300{
13301 garray_T ga;
13302 char_u *sep;
13303
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013304 if (argvars[0].v_type != VAR_LIST)
13305 {
13306 EMSG(_(e_listreq));
13307 return;
13308 }
13309 if (argvars[0].vval.v_list == NULL)
13310 return;
13311 if (argvars[1].v_type == VAR_UNKNOWN)
13312 sep = (char_u *)" ";
13313 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013315
13316 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317
13318 if (sep != NULL)
13319 {
13320 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013321 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 ga_append(&ga, NUL);
13323 rettv->vval.v_string = (char_u *)ga.ga_data;
13324 }
13325 else
13326 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013327}
13328
13329/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330 * "keys()" function
13331 */
13332 static void
13333f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *argvars;
13335 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013336{
13337 dict_list(argvars, rettv, 0);
13338}
13339
13340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 * "last_buffer_nr()" function.
13342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347{
13348 int n = 0;
13349 buf_T *buf;
13350
13351 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13352 if (n < buf->b_fnum)
13353 n = buf->b_fnum;
13354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356}
13357
13358/*
13359 * "len()" function
13360 */
13361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013365{
13366 switch (argvars[0].v_type)
13367 {
13368 case VAR_STRING:
13369 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->vval.v_number = (varnumber_T)STRLEN(
13371 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013372 break;
13373 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013375 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013376 case VAR_DICT:
13377 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13378 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013380 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013381 break;
13382 }
13383}
13384
Bram Moolenaar33570922005-01-25 22:26:29 +000013385static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013386
13387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013389 typval_T *argvars;
13390 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013391 int type;
13392{
13393#ifdef FEAT_LIBCALL
13394 char_u *string_in;
13395 char_u **string_result;
13396 int nr_result;
13397#endif
13398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013400 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013402
13403 if (check_restricted() || check_secure())
13404 return;
13405
13406#ifdef FEAT_LIBCALL
13407 /* The first two args must be strings, otherwise its meaningless */
13408 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13409 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013410 string_in = NULL;
13411 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013412 string_in = argvars[2].vval.v_string;
13413 if (type == VAR_NUMBER)
13414 string_result = NULL;
13415 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013417 if (mch_libcall(argvars[0].vval.v_string,
13418 argvars[1].vval.v_string,
13419 string_in,
13420 argvars[2].vval.v_number,
13421 string_result,
13422 &nr_result) == OK
13423 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013425 }
13426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427}
13428
13429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013430 * "libcall()" function
13431 */
13432 static void
13433f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 typval_T *argvars;
13435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436{
13437 libcall_common(argvars, rettv, VAR_STRING);
13438}
13439
13440/*
13441 * "libcallnr()" function
13442 */
13443 static void
13444f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 typval_T *argvars;
13446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013447{
13448 libcall_common(argvars, rettv, VAR_NUMBER);
13449}
13450
13451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 * "line(string)" function
13453 */
13454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459 linenr_T lnum = 0;
13460 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013461 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013463 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 if (fp != NULL)
13465 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467}
13468
13469/*
13470 * "line2byte(lnum)" function
13471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013474 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476{
13477#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479#else
13480 linenr_T lnum;
13481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13487 if (rettv->vval.v_number >= 0)
13488 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489#endif
13490}
13491
13492/*
13493 * "lispindent(lnum)" function
13494 */
13495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 typval_T *argvars;
13498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499{
13500#ifdef FEAT_LISP
13501 pos_T pos;
13502 linenr_T lnum;
13503
13504 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13507 {
13508 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 curwin->w_cursor = pos;
13511 }
13512 else
13513#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
13517/*
13518 * "localtime()" function
13519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013522 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526}
13527
Bram Moolenaar33570922005-01-25 22:26:29 +000013528static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529
13530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 int exact;
13535{
13536 char_u *keys;
13537 char_u *which;
13538 char_u buf[NUMBUFLEN];
13539 char_u *keys_buf = NULL;
13540 char_u *rhs;
13541 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013542 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013543 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013544 mapblock_T *mp;
13545 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546
13547 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->v_type = VAR_STRING;
13549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 if (*keys == NUL)
13553 return;
13554
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013555 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013557 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013559 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013560 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013561 if (argvars[3].v_type != VAR_UNKNOWN)
13562 get_dict = get_tv_number(&argvars[3]);
13563 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 else
13566 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013567 if (which == NULL)
13568 return;
13569
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 mode = get_map_mode(&which, 0);
13571
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013572 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013573 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013575
13576 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013578 /* Return a string. */
13579 if (rhs != NULL)
13580 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
Bram Moolenaarbd743252010-10-20 21:23:33 +020013582 }
13583 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13584 {
13585 /* Return a dictionary. */
13586 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13587 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13588 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589
Bram Moolenaarbd743252010-10-20 21:23:33 +020013590 dict_add_nr_str(dict, "lhs", 0L, lhs);
13591 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13592 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13593 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13594 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13595 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13596 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13597 dict_add_nr_str(dict, "mode", 0L, mapmode);
13598
13599 vim_free(lhs);
13600 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 }
13602}
13603
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013604#ifdef FEAT_FLOAT
13605/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013606 * "log()" function
13607 */
13608 static void
13609f_log(argvars, rettv)
13610 typval_T *argvars;
13611 typval_T *rettv;
13612{
13613 float_T f;
13614
13615 rettv->v_type = VAR_FLOAT;
13616 if (get_float_arg(argvars, &f) == OK)
13617 rettv->vval.v_float = log(f);
13618 else
13619 rettv->vval.v_float = 0.0;
13620}
13621
13622/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013623 * "log10()" function
13624 */
13625 static void
13626f_log10(argvars, rettv)
13627 typval_T *argvars;
13628 typval_T *rettv;
13629{
13630 float_T f;
13631
13632 rettv->v_type = VAR_FLOAT;
13633 if (get_float_arg(argvars, &f) == OK)
13634 rettv->vval.v_float = log10(f);
13635 else
13636 rettv->vval.v_float = 0.0;
13637}
13638#endif
13639
Bram Moolenaar1dced572012-04-05 16:54:08 +020013640#ifdef FEAT_LUA
13641/*
13642 * "luaeval()" function
13643 */
13644 static void
13645f_luaeval(argvars, rettv)
13646 typval_T *argvars;
13647 typval_T *rettv;
13648{
13649 char_u *str;
13650 char_u buf[NUMBUFLEN];
13651
13652 str = get_tv_string_buf(&argvars[0], buf);
13653 do_luaeval(str, argvars + 1, rettv);
13654}
13655#endif
13656
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013658 * "map()" function
13659 */
13660 static void
13661f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013662 typval_T *argvars;
13663 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013664{
13665 filter_map(argvars, rettv, TRUE);
13666}
13667
13668/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013669 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 */
13671 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013672f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *argvars;
13674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677}
13678
13679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013680 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 */
13682 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013683f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 typval_T *argvars;
13685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013687 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688}
13689
Bram Moolenaar33570922005-01-25 22:26:29 +000013690static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691
13692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013694 typval_T *argvars;
13695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 int type;
13697{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013698 char_u *str = NULL;
13699 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 char_u *pat;
13701 regmatch_T regmatch;
13702 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013703 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 char_u *save_cpo;
13705 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013706 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013707 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013708 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013709 list_T *l = NULL;
13710 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013711 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013712 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713
13714 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13715 save_cpo = p_cpo;
13716 p_cpo = (char_u *)"";
13717
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013718 rettv->vval.v_number = -1;
13719 if (type == 3)
13720 {
13721 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013722 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013723 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013724 }
13725 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013727 rettv->v_type = VAR_STRING;
13728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013731 if (argvars[0].v_type == VAR_LIST)
13732 {
13733 if ((l = argvars[0].vval.v_list) == NULL)
13734 goto theend;
13735 li = l->lv_first;
13736 }
13737 else
13738 expr = str = get_tv_string(&argvars[0]);
13739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13741 if (pat == NULL)
13742 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013743
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 int error = FALSE;
13747
13748 start = get_tv_number_chk(&argvars[2], &error);
13749 if (error)
13750 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013751 if (l != NULL)
13752 {
13753 li = list_find(l, start);
13754 if (li == NULL)
13755 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013756 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013757 }
13758 else
13759 {
13760 if (start < 0)
13761 start = 0;
13762 if (start > (long)STRLEN(str))
13763 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013764 /* When "count" argument is there ignore matches before "start",
13765 * otherwise skip part of the string. Differs when pattern is "^"
13766 * or "\<". */
13767 if (argvars[3].v_type != VAR_UNKNOWN)
13768 startcol = start;
13769 else
13770 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013771 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013772
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013773 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 nth = get_tv_number_chk(&argvars[3], &error);
13775 if (error)
13776 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 }
13778
13779 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13780 if (regmatch.regprog != NULL)
13781 {
13782 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013783
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013784 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013785 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013786 if (l != NULL)
13787 {
13788 if (li == NULL)
13789 {
13790 match = FALSE;
13791 break;
13792 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013793 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013794 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013795 if (str == NULL)
13796 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013797 }
13798
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013799 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013800
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013801 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013802 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013803 if (l == NULL && !match)
13804 break;
13805
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013806 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013807 if (l != NULL)
13808 {
13809 li = li->li_next;
13810 ++idx;
13811 }
13812 else
13813 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013814#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013815 startcol = (colnr_T)(regmatch.startp[0]
13816 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013817#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013818 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013819#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013821 }
13822
13823 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013825 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013827 int i;
13828
13829 /* return list with matched string and submatches */
13830 for (i = 0; i < NSUBEXP; ++i)
13831 {
13832 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013833 {
13834 if (list_append_string(rettv->vval.v_list,
13835 (char_u *)"", 0) == FAIL)
13836 break;
13837 }
13838 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013839 regmatch.startp[i],
13840 (int)(regmatch.endp[i] - regmatch.startp[i]))
13841 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013842 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013843 }
13844 }
13845 else if (type == 2)
13846 {
13847 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013848 if (l != NULL)
13849 copy_tv(&li->li_tv, rettv);
13850 else
13851 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013853 }
13854 else if (l != NULL)
13855 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 else
13857 {
13858 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013859 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 (varnumber_T)(regmatch.startp[0] - str);
13861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013864 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 }
13866 }
13867 vim_free(regmatch.regprog);
13868 }
13869
13870theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013871 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 p_cpo = save_cpo;
13873}
13874
13875/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013876 * "match()" function
13877 */
13878 static void
13879f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 typval_T *argvars;
13881 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013882{
13883 find_some_match(argvars, rettv, 1);
13884}
13885
13886/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013887 * "matchadd()" function
13888 */
13889 static void
13890f_matchadd(argvars, rettv)
13891 typval_T *argvars;
13892 typval_T *rettv;
13893{
13894#ifdef FEAT_SEARCH_EXTRA
13895 char_u buf[NUMBUFLEN];
13896 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13897 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13898 int prio = 10; /* default priority */
13899 int id = -1;
13900 int error = FALSE;
13901
13902 rettv->vval.v_number = -1;
13903
13904 if (grp == NULL || pat == NULL)
13905 return;
13906 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013907 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013908 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013909 if (argvars[3].v_type != VAR_UNKNOWN)
13910 id = get_tv_number_chk(&argvars[3], &error);
13911 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013912 if (error == TRUE)
13913 return;
13914 if (id >= 1 && id <= 3)
13915 {
13916 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13917 return;
13918 }
13919
13920 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13921#endif
13922}
13923
13924/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013925 * "matcharg()" function
13926 */
13927 static void
13928f_matcharg(argvars, rettv)
13929 typval_T *argvars;
13930 typval_T *rettv;
13931{
13932 if (rettv_list_alloc(rettv) == OK)
13933 {
13934#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013935 int id = get_tv_number(&argvars[0]);
13936 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013937
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013938 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013939 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013940 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13941 {
13942 list_append_string(rettv->vval.v_list,
13943 syn_id2name(m->hlg_id), -1);
13944 list_append_string(rettv->vval.v_list, m->pattern, -1);
13945 }
13946 else
13947 {
13948 list_append_string(rettv->vval.v_list, NUL, -1);
13949 list_append_string(rettv->vval.v_list, NUL, -1);
13950 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013951 }
13952#endif
13953 }
13954}
13955
13956/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013957 * "matchdelete()" function
13958 */
13959 static void
13960f_matchdelete(argvars, rettv)
13961 typval_T *argvars;
13962 typval_T *rettv;
13963{
13964#ifdef FEAT_SEARCH_EXTRA
13965 rettv->vval.v_number = match_delete(curwin,
13966 (int)get_tv_number(&argvars[0]), TRUE);
13967#endif
13968}
13969
13970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971 * "matchend()" function
13972 */
13973 static void
13974f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *argvars;
13976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013977{
13978 find_some_match(argvars, rettv, 0);
13979}
13980
13981/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013982 * "matchlist()" function
13983 */
13984 static void
13985f_matchlist(argvars, rettv)
13986 typval_T *argvars;
13987 typval_T *rettv;
13988{
13989 find_some_match(argvars, rettv, 3);
13990}
13991
13992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993 * "matchstr()" function
13994 */
13995 static void
13996f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013997 typval_T *argvars;
13998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013999{
14000 find_some_match(argvars, rettv, 2);
14001}
14002
Bram Moolenaar33570922005-01-25 22:26:29 +000014003static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014004
14005 static void
14006max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014007 typval_T *argvars;
14008 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014009 int domax;
14010{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014011 long n = 0;
14012 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014013 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014014
14015 if (argvars[0].v_type == VAR_LIST)
14016 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 list_T *l;
14018 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014019
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014020 l = argvars[0].vval.v_list;
14021 if (l != NULL)
14022 {
14023 li = l->lv_first;
14024 if (li != NULL)
14025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014027 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014028 {
14029 li = li->li_next;
14030 if (li == NULL)
14031 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014033 if (domax ? i > n : i < n)
14034 n = i;
14035 }
14036 }
14037 }
14038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014039 else if (argvars[0].v_type == VAR_DICT)
14040 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014041 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014042 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014043 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014044 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014045
14046 d = argvars[0].vval.v_dict;
14047 if (d != NULL)
14048 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014049 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014052 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014054 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014055 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014056 if (first)
14057 {
14058 n = i;
14059 first = FALSE;
14060 }
14061 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014062 n = i;
14063 }
14064 }
14065 }
14066 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014067 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014068 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014070}
14071
14072/*
14073 * "max()" function
14074 */
14075 static void
14076f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 typval_T *argvars;
14078 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014079{
14080 max_min(argvars, rettv, TRUE);
14081}
14082
14083/*
14084 * "min()" function
14085 */
14086 static void
14087f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014088 typval_T *argvars;
14089 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014090{
14091 max_min(argvars, rettv, FALSE);
14092}
14093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014094static int mkdir_recurse __ARGS((char_u *dir, int prot));
14095
14096/*
14097 * Create the directory in which "dir" is located, and higher levels when
14098 * needed.
14099 */
14100 static int
14101mkdir_recurse(dir, prot)
14102 char_u *dir;
14103 int prot;
14104{
14105 char_u *p;
14106 char_u *updir;
14107 int r = FAIL;
14108
14109 /* Get end of directory name in "dir".
14110 * We're done when it's "/" or "c:/". */
14111 p = gettail_sep(dir);
14112 if (p <= get_past_head(dir))
14113 return OK;
14114
14115 /* If the directory exists we're done. Otherwise: create it.*/
14116 updir = vim_strnsave(dir, (int)(p - dir));
14117 if (updir == NULL)
14118 return FAIL;
14119 if (mch_isdir(updir))
14120 r = OK;
14121 else if (mkdir_recurse(updir, prot) == OK)
14122 r = vim_mkdir_emsg(updir, prot);
14123 vim_free(updir);
14124 return r;
14125}
14126
14127#ifdef vim_mkdir
14128/*
14129 * "mkdir()" function
14130 */
14131 static void
14132f_mkdir(argvars, rettv)
14133 typval_T *argvars;
14134 typval_T *rettv;
14135{
14136 char_u *dir;
14137 char_u buf[NUMBUFLEN];
14138 int prot = 0755;
14139
14140 rettv->vval.v_number = FAIL;
14141 if (check_restricted() || check_secure())
14142 return;
14143
14144 dir = get_tv_string_buf(&argvars[0], buf);
14145 if (argvars[1].v_type != VAR_UNKNOWN)
14146 {
14147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 prot = get_tv_number_chk(&argvars[2], NULL);
14149 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014150 mkdir_recurse(dir, prot);
14151 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014153}
14154#endif
14155
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 * "mode()" function
14158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 typval_T *argvars;
14162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014164 char_u buf[3];
14165
14166 buf[1] = NUL;
14167 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168
14169#ifdef FEAT_VISUAL
14170 if (VIsual_active)
14171 {
14172 if (VIsual_select)
14173 buf[0] = VIsual_mode + 's' - 'v';
14174 else
14175 buf[0] = VIsual_mode;
14176 }
14177 else
14178#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014179 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14180 || State == CONFIRM)
14181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014183 if (State == ASKMORE)
14184 buf[1] = 'm';
14185 else if (State == CONFIRM)
14186 buf[1] = '?';
14187 }
14188 else if (State == EXTERNCMD)
14189 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 else if (State & INSERT)
14191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014192#ifdef FEAT_VREPLACE
14193 if (State & VREPLACE_FLAG)
14194 {
14195 buf[0] = 'R';
14196 buf[1] = 'v';
14197 }
14198 else
14199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 if (State & REPLACE_FLAG)
14201 buf[0] = 'R';
14202 else
14203 buf[0] = 'i';
14204 }
14205 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014208 if (exmode_active)
14209 buf[1] = 'v';
14210 }
14211 else if (exmode_active)
14212 {
14213 buf[0] = 'c';
14214 buf[1] = 'e';
14215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014219 if (finish_op)
14220 buf[1] = 'o';
14221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014223 /* Clear out the minor mode when the argument is not a non-zero number or
14224 * non-empty string. */
14225 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014226 buf[1] = NUL;
14227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 rettv->vval.v_string = vim_strsave(buf);
14229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230}
14231
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014232#ifdef FEAT_MZSCHEME
14233/*
14234 * "mzeval()" function
14235 */
14236 static void
14237f_mzeval(argvars, rettv)
14238 typval_T *argvars;
14239 typval_T *rettv;
14240{
14241 char_u *str;
14242 char_u buf[NUMBUFLEN];
14243
14244 str = get_tv_string_buf(&argvars[0], buf);
14245 do_mzeval(str, rettv);
14246}
14247#endif
14248
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250 * "nextnonblank()" function
14251 */
14252 static void
14253f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014254 typval_T *argvars;
14255 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256{
14257 linenr_T lnum;
14258
14259 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014262 {
14263 lnum = 0;
14264 break;
14265 }
14266 if (*skipwhite(ml_get(lnum)) != NUL)
14267 break;
14268 }
14269 rettv->vval.v_number = lnum;
14270}
14271
14272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 * "nr2char()" function
14274 */
14275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
14280 char_u buf[NUMBUFLEN];
14281
14282#ifdef FEAT_MBYTE
14283 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 else
14286#endif
14287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 buf[1] = NUL;
14290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 rettv->v_type = VAR_STRING;
14292 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014293}
14294
14295/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014296 * "or(expr, expr)" function
14297 */
14298 static void
14299f_or(argvars, rettv)
14300 typval_T *argvars;
14301 typval_T *rettv;
14302{
14303 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14304 | get_tv_number_chk(&argvars[1], NULL);
14305}
14306
14307/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014308 * "pathshorten()" function
14309 */
14310 static void
14311f_pathshorten(argvars, rettv)
14312 typval_T *argvars;
14313 typval_T *rettv;
14314{
14315 char_u *p;
14316
14317 rettv->v_type = VAR_STRING;
14318 p = get_tv_string_chk(&argvars[0]);
14319 if (p == NULL)
14320 rettv->vval.v_string = NULL;
14321 else
14322 {
14323 p = vim_strsave(p);
14324 rettv->vval.v_string = p;
14325 if (p != NULL)
14326 shorten_dir(p);
14327 }
14328}
14329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014330#ifdef FEAT_FLOAT
14331/*
14332 * "pow()" function
14333 */
14334 static void
14335f_pow(argvars, rettv)
14336 typval_T *argvars;
14337 typval_T *rettv;
14338{
14339 float_T fx, fy;
14340
14341 rettv->v_type = VAR_FLOAT;
14342 if (get_float_arg(argvars, &fx) == OK
14343 && get_float_arg(&argvars[1], &fy) == OK)
14344 rettv->vval.v_float = pow(fx, fy);
14345 else
14346 rettv->vval.v_float = 0.0;
14347}
14348#endif
14349
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014351 * "prevnonblank()" function
14352 */
14353 static void
14354f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014355 typval_T *argvars;
14356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357{
14358 linenr_T lnum;
14359
14360 lnum = get_tv_lnum(argvars);
14361 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14362 lnum = 0;
14363 else
14364 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14365 --lnum;
14366 rettv->vval.v_number = lnum;
14367}
14368
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014369#ifdef HAVE_STDARG_H
14370/* This dummy va_list is here because:
14371 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14372 * - locally in the function results in a "used before set" warning
14373 * - using va_start() to initialize it gives "function with fixed args" error */
14374static va_list ap;
14375#endif
14376
Bram Moolenaar8c711452005-01-14 21:53:12 +000014377/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014378 * "printf()" function
14379 */
14380 static void
14381f_printf(argvars, rettv)
14382 typval_T *argvars;
14383 typval_T *rettv;
14384{
14385 rettv->v_type = VAR_STRING;
14386 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014387#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014388 {
14389 char_u buf[NUMBUFLEN];
14390 int len;
14391 char_u *s;
14392 int saved_did_emsg = did_emsg;
14393 char *fmt;
14394
14395 /* Get the required length, allocate the buffer and do it for real. */
14396 did_emsg = FALSE;
14397 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014398 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014399 if (!did_emsg)
14400 {
14401 s = alloc(len + 1);
14402 if (s != NULL)
14403 {
14404 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014405 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014406 }
14407 }
14408 did_emsg |= saved_did_emsg;
14409 }
14410#endif
14411}
14412
14413/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014414 * "pumvisible()" function
14415 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014416 static void
14417f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014418 typval_T *argvars UNUSED;
14419 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014420{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014421#ifdef FEAT_INS_EXPAND
14422 if (pum_visible())
14423 rettv->vval.v_number = 1;
14424#endif
14425}
14426
14427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014428 * "range()" function
14429 */
14430 static void
14431f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014432 typval_T *argvars;
14433 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014434{
14435 long start;
14436 long end;
14437 long stride = 1;
14438 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014442 if (argvars[1].v_type == VAR_UNKNOWN)
14443 {
14444 end = start - 1;
14445 start = 0;
14446 }
14447 else
14448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014452 }
14453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 if (error)
14455 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014456 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014457 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014458 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014459 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014460 else
14461 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014462 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014463 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014464 if (list_append_number(rettv->vval.v_list,
14465 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014466 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014467 }
14468}
14469
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014470/*
14471 * "readfile()" function
14472 */
14473 static void
14474f_readfile(argvars, rettv)
14475 typval_T *argvars;
14476 typval_T *rettv;
14477{
14478 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014479 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014480 char_u *fname;
14481 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014482 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14483 int io_size = sizeof(buf);
14484 int readlen; /* size of last fread() */
14485 char_u *prev = NULL; /* previously read bytes, if any */
14486 long prevlen = 0; /* length of data in prev */
14487 long prevsize = 0; /* size of prev buffer */
14488 long maxline = MAXLNUM;
14489 long cnt = 0;
14490 char_u *p; /* position in buf */
14491 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014493 if (argvars[1].v_type != VAR_UNKNOWN)
14494 {
14495 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14496 binary = TRUE;
14497 if (argvars[2].v_type != VAR_UNKNOWN)
14498 maxline = get_tv_number(&argvars[2]);
14499 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014502 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014503
14504 /* Always open the file in binary mode, library functions have a mind of
14505 * their own about CR-LF conversion. */
14506 fname = get_tv_string(&argvars[0]);
14507 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14508 {
14509 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14510 return;
14511 }
14512
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014513 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014514 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014515 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014516
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014517 /* This for loop processes what was read, but is also entered at end
14518 * of file so that either:
14519 * - an incomplete line gets written
14520 * - a "binary" file gets an empty line at the end if it ends in a
14521 * newline. */
14522 for (p = buf, start = buf;
14523 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14524 ++p)
14525 {
14526 if (*p == '\n' || readlen <= 0)
14527 {
14528 listitem_T *li;
14529 char_u *s = NULL;
14530 long_u len = p - start;
14531
14532 /* Finished a line. Remove CRs before NL. */
14533 if (readlen > 0 && !binary)
14534 {
14535 while (len > 0 && start[len - 1] == '\r')
14536 --len;
14537 /* removal may cross back to the "prev" string */
14538 if (len == 0)
14539 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14540 --prevlen;
14541 }
14542 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014543 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014544 else
14545 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014546 /* Change "prev" buffer to be the right size. This way
14547 * the bytes are only copied once, and very long lines are
14548 * allocated only once. */
14549 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014550 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014551 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014552 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014553 prev = NULL; /* the list will own the string */
14554 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014555 }
14556 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014557 if (s == NULL)
14558 {
14559 do_outofmem_msg((long_u) prevlen + len + 1);
14560 failed = TRUE;
14561 break;
14562 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014563
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014564 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565 {
14566 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014567 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014568 break;
14569 }
14570 li->li_tv.v_type = VAR_STRING;
14571 li->li_tv.v_lock = 0;
14572 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014573 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014574
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014575 start = p + 1; /* step over newline */
14576 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014577 break;
14578 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014579 else if (*p == NUL)
14580 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014581#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014582 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14583 * when finding the BF and check the previous two bytes. */
14584 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014585 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014586 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14587 * + 1, these may be in the "prev" string. */
14588 char_u back1 = p >= buf + 1 ? p[-1]
14589 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14590 char_u back2 = p >= buf + 2 ? p[-2]
14591 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14592 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014594 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014595 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014596 char_u *dest = p - 2;
14597
14598 /* Usually a BOM is at the beginning of a file, and so at
14599 * the beginning of a line; then we can just step over it.
14600 */
14601 if (start == dest)
14602 start = p + 1;
14603 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014604 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014605 /* have to shuffle buf to close gap */
14606 int adjust_prevlen = 0;
14607
14608 if (dest < buf)
14609 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014610 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014611 dest = buf;
14612 }
14613 if (readlen > p - buf + 1)
14614 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14615 readlen -= 3 - adjust_prevlen;
14616 prevlen -= adjust_prevlen;
14617 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014618 }
14619 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014620 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014621#endif
14622 } /* for */
14623
14624 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14625 break;
14626 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014627 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014628 /* There's part of a line in buf, store it in "prev". */
14629 if (p - start + prevlen >= prevsize)
14630 {
14631 /* need bigger "prev" buffer */
14632 char_u *newprev;
14633
14634 /* A common use case is ordinary text files and "prev" gets a
14635 * fragment of a line, so the first allocation is made
14636 * small, to avoid repeatedly 'allocing' large and
14637 * 'reallocing' small. */
14638 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014639 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014640 else
14641 {
14642 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014643 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014644 prevsize = grow50pc > growmin ? grow50pc : growmin;
14645 }
14646 if ((newprev = vim_realloc(prev, prevsize)) == NULL)
14647 {
14648 do_outofmem_msg((long_u)prevsize);
14649 failed = TRUE;
14650 break;
14651 }
14652 prev = newprev;
14653 }
14654 /* Add the line part to end of "prev". */
14655 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014656 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014657 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014658 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014660 /*
14661 * For a negative line count use only the lines at the end of the file,
14662 * free the rest.
14663 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014664 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014665 while (cnt > -maxline)
14666 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014667 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014668 --cnt;
14669 }
14670
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014671 if (failed)
14672 {
14673 list_free(rettv->vval.v_list, TRUE);
14674 /* readfile doc says an empty list is returned on error */
14675 rettv->vval.v_list = list_alloc();
14676 }
14677
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014678 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014679 fclose(fd);
14680}
14681
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014682#if defined(FEAT_RELTIME)
14683static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14684
14685/*
14686 * Convert a List to proftime_T.
14687 * Return FAIL when there is something wrong.
14688 */
14689 static int
14690list2proftime(arg, tm)
14691 typval_T *arg;
14692 proftime_T *tm;
14693{
14694 long n1, n2;
14695 int error = FALSE;
14696
14697 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14698 || arg->vval.v_list->lv_len != 2)
14699 return FAIL;
14700 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14701 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14702# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014703 tm->HighPart = n1;
14704 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014705# else
14706 tm->tv_sec = n1;
14707 tm->tv_usec = n2;
14708# endif
14709 return error ? FAIL : OK;
14710}
14711#endif /* FEAT_RELTIME */
14712
14713/*
14714 * "reltime()" function
14715 */
14716 static void
14717f_reltime(argvars, rettv)
14718 typval_T *argvars;
14719 typval_T *rettv;
14720{
14721#ifdef FEAT_RELTIME
14722 proftime_T res;
14723 proftime_T start;
14724
14725 if (argvars[0].v_type == VAR_UNKNOWN)
14726 {
14727 /* No arguments: get current time. */
14728 profile_start(&res);
14729 }
14730 else if (argvars[1].v_type == VAR_UNKNOWN)
14731 {
14732 if (list2proftime(&argvars[0], &res) == FAIL)
14733 return;
14734 profile_end(&res);
14735 }
14736 else
14737 {
14738 /* Two arguments: compute the difference. */
14739 if (list2proftime(&argvars[0], &start) == FAIL
14740 || list2proftime(&argvars[1], &res) == FAIL)
14741 return;
14742 profile_sub(&res, &start);
14743 }
14744
14745 if (rettv_list_alloc(rettv) == OK)
14746 {
14747 long n1, n2;
14748
14749# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014750 n1 = res.HighPart;
14751 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014752# else
14753 n1 = res.tv_sec;
14754 n2 = res.tv_usec;
14755# endif
14756 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14757 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14758 }
14759#endif
14760}
14761
14762/*
14763 * "reltimestr()" function
14764 */
14765 static void
14766f_reltimestr(argvars, rettv)
14767 typval_T *argvars;
14768 typval_T *rettv;
14769{
14770#ifdef FEAT_RELTIME
14771 proftime_T tm;
14772#endif
14773
14774 rettv->v_type = VAR_STRING;
14775 rettv->vval.v_string = NULL;
14776#ifdef FEAT_RELTIME
14777 if (list2proftime(&argvars[0], &tm) == OK)
14778 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14779#endif
14780}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014781
Bram Moolenaar0d660222005-01-07 21:51:51 +000014782#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14783static void make_connection __ARGS((void));
14784static int check_connection __ARGS((void));
14785
14786 static void
14787make_connection()
14788{
14789 if (X_DISPLAY == NULL
14790# ifdef FEAT_GUI
14791 && !gui.in_use
14792# endif
14793 )
14794 {
14795 x_force_connect = TRUE;
14796 setup_term_clip();
14797 x_force_connect = FALSE;
14798 }
14799}
14800
14801 static int
14802check_connection()
14803{
14804 make_connection();
14805 if (X_DISPLAY == NULL)
14806 {
14807 EMSG(_("E240: No connection to Vim server"));
14808 return FAIL;
14809 }
14810 return OK;
14811}
14812#endif
14813
14814#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014815static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816
14817 static void
14818remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014819 typval_T *argvars;
14820 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014821 int expr;
14822{
14823 char_u *server_name;
14824 char_u *keys;
14825 char_u *r = NULL;
14826 char_u buf[NUMBUFLEN];
14827# ifdef WIN32
14828 HWND w;
14829# else
14830 Window w;
14831# endif
14832
14833 if (check_restricted() || check_secure())
14834 return;
14835
14836# ifdef FEAT_X11
14837 if (check_connection() == FAIL)
14838 return;
14839# endif
14840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 server_name = get_tv_string_chk(&argvars[0]);
14842 if (server_name == NULL)
14843 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014844 keys = get_tv_string_buf(&argvars[1], buf);
14845# ifdef WIN32
14846 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14847# else
14848 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14849 < 0)
14850# endif
14851 {
14852 if (r != NULL)
14853 EMSG(r); /* sending worked but evaluation failed */
14854 else
14855 EMSG2(_("E241: Unable to send to %s"), server_name);
14856 return;
14857 }
14858
14859 rettv->vval.v_string = r;
14860
14861 if (argvars[2].v_type != VAR_UNKNOWN)
14862 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014863 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014864 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014867 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014868 v.di_tv.v_type = VAR_STRING;
14869 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014870 idvar = get_tv_string_chk(&argvars[2]);
14871 if (idvar != NULL)
14872 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874 }
14875}
14876#endif
14877
14878/*
14879 * "remote_expr()" function
14880 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014881 static void
14882f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014884 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014885{
14886 rettv->v_type = VAR_STRING;
14887 rettv->vval.v_string = NULL;
14888#ifdef FEAT_CLIENTSERVER
14889 remote_common(argvars, rettv, TRUE);
14890#endif
14891}
14892
14893/*
14894 * "remote_foreground()" function
14895 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896 static void
14897f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014898 typval_T *argvars UNUSED;
14899 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014901#ifdef FEAT_CLIENTSERVER
14902# ifdef WIN32
14903 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904 {
14905 char_u *server_name = get_tv_string_chk(&argvars[0]);
14906
14907 if (server_name != NULL)
14908 serverForeground(server_name);
14909 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014910# else
14911 /* Send a foreground() expression to the server. */
14912 argvars[1].v_type = VAR_STRING;
14913 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14914 argvars[2].v_type = VAR_UNKNOWN;
14915 remote_common(argvars, rettv, TRUE);
14916 vim_free(argvars[1].vval.v_string);
14917# endif
14918#endif
14919}
14920
Bram Moolenaar0d660222005-01-07 21:51:51 +000014921 static void
14922f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014924 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014925{
14926#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014927 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014928 char_u *s = NULL;
14929# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014930 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014932 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933
14934 if (check_restricted() || check_secure())
14935 {
14936 rettv->vval.v_number = -1;
14937 return;
14938 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014939 serverid = get_tv_string_chk(&argvars[0]);
14940 if (serverid == NULL)
14941 {
14942 rettv->vval.v_number = -1;
14943 return; /* type error; errmsg already given */
14944 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014945# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014946 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 if (n == 0)
14948 rettv->vval.v_number = -1;
14949 else
14950 {
14951 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14952 rettv->vval.v_number = (s != NULL);
14953 }
14954# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 if (check_connection() == FAIL)
14956 return;
14957
14958 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014959 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960# endif
14961
14962 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014964 char_u *retvar;
14965
Bram Moolenaar33570922005-01-25 22:26:29 +000014966 v.di_tv.v_type = VAR_STRING;
14967 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 retvar = get_tv_string_chk(&argvars[1]);
14969 if (retvar != NULL)
14970 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014971 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972 }
14973#else
14974 rettv->vval.v_number = -1;
14975#endif
14976}
14977
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 static void
14979f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014981 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982{
14983 char_u *r = NULL;
14984
14985#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014986 char_u *serverid = get_tv_string_chk(&argvars[0]);
14987
14988 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989 {
14990# ifdef WIN32
14991 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014992 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014994 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995 if (n != 0)
14996 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14997 if (r == NULL)
14998# else
14999 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001# endif
15002 EMSG(_("E277: Unable to read a server reply"));
15003 }
15004#endif
15005 rettv->v_type = VAR_STRING;
15006 rettv->vval.v_string = r;
15007}
15008
15009/*
15010 * "remote_send()" function
15011 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015012 static void
15013f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015014 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016{
15017 rettv->v_type = VAR_STRING;
15018 rettv->vval.v_string = NULL;
15019#ifdef FEAT_CLIENTSERVER
15020 remote_common(argvars, rettv, FALSE);
15021#endif
15022}
15023
15024/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015025 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015026 */
15027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 typval_T *argvars;
15030 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015031{
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 list_T *l;
15033 listitem_T *item, *item2;
15034 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015035 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015036 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015037 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 dict_T *d;
15039 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015040 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015041
Bram Moolenaar8c711452005-01-14 21:53:12 +000015042 if (argvars[0].v_type == VAR_DICT)
15043 {
15044 if (argvars[2].v_type != VAR_UNKNOWN)
15045 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015046 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015047 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 key = get_tv_string_chk(&argvars[1]);
15050 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015052 di = dict_find(d, key, -1);
15053 if (di == NULL)
15054 EMSG2(_(e_dictkey), key);
15055 else
15056 {
15057 *rettv = di->di_tv;
15058 init_tv(&di->di_tv);
15059 dictitem_remove(d, di);
15060 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015062 }
15063 }
15064 else if (argvars[0].v_type != VAR_LIST)
15065 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015066 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015067 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015069 int error = FALSE;
15070
15071 idx = get_tv_number_chk(&argvars[1], &error);
15072 if (error)
15073 ; /* type error: do nothing, errmsg already given */
15074 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015075 EMSGN(_(e_listidx), idx);
15076 else
15077 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015078 if (argvars[2].v_type == VAR_UNKNOWN)
15079 {
15080 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015081 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015082 *rettv = item->li_tv;
15083 vim_free(item);
15084 }
15085 else
15086 {
15087 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 end = get_tv_number_chk(&argvars[2], &error);
15089 if (error)
15090 ; /* type error: do nothing */
15091 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015092 EMSGN(_(e_listidx), end);
15093 else
15094 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015095 int cnt = 0;
15096
15097 for (li = item; li != NULL; li = li->li_next)
15098 {
15099 ++cnt;
15100 if (li == item2)
15101 break;
15102 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015103 if (li == NULL) /* didn't find "item2" after "item" */
15104 EMSG(_(e_invrange));
15105 else
15106 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015107 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015108 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015109 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015110 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015111 l->lv_first = item;
15112 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015113 item->li_prev = NULL;
15114 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015115 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015116 }
15117 }
15118 }
15119 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015120 }
15121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122}
15123
15124/*
15125 * "rename({from}, {to})" function
15126 */
15127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 typval_T *argvars;
15130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131{
15132 char_u buf[NUMBUFLEN];
15133
15134 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15138 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139}
15140
15141/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015142 * "repeat()" function
15143 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015144 static void
15145f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 typval_T *argvars;
15147 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015148{
15149 char_u *p;
15150 int n;
15151 int slen;
15152 int len;
15153 char_u *r;
15154 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015155
15156 n = get_tv_number(&argvars[1]);
15157 if (argvars[0].v_type == VAR_LIST)
15158 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015159 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015160 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015161 if (list_extend(rettv->vval.v_list,
15162 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015163 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015164 }
15165 else
15166 {
15167 p = get_tv_string(&argvars[0]);
15168 rettv->v_type = VAR_STRING;
15169 rettv->vval.v_string = NULL;
15170
15171 slen = (int)STRLEN(p);
15172 len = slen * n;
15173 if (len <= 0)
15174 return;
15175
15176 r = alloc(len + 1);
15177 if (r != NULL)
15178 {
15179 for (i = 0; i < n; i++)
15180 mch_memmove(r + i * slen, p, (size_t)slen);
15181 r[len] = NUL;
15182 }
15183
15184 rettv->vval.v_string = r;
15185 }
15186}
15187
15188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 * "resolve()" function
15190 */
15191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *argvars;
15194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195{
15196 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015197#ifdef HAVE_READLINK
15198 char_u *buf = NULL;
15199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015201 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202#ifdef FEAT_SHORTCUT
15203 {
15204 char_u *v = NULL;
15205
15206 v = mch_resolve_shortcut(p);
15207 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 }
15212#else
15213# ifdef HAVE_READLINK
15214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 char_u *cpy;
15216 int len;
15217 char_u *remain = NULL;
15218 char_u *q;
15219 int is_relative_to_current = FALSE;
15220 int has_trailing_pathsep = FALSE;
15221 int limit = 100;
15222
15223 p = vim_strsave(p);
15224
15225 if (p[0] == '.' && (vim_ispathsep(p[1])
15226 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15227 is_relative_to_current = TRUE;
15228
15229 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015230 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015233 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235
15236 q = getnextcomp(p);
15237 if (*q != NUL)
15238 {
15239 /* Separate the first path component in "p", and keep the
15240 * remainder (beginning with the path separator). */
15241 remain = vim_strsave(q - 1);
15242 q[-1] = NUL;
15243 }
15244
Bram Moolenaard9462e32011-04-11 21:35:11 +020015245 buf = alloc(MAXPATHL + 1);
15246 if (buf == NULL)
15247 goto fail;
15248
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 for (;;)
15250 {
15251 for (;;)
15252 {
15253 len = readlink((char *)p, (char *)buf, MAXPATHL);
15254 if (len <= 0)
15255 break;
15256 buf[len] = NUL;
15257
15258 if (limit-- == 0)
15259 {
15260 vim_free(p);
15261 vim_free(remain);
15262 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 goto fail;
15265 }
15266
15267 /* Ensure that the result will have a trailing path separator
15268 * if the argument has one. */
15269 if (remain == NULL && has_trailing_pathsep)
15270 add_pathsep(buf);
15271
15272 /* Separate the first path component in the link value and
15273 * concatenate the remainders. */
15274 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15275 if (*q != NUL)
15276 {
15277 if (remain == NULL)
15278 remain = vim_strsave(q - 1);
15279 else
15280 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015281 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 if (cpy != NULL)
15283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 vim_free(remain);
15285 remain = cpy;
15286 }
15287 }
15288 q[-1] = NUL;
15289 }
15290
15291 q = gettail(p);
15292 if (q > p && *q == NUL)
15293 {
15294 /* Ignore trailing path separator. */
15295 q[-1] = NUL;
15296 q = gettail(p);
15297 }
15298 if (q > p && !mch_isFullName(buf))
15299 {
15300 /* symlink is relative to directory of argument */
15301 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15302 if (cpy != NULL)
15303 {
15304 STRCPY(cpy, p);
15305 STRCPY(gettail(cpy), buf);
15306 vim_free(p);
15307 p = cpy;
15308 }
15309 }
15310 else
15311 {
15312 vim_free(p);
15313 p = vim_strsave(buf);
15314 }
15315 }
15316
15317 if (remain == NULL)
15318 break;
15319
15320 /* Append the first path component of "remain" to "p". */
15321 q = getnextcomp(remain + 1);
15322 len = q - remain - (*q != NUL);
15323 cpy = vim_strnsave(p, STRLEN(p) + len);
15324 if (cpy != NULL)
15325 {
15326 STRNCAT(cpy, remain, len);
15327 vim_free(p);
15328 p = cpy;
15329 }
15330 /* Shorten "remain". */
15331 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015332 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 else
15334 {
15335 vim_free(remain);
15336 remain = NULL;
15337 }
15338 }
15339
15340 /* If the result is a relative path name, make it explicitly relative to
15341 * the current directory if and only if the argument had this form. */
15342 if (!vim_ispathsep(*p))
15343 {
15344 if (is_relative_to_current
15345 && *p != NUL
15346 && !(p[0] == '.'
15347 && (p[1] == NUL
15348 || vim_ispathsep(p[1])
15349 || (p[1] == '.'
15350 && (p[2] == NUL
15351 || vim_ispathsep(p[2]))))))
15352 {
15353 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015354 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 if (cpy != NULL)
15356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 vim_free(p);
15358 p = cpy;
15359 }
15360 }
15361 else if (!is_relative_to_current)
15362 {
15363 /* Strip leading "./". */
15364 q = p;
15365 while (q[0] == '.' && vim_ispathsep(q[1]))
15366 q += 2;
15367 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015368 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 }
15370 }
15371
15372 /* Ensure that the result will have no trailing path separator
15373 * if the argument had none. But keep "/" or "//". */
15374 if (!has_trailing_pathsep)
15375 {
15376 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015377 if (after_pathsep(p, q))
15378 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 }
15380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 }
15383# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385# endif
15386#endif
15387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389
15390#ifdef HAVE_READLINK
15391fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015392 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395}
15396
15397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015398 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 */
15400 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015401f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015402 typval_T *argvars;
15403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404{
Bram Moolenaar33570922005-01-25 22:26:29 +000015405 list_T *l;
15406 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407
Bram Moolenaar0d660222005-01-07 21:51:51 +000015408 if (argvars[0].v_type != VAR_LIST)
15409 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015410 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015411 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015412 {
15413 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015414 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015415 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015416 while (li != NULL)
15417 {
15418 ni = li->li_prev;
15419 list_append(l, li);
15420 li = ni;
15421 }
15422 rettv->vval.v_list = l;
15423 rettv->v_type = VAR_LIST;
15424 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015425 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427}
15428
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015429#define SP_NOMOVE 0x01 /* don't move cursor */
15430#define SP_REPEAT 0x02 /* repeat to find outer pair */
15431#define SP_RETCOUNT 0x04 /* return matchcount */
15432#define SP_SETPCMARK 0x08 /* set previous context mark */
15433#define SP_START 0x10 /* accept match at start position */
15434#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15435#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015436
Bram Moolenaar33570922005-01-25 22:26:29 +000015437static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015438
15439/*
15440 * Get flags for a search function.
15441 * Possibly sets "p_ws".
15442 * Returns BACKWARD, FORWARD or zero (for an error).
15443 */
15444 static int
15445get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015446 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015447 int *flagsp;
15448{
15449 int dir = FORWARD;
15450 char_u *flags;
15451 char_u nbuf[NUMBUFLEN];
15452 int mask;
15453
15454 if (varp->v_type != VAR_UNKNOWN)
15455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015456 flags = get_tv_string_buf_chk(varp, nbuf);
15457 if (flags == NULL)
15458 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015459 while (*flags != NUL)
15460 {
15461 switch (*flags)
15462 {
15463 case 'b': dir = BACKWARD; break;
15464 case 'w': p_ws = TRUE; break;
15465 case 'W': p_ws = FALSE; break;
15466 default: mask = 0;
15467 if (flagsp != NULL)
15468 switch (*flags)
15469 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015470 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015471 case 'e': mask = SP_END; break;
15472 case 'm': mask = SP_RETCOUNT; break;
15473 case 'n': mask = SP_NOMOVE; break;
15474 case 'p': mask = SP_SUBPAT; break;
15475 case 'r': mask = SP_REPEAT; break;
15476 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015477 }
15478 if (mask == 0)
15479 {
15480 EMSG2(_(e_invarg2), flags);
15481 dir = 0;
15482 }
15483 else
15484 *flagsp |= mask;
15485 }
15486 if (dir == 0)
15487 break;
15488 ++flags;
15489 }
15490 }
15491 return dir;
15492}
15493
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015495 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015497 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015498search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015499 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015500 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015501 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015503 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 char_u *pat;
15505 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015506 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 int save_p_ws = p_ws;
15508 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015509 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015510 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015511 proftime_T tm;
15512#ifdef FEAT_RELTIME
15513 long time_limit = 0;
15514#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015515 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015516 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015519 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015520 if (dir == 0)
15521 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015522 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015523 if (flags & SP_START)
15524 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015525 if (flags & SP_END)
15526 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015527
Bram Moolenaar76929292008-01-06 19:07:36 +000015528 /* Optional arguments: line number to stop searching and timeout. */
15529 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015530 {
15531 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15532 if (lnum_stop < 0)
15533 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015534#ifdef FEAT_RELTIME
15535 if (argvars[3].v_type != VAR_UNKNOWN)
15536 {
15537 time_limit = get_tv_number_chk(&argvars[3], NULL);
15538 if (time_limit < 0)
15539 goto theend;
15540 }
15541#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015542 }
15543
Bram Moolenaar76929292008-01-06 19:07:36 +000015544#ifdef FEAT_RELTIME
15545 /* Set the time limit, if there is one. */
15546 profile_setlimit(time_limit, &tm);
15547#endif
15548
Bram Moolenaar231334e2005-07-25 20:46:57 +000015549 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015550 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015551 * Check to make sure only those flags are set.
15552 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15553 * flags cannot be set. Check for that condition also.
15554 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015555 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015556 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015559 goto theend;
15560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015562 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015563 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015564 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015565 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015567 if (flags & SP_SUBPAT)
15568 retval = subpatnum;
15569 else
15570 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015571 if (flags & SP_SETPCMARK)
15572 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015574 if (match_pos != NULL)
15575 {
15576 /* Store the match cursor position */
15577 match_pos->lnum = pos.lnum;
15578 match_pos->col = pos.col + 1;
15579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 /* "/$" will put the cursor after the end of the line, may need to
15581 * correct that here */
15582 check_cursor();
15583 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015584
15585 /* If 'n' flag is used: restore cursor position. */
15586 if (flags & SP_NOMOVE)
15587 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015588 else
15589 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015590theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015592
15593 return retval;
15594}
15595
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015596#ifdef FEAT_FLOAT
15597/*
15598 * "round({float})" function
15599 */
15600 static void
15601f_round(argvars, rettv)
15602 typval_T *argvars;
15603 typval_T *rettv;
15604{
15605 float_T f;
15606
15607 rettv->v_type = VAR_FLOAT;
15608 if (get_float_arg(argvars, &f) == OK)
15609 /* round() is not in C90, use ceil() or floor() instead. */
15610 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15611 else
15612 rettv->vval.v_float = 0.0;
15613}
15614#endif
15615
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015616/*
15617 * "search()" function
15618 */
15619 static void
15620f_search(argvars, rettv)
15621 typval_T *argvars;
15622 typval_T *rettv;
15623{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015624 int flags = 0;
15625
15626 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627}
15628
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015630 * "searchdecl()" function
15631 */
15632 static void
15633f_searchdecl(argvars, rettv)
15634 typval_T *argvars;
15635 typval_T *rettv;
15636{
15637 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015638 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015639 int error = FALSE;
15640 char_u *name;
15641
15642 rettv->vval.v_number = 1; /* default: FAIL */
15643
15644 name = get_tv_string_chk(&argvars[0]);
15645 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015646 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015647 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015648 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15649 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15650 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015651 if (!error && name != NULL)
15652 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015653 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015654}
15655
15656/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015657 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015659 static int
15660searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015662 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
15664 char_u *spat, *mpat, *epat;
15665 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 int dir;
15668 int flags = 0;
15669 char_u nbuf1[NUMBUFLEN];
15670 char_u nbuf2[NUMBUFLEN];
15671 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015672 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015673 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015674 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015677 spat = get_tv_string_chk(&argvars[0]);
15678 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15679 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15680 if (spat == NULL || mpat == NULL || epat == NULL)
15681 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 /* Handle the optional fourth argument: flags */
15684 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015685 if (dir == 0)
15686 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015687
15688 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015689 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15690 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015691 if ((flags & (SP_END | SP_SUBPAT)) != 0
15692 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015693 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015694 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015695 goto theend;
15696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015698 /* Using 'r' implies 'W', otherwise it doesn't work. */
15699 if (flags & SP_REPEAT)
15700 p_ws = FALSE;
15701
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015702 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015703 if (argvars[3].v_type == VAR_UNKNOWN
15704 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 skip = (char_u *)"";
15706 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015709 if (argvars[5].v_type != VAR_UNKNOWN)
15710 {
15711 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15712 if (lnum_stop < 0)
15713 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015714#ifdef FEAT_RELTIME
15715 if (argvars[6].v_type != VAR_UNKNOWN)
15716 {
15717 time_limit = get_tv_number_chk(&argvars[6], NULL);
15718 if (time_limit < 0)
15719 goto theend;
15720 }
15721#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015722 }
15723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 if (skip == NULL)
15725 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015727 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015728 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015729
15730theend:
15731 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015732
15733 return retval;
15734}
15735
15736/*
15737 * "searchpair()" function
15738 */
15739 static void
15740f_searchpair(argvars, rettv)
15741 typval_T *argvars;
15742 typval_T *rettv;
15743{
15744 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15745}
15746
15747/*
15748 * "searchpairpos()" function
15749 */
15750 static void
15751f_searchpairpos(argvars, rettv)
15752 typval_T *argvars;
15753 typval_T *rettv;
15754{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015755 pos_T match_pos;
15756 int lnum = 0;
15757 int col = 0;
15758
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015759 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015760 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015761
15762 if (searchpair_cmn(argvars, &match_pos) > 0)
15763 {
15764 lnum = match_pos.lnum;
15765 col = match_pos.col;
15766 }
15767
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015768 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15769 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015770}
15771
15772/*
15773 * Search for a start/middle/end thing.
15774 * Used by searchpair(), see its documentation for the details.
15775 * Returns 0 or -1 for no match,
15776 */
15777 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015778do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15779 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015780 char_u *spat; /* start pattern */
15781 char_u *mpat; /* middle pattern */
15782 char_u *epat; /* end pattern */
15783 int dir; /* BACKWARD or FORWARD */
15784 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015785 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015786 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015787 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015788 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015789{
15790 char_u *save_cpo;
15791 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15792 long retval = 0;
15793 pos_T pos;
15794 pos_T firstpos;
15795 pos_T foundpos;
15796 pos_T save_cursor;
15797 pos_T save_pos;
15798 int n;
15799 int r;
15800 int nest = 1;
15801 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015802 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015803 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015804
15805 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15806 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015807 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015808
Bram Moolenaar76929292008-01-06 19:07:36 +000015809#ifdef FEAT_RELTIME
15810 /* Set the time limit, if there is one. */
15811 profile_setlimit(time_limit, &tm);
15812#endif
15813
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015814 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15815 * start/middle/end (pat3, for the top pair). */
15816 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15817 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15818 if (pat2 == NULL || pat3 == NULL)
15819 goto theend;
15820 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15821 if (*mpat == NUL)
15822 STRCPY(pat3, pat2);
15823 else
15824 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15825 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015826 if (flags & SP_START)
15827 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015828
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 save_cursor = curwin->w_cursor;
15830 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015831 clearpos(&firstpos);
15832 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 pat = pat3;
15834 for (;;)
15835 {
15836 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015837 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15839 /* didn't find it or found the first match again: FAIL */
15840 break;
15841
15842 if (firstpos.lnum == 0)
15843 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015844 if (equalpos(pos, foundpos))
15845 {
15846 /* Found the same position again. Can happen with a pattern that
15847 * has "\zs" at the end and searching backwards. Advance one
15848 * character and try again. */
15849 if (dir == BACKWARD)
15850 decl(&pos);
15851 else
15852 incl(&pos);
15853 }
15854 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015856 /* clear the start flag to avoid getting stuck here */
15857 options &= ~SEARCH_START;
15858
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 /* If the skip pattern matches, ignore this match. */
15860 if (*skip != NUL)
15861 {
15862 save_pos = curwin->w_cursor;
15863 curwin->w_cursor = pos;
15864 r = eval_to_bool(skip, &err, NULL, FALSE);
15865 curwin->w_cursor = save_pos;
15866 if (err)
15867 {
15868 /* Evaluating {skip} caused an error, break here. */
15869 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015870 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 break;
15872 }
15873 if (r)
15874 continue;
15875 }
15876
15877 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15878 {
15879 /* Found end when searching backwards or start when searching
15880 * forward: nested pair. */
15881 ++nest;
15882 pat = pat2; /* nested, don't search for middle */
15883 }
15884 else
15885 {
15886 /* Found end when searching forward or start when searching
15887 * backward: end of (nested) pair; or found middle in outer pair. */
15888 if (--nest == 1)
15889 pat = pat3; /* outer level, search for middle */
15890 }
15891
15892 if (nest == 0)
15893 {
15894 /* Found the match: return matchcount or line number. */
15895 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015896 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015898 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015899 if (flags & SP_SETPCMARK)
15900 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 curwin->w_cursor = pos;
15902 if (!(flags & SP_REPEAT))
15903 break;
15904 nest = 1; /* search for next unmatched */
15905 }
15906 }
15907
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015908 if (match_pos != NULL)
15909 {
15910 /* Store the match cursor position */
15911 match_pos->lnum = curwin->w_cursor.lnum;
15912 match_pos->col = curwin->w_cursor.col + 1;
15913 }
15914
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015916 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 curwin->w_cursor = save_cursor;
15918
15919theend:
15920 vim_free(pat2);
15921 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015922 if (p_cpo == empty_option)
15923 p_cpo = save_cpo;
15924 else
15925 /* Darn, evaluating the {skip} expression changed the value. */
15926 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015927
15928 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929}
15930
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015931/*
15932 * "searchpos()" function
15933 */
15934 static void
15935f_searchpos(argvars, rettv)
15936 typval_T *argvars;
15937 typval_T *rettv;
15938{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015939 pos_T match_pos;
15940 int lnum = 0;
15941 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015942 int n;
15943 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015944
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015945 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015946 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015947
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015948 n = search_cmn(argvars, &match_pos, &flags);
15949 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015950 {
15951 lnum = match_pos.lnum;
15952 col = match_pos.col;
15953 }
15954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015955 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15956 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015957 if (flags & SP_SUBPAT)
15958 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015959}
15960
15961
Bram Moolenaar0d660222005-01-07 21:51:51 +000015962 static void
15963f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015967#ifdef FEAT_CLIENTSERVER
15968 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015969 char_u *server = get_tv_string_chk(&argvars[0]);
15970 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971
Bram Moolenaar0d660222005-01-07 21:51:51 +000015972 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015973 if (server == NULL || reply == NULL)
15974 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975 if (check_restricted() || check_secure())
15976 return;
15977# ifdef FEAT_X11
15978 if (check_connection() == FAIL)
15979 return;
15980# endif
15981
15982 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015984 EMSG(_("E258: Unable to send to client"));
15985 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015987 rettv->vval.v_number = 0;
15988#else
15989 rettv->vval.v_number = -1;
15990#endif
15991}
15992
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993 static void
15994f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015995 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015997{
15998 char_u *r = NULL;
15999
16000#ifdef FEAT_CLIENTSERVER
16001# ifdef WIN32
16002 r = serverGetVimNames();
16003# else
16004 make_connection();
16005 if (X_DISPLAY != NULL)
16006 r = serverGetVimNames(X_DISPLAY);
16007# endif
16008#endif
16009 rettv->v_type = VAR_STRING;
16010 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011}
16012
16013/*
16014 * "setbufvar()" function
16015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016019 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020{
16021 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016024 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 char_u nbuf[NUMBUFLEN];
16026
16027 if (check_restricted() || check_secure())
16028 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16030 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016031 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 varp = &argvars[2];
16033
16034 if (buf != NULL && varname != NULL && varp != NULL)
16035 {
16036 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038
16039 if (*varname == '&')
16040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 long numval;
16042 char_u *strval;
16043 int error = FALSE;
16044
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 numval = get_tv_number_chk(varp, &error);
16047 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 if (!error && strval != NULL)
16049 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 }
16051 else
16052 {
16053 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16054 if (bufvarname != NULL)
16055 {
16056 STRCPY(bufvarname, "b:");
16057 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016058 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 vim_free(bufvarname);
16060 }
16061 }
16062
16063 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066}
16067
16068/*
16069 * "setcmdpos()" function
16070 */
16071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016072f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016073 typval_T *argvars;
16074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016076 int pos = (int)get_tv_number(&argvars[0]) - 1;
16077
16078 if (pos >= 0)
16079 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080}
16081
16082/*
16083 * "setline()" function
16084 */
16085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016086f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 typval_T *argvars;
16088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089{
16090 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016091 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016092 list_T *l = NULL;
16093 listitem_T *li = NULL;
16094 long added = 0;
16095 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016097 lnum = get_tv_lnum(&argvars[0]);
16098 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016100 l = argvars[1].vval.v_list;
16101 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016103 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016104 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016105
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016106 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016107 for (;;)
16108 {
16109 if (l != NULL)
16110 {
16111 /* list argument, get next string */
16112 if (li == NULL)
16113 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016115 li = li->li_next;
16116 }
16117
16118 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016119 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016120 break;
16121 if (lnum <= curbuf->b_ml.ml_line_count)
16122 {
16123 /* existing line, replace it */
16124 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16125 {
16126 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016127 if (lnum == curwin->w_cursor.lnum)
16128 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016129 rettv->vval.v_number = 0; /* OK */
16130 }
16131 }
16132 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16133 {
16134 /* lnum is one past the last line, append the line */
16135 ++added;
16136 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16137 rettv->vval.v_number = 0; /* OK */
16138 }
16139
16140 if (l == NULL) /* only one string argument */
16141 break;
16142 ++lnum;
16143 }
16144
16145 if (added > 0)
16146 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147}
16148
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016149static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16150
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016152 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016153 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016154 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016155set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016156 win_T *wp UNUSED;
16157 typval_T *list_arg UNUSED;
16158 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016159 typval_T *rettv;
16160{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016161#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016162 char_u *act;
16163 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016164#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016165
Bram Moolenaar2641f772005-03-25 21:58:17 +000016166 rettv->vval.v_number = -1;
16167
16168#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016169 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016170 EMSG(_(e_listreq));
16171 else
16172 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016173 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016174
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016175 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016176 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016177 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016178 if (act == NULL)
16179 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016180 if (*act == 'a' || *act == 'r')
16181 action = *act;
16182 }
16183
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016184 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016185 rettv->vval.v_number = 0;
16186 }
16187#endif
16188}
16189
16190/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016191 * "setloclist()" function
16192 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016193 static void
16194f_setloclist(argvars, rettv)
16195 typval_T *argvars;
16196 typval_T *rettv;
16197{
16198 win_T *win;
16199
16200 rettv->vval.v_number = -1;
16201
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016202 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016203 if (win != NULL)
16204 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16205}
16206
16207/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016208 * "setmatches()" function
16209 */
16210 static void
16211f_setmatches(argvars, rettv)
16212 typval_T *argvars;
16213 typval_T *rettv;
16214{
16215#ifdef FEAT_SEARCH_EXTRA
16216 list_T *l;
16217 listitem_T *li;
16218 dict_T *d;
16219
16220 rettv->vval.v_number = -1;
16221 if (argvars[0].v_type != VAR_LIST)
16222 {
16223 EMSG(_(e_listreq));
16224 return;
16225 }
16226 if ((l = argvars[0].vval.v_list) != NULL)
16227 {
16228
16229 /* To some extent make sure that we are dealing with a list from
16230 * "getmatches()". */
16231 li = l->lv_first;
16232 while (li != NULL)
16233 {
16234 if (li->li_tv.v_type != VAR_DICT
16235 || (d = li->li_tv.vval.v_dict) == NULL)
16236 {
16237 EMSG(_(e_invarg));
16238 return;
16239 }
16240 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16241 && dict_find(d, (char_u *)"pattern", -1) != NULL
16242 && dict_find(d, (char_u *)"priority", -1) != NULL
16243 && dict_find(d, (char_u *)"id", -1) != NULL))
16244 {
16245 EMSG(_(e_invarg));
16246 return;
16247 }
16248 li = li->li_next;
16249 }
16250
16251 clear_matches(curwin);
16252 li = l->lv_first;
16253 while (li != NULL)
16254 {
16255 d = li->li_tv.vval.v_dict;
16256 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16257 get_dict_string(d, (char_u *)"pattern", FALSE),
16258 (int)get_dict_number(d, (char_u *)"priority"),
16259 (int)get_dict_number(d, (char_u *)"id"));
16260 li = li->li_next;
16261 }
16262 rettv->vval.v_number = 0;
16263 }
16264#endif
16265}
16266
16267/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016268 * "setpos()" function
16269 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016270 static void
16271f_setpos(argvars, rettv)
16272 typval_T *argvars;
16273 typval_T *rettv;
16274{
16275 pos_T pos;
16276 int fnum;
16277 char_u *name;
16278
Bram Moolenaar08250432008-02-13 11:42:46 +000016279 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016280 name = get_tv_string_chk(argvars);
16281 if (name != NULL)
16282 {
16283 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16284 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016285 if (--pos.col < 0)
16286 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016287 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016288 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016289 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016290 if (fnum == curbuf->b_fnum)
16291 {
16292 curwin->w_cursor = pos;
16293 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016294 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016295 }
16296 else
16297 EMSG(_(e_invarg));
16298 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016299 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16300 {
16301 /* set mark */
16302 if (setmark_pos(name[1], &pos, fnum) == OK)
16303 rettv->vval.v_number = 0;
16304 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016305 else
16306 EMSG(_(e_invarg));
16307 }
16308 }
16309}
16310
16311/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016312 * "setqflist()" function
16313 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016314 static void
16315f_setqflist(argvars, rettv)
16316 typval_T *argvars;
16317 typval_T *rettv;
16318{
16319 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16320}
16321
16322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 * "setreg()" function
16324 */
16325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016326f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 typval_T *argvars;
16328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329{
16330 int regname;
16331 char_u *strregname;
16332 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 int append;
16335 char_u yank_type;
16336 long block_len;
16337
16338 block_len = -1;
16339 yank_type = MAUTO;
16340 append = FALSE;
16341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016342 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016345 if (strregname == NULL)
16346 return; /* type error; errmsg already given */
16347 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 if (regname == 0 || regname == '@')
16349 regname = '"';
16350 else if (regname == '=')
16351 return;
16352
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016353 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016355 stropt = get_tv_string_chk(&argvars[2]);
16356 if (stropt == NULL)
16357 return; /* type error */
16358 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 switch (*stropt)
16360 {
16361 case 'a': case 'A': /* append */
16362 append = TRUE;
16363 break;
16364 case 'v': case 'c': /* character-wise selection */
16365 yank_type = MCHAR;
16366 break;
16367 case 'V': case 'l': /* line-wise selection */
16368 yank_type = MLINE;
16369 break;
16370#ifdef FEAT_VISUAL
16371 case 'b': case Ctrl_V: /* block-wise selection */
16372 yank_type = MBLOCK;
16373 if (VIM_ISDIGIT(stropt[1]))
16374 {
16375 ++stropt;
16376 block_len = getdigits(&stropt) - 1;
16377 --stropt;
16378 }
16379 break;
16380#endif
16381 }
16382 }
16383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016384 strval = get_tv_string_chk(&argvars[1]);
16385 if (strval != NULL)
16386 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016388 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389}
16390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016391/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016392 * "settabvar()" function
16393 */
16394 static void
16395f_settabvar(argvars, rettv)
16396 typval_T *argvars;
16397 typval_T *rettv;
16398{
16399 tabpage_T *save_curtab;
16400 char_u *varname, *tabvarname;
16401 typval_T *varp;
16402 tabpage_T *tp;
16403
16404 rettv->vval.v_number = 0;
16405
16406 if (check_restricted() || check_secure())
16407 return;
16408
16409 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16410 varname = get_tv_string_chk(&argvars[1]);
16411 varp = &argvars[2];
16412
16413 if (tp != NULL && varname != NULL && varp != NULL)
16414 {
16415 save_curtab = curtab;
16416 goto_tabpage_tp(tp);
16417
16418 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16419 if (tabvarname != NULL)
16420 {
16421 STRCPY(tabvarname, "t:");
16422 STRCPY(tabvarname + 2, varname);
16423 set_var(tabvarname, varp, TRUE);
16424 vim_free(tabvarname);
16425 }
16426
16427 /* Restore current tabpage */
16428 if (valid_tabpage(save_curtab))
16429 goto_tabpage_tp(save_curtab);
16430 }
16431}
16432
16433/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016434 * "settabwinvar()" function
16435 */
16436 static void
16437f_settabwinvar(argvars, rettv)
16438 typval_T *argvars;
16439 typval_T *rettv;
16440{
16441 setwinvar(argvars, rettv, 1);
16442}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443
16444/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016445 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T *argvars;
16450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016452 setwinvar(argvars, rettv, 0);
16453}
16454
16455/*
16456 * "setwinvar()" and "settabwinvar()" functions
16457 */
16458 static void
16459setwinvar(argvars, rettv, off)
16460 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016461 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016462 int off;
16463{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 win_T *win;
16465#ifdef FEAT_WINDOWS
16466 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016467 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468#endif
16469 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016470 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016472 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473
16474 if (check_restricted() || check_secure())
16475 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016476
16477#ifdef FEAT_WINDOWS
16478 if (off == 1)
16479 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16480 else
16481 tp = curtab;
16482#endif
16483 win = find_win_by_nr(&argvars[off], tp);
16484 varname = get_tv_string_chk(&argvars[off + 1]);
16485 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486
16487 if (win != NULL && varname != NULL && varp != NULL)
16488 {
16489#ifdef FEAT_WINDOWS
16490 /* set curwin to be our win, temporarily */
16491 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016492 save_curtab = curtab;
16493 goto_tabpage_tp(tp);
16494 if (!win_valid(win))
16495 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 curwin = win;
16497 curbuf = curwin->w_buffer;
16498#endif
16499
16500 if (*varname == '&')
16501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 long numval;
16503 char_u *strval;
16504 int error = FALSE;
16505
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 numval = get_tv_number_chk(varp, &error);
16508 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 if (!error && strval != NULL)
16510 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 }
16512 else
16513 {
16514 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16515 if (winvarname != NULL)
16516 {
16517 STRCPY(winvarname, "w:");
16518 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016519 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 vim_free(winvarname);
16521 }
16522 }
16523
16524#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016525 /* Restore current tabpage and window, if still valid (autocomands can
16526 * make them invalid). */
16527 if (valid_tabpage(save_curtab))
16528 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 if (win_valid(save_curwin))
16530 {
16531 curwin = save_curwin;
16532 curbuf = curwin->w_buffer;
16533 }
16534#endif
16535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536}
16537
16538/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016539 * "shellescape({string})" function
16540 */
16541 static void
16542f_shellescape(argvars, rettv)
16543 typval_T *argvars;
16544 typval_T *rettv;
16545{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016546 rettv->vval.v_string = vim_strsave_shellescape(
16547 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016548 rettv->v_type = VAR_STRING;
16549}
16550
16551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 */
16554 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016556 typval_T *argvars;
16557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 p = get_tv_string(&argvars[0]);
16562 rettv->vval.v_string = vim_strsave(p);
16563 simplify_filename(rettv->vval.v_string); /* simplify in place */
16564 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565}
16566
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016567#ifdef FEAT_FLOAT
16568/*
16569 * "sin()" function
16570 */
16571 static void
16572f_sin(argvars, rettv)
16573 typval_T *argvars;
16574 typval_T *rettv;
16575{
16576 float_T f;
16577
16578 rettv->v_type = VAR_FLOAT;
16579 if (get_float_arg(argvars, &f) == OK)
16580 rettv->vval.v_float = sin(f);
16581 else
16582 rettv->vval.v_float = 0.0;
16583}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016584
16585/*
16586 * "sinh()" function
16587 */
16588 static void
16589f_sinh(argvars, rettv)
16590 typval_T *argvars;
16591 typval_T *rettv;
16592{
16593 float_T f;
16594
16595 rettv->v_type = VAR_FLOAT;
16596 if (get_float_arg(argvars, &f) == OK)
16597 rettv->vval.v_float = sinh(f);
16598 else
16599 rettv->vval.v_float = 0.0;
16600}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016601#endif
16602
Bram Moolenaar0d660222005-01-07 21:51:51 +000016603static int
16604#ifdef __BORLANDC__
16605 _RTLENTRYF
16606#endif
16607 item_compare __ARGS((const void *s1, const void *s2));
16608static int
16609#ifdef __BORLANDC__
16610 _RTLENTRYF
16611#endif
16612 item_compare2 __ARGS((const void *s1, const void *s2));
16613
16614static int item_compare_ic;
16615static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016616static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618#define ITEM_COMPARE_FAIL 999
16619
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623 static int
16624#ifdef __BORLANDC__
16625_RTLENTRYF
16626#endif
16627item_compare(s1, s2)
16628 const void *s1;
16629 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016631 char_u *p1, *p2;
16632 char_u *tofree1, *tofree2;
16633 int res;
16634 char_u numbuf1[NUMBUFLEN];
16635 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016637 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16638 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016639 if (p1 == NULL)
16640 p1 = (char_u *)"";
16641 if (p2 == NULL)
16642 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 if (item_compare_ic)
16644 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 res = STRCMP(p1, p2);
16647 vim_free(tofree1);
16648 vim_free(tofree2);
16649 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650}
16651
16652 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653#ifdef __BORLANDC__
16654_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016656item_compare2(s1, s2)
16657 const void *s1;
16658 const void *s2;
16659{
16660 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016661 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016662 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016663 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016665 /* shortcut after failure in previous call; compare all items equal */
16666 if (item_compare_func_err)
16667 return 0;
16668
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016669 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16670 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16672 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016673
16674 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016675 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016676 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16677 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678 clear_tv(&argv[0]);
16679 clear_tv(&argv[1]);
16680
16681 if (res == FAIL)
16682 res = ITEM_COMPARE_FAIL;
16683 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016684 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16685 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016686 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687 clear_tv(&rettv);
16688 return res;
16689}
16690
16691/*
16692 * "sort({list})" function
16693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 typval_T *argvars;
16697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698{
Bram Moolenaar33570922005-01-25 22:26:29 +000016699 list_T *l;
16700 listitem_T *li;
16701 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016702 long len;
16703 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705 if (argvars[0].v_type != VAR_LIST)
16706 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 else
16708 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016709 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016710 if (l == NULL || tv_check_lock(l->lv_lock,
16711 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016712 return;
16713 rettv->vval.v_list = l;
16714 rettv->v_type = VAR_LIST;
16715 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717 len = list_len(l);
16718 if (len <= 1)
16719 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720
Bram Moolenaar0d660222005-01-07 21:51:51 +000016721 item_compare_ic = FALSE;
16722 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016723 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 if (argvars[1].v_type != VAR_UNKNOWN)
16725 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016726 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016727 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016728 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 else
16730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 int error = FALSE;
16732
16733 i = get_tv_number_chk(&argvars[1], &error);
16734 if (error)
16735 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016736 if (i == 1)
16737 item_compare_ic = TRUE;
16738 else
16739 item_compare_func = get_tv_string(&argvars[1]);
16740 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016741
16742 if (argvars[2].v_type != VAR_UNKNOWN)
16743 {
16744 /* optional third argument: {dict} */
16745 if (argvars[2].v_type != VAR_DICT)
16746 {
16747 EMSG(_(e_dictreq));
16748 return;
16749 }
16750 item_compare_selfdict = argvars[2].vval.v_dict;
16751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016755 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016756 if (ptrs == NULL)
16757 return;
16758 i = 0;
16759 for (li = l->lv_first; li != NULL; li = li->li_next)
16760 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016762 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016763 /* test the compare function */
16764 if (item_compare_func != NULL
16765 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16766 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016767 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769 {
16770 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016771 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016772 item_compare_func == NULL ? item_compare : item_compare2);
16773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016774 if (!item_compare_func_err)
16775 {
16776 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016777 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016778 l->lv_len = 0;
16779 for (i = 0; i < len; ++i)
16780 list_append(l, ptrs[i]);
16781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782 }
16783
16784 vim_free(ptrs);
16785 }
16786}
16787
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016788/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016789 * "soundfold({word})" function
16790 */
16791 static void
16792f_soundfold(argvars, rettv)
16793 typval_T *argvars;
16794 typval_T *rettv;
16795{
16796 char_u *s;
16797
16798 rettv->v_type = VAR_STRING;
16799 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016800#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016801 rettv->vval.v_string = eval_soundfold(s);
16802#else
16803 rettv->vval.v_string = vim_strsave(s);
16804#endif
16805}
16806
16807/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016808 * "spellbadword()" function
16809 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016810 static void
16811f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016812 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016813 typval_T *rettv;
16814{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016815 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016816 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016817 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016819 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016820 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016821
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016822#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016823 if (argvars[0].v_type == VAR_UNKNOWN)
16824 {
16825 /* Find the start and length of the badly spelled word. */
16826 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16827 if (len != 0)
16828 word = ml_get_cursor();
16829 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016830 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016831 {
16832 char_u *str = get_tv_string_chk(&argvars[0]);
16833 int capcol = -1;
16834
16835 if (str != NULL)
16836 {
16837 /* Check the argument for spelling. */
16838 while (*str != NUL)
16839 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016840 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016841 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016842 {
16843 word = str;
16844 break;
16845 }
16846 str += len;
16847 }
16848 }
16849 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016850#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016851
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016852 list_append_string(rettv->vval.v_list, word, len);
16853 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016854 attr == HLF_SPB ? "bad" :
16855 attr == HLF_SPR ? "rare" :
16856 attr == HLF_SPL ? "local" :
16857 attr == HLF_SPC ? "caps" :
16858 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016859}
16860
16861/*
16862 * "spellsuggest()" function
16863 */
16864 static void
16865f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016866 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016867 typval_T *rettv;
16868{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016869#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016870 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016871 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016872 int maxcount;
16873 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016874 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016875 listitem_T *li;
16876 int need_capital = FALSE;
16877#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016878
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016879 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016880 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016881
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016882#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016883 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016884 {
16885 str = get_tv_string(&argvars[0]);
16886 if (argvars[1].v_type != VAR_UNKNOWN)
16887 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016888 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016889 if (maxcount <= 0)
16890 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016891 if (argvars[2].v_type != VAR_UNKNOWN)
16892 {
16893 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16894 if (typeerr)
16895 return;
16896 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016897 }
16898 else
16899 maxcount = 25;
16900
Bram Moolenaar4770d092006-01-12 23:22:24 +000016901 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016902
16903 for (i = 0; i < ga.ga_len; ++i)
16904 {
16905 str = ((char_u **)ga.ga_data)[i];
16906
16907 li = listitem_alloc();
16908 if (li == NULL)
16909 vim_free(str);
16910 else
16911 {
16912 li->li_tv.v_type = VAR_STRING;
16913 li->li_tv.v_lock = 0;
16914 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016915 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016916 }
16917 }
16918 ga_clear(&ga);
16919 }
16920#endif
16921}
16922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016924f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016925 typval_T *argvars;
16926 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927{
16928 char_u *str;
16929 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016930 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 regmatch_T regmatch;
16932 char_u patbuf[NUMBUFLEN];
16933 char_u *save_cpo;
16934 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016936 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016937 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938
16939 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16940 save_cpo = p_cpo;
16941 p_cpo = (char_u *)"";
16942
16943 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016944 if (argvars[1].v_type != VAR_UNKNOWN)
16945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16947 if (pat == NULL)
16948 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016949 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016951 }
16952 if (pat == NULL || *pat == NUL)
16953 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016955 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 if (typeerr)
16958 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16961 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016964 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016966 if (*str == NUL)
16967 match = FALSE; /* empty item at the end */
16968 else
16969 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 if (match)
16971 end = regmatch.startp[0];
16972 else
16973 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016974 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16975 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016977 if (list_append_string(rettv->vval.v_list, str,
16978 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 }
16981 if (!match)
16982 break;
16983 /* Advance to just after the match. */
16984 if (regmatch.endp[0] > str)
16985 col = 0;
16986 else
16987 {
16988 /* Don't get stuck at the same match. */
16989#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016990 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991#else
16992 col = 1;
16993#endif
16994 }
16995 str = regmatch.endp[0];
16996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002}
17003
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017004#ifdef FEAT_FLOAT
17005/*
17006 * "sqrt()" function
17007 */
17008 static void
17009f_sqrt(argvars, rettv)
17010 typval_T *argvars;
17011 typval_T *rettv;
17012{
17013 float_T f;
17014
17015 rettv->v_type = VAR_FLOAT;
17016 if (get_float_arg(argvars, &f) == OK)
17017 rettv->vval.v_float = sqrt(f);
17018 else
17019 rettv->vval.v_float = 0.0;
17020}
17021
17022/*
17023 * "str2float()" function
17024 */
17025 static void
17026f_str2float(argvars, rettv)
17027 typval_T *argvars;
17028 typval_T *rettv;
17029{
17030 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17031
17032 if (*p == '+')
17033 p = skipwhite(p + 1);
17034 (void)string2float(p, &rettv->vval.v_float);
17035 rettv->v_type = VAR_FLOAT;
17036}
17037#endif
17038
Bram Moolenaar2c932302006-03-18 21:42:09 +000017039/*
17040 * "str2nr()" function
17041 */
17042 static void
17043f_str2nr(argvars, rettv)
17044 typval_T *argvars;
17045 typval_T *rettv;
17046{
17047 int base = 10;
17048 char_u *p;
17049 long n;
17050
17051 if (argvars[1].v_type != VAR_UNKNOWN)
17052 {
17053 base = get_tv_number(&argvars[1]);
17054 if (base != 8 && base != 10 && base != 16)
17055 {
17056 EMSG(_(e_invarg));
17057 return;
17058 }
17059 }
17060
17061 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017062 if (*p == '+')
17063 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017064 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17065 rettv->vval.v_number = n;
17066}
17067
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068#ifdef HAVE_STRFTIME
17069/*
17070 * "strftime({format}[, {time}])" function
17071 */
17072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017073f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017074 typval_T *argvars;
17075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076{
17077 char_u result_buf[256];
17078 struct tm *curtime;
17079 time_t seconds;
17080 char_u *p;
17081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017084 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017085 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 seconds = time(NULL);
17087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017088 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 curtime = localtime(&seconds);
17090 /* MSVC returns NULL for an invalid value of seconds. */
17091 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017092 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 else
17094 {
17095# ifdef FEAT_MBYTE
17096 vimconv_T conv;
17097 char_u *enc;
17098
17099 conv.vc_type = CONV_NONE;
17100 enc = enc_locale();
17101 convert_setup(&conv, p_enc, enc);
17102 if (conv.vc_type != CONV_NONE)
17103 p = string_convert(&conv, p, NULL);
17104# endif
17105 if (p != NULL)
17106 (void)strftime((char *)result_buf, sizeof(result_buf),
17107 (char *)p, curtime);
17108 else
17109 result_buf[0] = NUL;
17110
17111# ifdef FEAT_MBYTE
17112 if (conv.vc_type != CONV_NONE)
17113 vim_free(p);
17114 convert_setup(&conv, enc, p_enc);
17115 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 else
17118# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120
17121# ifdef FEAT_MBYTE
17122 /* Release conversion descriptors */
17123 convert_setup(&conv, NULL, NULL);
17124 vim_free(enc);
17125# endif
17126 }
17127}
17128#endif
17129
17130/*
17131 * "stridx()" function
17132 */
17133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017134f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 typval_T *argvars;
17136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137{
17138 char_u buf[NUMBUFLEN];
17139 char_u *needle;
17140 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017141 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017145 needle = get_tv_string_chk(&argvars[1]);
17146 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 if (needle == NULL || haystack == NULL)
17149 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 if (argvars[2].v_type != VAR_UNKNOWN)
17152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017153 int error = FALSE;
17154
17155 start_idx = get_tv_number_chk(&argvars[2], &error);
17156 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017158 if (start_idx >= 0)
17159 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017160 }
17161
17162 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17163 if (pos != NULL)
17164 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165}
17166
17167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017168 * "string()" function
17169 */
17170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017171f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017172 typval_T *argvars;
17173 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017174{
17175 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017176 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017179 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017180 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017181 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017182 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183}
17184
17185/*
17186 * "strlen()" function
17187 */
17188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017190 typval_T *argvars;
17191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193 rettv->vval.v_number = (varnumber_T)(STRLEN(
17194 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195}
17196
17197/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017198 * "strchars()" function
17199 */
17200 static void
17201f_strchars(argvars, rettv)
17202 typval_T *argvars;
17203 typval_T *rettv;
17204{
17205 char_u *s = get_tv_string(&argvars[0]);
17206#ifdef FEAT_MBYTE
17207 varnumber_T len = 0;
17208
17209 while (*s != NUL)
17210 {
17211 mb_cptr2char_adv(&s);
17212 ++len;
17213 }
17214 rettv->vval.v_number = len;
17215#else
17216 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17217#endif
17218}
17219
17220/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017221 * "strdisplaywidth()" function
17222 */
17223 static void
17224f_strdisplaywidth(argvars, rettv)
17225 typval_T *argvars;
17226 typval_T *rettv;
17227{
17228 char_u *s = get_tv_string(&argvars[0]);
17229 int col = 0;
17230
17231 if (argvars[1].v_type != VAR_UNKNOWN)
17232 col = get_tv_number(&argvars[1]);
17233
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017234 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017235}
17236
17237/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017238 * "strwidth()" function
17239 */
17240 static void
17241f_strwidth(argvars, rettv)
17242 typval_T *argvars;
17243 typval_T *rettv;
17244{
17245 char_u *s = get_tv_string(&argvars[0]);
17246
17247 rettv->vval.v_number = (varnumber_T)(
17248#ifdef FEAT_MBYTE
17249 mb_string2cells(s, -1)
17250#else
17251 STRLEN(s)
17252#endif
17253 );
17254}
17255
17256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 * "strpart()" function
17258 */
17259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017260f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017261 typval_T *argvars;
17262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263{
17264 char_u *p;
17265 int n;
17266 int len;
17267 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017268 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 slen = (int)STRLEN(p);
17272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017273 n = get_tv_number_chk(&argvars[1], &error);
17274 if (error)
17275 len = 0;
17276 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017277 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 else
17279 len = slen - n; /* default len: all bytes that are available. */
17280
17281 /*
17282 * Only return the overlap between the specified part and the actual
17283 * string.
17284 */
17285 if (n < 0)
17286 {
17287 len += n;
17288 n = 0;
17289 }
17290 else if (n > slen)
17291 n = slen;
17292 if (len < 0)
17293 len = 0;
17294 else if (n + len > slen)
17295 len = slen - n;
17296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv->v_type = VAR_STRING;
17298 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299}
17300
17301/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017302 * "strridx()" function
17303 */
17304 static void
17305f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017306 typval_T *argvars;
17307 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308{
17309 char_u buf[NUMBUFLEN];
17310 char_u *needle;
17311 char_u *haystack;
17312 char_u *rest;
17313 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017314 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017316 needle = get_tv_string_chk(&argvars[1]);
17317 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017318
17319 rettv->vval.v_number = -1;
17320 if (needle == NULL || haystack == NULL)
17321 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017322
17323 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017324 if (argvars[2].v_type != VAR_UNKNOWN)
17325 {
17326 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017327 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017328 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017329 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017330 }
17331 else
17332 end_idx = haystack_len;
17333
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017335 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017336 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017337 lastmatch = haystack + end_idx;
17338 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017340 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017341 for (rest = haystack; *rest != '\0'; ++rest)
17342 {
17343 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017344 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017345 break;
17346 lastmatch = rest;
17347 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017348 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017349
17350 if (lastmatch == NULL)
17351 rettv->vval.v_number = -1;
17352 else
17353 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17354}
17355
17356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 * "strtrans()" function
17358 */
17359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 typval_T *argvars;
17362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 rettv->v_type = VAR_STRING;
17365 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366}
17367
17368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017369 * "submatch()" function
17370 */
17371 static void
17372f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 typval_T *argvars;
17374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375{
17376 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 rettv->vval.v_string =
17378 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017379}
17380
17381/*
17382 * "substitute()" function
17383 */
17384 static void
17385f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017386 typval_T *argvars;
17387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017388{
17389 char_u patbuf[NUMBUFLEN];
17390 char_u subbuf[NUMBUFLEN];
17391 char_u flagsbuf[NUMBUFLEN];
17392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017393 char_u *str = get_tv_string_chk(&argvars[0]);
17394 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17395 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17396 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17397
Bram Moolenaar0d660222005-01-07 21:51:51 +000017398 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017399 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17400 rettv->vval.v_string = NULL;
17401 else
17402 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017403}
17404
17405/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017406 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412{
17413 int id = 0;
17414#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017415 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 long col;
17417 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017418 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017420 lnum = get_tv_lnum(argvars); /* -1 on type error */
17421 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17422 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017424 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017425 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017426 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427#endif
17428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017429 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430}
17431
17432/*
17433 * "synIDattr(id, what [, mode])" function
17434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017437 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439{
17440 char_u *p = NULL;
17441#ifdef FEAT_SYN_HL
17442 int id;
17443 char_u *what;
17444 char_u *mode;
17445 char_u modebuf[NUMBUFLEN];
17446 int modec;
17447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017448 id = get_tv_number(&argvars[0]);
17449 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017454 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 modec = 0; /* replace invalid with current */
17456 }
17457 else
17458 {
17459#ifdef FEAT_GUI
17460 if (gui.in_use)
17461 modec = 'g';
17462 else
17463#endif
17464 if (t_colors > 1)
17465 modec = 'c';
17466 else
17467 modec = 't';
17468 }
17469
17470
17471 switch (TOLOWER_ASC(what[0]))
17472 {
17473 case 'b':
17474 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17475 p = highlight_color(id, what, modec);
17476 else /* bold */
17477 p = highlight_has_attr(id, HL_BOLD, modec);
17478 break;
17479
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017480 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 p = highlight_color(id, what, modec);
17482 break;
17483
17484 case 'i':
17485 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17486 p = highlight_has_attr(id, HL_INVERSE, modec);
17487 else /* italic */
17488 p = highlight_has_attr(id, HL_ITALIC, modec);
17489 break;
17490
17491 case 'n': /* name */
17492 p = get_highlight_name(NULL, id - 1);
17493 break;
17494
17495 case 'r': /* reverse */
17496 p = highlight_has_attr(id, HL_INVERSE, modec);
17497 break;
17498
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017499 case 's':
17500 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17501 p = highlight_color(id, what, modec);
17502 else /* standout */
17503 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 break;
17505
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017506 case 'u':
17507 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17508 /* underline */
17509 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17510 else
17511 /* undercurl */
17512 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 break;
17514 }
17515
17516 if (p != NULL)
17517 p = vim_strsave(p);
17518#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 rettv->v_type = VAR_STRING;
17520 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521}
17522
17523/*
17524 * "synIDtrans(id)" function
17525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017528 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530{
17531 int id;
17532
17533#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535
17536 if (id > 0)
17537 id = syn_get_final_id(id);
17538 else
17539#endif
17540 id = 0;
17541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543}
17544
17545/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017546 * "synconcealed(lnum, col)" function
17547 */
17548 static void
17549f_synconcealed(argvars, rettv)
17550 typval_T *argvars UNUSED;
17551 typval_T *rettv;
17552{
17553#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17554 long lnum;
17555 long col;
17556 int syntax_flags = 0;
17557 int cchar;
17558 int matchid = 0;
17559 char_u str[NUMBUFLEN];
17560#endif
17561
17562 rettv->v_type = VAR_LIST;
17563 rettv->vval.v_list = NULL;
17564
17565#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17566 lnum = get_tv_lnum(argvars); /* -1 on type error */
17567 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17568
17569 vim_memset(str, NUL, sizeof(str));
17570
17571 if (rettv_list_alloc(rettv) != FAIL)
17572 {
17573 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17574 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17575 && curwin->w_p_cole > 0)
17576 {
17577 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17578 syntax_flags = get_syntax_info(&matchid);
17579
17580 /* get the conceal character */
17581 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17582 {
17583 cchar = syn_get_sub_char();
17584 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17585 cchar = lcs_conceal;
17586 if (cchar != NUL)
17587 {
17588# ifdef FEAT_MBYTE
17589 if (has_mbyte)
17590 (*mb_char2bytes)(cchar, str);
17591 else
17592# endif
17593 str[0] = cchar;
17594 }
17595 }
17596 }
17597
17598 list_append_number(rettv->vval.v_list,
17599 (syntax_flags & HL_CONCEAL) != 0);
17600 /* -1 to auto-determine strlen */
17601 list_append_string(rettv->vval.v_list, str, -1);
17602 list_append_number(rettv->vval.v_list, matchid);
17603 }
17604#endif
17605}
17606
17607/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017608 * "synstack(lnum, col)" function
17609 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017610 static void
17611f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017612 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017613 typval_T *rettv;
17614{
17615#ifdef FEAT_SYN_HL
17616 long lnum;
17617 long col;
17618 int i;
17619 int id;
17620#endif
17621
17622 rettv->v_type = VAR_LIST;
17623 rettv->vval.v_list = NULL;
17624
17625#ifdef FEAT_SYN_HL
17626 lnum = get_tv_lnum(argvars); /* -1 on type error */
17627 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17628
17629 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017630 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017631 && rettv_list_alloc(rettv) != FAIL)
17632 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017633 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017634 for (i = 0; ; ++i)
17635 {
17636 id = syn_get_stack_item(i);
17637 if (id < 0)
17638 break;
17639 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17640 break;
17641 }
17642 }
17643#endif
17644}
17645
17646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 * "system()" function
17648 */
17649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017651 typval_T *argvars;
17652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017654 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017656 char_u *infile = NULL;
17657 char_u buf[NUMBUFLEN];
17658 int err = FALSE;
17659 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017661 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017662 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017663
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017664 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017665 {
17666 /*
17667 * Write the string to a temp file, to be used for input of the shell
17668 * command.
17669 */
17670 if ((infile = vim_tempname('i')) == NULL)
17671 {
17672 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017673 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017674 }
17675
17676 fd = mch_fopen((char *)infile, WRITEBIN);
17677 if (fd == NULL)
17678 {
17679 EMSG2(_(e_notopen), infile);
17680 goto done;
17681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017682 p = get_tv_string_buf_chk(&argvars[1], buf);
17683 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017684 {
17685 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017686 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017687 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017688 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17689 err = TRUE;
17690 if (fclose(fd) != 0)
17691 err = TRUE;
17692 if (err)
17693 {
17694 EMSG(_("E677: Error writing temp file"));
17695 goto done;
17696 }
17697 }
17698
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017699 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17700 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017701
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702#ifdef USE_CR
17703 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017704 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 {
17706 char_u *s;
17707
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017708 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 {
17710 if (*s == CAR)
17711 *s = NL;
17712 }
17713 }
17714#else
17715# ifdef USE_CRNL
17716 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017717 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 {
17719 char_u *s, *d;
17720
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017721 d = res;
17722 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 {
17724 if (s[0] == CAR && s[1] == NL)
17725 ++s;
17726 *d++ = *s;
17727 }
17728 *d = NUL;
17729 }
17730# endif
17731#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017732
17733done:
17734 if (infile != NULL)
17735 {
17736 mch_remove(infile);
17737 vim_free(infile);
17738 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739 rettv->v_type = VAR_STRING;
17740 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741}
17742
17743/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017744 * "tabpagebuflist()" function
17745 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017746 static void
17747f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017748 typval_T *argvars UNUSED;
17749 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017750{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017751#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017752 tabpage_T *tp;
17753 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017754
17755 if (argvars[0].v_type == VAR_UNKNOWN)
17756 wp = firstwin;
17757 else
17758 {
17759 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17760 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017761 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017762 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017763 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017764 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017765 for (; wp != NULL; wp = wp->w_next)
17766 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017767 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017768 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017769 }
17770#endif
17771}
17772
17773
17774/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017775 * "tabpagenr()" function
17776 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017777 static void
17778f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017779 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017780 typval_T *rettv;
17781{
17782 int nr = 1;
17783#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017784 char_u *arg;
17785
17786 if (argvars[0].v_type != VAR_UNKNOWN)
17787 {
17788 arg = get_tv_string_chk(&argvars[0]);
17789 nr = 0;
17790 if (arg != NULL)
17791 {
17792 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017793 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017794 else
17795 EMSG2(_(e_invexpr2), arg);
17796 }
17797 }
17798 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017799 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017800#endif
17801 rettv->vval.v_number = nr;
17802}
17803
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017804
17805#ifdef FEAT_WINDOWS
17806static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17807
17808/*
17809 * Common code for tabpagewinnr() and winnr().
17810 */
17811 static int
17812get_winnr(tp, argvar)
17813 tabpage_T *tp;
17814 typval_T *argvar;
17815{
17816 win_T *twin;
17817 int nr = 1;
17818 win_T *wp;
17819 char_u *arg;
17820
17821 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17822 if (argvar->v_type != VAR_UNKNOWN)
17823 {
17824 arg = get_tv_string_chk(argvar);
17825 if (arg == NULL)
17826 nr = 0; /* type error; errmsg already given */
17827 else if (STRCMP(arg, "$") == 0)
17828 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17829 else if (STRCMP(arg, "#") == 0)
17830 {
17831 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17832 if (twin == NULL)
17833 nr = 0;
17834 }
17835 else
17836 {
17837 EMSG2(_(e_invexpr2), arg);
17838 nr = 0;
17839 }
17840 }
17841
17842 if (nr > 0)
17843 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17844 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017845 {
17846 if (wp == NULL)
17847 {
17848 /* didn't find it in this tabpage */
17849 nr = 0;
17850 break;
17851 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017852 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017853 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017854 return nr;
17855}
17856#endif
17857
17858/*
17859 * "tabpagewinnr()" function
17860 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017861 static void
17862f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017863 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017864 typval_T *rettv;
17865{
17866 int nr = 1;
17867#ifdef FEAT_WINDOWS
17868 tabpage_T *tp;
17869
17870 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17871 if (tp == NULL)
17872 nr = 0;
17873 else
17874 nr = get_winnr(tp, &argvars[1]);
17875#endif
17876 rettv->vval.v_number = nr;
17877}
17878
17879
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017880/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017881 * "tagfiles()" function
17882 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017883 static void
17884f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017885 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017886 typval_T *rettv;
17887{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017888 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017889 tagname_T tn;
17890 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017891
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017892 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017893 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017894 fname = alloc(MAXPATHL);
17895 if (fname == NULL)
17896 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017898 for (first = TRUE; ; first = FALSE)
17899 if (get_tagfname(&tn, first, fname) == FAIL
17900 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017901 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017902 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017903 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017904}
17905
17906/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017907 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017908 */
17909 static void
17910f_taglist(argvars, rettv)
17911 typval_T *argvars;
17912 typval_T *rettv;
17913{
17914 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017915
17916 tag_pattern = get_tv_string(&argvars[0]);
17917
17918 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017919 if (*tag_pattern == NUL)
17920 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017921
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017922 if (rettv_list_alloc(rettv) == OK)
17923 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017924}
17925
17926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 * "tempname()" function
17928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017930f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933{
17934 static int x = 'A';
17935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936 rettv->v_type = VAR_STRING;
17937 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938
17939 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17940 * names. Skip 'I' and 'O', they are used for shell redirection. */
17941 do
17942 {
17943 if (x == 'Z')
17944 x = '0';
17945 else if (x == '9')
17946 x = 'A';
17947 else
17948 {
17949#ifdef EBCDIC
17950 if (x == 'I')
17951 x = 'J';
17952 else if (x == 'R')
17953 x = 'S';
17954 else
17955#endif
17956 ++x;
17957 }
17958 } while (x == 'I' || x == 'O');
17959}
17960
17961/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017962 * "test(list)" function: Just checking the walls...
17963 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017964 static void
17965f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017966 typval_T *argvars UNUSED;
17967 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017968{
17969 /* Used for unit testing. Change the code below to your liking. */
17970#if 0
17971 listitem_T *li;
17972 list_T *l;
17973 char_u *bad, *good;
17974
17975 if (argvars[0].v_type != VAR_LIST)
17976 return;
17977 l = argvars[0].vval.v_list;
17978 if (l == NULL)
17979 return;
17980 li = l->lv_first;
17981 if (li == NULL)
17982 return;
17983 bad = get_tv_string(&li->li_tv);
17984 li = li->li_next;
17985 if (li == NULL)
17986 return;
17987 good = get_tv_string(&li->li_tv);
17988 rettv->vval.v_number = test_edit_score(bad, good);
17989#endif
17990}
17991
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017992#ifdef FEAT_FLOAT
17993/*
17994 * "tan()" function
17995 */
17996 static void
17997f_tan(argvars, rettv)
17998 typval_T *argvars;
17999 typval_T *rettv;
18000{
18001 float_T f;
18002
18003 rettv->v_type = VAR_FLOAT;
18004 if (get_float_arg(argvars, &f) == OK)
18005 rettv->vval.v_float = tan(f);
18006 else
18007 rettv->vval.v_float = 0.0;
18008}
18009
18010/*
18011 * "tanh()" function
18012 */
18013 static void
18014f_tanh(argvars, rettv)
18015 typval_T *argvars;
18016 typval_T *rettv;
18017{
18018 float_T f;
18019
18020 rettv->v_type = VAR_FLOAT;
18021 if (get_float_arg(argvars, &f) == OK)
18022 rettv->vval.v_float = tanh(f);
18023 else
18024 rettv->vval.v_float = 0.0;
18025}
18026#endif
18027
Bram Moolenaard52d9742005-08-21 22:20:28 +000018028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 * "tolower(string)" function
18030 */
18031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018033 typval_T *argvars;
18034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035{
18036 char_u *p;
18037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018038 p = vim_strsave(get_tv_string(&argvars[0]));
18039 rettv->v_type = VAR_STRING;
18040 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041
18042 if (p != NULL)
18043 while (*p != NUL)
18044 {
18045#ifdef FEAT_MBYTE
18046 int l;
18047
18048 if (enc_utf8)
18049 {
18050 int c, lc;
18051
18052 c = utf_ptr2char(p);
18053 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018054 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 /* TODO: reallocate string when byte count changes. */
18056 if (utf_char2len(lc) == l)
18057 utf_char2bytes(lc, p);
18058 p += l;
18059 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018060 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 p += l; /* skip multi-byte character */
18062 else
18063#endif
18064 {
18065 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18066 ++p;
18067 }
18068 }
18069}
18070
18071/*
18072 * "toupper(string)" function
18073 */
18074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018075f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 typval_T *argvars;
18077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018079 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018080 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081}
18082
18083/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018084 * "tr(string, fromstr, tostr)" function
18085 */
18086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018088 typval_T *argvars;
18089 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018090{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018091 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018092 char_u *fromstr;
18093 char_u *tostr;
18094 char_u *p;
18095#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018096 int inlen;
18097 int fromlen;
18098 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018099 int idx;
18100 char_u *cpstr;
18101 int cplen;
18102 int first = TRUE;
18103#endif
18104 char_u buf[NUMBUFLEN];
18105 char_u buf2[NUMBUFLEN];
18106 garray_T ga;
18107
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018108 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018109 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18110 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018111
18112 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018113 rettv->v_type = VAR_STRING;
18114 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018115 if (fromstr == NULL || tostr == NULL)
18116 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018117 ga_init2(&ga, (int)sizeof(char), 80);
18118
18119#ifdef FEAT_MBYTE
18120 if (!has_mbyte)
18121#endif
18122 /* not multi-byte: fromstr and tostr must be the same length */
18123 if (STRLEN(fromstr) != STRLEN(tostr))
18124 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018125#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018126error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018127#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018128 EMSG2(_(e_invarg2), fromstr);
18129 ga_clear(&ga);
18130 return;
18131 }
18132
18133 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018134 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018135 {
18136#ifdef FEAT_MBYTE
18137 if (has_mbyte)
18138 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018139 inlen = (*mb_ptr2len)(in_str);
18140 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018141 cplen = inlen;
18142 idx = 0;
18143 for (p = fromstr; *p != NUL; p += fromlen)
18144 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018145 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018146 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018147 {
18148 for (p = tostr; *p != NUL; p += tolen)
18149 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018150 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018151 if (idx-- == 0)
18152 {
18153 cplen = tolen;
18154 cpstr = p;
18155 break;
18156 }
18157 }
18158 if (*p == NUL) /* tostr is shorter than fromstr */
18159 goto error;
18160 break;
18161 }
18162 ++idx;
18163 }
18164
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018165 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018166 {
18167 /* Check that fromstr and tostr have the same number of
18168 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018169 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018170 first = FALSE;
18171 for (p = tostr; *p != NUL; p += tolen)
18172 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018173 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018174 --idx;
18175 }
18176 if (idx != 0)
18177 goto error;
18178 }
18179
18180 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018181 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018182 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018183
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018184 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018185 }
18186 else
18187#endif
18188 {
18189 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018190 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018191 if (p != NULL)
18192 ga_append(&ga, tostr[p - fromstr]);
18193 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018194 ga_append(&ga, *in_str);
18195 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018196 }
18197 }
18198
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018199 /* add a terminating NUL */
18200 ga_grow(&ga, 1);
18201 ga_append(&ga, NUL);
18202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018203 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018204}
18205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018206#ifdef FEAT_FLOAT
18207/*
18208 * "trunc({float})" function
18209 */
18210 static void
18211f_trunc(argvars, rettv)
18212 typval_T *argvars;
18213 typval_T *rettv;
18214{
18215 float_T f;
18216
18217 rettv->v_type = VAR_FLOAT;
18218 if (get_float_arg(argvars, &f) == OK)
18219 /* trunc() is not in C90, use floor() or ceil() instead. */
18220 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18221 else
18222 rettv->vval.v_float = 0.0;
18223}
18224#endif
18225
Bram Moolenaar8299df92004-07-10 09:47:34 +000018226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 * "type(expr)" function
18228 */
18229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018230f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018231 typval_T *argvars;
18232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018234 int n;
18235
18236 switch (argvars[0].v_type)
18237 {
18238 case VAR_NUMBER: n = 0; break;
18239 case VAR_STRING: n = 1; break;
18240 case VAR_FUNC: n = 2; break;
18241 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018242 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018243#ifdef FEAT_FLOAT
18244 case VAR_FLOAT: n = 5; break;
18245#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018246 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18247 }
18248 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249}
18250
18251/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018252 * "undofile(name)" function
18253 */
18254 static void
18255f_undofile(argvars, rettv)
18256 typval_T *argvars;
18257 typval_T *rettv;
18258{
18259 rettv->v_type = VAR_STRING;
18260#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018261 {
18262 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18263
18264 if (ffname != NULL)
18265 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18266 vim_free(ffname);
18267 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018268#else
18269 rettv->vval.v_string = NULL;
18270#endif
18271}
18272
18273/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018274 * "undotree()" function
18275 */
18276 static void
18277f_undotree(argvars, rettv)
18278 typval_T *argvars UNUSED;
18279 typval_T *rettv;
18280{
18281 if (rettv_dict_alloc(rettv) == OK)
18282 {
18283 dict_T *dict = rettv->vval.v_dict;
18284 list_T *list;
18285
Bram Moolenaar730cde92010-06-27 05:18:54 +020018286 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018287 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018288 dict_add_nr_str(dict, "save_last",
18289 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018290 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18291 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018292 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018293
18294 list = list_alloc();
18295 if (list != NULL)
18296 {
18297 u_eval_tree(curbuf->b_u_oldhead, list);
18298 dict_add_list(dict, "entries", list);
18299 }
18300 }
18301}
18302
18303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018304 * "values(dict)" function
18305 */
18306 static void
18307f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018308 typval_T *argvars;
18309 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018310{
18311 dict_list(argvars, rettv, 1);
18312}
18313
18314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 * "virtcol(string)" function
18316 */
18317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018318f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018319 typval_T *argvars;
18320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321{
18322 colnr_T vcol = 0;
18323 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018324 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018326 fp = var2fpos(&argvars[0], FALSE, &fnum);
18327 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18328 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 {
18330 getvvcol(curwin, fp, NULL, NULL, &vcol);
18331 ++vcol;
18332 }
18333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335}
18336
18337/*
18338 * "visualmode()" function
18339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018341f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018342 typval_T *argvars UNUSED;
18343 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344{
18345#ifdef FEAT_VISUAL
18346 char_u str[2];
18347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018348 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 str[0] = curbuf->b_visual_mode_eval;
18350 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352
18353 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018354 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356#endif
18357}
18358
18359/*
18360 * "winbufnr(nr)" function
18361 */
18362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018363f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018364 typval_T *argvars;
18365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366{
18367 win_T *wp;
18368
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018369 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018373 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374}
18375
18376/*
18377 * "wincol()" function
18378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018380f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018381 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383{
18384 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386}
18387
18388/*
18389 * "winheight(nr)" function
18390 */
18391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018393 typval_T *argvars;
18394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395{
18396 win_T *wp;
18397
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018398 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403}
18404
18405/*
18406 * "winline()" function
18407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412{
18413 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018414 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415}
18416
18417/*
18418 * "winnr()" function
18419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018422 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424{
18425 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018426
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018428 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018430 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431}
18432
18433/*
18434 * "winrestcmd()" function
18435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018437f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440{
18441#ifdef FEAT_WINDOWS
18442 win_T *wp;
18443 int winnr = 1;
18444 garray_T ga;
18445 char_u buf[50];
18446
18447 ga_init2(&ga, (int)sizeof(char), 70);
18448 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18449 {
18450 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18451 ga_concat(&ga, buf);
18452# ifdef FEAT_VERTSPLIT
18453 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18454 ga_concat(&ga, buf);
18455# endif
18456 ++winnr;
18457 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018458 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018460 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018462 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465}
18466
18467/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018468 * "winrestview()" function
18469 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018470 static void
18471f_winrestview(argvars, rettv)
18472 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018473 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018474{
18475 dict_T *dict;
18476
18477 if (argvars[0].v_type != VAR_DICT
18478 || (dict = argvars[0].vval.v_dict) == NULL)
18479 EMSG(_(e_invarg));
18480 else
18481 {
18482 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18483 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18484#ifdef FEAT_VIRTUALEDIT
18485 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18486#endif
18487 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018488 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018489
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018490 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018491#ifdef FEAT_DIFF
18492 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18493#endif
18494 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18495 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18496
18497 check_cursor();
18498 changed_cline_bef_curs();
18499 invalidate_botline();
18500 redraw_later(VALID);
18501
18502 if (curwin->w_topline == 0)
18503 curwin->w_topline = 1;
18504 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18505 curwin->w_topline = curbuf->b_ml.ml_line_count;
18506#ifdef FEAT_DIFF
18507 check_topfill(curwin, TRUE);
18508#endif
18509 }
18510}
18511
18512/*
18513 * "winsaveview()" function
18514 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018515 static void
18516f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018517 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018518 typval_T *rettv;
18519{
18520 dict_T *dict;
18521
Bram Moolenaara800b422010-06-27 01:15:55 +020018522 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018523 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018524 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018525
18526 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18527 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18528#ifdef FEAT_VIRTUALEDIT
18529 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18530#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018531 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018532 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18533
18534 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18535#ifdef FEAT_DIFF
18536 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18537#endif
18538 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18539 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18540}
18541
18542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 * "winwidth(nr)" function
18544 */
18545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018546f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018547 typval_T *argvars;
18548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549{
18550 win_T *wp;
18551
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018552 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 else
18556#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018557 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560#endif
18561}
18562
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018564 * "writefile()" function
18565 */
18566 static void
18567f_writefile(argvars, rettv)
18568 typval_T *argvars;
18569 typval_T *rettv;
18570{
18571 int binary = FALSE;
18572 char_u *fname;
18573 FILE *fd;
18574 listitem_T *li;
18575 char_u *s;
18576 int ret = 0;
18577 int c;
18578
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018579 if (check_restricted() || check_secure())
18580 return;
18581
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018582 if (argvars[0].v_type != VAR_LIST)
18583 {
18584 EMSG2(_(e_listarg), "writefile()");
18585 return;
18586 }
18587 if (argvars[0].vval.v_list == NULL)
18588 return;
18589
18590 if (argvars[2].v_type != VAR_UNKNOWN
18591 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18592 binary = TRUE;
18593
18594 /* Always open the file in binary mode, library functions have a mind of
18595 * their own about CR-LF conversion. */
18596 fname = get_tv_string(&argvars[1]);
18597 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18598 {
18599 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18600 ret = -1;
18601 }
18602 else
18603 {
18604 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18605 li = li->li_next)
18606 {
18607 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18608 {
18609 if (*s == '\n')
18610 c = putc(NUL, fd);
18611 else
18612 c = putc(*s, fd);
18613 if (c == EOF)
18614 {
18615 ret = -1;
18616 break;
18617 }
18618 }
18619 if (!binary || li->li_next != NULL)
18620 if (putc('\n', fd) == EOF)
18621 {
18622 ret = -1;
18623 break;
18624 }
18625 if (ret < 0)
18626 {
18627 EMSG(_(e_write));
18628 break;
18629 }
18630 }
18631 fclose(fd);
18632 }
18633
18634 rettv->vval.v_number = ret;
18635}
18636
18637/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018638 * "xor(expr, expr)" function
18639 */
18640 static void
18641f_xor(argvars, rettv)
18642 typval_T *argvars;
18643 typval_T *rettv;
18644{
18645 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18646 ^ get_tv_number_chk(&argvars[1], NULL);
18647}
18648
18649
18650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018652 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 */
18654 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018655var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018656 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018657 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018658 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018660 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018662 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663
Bram Moolenaara5525202006-03-02 22:52:09 +000018664 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018665 if (varp->v_type == VAR_LIST)
18666 {
18667 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018668 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018669 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018670 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018671
18672 l = varp->vval.v_list;
18673 if (l == NULL)
18674 return NULL;
18675
18676 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018677 pos.lnum = list_find_nr(l, 0L, &error);
18678 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018679 return NULL; /* invalid line number */
18680
18681 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018682 pos.col = list_find_nr(l, 1L, &error);
18683 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018684 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018685 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018686
18687 /* We accept "$" for the column number: last column. */
18688 li = list_find(l, 1L);
18689 if (li != NULL && li->li_tv.v_type == VAR_STRING
18690 && li->li_tv.vval.v_string != NULL
18691 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18692 pos.col = len + 1;
18693
Bram Moolenaara5525202006-03-02 22:52:09 +000018694 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018695 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018696 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018697 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018698
Bram Moolenaara5525202006-03-02 22:52:09 +000018699#ifdef FEAT_VIRTUALEDIT
18700 /* Get the virtual offset. Defaults to zero. */
18701 pos.coladd = list_find_nr(l, 2L, &error);
18702 if (error)
18703 pos.coladd = 0;
18704#endif
18705
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018706 return &pos;
18707 }
18708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018709 name = get_tv_string_chk(varp);
18710 if (name == NULL)
18711 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018712 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018714#ifdef FEAT_VISUAL
18715 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18716 {
18717 if (VIsual_active)
18718 return &VIsual;
18719 return &curwin->w_cursor;
18720 }
18721#endif
18722 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018724 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18726 return NULL;
18727 return pp;
18728 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018729
18730#ifdef FEAT_VIRTUALEDIT
18731 pos.coladd = 0;
18732#endif
18733
Bram Moolenaar477933c2007-07-17 14:32:23 +000018734 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018735 {
18736 pos.col = 0;
18737 if (name[1] == '0') /* "w0": first visible line */
18738 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018739 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018740 pos.lnum = curwin->w_topline;
18741 return &pos;
18742 }
18743 else if (name[1] == '$') /* "w$": last visible line */
18744 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018745 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018746 pos.lnum = curwin->w_botline - 1;
18747 return &pos;
18748 }
18749 }
18750 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018752 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 {
18754 pos.lnum = curbuf->b_ml.ml_line_count;
18755 pos.col = 0;
18756 }
18757 else
18758 {
18759 pos.lnum = curwin->w_cursor.lnum;
18760 pos.col = (colnr_T)STRLEN(ml_get_curline());
18761 }
18762 return &pos;
18763 }
18764 return NULL;
18765}
18766
18767/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018768 * Convert list in "arg" into a position and optional file number.
18769 * When "fnump" is NULL there is no file number, only 3 items.
18770 * Note that the column is passed on as-is, the caller may want to decrement
18771 * it to use 1 for the first column.
18772 * Return FAIL when conversion is not possible, doesn't check the position for
18773 * validity.
18774 */
18775 static int
18776list2fpos(arg, posp, fnump)
18777 typval_T *arg;
18778 pos_T *posp;
18779 int *fnump;
18780{
18781 list_T *l = arg->vval.v_list;
18782 long i = 0;
18783 long n;
18784
Bram Moolenaarbde35262006-07-23 20:12:24 +000018785 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18786 * when "fnump" isn't NULL and "coladd" is optional. */
18787 if (arg->v_type != VAR_LIST
18788 || l == NULL
18789 || l->lv_len < (fnump == NULL ? 2 : 3)
18790 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018791 return FAIL;
18792
18793 if (fnump != NULL)
18794 {
18795 n = list_find_nr(l, i++, NULL); /* fnum */
18796 if (n < 0)
18797 return FAIL;
18798 if (n == 0)
18799 n = curbuf->b_fnum; /* current buffer */
18800 *fnump = n;
18801 }
18802
18803 n = list_find_nr(l, i++, NULL); /* lnum */
18804 if (n < 0)
18805 return FAIL;
18806 posp->lnum = n;
18807
18808 n = list_find_nr(l, i++, NULL); /* col */
18809 if (n < 0)
18810 return FAIL;
18811 posp->col = n;
18812
18813#ifdef FEAT_VIRTUALEDIT
18814 n = list_find_nr(l, i, NULL);
18815 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018816 posp->coladd = 0;
18817 else
18818 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018819#endif
18820
18821 return OK;
18822}
18823
18824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 * Get the length of an environment variable name.
18826 * Advance "arg" to the first character after the name.
18827 * Return 0 for error.
18828 */
18829 static int
18830get_env_len(arg)
18831 char_u **arg;
18832{
18833 char_u *p;
18834 int len;
18835
18836 for (p = *arg; vim_isIDc(*p); ++p)
18837 ;
18838 if (p == *arg) /* no name found */
18839 return 0;
18840
18841 len = (int)(p - *arg);
18842 *arg = p;
18843 return len;
18844}
18845
18846/*
18847 * Get the length of the name of a function or internal variable.
18848 * "arg" is advanced to the first non-white character after the name.
18849 * Return 0 if something is wrong.
18850 */
18851 static int
18852get_id_len(arg)
18853 char_u **arg;
18854{
18855 char_u *p;
18856 int len;
18857
18858 /* Find the end of the name. */
18859 for (p = *arg; eval_isnamec(*p); ++p)
18860 ;
18861 if (p == *arg) /* no name found */
18862 return 0;
18863
18864 len = (int)(p - *arg);
18865 *arg = skipwhite(p);
18866
18867 return len;
18868}
18869
18870/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018871 * Get the length of the name of a variable or function.
18872 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018874 * Return -1 if curly braces expansion failed.
18875 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 * If the name contains 'magic' {}'s, expand them and return the
18877 * expanded name in an allocated string via 'alias' - caller must free.
18878 */
18879 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018880get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 char_u **arg;
18882 char_u **alias;
18883 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018884 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
18886 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 char_u *p;
18888 char_u *expr_start;
18889 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890
18891 *alias = NULL; /* default to no alias */
18892
18893 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18894 && (*arg)[2] == (int)KE_SNR)
18895 {
18896 /* hard coded <SNR>, already translated */
18897 *arg += 3;
18898 return get_id_len(arg) + 3;
18899 }
18900 len = eval_fname_script(*arg);
18901 if (len > 0)
18902 {
18903 /* literal "<SID>", "s:" or "<SNR>" */
18904 *arg += len;
18905 }
18906
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018908 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018910 p = find_name_end(*arg, &expr_start, &expr_end,
18911 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 if (expr_start != NULL)
18913 {
18914 char_u *temp_string;
18915
18916 if (!evaluate)
18917 {
18918 len += (int)(p - *arg);
18919 *arg = skipwhite(p);
18920 return len;
18921 }
18922
18923 /*
18924 * Include any <SID> etc in the expanded string:
18925 * Thus the -len here.
18926 */
18927 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18928 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018929 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 *alias = temp_string;
18931 *arg = skipwhite(p);
18932 return (int)STRLEN(temp_string);
18933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934
18935 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018936 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 EMSG2(_(e_invexpr2), *arg);
18938
18939 return len;
18940}
18941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942/*
18943 * Find the end of a variable or function name, taking care of magic braces.
18944 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18945 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018946 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018947 * Return a pointer to just after the name. Equal to "arg" if there is no
18948 * valid name.
18949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018951find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 char_u *arg;
18953 char_u **expr_start;
18954 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018955 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018957 int mb_nest = 0;
18958 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 char_u *p;
18960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018961 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963 *expr_start = NULL;
18964 *expr_end = NULL;
18965 }
18966
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018967 /* Quick check for valid starting character. */
18968 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18969 return arg;
18970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018971 for (p = arg; *p != NUL
18972 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018973 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018974 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018976 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018977 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018978 if (*p == '\'')
18979 {
18980 /* skip over 'string' to avoid counting [ and ] inside it. */
18981 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18982 ;
18983 if (*p == NUL)
18984 break;
18985 }
18986 else if (*p == '"')
18987 {
18988 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18989 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18990 if (*p == '\\' && p[1] != NUL)
18991 ++p;
18992 if (*p == NUL)
18993 break;
18994 }
18995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018996 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018998 if (*p == '[')
18999 ++br_nest;
19000 else if (*p == ']')
19001 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019004 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 if (*p == '{')
19007 {
19008 mb_nest++;
19009 if (expr_start != NULL && *expr_start == NULL)
19010 *expr_start = p;
19011 }
19012 else if (*p == '}')
19013 {
19014 mb_nest--;
19015 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19016 *expr_end = p;
19017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 }
19020
19021 return p;
19022}
19023
19024/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019025 * Expands out the 'magic' {}'s in a variable/function name.
19026 * Note that this can call itself recursively, to deal with
19027 * constructs like foo{bar}{baz}{bam}
19028 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19029 * "in_start" ^
19030 * "expr_start" ^
19031 * "expr_end" ^
19032 * "in_end" ^
19033 *
19034 * Returns a new allocated string, which the caller must free.
19035 * Returns NULL for failure.
19036 */
19037 static char_u *
19038make_expanded_name(in_start, expr_start, expr_end, in_end)
19039 char_u *in_start;
19040 char_u *expr_start;
19041 char_u *expr_end;
19042 char_u *in_end;
19043{
19044 char_u c1;
19045 char_u *retval = NULL;
19046 char_u *temp_result;
19047 char_u *nextcmd = NULL;
19048
19049 if (expr_end == NULL || in_end == NULL)
19050 return NULL;
19051 *expr_start = NUL;
19052 *expr_end = NUL;
19053 c1 = *in_end;
19054 *in_end = NUL;
19055
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019056 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019057 if (temp_result != NULL && nextcmd == NULL)
19058 {
19059 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19060 + (in_end - expr_end) + 1));
19061 if (retval != NULL)
19062 {
19063 STRCPY(retval, in_start);
19064 STRCAT(retval, temp_result);
19065 STRCAT(retval, expr_end + 1);
19066 }
19067 }
19068 vim_free(temp_result);
19069
19070 *in_end = c1; /* put char back for error messages */
19071 *expr_start = '{';
19072 *expr_end = '}';
19073
19074 if (retval != NULL)
19075 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019076 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019077 if (expr_start != NULL)
19078 {
19079 /* Further expansion! */
19080 temp_result = make_expanded_name(retval, expr_start,
19081 expr_end, temp_result);
19082 vim_free(retval);
19083 retval = temp_result;
19084 }
19085 }
19086
19087 return retval;
19088}
19089
19090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019092 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 */
19094 static int
19095eval_isnamec(c)
19096 int c;
19097{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019098 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19099}
19100
19101/*
19102 * Return TRUE if character "c" can be used as the first character in a
19103 * variable or function name (excluding '{' and '}').
19104 */
19105 static int
19106eval_isnamec1(c)
19107 int c;
19108{
19109 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110}
19111
19112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 * Set number v: variable to "val".
19114 */
19115 void
19116set_vim_var_nr(idx, val)
19117 int idx;
19118 long val;
19119{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019120 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121}
19122
19123/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019124 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 */
19126 long
19127get_vim_var_nr(idx)
19128 int idx;
19129{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019130 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131}
19132
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019133/*
19134 * Get string v: variable value. Uses a static buffer, can only be used once.
19135 */
19136 char_u *
19137get_vim_var_str(idx)
19138 int idx;
19139{
19140 return get_tv_string(&vimvars[idx].vv_tv);
19141}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019142
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019144 * Get List v: variable value. Caller must take care of reference count when
19145 * needed.
19146 */
19147 list_T *
19148get_vim_var_list(idx)
19149 int idx;
19150{
19151 return vimvars[idx].vv_list;
19152}
19153
19154/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019155 * Set v:char to character "c".
19156 */
19157 void
19158set_vim_var_char(c)
19159 int c;
19160{
19161#ifdef FEAT_MBYTE
19162 char_u buf[MB_MAXBYTES];
19163#else
19164 char_u buf[2];
19165#endif
19166
19167#ifdef FEAT_MBYTE
19168 if (has_mbyte)
19169 buf[(*mb_char2bytes)(c, buf)] = NUL;
19170 else
19171#endif
19172 {
19173 buf[0] = c;
19174 buf[1] = NUL;
19175 }
19176 set_vim_var_string(VV_CHAR, buf, -1);
19177}
19178
19179/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019180 * Set v:count to "count" and v:count1 to "count1".
19181 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 */
19183 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019184set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 long count;
19186 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019187 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019189 if (set_prevcount)
19190 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019191 vimvars[VV_COUNT].vv_nr = count;
19192 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193}
19194
19195/*
19196 * Set string v: variable to a copy of "val".
19197 */
19198 void
19199set_vim_var_string(idx, val, len)
19200 int idx;
19201 char_u *val;
19202 int len; /* length of "val" to use or -1 (whole string) */
19203{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019204 /* Need to do this (at least) once, since we can't initialize a union.
19205 * Will always be invoked when "v:progname" is set. */
19206 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19207
Bram Moolenaare9a41262005-01-15 22:18:47 +000019208 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019210 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019212 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019214 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215}
19216
19217/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019218 * Set List v: variable to "val".
19219 */
19220 void
19221set_vim_var_list(idx, val)
19222 int idx;
19223 list_T *val;
19224{
19225 list_unref(vimvars[idx].vv_list);
19226 vimvars[idx].vv_list = val;
19227 if (val != NULL)
19228 ++val->lv_refcount;
19229}
19230
19231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 * Set v:register if needed.
19233 */
19234 void
19235set_reg_var(c)
19236 int c;
19237{
19238 char_u regname;
19239
19240 if (c == 0 || c == ' ')
19241 regname = '"';
19242 else
19243 regname = c;
19244 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019245 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 set_vim_var_string(VV_REG, &regname, 1);
19247}
19248
19249/*
19250 * Get or set v:exception. If "oldval" == NULL, return the current value.
19251 * Otherwise, restore the value to "oldval" and return NULL.
19252 * Must always be called in pairs to save and restore v:exception! Does not
19253 * take care of memory allocations.
19254 */
19255 char_u *
19256v_exception(oldval)
19257 char_u *oldval;
19258{
19259 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019260 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261
Bram Moolenaare9a41262005-01-15 22:18:47 +000019262 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 return NULL;
19264}
19265
19266/*
19267 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19268 * Otherwise, restore the value to "oldval" and return NULL.
19269 * Must always be called in pairs to save and restore v:throwpoint! Does not
19270 * take care of memory allocations.
19271 */
19272 char_u *
19273v_throwpoint(oldval)
19274 char_u *oldval;
19275{
19276 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019277 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278
Bram Moolenaare9a41262005-01-15 22:18:47 +000019279 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 return NULL;
19281}
19282
19283#if defined(FEAT_AUTOCMD) || defined(PROTO)
19284/*
19285 * Set v:cmdarg.
19286 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19287 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19288 * Must always be called in pairs!
19289 */
19290 char_u *
19291set_cmdarg(eap, oldarg)
19292 exarg_T *eap;
19293 char_u *oldarg;
19294{
19295 char_u *oldval;
19296 char_u *newval;
19297 unsigned len;
19298
Bram Moolenaare9a41262005-01-15 22:18:47 +000019299 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019300 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019302 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019303 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019304 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 }
19306
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019307 if (eap->force_bin == FORCE_BIN)
19308 len = 6;
19309 else if (eap->force_bin == FORCE_NOBIN)
19310 len = 8;
19311 else
19312 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019313
19314 if (eap->read_edit)
19315 len += 7;
19316
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019317 if (eap->force_ff != 0)
19318 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19319# ifdef FEAT_MBYTE
19320 if (eap->force_enc != 0)
19321 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019322 if (eap->bad_char != 0)
19323 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019324# endif
19325
19326 newval = alloc(len + 1);
19327 if (newval == NULL)
19328 return NULL;
19329
19330 if (eap->force_bin == FORCE_BIN)
19331 sprintf((char *)newval, " ++bin");
19332 else if (eap->force_bin == FORCE_NOBIN)
19333 sprintf((char *)newval, " ++nobin");
19334 else
19335 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019336
19337 if (eap->read_edit)
19338 STRCAT(newval, " ++edit");
19339
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019340 if (eap->force_ff != 0)
19341 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19342 eap->cmd + eap->force_ff);
19343# ifdef FEAT_MBYTE
19344 if (eap->force_enc != 0)
19345 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19346 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019347 if (eap->bad_char == BAD_KEEP)
19348 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19349 else if (eap->bad_char == BAD_DROP)
19350 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19351 else if (eap->bad_char != 0)
19352 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019353# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019354 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019355 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356}
19357#endif
19358
19359/*
19360 * Get the value of internal variable "name".
19361 * Return OK or FAIL.
19362 */
19363 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019364get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 char_u *name;
19366 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019368 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369{
19370 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019371 typval_T *tv = NULL;
19372 typval_T atv;
19373 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375
19376 /* truncate the name, so that we can use strcmp() */
19377 cc = name[len];
19378 name[len] = NUL;
19379
19380 /*
19381 * Check for "b:changedtick".
19382 */
19383 if (STRCMP(name, "b:changedtick") == 0)
19384 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019385 atv.v_type = VAR_NUMBER;
19386 atv.vval.v_number = curbuf->b_changedtick;
19387 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 }
19389
19390 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 * Check for user-defined variables.
19392 */
19393 else
19394 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019395 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019397 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 }
19399
Bram Moolenaare9a41262005-01-15 22:18:47 +000019400 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019402 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019403 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 ret = FAIL;
19405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019407 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
19409 name[len] = cc;
19410
19411 return ret;
19412}
19413
19414/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019415 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19416 * Also handle function call with Funcref variable: func(expr)
19417 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19418 */
19419 static int
19420handle_subscript(arg, rettv, evaluate, verbose)
19421 char_u **arg;
19422 typval_T *rettv;
19423 int evaluate; /* do more than finding the end */
19424 int verbose; /* give error messages */
19425{
19426 int ret = OK;
19427 dict_T *selfdict = NULL;
19428 char_u *s;
19429 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019430 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019431
19432 while (ret == OK
19433 && (**arg == '['
19434 || (**arg == '.' && rettv->v_type == VAR_DICT)
19435 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19436 && !vim_iswhite(*(*arg - 1)))
19437 {
19438 if (**arg == '(')
19439 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019440 /* need to copy the funcref so that we can clear rettv */
19441 functv = *rettv;
19442 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019443
19444 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019445 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019446 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019447 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19448 &len, evaluate, selfdict);
19449
19450 /* Clear the funcref afterwards, so that deleting it while
19451 * evaluating the arguments is possible (see test55). */
19452 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019453
19454 /* Stop the expression evaluation when immediately aborting on
19455 * error, or when an interrupt occurred or an exception was thrown
19456 * but not caught. */
19457 if (aborting())
19458 {
19459 if (ret == OK)
19460 clear_tv(rettv);
19461 ret = FAIL;
19462 }
19463 dict_unref(selfdict);
19464 selfdict = NULL;
19465 }
19466 else /* **arg == '[' || **arg == '.' */
19467 {
19468 dict_unref(selfdict);
19469 if (rettv->v_type == VAR_DICT)
19470 {
19471 selfdict = rettv->vval.v_dict;
19472 if (selfdict != NULL)
19473 ++selfdict->dv_refcount;
19474 }
19475 else
19476 selfdict = NULL;
19477 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19478 {
19479 clear_tv(rettv);
19480 ret = FAIL;
19481 }
19482 }
19483 }
19484 dict_unref(selfdict);
19485 return ret;
19486}
19487
19488/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019489 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019490 * value).
19491 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019492 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019493alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019494{
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019496}
19497
19498/*
19499 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 * The string "s" must have been allocated, it is consumed.
19501 * Return NULL for out of memory, the variable otherwise.
19502 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019503 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 char_u *s;
19506{
Bram Moolenaar33570922005-01-25 22:26:29 +000019507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 rettv = alloc_tv();
19510 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019512 rettv->v_type = VAR_STRING;
19513 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 }
19515 else
19516 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019517 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518}
19519
19520/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019521 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019523 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019524free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019525 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526{
19527 if (varp != NULL)
19528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019529 switch (varp->v_type)
19530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019531 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019532 func_unref(varp->vval.v_string);
19533 /*FALLTHROUGH*/
19534 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019535 vim_free(varp->vval.v_string);
19536 break;
19537 case VAR_LIST:
19538 list_unref(varp->vval.v_list);
19539 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019540 case VAR_DICT:
19541 dict_unref(varp->vval.v_dict);
19542 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019543 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019544#ifdef FEAT_FLOAT
19545 case VAR_FLOAT:
19546#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019547 case VAR_UNKNOWN:
19548 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019549 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019550 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019551 break;
19552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 vim_free(varp);
19554 }
19555}
19556
19557/*
19558 * Free the memory for a variable value and set the value to NULL or 0.
19559 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019560 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
19564 if (varp != NULL)
19565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019566 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019568 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019569 func_unref(varp->vval.v_string);
19570 /*FALLTHROUGH*/
19571 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019572 vim_free(varp->vval.v_string);
19573 varp->vval.v_string = NULL;
19574 break;
19575 case VAR_LIST:
19576 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019577 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019578 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019579 case VAR_DICT:
19580 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019581 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019582 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019583 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019584 varp->vval.v_number = 0;
19585 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019586#ifdef FEAT_FLOAT
19587 case VAR_FLOAT:
19588 varp->vval.v_float = 0.0;
19589 break;
19590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019591 case VAR_UNKNOWN:
19592 break;
19593 default:
19594 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019596 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 }
19598}
19599
19600/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019601 * Set the value of a variable to NULL without freeing items.
19602 */
19603 static void
19604init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019606{
19607 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019608 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609}
19610
19611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 * Get the number value of a variable.
19613 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019614 * For incompatible types, return 0.
19615 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19616 * caller of incompatible types: it sets *denote to TRUE if "denote"
19617 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 */
19619 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019623 int error = FALSE;
19624
19625 return get_tv_number_chk(varp, &error); /* return 0L on error */
19626}
19627
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019628 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019629get_tv_number_chk(varp, denote)
19630 typval_T *varp;
19631 int *denote;
19632{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019633 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019635 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019637 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019638 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019639#ifdef FEAT_FLOAT
19640 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019641 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019642 break;
19643#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019644 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019645 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019646 break;
19647 case VAR_STRING:
19648 if (varp->vval.v_string != NULL)
19649 vim_str2nr(varp->vval.v_string, NULL, NULL,
19650 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019651 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019652 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019653 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019654 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019655 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019656 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019657 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019658 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019659 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019660 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019662 if (denote == NULL) /* useful for values that must be unsigned */
19663 n = -1;
19664 else
19665 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019666 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667}
19668
19669/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019670 * Get the lnum from the first argument.
19671 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019672 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 */
19674 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019675get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019676 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677{
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 linenr_T lnum;
19680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019681 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 if (lnum == 0) /* no valid number, try using line() */
19683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 rettv.v_type = VAR_NUMBER;
19685 f_line(argvars, &rettv);
19686 lnum = rettv.vval.v_number;
19687 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 }
19689 return lnum;
19690}
19691
19692/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019693 * Get the lnum from the first argument.
19694 * Also accepts "$", then "buf" is used.
19695 * Returns 0 on error.
19696 */
19697 static linenr_T
19698get_tv_lnum_buf(argvars, buf)
19699 typval_T *argvars;
19700 buf_T *buf;
19701{
19702 if (argvars[0].v_type == VAR_STRING
19703 && argvars[0].vval.v_string != NULL
19704 && argvars[0].vval.v_string[0] == '$'
19705 && buf != NULL)
19706 return buf->b_ml.ml_line_count;
19707 return get_tv_number_chk(&argvars[0], NULL);
19708}
19709
19710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 * Get the string value of a variable.
19712 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019713 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19714 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 * If the String variable has never been set, return an empty string.
19716 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019717 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19718 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 */
19720 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723{
19724 static char_u mybuf[NUMBUFLEN];
19725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727}
19728
19729 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019732 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019734 char_u *res = get_tv_string_buf_chk(varp, buf);
19735
19736 return res != NULL ? res : (char_u *)"";
19737}
19738
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019739 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019740get_tv_string_chk(varp)
19741 typval_T *varp;
19742{
19743 static char_u mybuf[NUMBUFLEN];
19744
19745 return get_tv_string_buf_chk(varp, mybuf);
19746}
19747
19748 static char_u *
19749get_tv_string_buf_chk(varp, buf)
19750 typval_T *varp;
19751 char_u *buf;
19752{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019753 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 case VAR_NUMBER:
19756 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19757 return buf;
19758 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019759 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019760 break;
19761 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019762 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019763 break;
19764 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019765 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019766 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019767#ifdef FEAT_FLOAT
19768 case VAR_FLOAT:
19769 EMSG(_("E806: using Float as a String"));
19770 break;
19771#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019772 case VAR_STRING:
19773 if (varp->vval.v_string != NULL)
19774 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019775 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019776 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019780 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781}
19782
19783/*
19784 * Find variable "name" in the list of variables.
19785 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019786 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019787 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019790 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019791find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797
Bram Moolenaara7043832005-01-21 11:56:39 +000019798 ht = find_var_ht(name, &varname);
19799 if (htp != NULL)
19800 *htp = ht;
19801 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019803 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804}
19805
19806/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019807 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019808 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019811find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019813 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019814 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019815{
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 hashitem_T *hi;
19817
19818 if (*varname == NUL)
19819 {
19820 /* Must be something like "s:", otherwise "ht" would be NULL. */
19821 switch (varname[-2])
19822 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019823 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 case 'g': return &globvars_var;
19825 case 'v': return &vimvars_var;
19826 case 'b': return &curbuf->b_bufvar;
19827 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019828#ifdef FEAT_WINDOWS
19829 case 't': return &curtab->tp_winvar;
19830#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019831 case 'l': return current_funccal == NULL
19832 ? NULL : &current_funccal->l_vars_var;
19833 case 'a': return current_funccal == NULL
19834 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 }
19836 return NULL;
19837 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019838
19839 hi = hash_find(ht, varname);
19840 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019841 {
19842 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019843 * worked find the variable again. Don't auto-load a script if it was
19844 * loaded already, otherwise it would be loaded every time when
19845 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019846 if (ht == &globvarht && !writing)
19847 {
19848 /* Note: script_autoload() may make "hi" invalid. It must either
19849 * be obtained again or not used. */
19850 if (!script_autoload(varname, FALSE) || aborting())
19851 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019852 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019853 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019854 if (HASHITEM_EMPTY(hi))
19855 return NULL;
19856 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019857 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019858}
19859
19860/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019862 * Set "varname" to the start of name without ':'.
19863 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019865find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 char_u *name;
19867 char_u **varname;
19868{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019869 hashitem_T *hi;
19870
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 if (name[1] != ':')
19872 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019873 /* The name must not start with a colon or #. */
19874 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 return NULL;
19876 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019877
19878 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019879 hi = hash_find(&compat_hashtab, name);
19880 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019881 return &compat_hashtab;
19882
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019884 return &globvarht; /* global variable */
19885 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 }
19887 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019888 if (*name == 'g') /* global variable */
19889 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019890 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19891 */
19892 if (vim_strchr(name + 2, ':') != NULL
19893 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019894 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019898 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019899#ifdef FEAT_WINDOWS
19900 if (*name == 't') /* tab page variable */
19901 return &curtab->tp_vars.dv_hashtab;
19902#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019903 if (*name == 'v') /* v: variable */
19904 return &vimvarht;
19905 if (*name == 'a' && current_funccal != NULL) /* function argument */
19906 return &current_funccal->l_avars.dv_hashtab;
19907 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19908 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 if (*name == 's' /* script variable */
19910 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19911 return &SCRIPT_VARS(current_SID);
19912 return NULL;
19913}
19914
19915/*
19916 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019917 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 * Returns NULL when it doesn't exist.
19919 */
19920 char_u *
19921get_var_value(name)
19922 char_u *name;
19923{
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925
Bram Moolenaara7043832005-01-21 11:56:39 +000019926 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 if (v == NULL)
19928 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019929 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930}
19931
19932/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019933 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 * sourcing this script and when executing functions defined in the script.
19935 */
19936 void
19937new_script_vars(id)
19938 scid_T id;
19939{
Bram Moolenaara7043832005-01-21 11:56:39 +000019940 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 hashtab_T *ht;
19942 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019943
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19945 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019946 /* Re-allocating ga_data means that an ht_array pointing to
19947 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019949 for (i = 1; i <= ga_scripts.ga_len; ++i)
19950 {
19951 ht = &SCRIPT_VARS(i);
19952 if (ht->ht_mask == HT_INIT_SIZE - 1)
19953 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019954 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019956 }
19957
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 while (ga_scripts.ga_len < id)
19959 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019960 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019961 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019962 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 }
19965 }
19966}
19967
19968/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019969 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19970 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 */
19972 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019973init_var_dict(dict, dict_var)
19974 dict_T *dict;
19975 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976{
Bram Moolenaar33570922005-01-25 22:26:29 +000019977 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019978 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019979 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019980 dict_var->di_tv.vval.v_dict = dict;
19981 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019982 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19984 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985}
19986
19987/*
19988 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019989 * Frees all allocated variables and the value they contain.
19990 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 */
19992 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019993vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 hashtab_T *ht;
19995{
19996 vars_clear_ext(ht, TRUE);
19997}
19998
19999/*
20000 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20001 */
20002 static void
20003vars_clear_ext(ht, free_val)
20004 hashtab_T *ht;
20005 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006{
Bram Moolenaara7043832005-01-21 11:56:39 +000020007 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020008 hashitem_T *hi;
20009 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010
Bram Moolenaar33570922005-01-25 22:26:29 +000020011 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020012 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020013 for (hi = ht->ht_array; todo > 0; ++hi)
20014 {
20015 if (!HASHITEM_EMPTY(hi))
20016 {
20017 --todo;
20018
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020020 * ht_array might change then. hash_clear() takes care of it
20021 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020022 v = HI2DI(hi);
20023 if (free_val)
20024 clear_tv(&v->di_tv);
20025 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20026 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020027 }
20028 }
20029 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020030 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031}
20032
Bram Moolenaara7043832005-01-21 11:56:39 +000020033/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 * Delete a variable from hashtab "ht" at item "hi".
20035 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020038delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020039 hashtab_T *ht;
20040 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041{
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020043
20044 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020045 clear_tv(&di->di_tv);
20046 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047}
20048
20049/*
20050 * List the value of one internal variable.
20051 */
20052 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020053list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020056 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020058 char_u *tofree;
20059 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020060 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020061
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020062 current_copyID += COPYID_INC;
20063 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020065 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020066 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067}
20068
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020070list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 char_u *prefix;
20072 char_u *name;
20073 int type;
20074 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020075 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
Bram Moolenaar31859182007-08-14 20:41:13 +000020077 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20078 msg_start();
20079 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 if (name != NULL) /* "a:" vars don't have a name stored */
20081 msg_puts(name);
20082 msg_putchar(' ');
20083 msg_advance(22);
20084 if (type == VAR_NUMBER)
20085 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086 else if (type == VAR_FUNC)
20087 msg_putchar('*');
20088 else if (type == VAR_LIST)
20089 {
20090 msg_putchar('[');
20091 if (*string == '[')
20092 ++string;
20093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020094 else if (type == VAR_DICT)
20095 {
20096 msg_putchar('{');
20097 if (*string == '{')
20098 ++string;
20099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 else
20101 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020102
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020104
20105 if (type == VAR_FUNC)
20106 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020107 if (*first)
20108 {
20109 msg_clr_eos();
20110 *first = FALSE;
20111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112}
20113
20114/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 * If the variable already exists, the value is updated.
20117 * Otherwise the variable is created.
20118 */
20119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020123 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124{
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020129 ht = find_var_ht(name, &varname);
20130 if (ht == NULL || *varname == NUL)
20131 {
20132 EMSG2(_(e_illvar), name);
20133 return;
20134 }
20135 v = find_var_in_ht(ht, varname, TRUE);
20136
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020137 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20138 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020139
Bram Moolenaar33570922005-01-25 22:26:29 +000020140 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020143 if (var_check_ro(v->di_flags, name)
20144 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 return;
20146 if (v->di_tv.v_type != tv->v_type
20147 && !((v->di_tv.v_type == VAR_STRING
20148 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020149 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020150 || tv->v_type == VAR_NUMBER))
20151#ifdef FEAT_FLOAT
20152 && !((v->di_tv.v_type == VAR_NUMBER
20153 || v->di_tv.v_type == VAR_FLOAT)
20154 && (tv->v_type == VAR_NUMBER
20155 || tv->v_type == VAR_FLOAT))
20156#endif
20157 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020158 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020159 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020160 return;
20161 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020162
20163 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020164 * Handle setting internal v: variables separately: we don't change
20165 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 */
20167 if (ht == &vimvarht)
20168 {
20169 if (v->di_tv.v_type == VAR_STRING)
20170 {
20171 vim_free(v->di_tv.vval.v_string);
20172 if (copy || tv->v_type != VAR_STRING)
20173 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20174 else
20175 {
20176 /* Take over the string to avoid an extra alloc/free. */
20177 v->di_tv.vval.v_string = tv->vval.v_string;
20178 tv->vval.v_string = NULL;
20179 }
20180 }
20181 else if (v->di_tv.v_type != VAR_NUMBER)
20182 EMSG2(_(e_intern2), "set_var()");
20183 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020184 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020186 if (STRCMP(varname, "searchforward") == 0)
20187 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20188 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 return;
20190 }
20191
20192 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 }
20194 else /* add a new variable */
20195 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020196 /* Can't add "v:" variable. */
20197 if (ht == &vimvarht)
20198 {
20199 EMSG2(_(e_illvar), name);
20200 return;
20201 }
20202
Bram Moolenaar92124a32005-06-17 22:03:40 +000020203 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020204 if (!valid_varname(varname))
20205 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020206
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020207 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20208 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020209 if (v == NULL)
20210 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020214 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 return;
20216 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020217 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020219
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020220 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020222 else
20223 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020224 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020225 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020230/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020231 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 * Also give an error message.
20233 */
20234 static int
20235var_check_ro(flags, name)
20236 int flags;
20237 char_u *name;
20238{
20239 if (flags & DI_FLAGS_RO)
20240 {
20241 EMSG2(_(e_readonlyvar), name);
20242 return TRUE;
20243 }
20244 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20245 {
20246 EMSG2(_(e_readonlysbx), name);
20247 return TRUE;
20248 }
20249 return FALSE;
20250}
20251
20252/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020253 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20254 * Also give an error message.
20255 */
20256 static int
20257var_check_fixed(flags, name)
20258 int flags;
20259 char_u *name;
20260{
20261 if (flags & DI_FLAGS_FIX)
20262 {
20263 EMSG2(_("E795: Cannot delete variable %s"), name);
20264 return TRUE;
20265 }
20266 return FALSE;
20267}
20268
20269/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020270 * Check if a funcref is assigned to a valid variable name.
20271 * Return TRUE and give an error if not.
20272 */
20273 static int
20274var_check_func_name(name, new_var)
20275 char_u *name; /* points to start of variable name */
20276 int new_var; /* TRUE when creating the variable */
20277{
20278 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20279 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20280 ? name[2] : name[0]))
20281 {
20282 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20283 name);
20284 return TRUE;
20285 }
20286 /* Don't allow hiding a function. When "v" is not NULL we might be
20287 * assigning another function to the same var, the type is checked
20288 * below. */
20289 if (new_var && function_exists(name))
20290 {
20291 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20292 name);
20293 return TRUE;
20294 }
20295 return FALSE;
20296}
20297
20298/*
20299 * Check if a variable name is valid.
20300 * Return FALSE and give an error if not.
20301 */
20302 static int
20303valid_varname(varname)
20304 char_u *varname;
20305{
20306 char_u *p;
20307
20308 for (p = varname; *p != NUL; ++p)
20309 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20310 && *p != AUTOLOAD_CHAR)
20311 {
20312 EMSG2(_(e_illvar), varname);
20313 return FALSE;
20314 }
20315 return TRUE;
20316}
20317
20318/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020319 * Return TRUE if typeval "tv" is set to be locked (immutable).
20320 * Also give an error message, using "name".
20321 */
20322 static int
20323tv_check_lock(lock, name)
20324 int lock;
20325 char_u *name;
20326{
20327 if (lock & VAR_LOCKED)
20328 {
20329 EMSG2(_("E741: Value is locked: %s"),
20330 name == NULL ? (char_u *)_("Unknown") : name);
20331 return TRUE;
20332 }
20333 if (lock & VAR_FIXED)
20334 {
20335 EMSG2(_("E742: Cannot change value of %s"),
20336 name == NULL ? (char_u *)_("Unknown") : name);
20337 return TRUE;
20338 }
20339 return FALSE;
20340}
20341
20342/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020343 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020344 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020345 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020346 * It is OK for "from" and "to" to point to the same item. This is used to
20347 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020348 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020349 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 typval_T *from;
20352 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020354 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020355 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020356 switch (from->v_type)
20357 {
20358 case VAR_NUMBER:
20359 to->vval.v_number = from->vval.v_number;
20360 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020361#ifdef FEAT_FLOAT
20362 case VAR_FLOAT:
20363 to->vval.v_float = from->vval.v_float;
20364 break;
20365#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020366 case VAR_STRING:
20367 case VAR_FUNC:
20368 if (from->vval.v_string == NULL)
20369 to->vval.v_string = NULL;
20370 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020371 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020372 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020373 if (from->v_type == VAR_FUNC)
20374 func_ref(to->vval.v_string);
20375 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020376 break;
20377 case VAR_LIST:
20378 if (from->vval.v_list == NULL)
20379 to->vval.v_list = NULL;
20380 else
20381 {
20382 to->vval.v_list = from->vval.v_list;
20383 ++to->vval.v_list->lv_refcount;
20384 }
20385 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020386 case VAR_DICT:
20387 if (from->vval.v_dict == NULL)
20388 to->vval.v_dict = NULL;
20389 else
20390 {
20391 to->vval.v_dict = from->vval.v_dict;
20392 ++to->vval.v_dict->dv_refcount;
20393 }
20394 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020395 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020396 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397 break;
20398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399}
20400
20401/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020402 * Make a copy of an item.
20403 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020404 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20405 * reference to an already copied list/dict can be used.
20406 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020407 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020408 static int
20409item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 typval_T *from;
20411 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020412 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020413 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020414{
20415 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020416 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020417
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020419 {
20420 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020421 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020422 }
20423 ++recurse;
20424
20425 switch (from->v_type)
20426 {
20427 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020428#ifdef FEAT_FLOAT
20429 case VAR_FLOAT:
20430#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020431 case VAR_STRING:
20432 case VAR_FUNC:
20433 copy_tv(from, to);
20434 break;
20435 case VAR_LIST:
20436 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020437 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020438 if (from->vval.v_list == NULL)
20439 to->vval.v_list = NULL;
20440 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20441 {
20442 /* use the copy made earlier */
20443 to->vval.v_list = from->vval.v_list->lv_copylist;
20444 ++to->vval.v_list->lv_refcount;
20445 }
20446 else
20447 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20448 if (to->vval.v_list == NULL)
20449 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020450 break;
20451 case VAR_DICT:
20452 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020453 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020454 if (from->vval.v_dict == NULL)
20455 to->vval.v_dict = NULL;
20456 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20457 {
20458 /* use the copy made earlier */
20459 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20460 ++to->vval.v_dict->dv_refcount;
20461 }
20462 else
20463 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20464 if (to->vval.v_dict == NULL)
20465 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020466 break;
20467 default:
20468 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020469 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020470 }
20471 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020472 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020473}
20474
20475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 * ":echo expr1 ..." print each argument separated with a space, add a
20477 * newline at the end.
20478 * ":echon expr1 ..." print each argument plain.
20479 */
20480 void
20481ex_echo(eap)
20482 exarg_T *eap;
20483{
20484 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020486 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 char_u *p;
20488 int needclr = TRUE;
20489 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020490 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491
20492 if (eap->skip)
20493 ++emsg_skip;
20494 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20495 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020496 /* If eval1() causes an error message the text from the command may
20497 * still need to be cleared. E.g., "echo 22,44". */
20498 need_clr_eos = needclr;
20499
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020501 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 {
20503 /*
20504 * Report the invalid expression unless the expression evaluation
20505 * has been cancelled due to an aborting error, an interrupt, or an
20506 * exception.
20507 */
20508 if (!aborting())
20509 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020510 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 break;
20512 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020513 need_clr_eos = FALSE;
20514
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 if (!eap->skip)
20516 {
20517 if (atstart)
20518 {
20519 atstart = FALSE;
20520 /* Call msg_start() after eval1(), evaluating the expression
20521 * may cause a message to appear. */
20522 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020523 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020524 /* Mark the saved text as finishing the line, so that what
20525 * follows is displayed on a new line when scrolling back
20526 * at the more prompt. */
20527 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 }
20531 else if (eap->cmdidx == CMD_echo)
20532 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020533 current_copyID += COPYID_INC;
20534 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020535 if (p != NULL)
20536 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020538 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020540 if (*p != TAB && needclr)
20541 {
20542 /* remove any text still there from the command */
20543 msg_clr_eos();
20544 needclr = FALSE;
20545 }
20546 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 }
20548 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020549 {
20550#ifdef FEAT_MBYTE
20551 if (has_mbyte)
20552 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020553 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020554
20555 (void)msg_outtrans_len_attr(p, i, echo_attr);
20556 p += i - 1;
20557 }
20558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020560 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020563 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 arg = skipwhite(arg);
20567 }
20568 eap->nextcmd = check_nextcmd(arg);
20569
20570 if (eap->skip)
20571 --emsg_skip;
20572 else
20573 {
20574 /* remove text that may still be there from the command */
20575 if (needclr)
20576 msg_clr_eos();
20577 if (eap->cmdidx == CMD_echo)
20578 msg_end();
20579 }
20580}
20581
20582/*
20583 * ":echohl {name}".
20584 */
20585 void
20586ex_echohl(eap)
20587 exarg_T *eap;
20588{
20589 int id;
20590
20591 id = syn_name2id(eap->arg);
20592 if (id == 0)
20593 echo_attr = 0;
20594 else
20595 echo_attr = syn_id2attr(id);
20596}
20597
20598/*
20599 * ":execute expr1 ..." execute the result of an expression.
20600 * ":echomsg expr1 ..." Print a message
20601 * ":echoerr expr1 ..." Print an error
20602 * Each gets spaces around each argument and a newline at the end for
20603 * echo commands
20604 */
20605 void
20606ex_execute(eap)
20607 exarg_T *eap;
20608{
20609 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020610 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 int ret = OK;
20612 char_u *p;
20613 garray_T ga;
20614 int len;
20615 int save_did_emsg;
20616
20617 ga_init2(&ga, 1, 80);
20618
20619 if (eap->skip)
20620 ++emsg_skip;
20621 while (*arg != NUL && *arg != '|' && *arg != '\n')
20622 {
20623 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 {
20626 /*
20627 * Report the invalid expression unless the expression evaluation
20628 * has been cancelled due to an aborting error, an interrupt, or an
20629 * exception.
20630 */
20631 if (!aborting())
20632 EMSG2(_(e_invexpr2), p);
20633 ret = FAIL;
20634 break;
20635 }
20636
20637 if (!eap->skip)
20638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 len = (int)STRLEN(p);
20641 if (ga_grow(&ga, len + 2) == FAIL)
20642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020643 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 ret = FAIL;
20645 break;
20646 }
20647 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 ga.ga_len += len;
20651 }
20652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 arg = skipwhite(arg);
20655 }
20656
20657 if (ret != FAIL && ga.ga_data != NULL)
20658 {
20659 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020662 out_flush();
20663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 else if (eap->cmdidx == CMD_echoerr)
20665 {
20666 /* We don't want to abort following commands, restore did_emsg. */
20667 save_did_emsg = did_emsg;
20668 EMSG((char_u *)ga.ga_data);
20669 if (!force_abort)
20670 did_emsg = save_did_emsg;
20671 }
20672 else if (eap->cmdidx == CMD_execute)
20673 do_cmdline((char_u *)ga.ga_data,
20674 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20675 }
20676
20677 ga_clear(&ga);
20678
20679 if (eap->skip)
20680 --emsg_skip;
20681
20682 eap->nextcmd = check_nextcmd(arg);
20683}
20684
20685/*
20686 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20687 * "arg" points to the "&" or '+' when called, to "option" when returning.
20688 * Returns NULL when no option name found. Otherwise pointer to the char
20689 * after the option name.
20690 */
20691 static char_u *
20692find_option_end(arg, opt_flags)
20693 char_u **arg;
20694 int *opt_flags;
20695{
20696 char_u *p = *arg;
20697
20698 ++p;
20699 if (*p == 'g' && p[1] == ':')
20700 {
20701 *opt_flags = OPT_GLOBAL;
20702 p += 2;
20703 }
20704 else if (*p == 'l' && p[1] == ':')
20705 {
20706 *opt_flags = OPT_LOCAL;
20707 p += 2;
20708 }
20709 else
20710 *opt_flags = 0;
20711
20712 if (!ASCII_ISALPHA(*p))
20713 return NULL;
20714 *arg = p;
20715
20716 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20717 p += 4; /* termcap option */
20718 else
20719 while (ASCII_ISALPHA(*p))
20720 ++p;
20721 return p;
20722}
20723
20724/*
20725 * ":function"
20726 */
20727 void
20728ex_function(eap)
20729 exarg_T *eap;
20730{
20731 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020732 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 int j;
20734 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 char_u *name = NULL;
20737 char_u *p;
20738 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020739 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 garray_T newargs;
20741 garray_T newlines;
20742 int varargs = FALSE;
20743 int mustend = FALSE;
20744 int flags = 0;
20745 ufunc_T *fp;
20746 int indent;
20747 int nesting;
20748 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020749 dictitem_T *v;
20750 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020751 static int func_nr = 0; /* number for nameless function */
20752 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020753 hashtab_T *ht;
20754 int todo;
20755 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020756 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757
20758 /*
20759 * ":function" without argument: list functions.
20760 */
20761 if (ends_excmd(*eap->arg))
20762 {
20763 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020765 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020766 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020767 {
20768 if (!HASHITEM_EMPTY(hi))
20769 {
20770 --todo;
20771 fp = HI2UF(hi);
20772 if (!isdigit(*fp->uf_name))
20773 list_func_head(fp, FALSE);
20774 }
20775 }
20776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 eap->nextcmd = check_nextcmd(eap->arg);
20778 return;
20779 }
20780
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020781 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020782 * ":function /pat": list functions matching pattern.
20783 */
20784 if (*eap->arg == '/')
20785 {
20786 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20787 if (!eap->skip)
20788 {
20789 regmatch_T regmatch;
20790
20791 c = *p;
20792 *p = NUL;
20793 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20794 *p = c;
20795 if (regmatch.regprog != NULL)
20796 {
20797 regmatch.rm_ic = p_ic;
20798
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020799 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020800 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20801 {
20802 if (!HASHITEM_EMPTY(hi))
20803 {
20804 --todo;
20805 fp = HI2UF(hi);
20806 if (!isdigit(*fp->uf_name)
20807 && vim_regexec(&regmatch, fp->uf_name, 0))
20808 list_func_head(fp, FALSE);
20809 }
20810 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020811 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020812 }
20813 }
20814 if (*p == '/')
20815 ++p;
20816 eap->nextcmd = check_nextcmd(p);
20817 return;
20818 }
20819
20820 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020821 * Get the function name. There are these situations:
20822 * func normal function name
20823 * "name" == func, "fudi.fd_dict" == NULL
20824 * dict.func new dictionary entry
20825 * "name" == NULL, "fudi.fd_dict" set,
20826 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20827 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020828 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020829 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20830 * dict.func existing dict entry that's not a Funcref
20831 * "name" == NULL, "fudi.fd_dict" set,
20832 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020835 name = trans_function_name(&p, eap->skip, 0, &fudi);
20836 paren = (vim_strchr(p, '(') != NULL);
20837 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 {
20839 /*
20840 * Return on an invalid expression in braces, unless the expression
20841 * evaluation has been cancelled due to an aborting error, an
20842 * interrupt, or an exception.
20843 */
20844 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020846 if (!eap->skip && fudi.fd_newkey != NULL)
20847 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020848 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 else
20852 eap->skip = TRUE;
20853 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020854
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 /* An error in a function call during evaluation of an expression in magic
20856 * braces should not cause the function not to be defined. */
20857 saved_did_emsg = did_emsg;
20858 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859
20860 /*
20861 * ":function func" with only function name: list function.
20862 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020863 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 {
20865 if (!ends_excmd(*skipwhite(p)))
20866 {
20867 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020868 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 }
20870 eap->nextcmd = check_nextcmd(p);
20871 if (eap->nextcmd != NULL)
20872 *p = NUL;
20873 if (!eap->skip && !got_int)
20874 {
20875 fp = find_func(name);
20876 if (fp != NULL)
20877 {
20878 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020879 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020881 if (FUNCLINE(fp, j) == NULL)
20882 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 msg_putchar('\n');
20884 msg_outnum((long)(j + 1));
20885 if (j < 9)
20886 msg_putchar(' ');
20887 if (j < 99)
20888 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020889 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 out_flush(); /* show a line at a time */
20891 ui_breakcheck();
20892 }
20893 if (!got_int)
20894 {
20895 msg_putchar('\n');
20896 msg_puts((char_u *)" endfunction");
20897 }
20898 }
20899 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020900 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020902 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 }
20904
20905 /*
20906 * ":function name(arg1, arg2)" Define function.
20907 */
20908 p = skipwhite(p);
20909 if (*p != '(')
20910 {
20911 if (!eap->skip)
20912 {
20913 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 }
20916 /* attempt to continue by skipping some text */
20917 if (vim_strchr(p, '(') != NULL)
20918 p = vim_strchr(p, '(');
20919 }
20920 p = skipwhite(p + 1);
20921
20922 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20923 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20924
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020925 if (!eap->skip)
20926 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020927 /* Check the name of the function. Unless it's a dictionary function
20928 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020929 if (name != NULL)
20930 arg = name;
20931 else
20932 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020933 if (arg != NULL && (fudi.fd_di == NULL
20934 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020935 {
20936 if (*arg == K_SPECIAL)
20937 j = 3;
20938 else
20939 j = 0;
20940 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20941 : eval_isnamec(arg[j])))
20942 ++j;
20943 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020944 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020945 }
20946 }
20947
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 /*
20949 * Isolate the arguments: "arg1, arg2, ...)"
20950 */
20951 while (*p != ')')
20952 {
20953 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20954 {
20955 varargs = TRUE;
20956 p += 3;
20957 mustend = TRUE;
20958 }
20959 else
20960 {
20961 arg = p;
20962 while (ASCII_ISALNUM(*p) || *p == '_')
20963 ++p;
20964 if (arg == p || isdigit(*arg)
20965 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20966 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20967 {
20968 if (!eap->skip)
20969 EMSG2(_("E125: Illegal argument: %s"), arg);
20970 break;
20971 }
20972 if (ga_grow(&newargs, 1) == FAIL)
20973 goto erret;
20974 c = *p;
20975 *p = NUL;
20976 arg = vim_strsave(arg);
20977 if (arg == NULL)
20978 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020979
20980 /* Check for duplicate argument name. */
20981 for (i = 0; i < newargs.ga_len; ++i)
20982 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20983 {
20984 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20985 goto erret;
20986 }
20987
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20989 *p = c;
20990 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 if (*p == ',')
20992 ++p;
20993 else
20994 mustend = TRUE;
20995 }
20996 p = skipwhite(p);
20997 if (mustend && *p != ')')
20998 {
20999 if (!eap->skip)
21000 EMSG2(_(e_invarg2), eap->arg);
21001 break;
21002 }
21003 }
21004 ++p; /* skip the ')' */
21005
Bram Moolenaare9a41262005-01-15 22:18:47 +000021006 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 for (;;)
21008 {
21009 p = skipwhite(p);
21010 if (STRNCMP(p, "range", 5) == 0)
21011 {
21012 flags |= FC_RANGE;
21013 p += 5;
21014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021015 else if (STRNCMP(p, "dict", 4) == 0)
21016 {
21017 flags |= FC_DICT;
21018 p += 4;
21019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 else if (STRNCMP(p, "abort", 5) == 0)
21021 {
21022 flags |= FC_ABORT;
21023 p += 5;
21024 }
21025 else
21026 break;
21027 }
21028
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021029 /* When there is a line break use what follows for the function body.
21030 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21031 if (*p == '\n')
21032 line_arg = p + 1;
21033 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 EMSG(_(e_trailing));
21035
21036 /*
21037 * Read the body of the function, until ":endfunction" is found.
21038 */
21039 if (KeyTyped)
21040 {
21041 /* Check if the function already exists, don't let the user type the
21042 * whole function before telling him it doesn't work! For a script we
21043 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021044 if (!eap->skip && !eap->forceit)
21045 {
21046 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21047 EMSG(_(e_funcdict));
21048 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021049 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021052 if (!eap->skip && did_emsg)
21053 goto erret;
21054
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 msg_putchar('\n'); /* don't overwrite the function name */
21056 cmdline_row = msg_row;
21057 }
21058
21059 indent = 2;
21060 nesting = 0;
21061 for (;;)
21062 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021063 if (KeyTyped)
21064 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021066 sourcing_lnum_off = sourcing_lnum;
21067
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021068 if (line_arg != NULL)
21069 {
21070 /* Use eap->arg, split up in parts by line breaks. */
21071 theline = line_arg;
21072 p = vim_strchr(theline, '\n');
21073 if (p == NULL)
21074 line_arg += STRLEN(line_arg);
21075 else
21076 {
21077 *p = NUL;
21078 line_arg = p + 1;
21079 }
21080 }
21081 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 theline = getcmdline(':', 0L, indent);
21083 else
21084 theline = eap->getline(':', eap->cookie, indent);
21085 if (KeyTyped)
21086 lines_left = Rows - 1;
21087 if (theline == NULL)
21088 {
21089 EMSG(_("E126: Missing :endfunction"));
21090 goto erret;
21091 }
21092
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021093 /* Detect line continuation: sourcing_lnum increased more than one. */
21094 if (sourcing_lnum > sourcing_lnum_off + 1)
21095 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21096 else
21097 sourcing_lnum_off = 0;
21098
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 if (skip_until != NULL)
21100 {
21101 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21102 * don't check for ":endfunc". */
21103 if (STRCMP(theline, skip_until) == 0)
21104 {
21105 vim_free(skip_until);
21106 skip_until = NULL;
21107 }
21108 }
21109 else
21110 {
21111 /* skip ':' and blanks*/
21112 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21113 ;
21114
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021115 /* Check for "endfunction". */
21116 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021118 if (line_arg == NULL)
21119 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 break;
21121 }
21122
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021123 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 * at "end". */
21125 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21126 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021127 else if (STRNCMP(p, "if", 2) == 0
21128 || STRNCMP(p, "wh", 2) == 0
21129 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 || STRNCMP(p, "try", 3) == 0)
21131 indent += 2;
21132
21133 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021134 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021136 if (*p == '!')
21137 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 p += eval_fname_script(p);
21139 if (ASCII_ISALPHA(*p))
21140 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021141 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 if (*skipwhite(p) == '(')
21143 {
21144 ++nesting;
21145 indent += 2;
21146 }
21147 }
21148 }
21149
21150 /* Check for ":append" or ":insert". */
21151 p = skip_range(p, NULL);
21152 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21153 || (p[0] == 'i'
21154 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21155 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21156 skip_until = vim_strsave((char_u *)".");
21157
21158 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21159 arg = skipwhite(skiptowhite(p));
21160 if (arg[0] == '<' && arg[1] =='<'
21161 && ((p[0] == 'p' && p[1] == 'y'
21162 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21163 || (p[0] == 'p' && p[1] == 'e'
21164 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21165 || (p[0] == 't' && p[1] == 'c'
21166 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021167 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21168 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21170 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021171 || (p[0] == 'm' && p[1] == 'z'
21172 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 ))
21174 {
21175 /* ":python <<" continues until a dot, like ":append" */
21176 p = skipwhite(arg + 2);
21177 if (*p == NUL)
21178 skip_until = vim_strsave((char_u *)".");
21179 else
21180 skip_until = vim_strsave(p);
21181 }
21182 }
21183
21184 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021185 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021186 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021187 if (line_arg == NULL)
21188 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021190 }
21191
21192 /* Copy the line to newly allocated memory. get_one_sourceline()
21193 * allocates 250 bytes per line, this saves 80% on average. The cost
21194 * is an extra alloc/free. */
21195 p = vim_strsave(theline);
21196 if (p != NULL)
21197 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021198 if (line_arg == NULL)
21199 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021200 theline = p;
21201 }
21202
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021203 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21204
21205 /* Add NULL lines for continuation lines, so that the line count is
21206 * equal to the index in the growarray. */
21207 while (sourcing_lnum_off-- > 0)
21208 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021209
21210 /* Check for end of eap->arg. */
21211 if (line_arg != NULL && *line_arg == NUL)
21212 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 }
21214
21215 /* Don't define the function when skipping commands or when an error was
21216 * detected. */
21217 if (eap->skip || did_emsg)
21218 goto erret;
21219
21220 /*
21221 * If there are no errors, add the function
21222 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021223 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021224 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021225 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021226 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021227 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021228 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021229 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021230 goto erret;
21231 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021232
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021233 fp = find_func(name);
21234 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021236 if (!eap->forceit)
21237 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021238 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021239 goto erret;
21240 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021241 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021242 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021243 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244 name);
21245 goto erret;
21246 }
21247 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021248 ga_clear_strings(&(fp->uf_args));
21249 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021250 vim_free(name);
21251 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 }
21254 else
21255 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021256 char numbuf[20];
21257
21258 fp = NULL;
21259 if (fudi.fd_newkey == NULL && !eap->forceit)
21260 {
21261 EMSG(_(e_funcdict));
21262 goto erret;
21263 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021264 if (fudi.fd_di == NULL)
21265 {
21266 /* Can't add a function to a locked dictionary */
21267 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21268 goto erret;
21269 }
21270 /* Can't change an existing function if it is locked */
21271 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21272 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021273
21274 /* Give the function a sequential number. Can only be used with a
21275 * Funcref! */
21276 vim_free(name);
21277 sprintf(numbuf, "%d", ++func_nr);
21278 name = vim_strsave((char_u *)numbuf);
21279 if (name == NULL)
21280 goto erret;
21281 }
21282
21283 if (fp == NULL)
21284 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021285 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021286 {
21287 int slen, plen;
21288 char_u *scriptname;
21289
21290 /* Check that the autoload name matches the script name. */
21291 j = FAIL;
21292 if (sourcing_name != NULL)
21293 {
21294 scriptname = autoload_name(name);
21295 if (scriptname != NULL)
21296 {
21297 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021298 plen = (int)STRLEN(p);
21299 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021300 if (slen > plen && fnamecmp(p,
21301 sourcing_name + slen - plen) == 0)
21302 j = OK;
21303 vim_free(scriptname);
21304 }
21305 }
21306 if (j == FAIL)
21307 {
21308 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21309 goto erret;
21310 }
21311 }
21312
21313 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 if (fp == NULL)
21315 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021316
21317 if (fudi.fd_dict != NULL)
21318 {
21319 if (fudi.fd_di == NULL)
21320 {
21321 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021322 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021323 if (fudi.fd_di == NULL)
21324 {
21325 vim_free(fp);
21326 goto erret;
21327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021328 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21329 {
21330 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021331 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021332 goto erret;
21333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021334 }
21335 else
21336 /* overwrite existing dict entry */
21337 clear_tv(&fudi.fd_di->di_tv);
21338 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021339 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021340 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021341 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021342
21343 /* behave like "dict" was used */
21344 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021345 }
21346
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021348 STRCPY(fp->uf_name, name);
21349 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 fp->uf_args = newargs;
21352 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021353#ifdef FEAT_PROFILE
21354 fp->uf_tml_count = NULL;
21355 fp->uf_tml_total = NULL;
21356 fp->uf_tml_self = NULL;
21357 fp->uf_profiling = FALSE;
21358 if (prof_def_func())
21359 func_do_profile(fp);
21360#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021361 fp->uf_varargs = varargs;
21362 fp->uf_flags = flags;
21363 fp->uf_calls = 0;
21364 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021365 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366
21367erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 ga_clear_strings(&newargs);
21369 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370ret_free:
21371 vim_free(skip_until);
21372 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375}
21376
21377/*
21378 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021379 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021381 * flags:
21382 * TFN_INT: internal function name OK
21383 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 * Advances "pp" to just after the function name (if no error).
21385 */
21386 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021387trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 char_u **pp;
21389 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021390 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021391 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021393 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 char_u *start;
21395 char_u *end;
21396 int lead;
21397 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021399 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021400
21401 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021404
21405 /* Check for hard coded <SNR>: already translated function ID (from a user
21406 * command). */
21407 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21408 && (*pp)[2] == (int)KE_SNR)
21409 {
21410 *pp += 3;
21411 len = get_id_len(pp) + 3;
21412 return vim_strnsave(start, len);
21413 }
21414
21415 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21416 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021418 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021420
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021421 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21422 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021423 if (end == start)
21424 {
21425 if (!skip)
21426 EMSG(_("E129: Function name required"));
21427 goto theend;
21428 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021429 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021430 {
21431 /*
21432 * Report an invalid expression in braces, unless the expression
21433 * evaluation has been cancelled due to an aborting error, an
21434 * interrupt, or an exception.
21435 */
21436 if (!aborting())
21437 {
21438 if (end != NULL)
21439 EMSG2(_(e_invarg2), start);
21440 }
21441 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021442 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021443 goto theend;
21444 }
21445
21446 if (lv.ll_tv != NULL)
21447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021448 if (fdp != NULL)
21449 {
21450 fdp->fd_dict = lv.ll_dict;
21451 fdp->fd_newkey = lv.ll_newkey;
21452 lv.ll_newkey = NULL;
21453 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021455 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21456 {
21457 name = vim_strsave(lv.ll_tv->vval.v_string);
21458 *pp = end;
21459 }
21460 else
21461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021462 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21463 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021464 EMSG(_(e_funcref));
21465 else
21466 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021467 name = NULL;
21468 }
21469 goto theend;
21470 }
21471
21472 if (lv.ll_name == NULL)
21473 {
21474 /* Error found, but continue after the function name. */
21475 *pp = end;
21476 goto theend;
21477 }
21478
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021479 /* Check if the name is a Funcref. If so, use the value. */
21480 if (lv.ll_exp_name != NULL)
21481 {
21482 len = (int)STRLEN(lv.ll_exp_name);
21483 name = deref_func_name(lv.ll_exp_name, &len);
21484 if (name == lv.ll_exp_name)
21485 name = NULL;
21486 }
21487 else
21488 {
21489 len = (int)(end - *pp);
21490 name = deref_func_name(*pp, &len);
21491 if (name == *pp)
21492 name = NULL;
21493 }
21494 if (name != NULL)
21495 {
21496 name = vim_strsave(name);
21497 *pp = end;
21498 goto theend;
21499 }
21500
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021501 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021502 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021503 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021504 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21505 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21506 {
21507 /* When there was "s:" already or the name expanded to get a
21508 * leading "s:" then remove it. */
21509 lv.ll_name += 2;
21510 len -= 2;
21511 lead = 2;
21512 }
21513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021514 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021515 {
21516 if (lead == 2) /* skip over "s:" */
21517 lv.ll_name += 2;
21518 len = (int)(end - lv.ll_name);
21519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021520
21521 /*
21522 * Copy the function name to allocated memory.
21523 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21524 * Accept <SNR>123_name() outside a script.
21525 */
21526 if (skip)
21527 lead = 0; /* do nothing */
21528 else if (lead > 0)
21529 {
21530 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021531 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21532 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021533 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021534 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021535 if (current_SID <= 0)
21536 {
21537 EMSG(_(e_usingsid));
21538 goto theend;
21539 }
21540 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21541 lead += (int)STRLEN(sid_buf);
21542 }
21543 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021544 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021545 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021546 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021547 goto theend;
21548 }
21549 name = alloc((unsigned)(len + lead + 1));
21550 if (name != NULL)
21551 {
21552 if (lead > 0)
21553 {
21554 name[0] = K_SPECIAL;
21555 name[1] = KS_EXTRA;
21556 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021557 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021558 STRCPY(name + 3, sid_buf);
21559 }
21560 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21561 name[len + lead] = NUL;
21562 }
21563 *pp = end;
21564
21565theend:
21566 clear_lval(&lv);
21567 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568}
21569
21570/*
21571 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21572 * Return 2 if "p" starts with "s:".
21573 * Return 0 otherwise.
21574 */
21575 static int
21576eval_fname_script(p)
21577 char_u *p;
21578{
21579 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21580 || STRNICMP(p + 1, "SNR>", 4) == 0))
21581 return 5;
21582 if (p[0] == 's' && p[1] == ':')
21583 return 2;
21584 return 0;
21585}
21586
21587/*
21588 * Return TRUE if "p" starts with "<SID>" or "s:".
21589 * Only works if eval_fname_script() returned non-zero for "p"!
21590 */
21591 static int
21592eval_fname_sid(p)
21593 char_u *p;
21594{
21595 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21596}
21597
21598/*
21599 * List the head of the function: "name(arg1, arg2)".
21600 */
21601 static void
21602list_func_head(fp, indent)
21603 ufunc_T *fp;
21604 int indent;
21605{
21606 int j;
21607
21608 msg_start();
21609 if (indent)
21610 MSG_PUTS(" ");
21611 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021612 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 {
21614 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021615 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 }
21617 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021618 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021620 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 {
21622 if (j)
21623 MSG_PUTS(", ");
21624 msg_puts(FUNCARG(fp, j));
21625 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021626 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 {
21628 if (j)
21629 MSG_PUTS(", ");
21630 MSG_PUTS("...");
21631 }
21632 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021633 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021634 if (p_verbose > 0)
21635 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636}
21637
21638/*
21639 * Find a function by name, return pointer to it in ufuncs.
21640 * Return NULL for unknown function.
21641 */
21642 static ufunc_T *
21643find_func(name)
21644 char_u *name;
21645{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021646 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021648 hi = hash_find(&func_hashtab, name);
21649 if (!HASHITEM_EMPTY(hi))
21650 return HI2UF(hi);
21651 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652}
21653
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021654#if defined(EXITFREE) || defined(PROTO)
21655 void
21656free_all_functions()
21657{
21658 hashitem_T *hi;
21659
21660 /* Need to start all over every time, because func_free() may change the
21661 * hash table. */
21662 while (func_hashtab.ht_used > 0)
21663 for (hi = func_hashtab.ht_array; ; ++hi)
21664 if (!HASHITEM_EMPTY(hi))
21665 {
21666 func_free(HI2UF(hi));
21667 break;
21668 }
21669}
21670#endif
21671
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021672/*
21673 * Return TRUE if a function "name" exists.
21674 */
21675 static int
21676function_exists(name)
21677 char_u *name;
21678{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021679 char_u *nm = name;
21680 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021681 int n = FALSE;
21682
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021683 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021684 nm = skipwhite(nm);
21685
21686 /* Only accept "funcname", "funcname ", "funcname (..." and
21687 * "funcname(...", not "funcname!...". */
21688 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021690 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021691 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021692 else
21693 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021694 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021695 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021696 return n;
21697}
21698
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021699/*
21700 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021701 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021702 */
21703 static int
21704builtin_function(name)
21705 char_u *name;
21706{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021707 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21708 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021709}
21710
Bram Moolenaar05159a02005-02-26 23:04:13 +000021711#if defined(FEAT_PROFILE) || defined(PROTO)
21712/*
21713 * Start profiling function "fp".
21714 */
21715 static void
21716func_do_profile(fp)
21717 ufunc_T *fp;
21718{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021719 int len = fp->uf_lines.ga_len;
21720
21721 if (len == 0)
21722 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021723 fp->uf_tm_count = 0;
21724 profile_zero(&fp->uf_tm_self);
21725 profile_zero(&fp->uf_tm_total);
21726 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021727 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021728 if (fp->uf_tml_total == NULL)
21729 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021730 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021731 if (fp->uf_tml_self == NULL)
21732 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021733 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021734 fp->uf_tml_idx = -1;
21735 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21736 || fp->uf_tml_self == NULL)
21737 return; /* out of memory */
21738
21739 fp->uf_profiling = TRUE;
21740}
21741
21742/*
21743 * Dump the profiling results for all functions in file "fd".
21744 */
21745 void
21746func_dump_profile(fd)
21747 FILE *fd;
21748{
21749 hashitem_T *hi;
21750 int todo;
21751 ufunc_T *fp;
21752 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021753 ufunc_T **sorttab;
21754 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021755
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021756 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021757 if (todo == 0)
21758 return; /* nothing to dump */
21759
Bram Moolenaar73830342005-02-28 22:48:19 +000021760 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21761
Bram Moolenaar05159a02005-02-26 23:04:13 +000021762 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21763 {
21764 if (!HASHITEM_EMPTY(hi))
21765 {
21766 --todo;
21767 fp = HI2UF(hi);
21768 if (fp->uf_profiling)
21769 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021770 if (sorttab != NULL)
21771 sorttab[st_len++] = fp;
21772
Bram Moolenaar05159a02005-02-26 23:04:13 +000021773 if (fp->uf_name[0] == K_SPECIAL)
21774 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21775 else
21776 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21777 if (fp->uf_tm_count == 1)
21778 fprintf(fd, "Called 1 time\n");
21779 else
21780 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21781 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21782 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21783 fprintf(fd, "\n");
21784 fprintf(fd, "count total (s) self (s)\n");
21785
21786 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21787 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021788 if (FUNCLINE(fp, i) == NULL)
21789 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021790 prof_func_line(fd, fp->uf_tml_count[i],
21791 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021792 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21793 }
21794 fprintf(fd, "\n");
21795 }
21796 }
21797 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021798
21799 if (sorttab != NULL && st_len > 0)
21800 {
21801 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21802 prof_total_cmp);
21803 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21804 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21805 prof_self_cmp);
21806 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21807 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021808
21809 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021810}
Bram Moolenaar73830342005-02-28 22:48:19 +000021811
21812 static void
21813prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21814 FILE *fd;
21815 ufunc_T **sorttab;
21816 int st_len;
21817 char *title;
21818 int prefer_self; /* when equal print only self time */
21819{
21820 int i;
21821 ufunc_T *fp;
21822
21823 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21824 fprintf(fd, "count total (s) self (s) function\n");
21825 for (i = 0; i < 20 && i < st_len; ++i)
21826 {
21827 fp = sorttab[i];
21828 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21829 prefer_self);
21830 if (fp->uf_name[0] == K_SPECIAL)
21831 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21832 else
21833 fprintf(fd, " %s()\n", fp->uf_name);
21834 }
21835 fprintf(fd, "\n");
21836}
21837
21838/*
21839 * Print the count and times for one function or function line.
21840 */
21841 static void
21842prof_func_line(fd, count, total, self, prefer_self)
21843 FILE *fd;
21844 int count;
21845 proftime_T *total;
21846 proftime_T *self;
21847 int prefer_self; /* when equal print only self time */
21848{
21849 if (count > 0)
21850 {
21851 fprintf(fd, "%5d ", count);
21852 if (prefer_self && profile_equal(total, self))
21853 fprintf(fd, " ");
21854 else
21855 fprintf(fd, "%s ", profile_msg(total));
21856 if (!prefer_self && profile_equal(total, self))
21857 fprintf(fd, " ");
21858 else
21859 fprintf(fd, "%s ", profile_msg(self));
21860 }
21861 else
21862 fprintf(fd, " ");
21863}
21864
21865/*
21866 * Compare function for total time sorting.
21867 */
21868 static int
21869#ifdef __BORLANDC__
21870_RTLENTRYF
21871#endif
21872prof_total_cmp(s1, s2)
21873 const void *s1;
21874 const void *s2;
21875{
21876 ufunc_T *p1, *p2;
21877
21878 p1 = *(ufunc_T **)s1;
21879 p2 = *(ufunc_T **)s2;
21880 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21881}
21882
21883/*
21884 * Compare function for self time sorting.
21885 */
21886 static int
21887#ifdef __BORLANDC__
21888_RTLENTRYF
21889#endif
21890prof_self_cmp(s1, s2)
21891 const void *s1;
21892 const void *s2;
21893{
21894 ufunc_T *p1, *p2;
21895
21896 p1 = *(ufunc_T **)s1;
21897 p2 = *(ufunc_T **)s2;
21898 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21899}
21900
Bram Moolenaar05159a02005-02-26 23:04:13 +000021901#endif
21902
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021903/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021904 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021905 * Return TRUE if a package was loaded.
21906 */
21907 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021908script_autoload(name, reload)
21909 char_u *name;
21910 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021911{
21912 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021913 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021914 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021915 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021916
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021917 /* Return quickly when autoload disabled. */
21918 if (no_autoload)
21919 return FALSE;
21920
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021921 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021922 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021923 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021924 return FALSE;
21925
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021926 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021927
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021928 /* Find the name in the list of previously loaded package names. Skip
21929 * "autoload/", it's always the same. */
21930 for (i = 0; i < ga_loaded.ga_len; ++i)
21931 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21932 break;
21933 if (!reload && i < ga_loaded.ga_len)
21934 ret = FALSE; /* was loaded already */
21935 else
21936 {
21937 /* Remember the name if it wasn't loaded already. */
21938 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21939 {
21940 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21941 tofree = NULL;
21942 }
21943
21944 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021945 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021946 ret = TRUE;
21947 }
21948
21949 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021950 return ret;
21951}
21952
21953/*
21954 * Return the autoload script name for a function or variable name.
21955 * Returns NULL when out of memory.
21956 */
21957 static char_u *
21958autoload_name(name)
21959 char_u *name;
21960{
21961 char_u *p;
21962 char_u *scriptname;
21963
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021964 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021965 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21966 if (scriptname == NULL)
21967 return FALSE;
21968 STRCPY(scriptname, "autoload/");
21969 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021970 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021971 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021972 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021973 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021974 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021975}
21976
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21978
21979/*
21980 * Function given to ExpandGeneric() to obtain the list of user defined
21981 * function names.
21982 */
21983 char_u *
21984get_user_func_name(xp, idx)
21985 expand_T *xp;
21986 int idx;
21987{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021988 static long_u done;
21989 static hashitem_T *hi;
21990 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991
21992 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021994 done = 0;
21995 hi = func_hashtab.ht_array;
21996 }
21997 if (done < func_hashtab.ht_used)
21998 {
21999 if (done++ > 0)
22000 ++hi;
22001 while (HASHITEM_EMPTY(hi))
22002 ++hi;
22003 fp = HI2UF(hi);
22004
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022005 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022006 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022007
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022008 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22009 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010
22011 cat_func_name(IObuff, fp);
22012 if (xp->xp_context != EXPAND_USER_FUNC)
22013 {
22014 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022015 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 STRCAT(IObuff, ")");
22017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 return IObuff;
22019 }
22020 return NULL;
22021}
22022
22023#endif /* FEAT_CMDL_COMPL */
22024
22025/*
22026 * Copy the function name of "fp" to buffer "buf".
22027 * "buf" must be able to hold the function name plus three bytes.
22028 * Takes care of script-local function names.
22029 */
22030 static void
22031cat_func_name(buf, fp)
22032 char_u *buf;
22033 ufunc_T *fp;
22034{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022035 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 {
22037 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022038 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 }
22040 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022041 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042}
22043
22044/*
22045 * ":delfunction {name}"
22046 */
22047 void
22048ex_delfunction(eap)
22049 exarg_T *eap;
22050{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022051 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 char_u *p;
22053 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022054 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055
22056 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022057 name = trans_function_name(&p, eap->skip, 0, &fudi);
22058 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022060 {
22061 if (fudi.fd_dict != NULL && !eap->skip)
22062 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 if (!ends_excmd(*skipwhite(p)))
22066 {
22067 vim_free(name);
22068 EMSG(_(e_trailing));
22069 return;
22070 }
22071 eap->nextcmd = check_nextcmd(p);
22072 if (eap->nextcmd != NULL)
22073 *p = NUL;
22074
22075 if (!eap->skip)
22076 fp = find_func(name);
22077 vim_free(name);
22078
22079 if (!eap->skip)
22080 {
22081 if (fp == NULL)
22082 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022083 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 return;
22085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022086 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 {
22088 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22089 return;
22090 }
22091
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022092 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022094 /* Delete the dict item that refers to the function, it will
22095 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022096 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022098 else
22099 func_free(fp);
22100 }
22101}
22102
22103/*
22104 * Free a function and remove it from the list of functions.
22105 */
22106 static void
22107func_free(fp)
22108 ufunc_T *fp;
22109{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022110 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022111
22112 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022113 ga_clear_strings(&(fp->uf_args));
22114 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022115#ifdef FEAT_PROFILE
22116 vim_free(fp->uf_tml_count);
22117 vim_free(fp->uf_tml_total);
22118 vim_free(fp->uf_tml_self);
22119#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022120
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022121 /* remove the function from the function hashtable */
22122 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22123 if (HASHITEM_EMPTY(hi))
22124 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022126 hash_remove(&func_hashtab, hi);
22127
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022128 vim_free(fp);
22129}
22130
22131/*
22132 * Unreference a Function: decrement the reference count and free it when it
22133 * becomes zero. Only for numbered functions.
22134 */
22135 static void
22136func_unref(name)
22137 char_u *name;
22138{
22139 ufunc_T *fp;
22140
22141 if (name != NULL && isdigit(*name))
22142 {
22143 fp = find_func(name);
22144 if (fp == NULL)
22145 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022146 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022147 {
22148 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022149 * when "uf_calls" becomes zero. */
22150 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022151 func_free(fp);
22152 }
22153 }
22154}
22155
22156/*
22157 * Count a reference to a Function.
22158 */
22159 static void
22160func_ref(name)
22161 char_u *name;
22162{
22163 ufunc_T *fp;
22164
22165 if (name != NULL && isdigit(*name))
22166 {
22167 fp = find_func(name);
22168 if (fp == NULL)
22169 EMSG2(_(e_intern2), "func_ref()");
22170 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022171 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 }
22173}
22174
22175/*
22176 * Call a user function.
22177 */
22178 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022179call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 ufunc_T *fp; /* pointer to function */
22181 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 typval_T *argvars; /* arguments */
22183 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 linenr_T firstline; /* first line of range */
22185 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187{
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 char_u *save_sourcing_name;
22189 linenr_T save_sourcing_lnum;
22190 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022191 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 int save_did_emsg;
22193 static int depth = 0;
22194 dictitem_T *v;
22195 int fixvar_idx = 0; /* index in fixvar[] */
22196 int i;
22197 int ai;
22198 char_u numbuf[NUMBUFLEN];
22199 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022200#ifdef FEAT_PROFILE
22201 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022202 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204
22205 /* If depth of calling is getting too high, don't execute the function */
22206 if (depth >= p_mfd)
22207 {
22208 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022209 rettv->v_type = VAR_NUMBER;
22210 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 return;
22212 }
22213 ++depth;
22214
22215 line_breakcheck(); /* check for CTRL-C hit */
22216
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022217 fc = (funccall_T *)alloc(sizeof(funccall_T));
22218 fc->caller = current_funccal;
22219 current_funccal = fc;
22220 fc->func = fp;
22221 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022223 fc->linenr = 0;
22224 fc->returned = FALSE;
22225 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022227 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22228 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229
Bram Moolenaar33570922005-01-25 22:26:29 +000022230 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022231 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22233 * each argument variable and saves a lot of time.
22234 */
22235 /*
22236 * Init l: variables.
22237 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022238 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022239 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022240 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022241 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22242 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022243 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022244 name = v->di_key;
22245 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022246 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022247 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022248 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022249 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022250 v->di_tv.vval.v_dict = selfdict;
22251 ++selfdict->dv_refcount;
22252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022253
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 /*
22255 * Init a: variables.
22256 * Set a:0 to "argcount".
22257 * Set a:000 to a list with room for the "..." arguments.
22258 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022259 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22260 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022261 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022262 /* Use "name" to avoid a warning from some compiler that checks the
22263 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022264 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022265 name = v->di_key;
22266 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022268 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022269 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022270 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022271 v->di_tv.vval.v_list = &fc->l_varlist;
22272 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22273 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22274 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022275
22276 /*
22277 * Set a:firstline to "firstline" and a:lastline to "lastline".
22278 * Set a:name to named arguments.
22279 * Set a:N to the "..." arguments.
22280 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022281 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022283 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022284 (varnumber_T)lastline);
22285 for (i = 0; i < argcount; ++i)
22286 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022287 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 if (ai < 0)
22289 /* named argument a:name */
22290 name = FUNCARG(fp, i);
22291 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022292 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022293 /* "..." argument a:1, a:2, etc. */
22294 sprintf((char *)numbuf, "%d", ai + 1);
22295 name = numbuf;
22296 }
22297 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22298 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022299 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022300 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22301 }
22302 else
22303 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022304 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22305 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 if (v == NULL)
22307 break;
22308 v->di_flags = DI_FLAGS_RO;
22309 }
22310 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022311 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022312
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022313 /* Note: the values are copied directly to avoid alloc/free.
22314 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022315 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022316 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022317
22318 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22319 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022320 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22321 fc->l_listitems[ai].li_tv = argvars[i];
22322 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022323 }
22324 }
22325
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 /* Don't redraw while executing the function. */
22327 ++RedrawingDisabled;
22328 save_sourcing_name = sourcing_name;
22329 save_sourcing_lnum = sourcing_lnum;
22330 sourcing_lnum = 1;
22331 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022332 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 if (sourcing_name != NULL)
22334 {
22335 if (save_sourcing_name != NULL
22336 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22337 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22338 else
22339 STRCPY(sourcing_name, "function ");
22340 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22341
22342 if (p_verbose >= 12)
22343 {
22344 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022345 verbose_enter_scroll();
22346
Bram Moolenaar555b2802005-05-19 21:08:39 +000022347 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 if (p_verbose >= 14)
22349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022351 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022352 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022353 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354
22355 msg_puts((char_u *)"(");
22356 for (i = 0; i < argcount; ++i)
22357 {
22358 if (i > 0)
22359 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022360 if (argvars[i].v_type == VAR_NUMBER)
22361 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 else
22363 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022364 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22365 if (s != NULL)
22366 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022367 if (vim_strsize(s) > MSG_BUF_CLEN)
22368 {
22369 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22370 s = buf;
22371 }
22372 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022373 vim_free(tofree);
22374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 }
22376 }
22377 msg_puts((char_u *)")");
22378 }
22379 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022380
22381 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 --no_wait_return;
22383 }
22384 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022385#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022386 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022387 {
22388 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22389 func_do_profile(fp);
22390 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022391 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022392 {
22393 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022394 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022395 profile_zero(&fp->uf_tm_children);
22396 }
22397 script_prof_save(&wait_start);
22398 }
22399#endif
22400
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022402 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 save_did_emsg = did_emsg;
22404 did_emsg = FALSE;
22405
22406 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022407 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22409
22410 --RedrawingDisabled;
22411
22412 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022413 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415 clear_tv(rettv);
22416 rettv->v_type = VAR_NUMBER;
22417 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 }
22419
Bram Moolenaar05159a02005-02-26 23:04:13 +000022420#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022421 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022422 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022423 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022424 profile_end(&call_start);
22425 profile_sub_wait(&wait_start, &call_start);
22426 profile_add(&fp->uf_tm_total, &call_start);
22427 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022428 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022429 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022430 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22431 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022432 }
22433 }
22434#endif
22435
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 /* when being verbose, mention the return value */
22437 if (p_verbose >= 12)
22438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022440 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022443 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022444 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022445 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022446 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022449 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022450 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022451 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022452 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022453
Bram Moolenaar555b2802005-05-19 21:08:39 +000022454 /* The value may be very long. Skip the middle part, so that we
22455 * have some idea how it starts and ends. smsg() would always
22456 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022457 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022458 if (s != NULL)
22459 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022460 if (vim_strsize(s) > MSG_BUF_CLEN)
22461 {
22462 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22463 s = buf;
22464 }
22465 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022466 vim_free(tofree);
22467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 }
22469 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022470
22471 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 --no_wait_return;
22473 }
22474
22475 vim_free(sourcing_name);
22476 sourcing_name = save_sourcing_name;
22477 sourcing_lnum = save_sourcing_lnum;
22478 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022479#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022480 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022481 script_prof_restore(&wait_start);
22482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483
22484 if (p_verbose >= 12 && sourcing_name != NULL)
22485 {
22486 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022487 verbose_enter_scroll();
22488
Bram Moolenaar555b2802005-05-19 21:08:39 +000022489 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022491
22492 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 --no_wait_return;
22494 }
22495
22496 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022497 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022499
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022500 /* If the a:000 list and the l: and a: dicts are not referenced we can
22501 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022502 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22503 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22504 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22505 {
22506 free_funccal(fc, FALSE);
22507 }
22508 else
22509 {
22510 hashitem_T *hi;
22511 listitem_T *li;
22512 int todo;
22513
22514 /* "fc" is still in use. This can happen when returning "a:000" or
22515 * assigning "l:" to a global variable.
22516 * Link "fc" in the list for garbage collection later. */
22517 fc->caller = previous_funccal;
22518 previous_funccal = fc;
22519
22520 /* Make a copy of the a: variables, since we didn't do that above. */
22521 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22522 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22523 {
22524 if (!HASHITEM_EMPTY(hi))
22525 {
22526 --todo;
22527 v = HI2DI(hi);
22528 copy_tv(&v->di_tv, &v->di_tv);
22529 }
22530 }
22531
22532 /* Make a copy of the a:000 items, since we didn't do that above. */
22533 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22534 copy_tv(&li->li_tv, &li->li_tv);
22535 }
22536}
22537
22538/*
22539 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022540 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022541 */
22542 static int
22543can_free_funccal(fc, copyID)
22544 funccall_T *fc;
22545 int copyID;
22546{
22547 return (fc->l_varlist.lv_copyID != copyID
22548 && fc->l_vars.dv_copyID != copyID
22549 && fc->l_avars.dv_copyID != copyID);
22550}
22551
22552/*
22553 * Free "fc" and what it contains.
22554 */
22555 static void
22556free_funccal(fc, free_val)
22557 funccall_T *fc;
22558 int free_val; /* a: vars were allocated */
22559{
22560 listitem_T *li;
22561
22562 /* The a: variables typevals may not have been allocated, only free the
22563 * allocated variables. */
22564 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22565
22566 /* free all l: variables */
22567 vars_clear(&fc->l_vars.dv_hashtab);
22568
22569 /* Free the a:000 variables if they were allocated. */
22570 if (free_val)
22571 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22572 clear_tv(&li->li_tv);
22573
22574 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575}
22576
22577/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 * Add a number variable "name" to dict "dp" with value "nr".
22579 */
22580 static void
22581add_nr_var(dp, v, name, nr)
22582 dict_T *dp;
22583 dictitem_T *v;
22584 char *name;
22585 varnumber_T nr;
22586{
22587 STRCPY(v->di_key, name);
22588 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22589 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22590 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022591 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 v->di_tv.vval.v_number = nr;
22593}
22594
22595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 * ":return [expr]"
22597 */
22598 void
22599ex_return(eap)
22600 exarg_T *eap;
22601{
22602 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022603 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 int returning = FALSE;
22605
22606 if (current_funccal == NULL)
22607 {
22608 EMSG(_("E133: :return not inside a function"));
22609 return;
22610 }
22611
22612 if (eap->skip)
22613 ++emsg_skip;
22614
22615 eap->nextcmd = NULL;
22616 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022617 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 {
22619 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022620 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022622 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 }
22624 /* It's safer to return also on error. */
22625 else if (!eap->skip)
22626 {
22627 /*
22628 * Return unless the expression evaluation has been cancelled due to an
22629 * aborting error, an interrupt, or an exception.
22630 */
22631 if (!aborting())
22632 returning = do_return(eap, FALSE, TRUE, NULL);
22633 }
22634
22635 /* When skipping or the return gets pending, advance to the next command
22636 * in this line (!returning). Otherwise, ignore the rest of the line.
22637 * Following lines will be ignored by get_func_line(). */
22638 if (returning)
22639 eap->nextcmd = NULL;
22640 else if (eap->nextcmd == NULL) /* no argument */
22641 eap->nextcmd = check_nextcmd(arg);
22642
22643 if (eap->skip)
22644 --emsg_skip;
22645}
22646
22647/*
22648 * Return from a function. Possibly makes the return pending. Also called
22649 * for a pending return at the ":endtry" or after returning from an extra
22650 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022652 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 * FALSE when the return gets pending.
22654 */
22655 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022656do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 exarg_T *eap;
22658 int reanimate;
22659 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022660 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661{
22662 int idx;
22663 struct condstack *cstack = eap->cstack;
22664
22665 if (reanimate)
22666 /* Undo the return. */
22667 current_funccal->returned = FALSE;
22668
22669 /*
22670 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22671 * not in its finally clause (which then is to be executed next) is found.
22672 * In this case, make the ":return" pending for execution at the ":endtry".
22673 * Otherwise, return normally.
22674 */
22675 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22676 if (idx >= 0)
22677 {
22678 cstack->cs_pending[idx] = CSTP_RETURN;
22679
22680 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022681 /* A pending return again gets pending. "rettv" points to an
22682 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022684 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 else
22686 {
22687 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022688 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022690 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022692 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 {
22694 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022695 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022696 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 else
22698 EMSG(_(e_outofmem));
22699 }
22700 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022701 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702
22703 if (reanimate)
22704 {
22705 /* The pending return value could be overwritten by a ":return"
22706 * without argument in a finally clause; reset the default
22707 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022708 current_funccal->rettv->v_type = VAR_NUMBER;
22709 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 }
22711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022712 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 }
22714 else
22715 {
22716 current_funccal->returned = TRUE;
22717
22718 /* If the return is carried out now, store the return value. For
22719 * a return immediately after reanimation, the value is already
22720 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022721 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022723 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022726 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 }
22728 }
22729
22730 return idx < 0;
22731}
22732
22733/*
22734 * Free the variable with a pending return value.
22735 */
22736 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022737discard_pending_return(rettv)
22738 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739{
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741}
22742
22743/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022744 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 * is an allocated string. Used by report_pending() for verbose messages.
22746 */
22747 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022748get_return_cmd(rettv)
22749 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022751 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022752 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022753 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022755 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022756 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022757 if (s == NULL)
22758 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022759
22760 STRCPY(IObuff, ":return ");
22761 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22762 if (STRLEN(s) + 8 >= IOSIZE)
22763 STRCPY(IObuff + IOSIZE - 4, "...");
22764 vim_free(tofree);
22765 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766}
22767
22768/*
22769 * Get next function line.
22770 * Called by do_cmdline() to get the next line.
22771 * Returns allocated string, or NULL for end of function.
22772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 char_u *
22774get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022775 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022777 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778{
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022780 ufunc_T *fp = fcp->func;
22781 char_u *retval;
22782 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783
22784 /* If breakpoints have been added/deleted need to check for it. */
22785 if (fcp->dbg_tick != debug_tick)
22786 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022787 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 sourcing_lnum);
22789 fcp->dbg_tick = debug_tick;
22790 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022791#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022792 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022793 func_line_end(cookie);
22794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795
Bram Moolenaar05159a02005-02-26 23:04:13 +000022796 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022797 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22798 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 retval = NULL;
22800 else
22801 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022802 /* Skip NULL lines (continuation lines). */
22803 while (fcp->linenr < gap->ga_len
22804 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22805 ++fcp->linenr;
22806 if (fcp->linenr >= gap->ga_len)
22807 retval = NULL;
22808 else
22809 {
22810 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22811 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022812#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022813 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022814 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022815#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 }
22818
22819 /* Did we encounter a breakpoint? */
22820 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22821 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022822 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022824 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 sourcing_lnum);
22826 fcp->dbg_tick = debug_tick;
22827 }
22828
22829 return retval;
22830}
22831
Bram Moolenaar05159a02005-02-26 23:04:13 +000022832#if defined(FEAT_PROFILE) || defined(PROTO)
22833/*
22834 * Called when starting to read a function line.
22835 * "sourcing_lnum" must be correct!
22836 * When skipping lines it may not actually be executed, but we won't find out
22837 * until later and we need to store the time now.
22838 */
22839 void
22840func_line_start(cookie)
22841 void *cookie;
22842{
22843 funccall_T *fcp = (funccall_T *)cookie;
22844 ufunc_T *fp = fcp->func;
22845
22846 if (fp->uf_profiling && sourcing_lnum >= 1
22847 && sourcing_lnum <= fp->uf_lines.ga_len)
22848 {
22849 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022850 /* Skip continuation lines. */
22851 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22852 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022853 fp->uf_tml_execed = FALSE;
22854 profile_start(&fp->uf_tml_start);
22855 profile_zero(&fp->uf_tml_children);
22856 profile_get_wait(&fp->uf_tml_wait);
22857 }
22858}
22859
22860/*
22861 * Called when actually executing a function line.
22862 */
22863 void
22864func_line_exec(cookie)
22865 void *cookie;
22866{
22867 funccall_T *fcp = (funccall_T *)cookie;
22868 ufunc_T *fp = fcp->func;
22869
22870 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22871 fp->uf_tml_execed = TRUE;
22872}
22873
22874/*
22875 * Called when done with a function line.
22876 */
22877 void
22878func_line_end(cookie)
22879 void *cookie;
22880{
22881 funccall_T *fcp = (funccall_T *)cookie;
22882 ufunc_T *fp = fcp->func;
22883
22884 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22885 {
22886 if (fp->uf_tml_execed)
22887 {
22888 ++fp->uf_tml_count[fp->uf_tml_idx];
22889 profile_end(&fp->uf_tml_start);
22890 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022891 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022892 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22893 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022894 }
22895 fp->uf_tml_idx = -1;
22896 }
22897}
22898#endif
22899
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900/*
22901 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022902 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 */
22904 int
22905func_has_ended(cookie)
22906 void *cookie;
22907{
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909
22910 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22911 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022912 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 || fcp->returned);
22914}
22915
22916/*
22917 * return TRUE if cookie indicates a function which "abort"s on errors.
22918 */
22919 int
22920func_has_abort(cookie)
22921 void *cookie;
22922{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022923 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924}
22925
22926#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22927typedef enum
22928{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022929 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22930 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22931 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932} var_flavour_T;
22933
22934static var_flavour_T var_flavour __ARGS((char_u *varname));
22935
22936 static var_flavour_T
22937var_flavour(varname)
22938 char_u *varname;
22939{
22940 char_u *p = varname;
22941
22942 if (ASCII_ISUPPER(*p))
22943 {
22944 while (*(++p))
22945 if (ASCII_ISLOWER(*p))
22946 return VAR_FLAVOUR_SESSION;
22947 return VAR_FLAVOUR_VIMINFO;
22948 }
22949 else
22950 return VAR_FLAVOUR_DEFAULT;
22951}
22952#endif
22953
22954#if defined(FEAT_VIMINFO) || defined(PROTO)
22955/*
22956 * Restore global vars that start with a capital from the viminfo file
22957 */
22958 int
22959read_viminfo_varlist(virp, writing)
22960 vir_T *virp;
22961 int writing;
22962{
22963 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022964 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022965 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966
22967 if (!writing && (find_viminfo_parameter('!') != NULL))
22968 {
22969 tab = vim_strchr(virp->vir_line + 1, '\t');
22970 if (tab != NULL)
22971 {
22972 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022973 switch (*tab)
22974 {
22975 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022976#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022977 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022978#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022979 case 'D': type = VAR_DICT; break;
22980 case 'L': type = VAR_LIST; break;
22981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982
22983 tab = vim_strchr(tab, '\t');
22984 if (tab != NULL)
22985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022986 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022987 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022988 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022990#ifdef FEAT_FLOAT
22991 else if (type == VAR_FLOAT)
22992 (void)string2float(tab + 1, &tv.vval.v_float);
22993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022995 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022996 if (type == VAR_DICT || type == VAR_LIST)
22997 {
22998 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22999
23000 if (etv == NULL)
23001 /* Failed to parse back the dict or list, use it as a
23002 * string. */
23003 tv.v_type = VAR_STRING;
23004 else
23005 {
23006 vim_free(tv.vval.v_string);
23007 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023008 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023009 }
23010 }
23011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023012 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023013
23014 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023015 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023016 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23017 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 }
23019 }
23020 }
23021
23022 return viminfo_readline(virp);
23023}
23024
23025/*
23026 * Write global vars that start with a capital to the viminfo file
23027 */
23028 void
23029write_viminfo_varlist(fp)
23030 FILE *fp;
23031{
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 hashitem_T *hi;
23033 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023034 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023035 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023036 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023037 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023038 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039
23040 if (find_viminfo_parameter('!') == NULL)
23041 return;
23042
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023043 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023045 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023046 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023048 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023050 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 this_var = HI2DI(hi);
23052 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023053 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023054 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023055 {
23056 case VAR_STRING: s = "STR"; break;
23057 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023058#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023059 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023060#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023061 case VAR_DICT: s = "DIC"; break;
23062 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023063 default: continue;
23064 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023066 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023067 if (p != NULL)
23068 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023069 vim_free(tofree);
23070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 }
23072 }
23073}
23074#endif
23075
23076#if defined(FEAT_SESSION) || defined(PROTO)
23077 int
23078store_session_globals(fd)
23079 FILE *fd;
23080{
Bram Moolenaar33570922005-01-25 22:26:29 +000023081 hashitem_T *hi;
23082 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023083 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 char_u *p, *t;
23085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023086 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023089 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023091 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 this_var = HI2DI(hi);
23093 if ((this_var->di_tv.v_type == VAR_NUMBER
23094 || this_var->di_tv.v_type == VAR_STRING)
23095 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023096 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023097 /* Escape special characters with a backslash. Turn a LF and
23098 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023099 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023100 (char_u *)"\\\"\n\r");
23101 if (p == NULL) /* out of memory */
23102 break;
23103 for (t = p; *t != NUL; ++t)
23104 if (*t == '\n')
23105 *t = 'n';
23106 else if (*t == '\r')
23107 *t = 'r';
23108 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 this_var->di_key,
23110 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23111 : ' ',
23112 p,
23113 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23114 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023115 || put_eol(fd) == FAIL)
23116 {
23117 vim_free(p);
23118 return FAIL;
23119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 vim_free(p);
23121 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023122#ifdef FEAT_FLOAT
23123 else if (this_var->di_tv.v_type == VAR_FLOAT
23124 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23125 {
23126 float_T f = this_var->di_tv.vval.v_float;
23127 int sign = ' ';
23128
23129 if (f < 0)
23130 {
23131 f = -f;
23132 sign = '-';
23133 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023134 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023135 this_var->di_key, sign, f) < 0)
23136 || put_eol(fd) == FAIL)
23137 return FAIL;
23138 }
23139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 }
23141 }
23142 return OK;
23143}
23144#endif
23145
Bram Moolenaar661b1822005-07-28 22:36:45 +000023146/*
23147 * Display script name where an item was last set.
23148 * Should only be invoked when 'verbose' is non-zero.
23149 */
23150 void
23151last_set_msg(scriptID)
23152 scid_T scriptID;
23153{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023154 char_u *p;
23155
Bram Moolenaar661b1822005-07-28 22:36:45 +000023156 if (scriptID != 0)
23157 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023158 p = home_replace_save(NULL, get_scriptname(scriptID));
23159 if (p != NULL)
23160 {
23161 verbose_enter();
23162 MSG_PUTS(_("\n\tLast set from "));
23163 MSG_PUTS(p);
23164 vim_free(p);
23165 verbose_leave();
23166 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023167 }
23168}
23169
Bram Moolenaard812df62008-11-09 12:46:09 +000023170/*
23171 * List v:oldfiles in a nice way.
23172 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023173 void
23174ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023175 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023176{
23177 list_T *l = vimvars[VV_OLDFILES].vv_list;
23178 listitem_T *li;
23179 int nr = 0;
23180
23181 if (l == NULL)
23182 msg((char_u *)_("No old files"));
23183 else
23184 {
23185 msg_start();
23186 msg_scroll = TRUE;
23187 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23188 {
23189 msg_outnum((long)++nr);
23190 MSG_PUTS(": ");
23191 msg_outtrans(get_tv_string(&li->li_tv));
23192 msg_putchar('\n');
23193 out_flush(); /* output one line at a time */
23194 ui_breakcheck();
23195 }
23196 /* Assume "got_int" was set to truncate the listing. */
23197 got_int = FALSE;
23198
23199#ifdef FEAT_BROWSE_CMD
23200 if (cmdmod.browse)
23201 {
23202 quit_more = FALSE;
23203 nr = prompt_for_number(FALSE);
23204 msg_starthere();
23205 if (nr > 0)
23206 {
23207 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23208 (long)nr);
23209
23210 if (p != NULL)
23211 {
23212 p = expand_env_save(p);
23213 eap->arg = p;
23214 eap->cmdidx = CMD_edit;
23215 cmdmod.browse = FALSE;
23216 do_exedit(eap, NULL);
23217 vim_free(p);
23218 }
23219 }
23220 }
23221#endif
23222 }
23223}
23224
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225#endif /* FEAT_EVAL */
23226
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023228#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229
23230#ifdef WIN3264
23231/*
23232 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23233 */
23234static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23235static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23236static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23237
23238/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023239 * Get the short path (8.3) for the filename in "fnamep".
23240 * Only works for a valid file name.
23241 * When the path gets longer "fnamep" is changed and the allocated buffer
23242 * is put in "bufp".
23243 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23244 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 */
23246 static int
23247get_short_pathname(fnamep, bufp, fnamelen)
23248 char_u **fnamep;
23249 char_u **bufp;
23250 int *fnamelen;
23251{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023252 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 char_u *newbuf;
23254
23255 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 l = GetShortPathName(*fnamep, *fnamep, len);
23257 if (l > len - 1)
23258 {
23259 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023260 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 newbuf = vim_strnsave(*fnamep, l);
23262 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264
23265 vim_free(*bufp);
23266 *fnamep = *bufp = newbuf;
23267
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023268 /* Really should always succeed, as the buffer is big enough. */
23269 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 }
23271
23272 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023273 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274}
23275
23276/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023277 * Get the short path (8.3) for the filename in "fname". The converted
23278 * path is returned in "bufp".
23279 *
23280 * Some of the directories specified in "fname" may not exist. This function
23281 * will shorten the existing directories at the beginning of the path and then
23282 * append the remaining non-existing path.
23283 *
23284 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023285 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023286 * bufp - Pointer to an allocated buffer for the filename.
23287 * fnamelen - Length of the filename pointed to by fname
23288 *
23289 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 */
23291 static int
23292shortpath_for_invalid_fname(fname, bufp, fnamelen)
23293 char_u **fname;
23294 char_u **bufp;
23295 int *fnamelen;
23296{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023297 char_u *short_fname, *save_fname, *pbuf_unused;
23298 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023300 int old_len, len;
23301 int new_len, sfx_len;
23302 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303
23304 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023305 old_len = *fnamelen;
23306 save_fname = vim_strnsave(*fname, old_len);
23307 pbuf_unused = NULL;
23308 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023310 endp = save_fname + old_len - 1; /* Find the end of the copy */
23311 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023313 /*
23314 * Try shortening the supplied path till it succeeds by removing one
23315 * directory at a time from the tail of the path.
23316 */
23317 len = 0;
23318 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023320 /* go back one path-separator */
23321 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23322 --endp;
23323 if (endp <= save_fname)
23324 break; /* processed the complete path */
23325
23326 /*
23327 * Replace the path separator with a NUL and try to shorten the
23328 * resulting path.
23329 */
23330 ch = *endp;
23331 *endp = 0;
23332 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023333 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023334 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23335 {
23336 retval = FAIL;
23337 goto theend;
23338 }
23339 *endp = ch; /* preserve the string */
23340
23341 if (len > 0)
23342 break; /* successfully shortened the path */
23343
23344 /* failed to shorten the path. Skip the path separator */
23345 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 }
23347
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023348 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023350 /*
23351 * Succeeded in shortening the path. Now concatenate the shortened
23352 * path with the remaining path at the tail.
23353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023355 /* Compute the length of the new path. */
23356 sfx_len = (int)(save_endp - endp) + 1;
23357 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023359 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023361 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023363 /* There is not enough space in the currently allocated string,
23364 * copy it to a buffer big enough. */
23365 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023367 {
23368 retval = FAIL;
23369 goto theend;
23370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 }
23372 else
23373 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023374 /* Transfer short_fname to the main buffer (it's big enough),
23375 * unless get_short_pathname() did its work in-place. */
23376 *fname = *bufp = save_fname;
23377 if (short_fname != save_fname)
23378 vim_strncpy(save_fname, short_fname, len);
23379 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023381
23382 /* concat the not-shortened part of the path */
23383 vim_strncpy(*fname + len, endp, sfx_len);
23384 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023386
23387theend:
23388 vim_free(pbuf_unused);
23389 vim_free(save_fname);
23390
23391 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392}
23393
23394/*
23395 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023396 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 */
23398 static int
23399shortpath_for_partial(fnamep, bufp, fnamelen)
23400 char_u **fnamep;
23401 char_u **bufp;
23402 int *fnamelen;
23403{
23404 int sepcount, len, tflen;
23405 char_u *p;
23406 char_u *pbuf, *tfname;
23407 int hasTilde;
23408
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023409 /* Count up the path separators from the RHS.. so we know which part
23410 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023412 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 if (vim_ispathsep(*p))
23414 ++sepcount;
23415
23416 /* Need full path first (use expand_env() to remove a "~/") */
23417 hasTilde = (**fnamep == '~');
23418 if (hasTilde)
23419 pbuf = tfname = expand_env_save(*fnamep);
23420 else
23421 pbuf = tfname = FullName_save(*fnamep, FALSE);
23422
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023423 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023425 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023427
23428 if (len == 0)
23429 {
23430 /* Don't have a valid filename, so shorten the rest of the
23431 * path if we can. This CAN give us invalid 8.3 filenames, but
23432 * there's not a lot of point in guessing what it might be.
23433 */
23434 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023435 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 }
23438
23439 /* Count the paths backward to find the beginning of the desired string. */
23440 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023441 {
23442#ifdef FEAT_MBYTE
23443 if (has_mbyte)
23444 p -= mb_head_off(tfname, p);
23445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 if (vim_ispathsep(*p))
23447 {
23448 if (sepcount == 0 || (hasTilde && sepcount == 1))
23449 break;
23450 else
23451 sepcount --;
23452 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 if (hasTilde)
23455 {
23456 --p;
23457 if (p >= tfname)
23458 *p = '~';
23459 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 }
23462 else
23463 ++p;
23464
23465 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23466 vim_free(*bufp);
23467 *fnamelen = (int)STRLEN(p);
23468 *bufp = pbuf;
23469 *fnamep = p;
23470
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023471 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472}
23473#endif /* WIN3264 */
23474
23475/*
23476 * Adjust a filename, according to a string of modifiers.
23477 * *fnamep must be NUL terminated when called. When returning, the length is
23478 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023479 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 * When there is an error, *fnamep is set to NULL.
23481 */
23482 int
23483modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23484 char_u *src; /* string with modifiers */
23485 int *usedlen; /* characters after src that are used */
23486 char_u **fnamep; /* file name so far */
23487 char_u **bufp; /* buffer for allocated file name or NULL */
23488 int *fnamelen; /* length of fnamep */
23489{
23490 int valid = 0;
23491 char_u *tail;
23492 char_u *s, *p, *pbuf;
23493 char_u dirname[MAXPATHL];
23494 int c;
23495 int has_fullname = 0;
23496#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023497 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 int has_shortname = 0;
23499#endif
23500
23501repeat:
23502 /* ":p" - full path/file_name */
23503 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23504 {
23505 has_fullname = 1;
23506
23507 valid |= VALID_PATH;
23508 *usedlen += 2;
23509
23510 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23511 if ((*fnamep)[0] == '~'
23512#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23513 && ((*fnamep)[1] == '/'
23514# ifdef BACKSLASH_IN_FILENAME
23515 || (*fnamep)[1] == '\\'
23516# endif
23517 || (*fnamep)[1] == NUL)
23518
23519#endif
23520 )
23521 {
23522 *fnamep = expand_env_save(*fnamep);
23523 vim_free(*bufp); /* free any allocated file name */
23524 *bufp = *fnamep;
23525 if (*fnamep == NULL)
23526 return -1;
23527 }
23528
23529 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023530 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 {
23532 if (vim_ispathsep(*p)
23533 && p[1] == '.'
23534 && (p[2] == NUL
23535 || vim_ispathsep(p[2])
23536 || (p[2] == '.'
23537 && (p[3] == NUL || vim_ispathsep(p[3])))))
23538 break;
23539 }
23540
23541 /* FullName_save() is slow, don't use it when not needed. */
23542 if (*p != NUL || !vim_isAbsName(*fnamep))
23543 {
23544 *fnamep = FullName_save(*fnamep, *p != NUL);
23545 vim_free(*bufp); /* free any allocated file name */
23546 *bufp = *fnamep;
23547 if (*fnamep == NULL)
23548 return -1;
23549 }
23550
23551 /* Append a path separator to a directory. */
23552 if (mch_isdir(*fnamep))
23553 {
23554 /* Make room for one or two extra characters. */
23555 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23556 vim_free(*bufp); /* free any allocated file name */
23557 *bufp = *fnamep;
23558 if (*fnamep == NULL)
23559 return -1;
23560 add_pathsep(*fnamep);
23561 }
23562 }
23563
23564 /* ":." - path relative to the current directory */
23565 /* ":~" - path relative to the home directory */
23566 /* ":8" - shortname path - postponed till after */
23567 while (src[*usedlen] == ':'
23568 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23569 {
23570 *usedlen += 2;
23571 if (c == '8')
23572 {
23573#ifdef WIN3264
23574 has_shortname = 1; /* Postpone this. */
23575#endif
23576 continue;
23577 }
23578 pbuf = NULL;
23579 /* Need full path first (use expand_env() to remove a "~/") */
23580 if (!has_fullname)
23581 {
23582 if (c == '.' && **fnamep == '~')
23583 p = pbuf = expand_env_save(*fnamep);
23584 else
23585 p = pbuf = FullName_save(*fnamep, FALSE);
23586 }
23587 else
23588 p = *fnamep;
23589
23590 has_fullname = 0;
23591
23592 if (p != NULL)
23593 {
23594 if (c == '.')
23595 {
23596 mch_dirname(dirname, MAXPATHL);
23597 s = shorten_fname(p, dirname);
23598 if (s != NULL)
23599 {
23600 *fnamep = s;
23601 if (pbuf != NULL)
23602 {
23603 vim_free(*bufp); /* free any allocated file name */
23604 *bufp = pbuf;
23605 pbuf = NULL;
23606 }
23607 }
23608 }
23609 else
23610 {
23611 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23612 /* Only replace it when it starts with '~' */
23613 if (*dirname == '~')
23614 {
23615 s = vim_strsave(dirname);
23616 if (s != NULL)
23617 {
23618 *fnamep = s;
23619 vim_free(*bufp);
23620 *bufp = s;
23621 }
23622 }
23623 }
23624 vim_free(pbuf);
23625 }
23626 }
23627
23628 tail = gettail(*fnamep);
23629 *fnamelen = (int)STRLEN(*fnamep);
23630
23631 /* ":h" - head, remove "/file_name", can be repeated */
23632 /* Don't remove the first "/" or "c:\" */
23633 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23634 {
23635 valid |= VALID_HEAD;
23636 *usedlen += 2;
23637 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023638 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023639 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 *fnamelen = (int)(tail - *fnamep);
23641#ifdef VMS
23642 if (*fnamelen > 0)
23643 *fnamelen += 1; /* the path separator is part of the path */
23644#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023645 if (*fnamelen == 0)
23646 {
23647 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23648 p = vim_strsave((char_u *)".");
23649 if (p == NULL)
23650 return -1;
23651 vim_free(*bufp);
23652 *bufp = *fnamep = tail = p;
23653 *fnamelen = 1;
23654 }
23655 else
23656 {
23657 while (tail > s && !after_pathsep(s, tail))
23658 mb_ptr_back(*fnamep, tail);
23659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 }
23661
23662 /* ":8" - shortname */
23663 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23664 {
23665 *usedlen += 2;
23666#ifdef WIN3264
23667 has_shortname = 1;
23668#endif
23669 }
23670
23671#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023672 /*
23673 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 */
23675 if (has_shortname)
23676 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023677 /* Copy the string if it is shortened by :h and when it wasn't copied
23678 * yet, because we are going to change it in place. Avoids changing
23679 * the buffer name for "%:8". */
23680 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 {
23682 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023683 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 return -1;
23685 vim_free(*bufp);
23686 *bufp = *fnamep = p;
23687 }
23688
23689 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023690 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 if (!has_fullname && !vim_isAbsName(*fnamep))
23692 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023693 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023694 return -1;
23695 }
23696 else
23697 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023698 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699
Bram Moolenaardc935552011-08-17 15:23:23 +020023700 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023702 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 return -1;
23704
23705 if (l == 0)
23706 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023707 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023709 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 return -1;
23711 }
23712 *fnamelen = l;
23713 }
23714 }
23715#endif /* WIN3264 */
23716
23717 /* ":t" - tail, just the basename */
23718 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23719 {
23720 *usedlen += 2;
23721 *fnamelen -= (int)(tail - *fnamep);
23722 *fnamep = tail;
23723 }
23724
23725 /* ":e" - extension, can be repeated */
23726 /* ":r" - root, without extension, can be repeated */
23727 while (src[*usedlen] == ':'
23728 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23729 {
23730 /* find a '.' in the tail:
23731 * - for second :e: before the current fname
23732 * - otherwise: The last '.'
23733 */
23734 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23735 s = *fnamep - 2;
23736 else
23737 s = *fnamep + *fnamelen - 1;
23738 for ( ; s > tail; --s)
23739 if (s[0] == '.')
23740 break;
23741 if (src[*usedlen + 1] == 'e') /* :e */
23742 {
23743 if (s > tail)
23744 {
23745 *fnamelen += (int)(*fnamep - (s + 1));
23746 *fnamep = s + 1;
23747#ifdef VMS
23748 /* cut version from the extension */
23749 s = *fnamep + *fnamelen - 1;
23750 for ( ; s > *fnamep; --s)
23751 if (s[0] == ';')
23752 break;
23753 if (s > *fnamep)
23754 *fnamelen = s - *fnamep;
23755#endif
23756 }
23757 else if (*fnamep <= tail)
23758 *fnamelen = 0;
23759 }
23760 else /* :r */
23761 {
23762 if (s > tail) /* remove one extension */
23763 *fnamelen = (int)(s - *fnamep);
23764 }
23765 *usedlen += 2;
23766 }
23767
23768 /* ":s?pat?foo?" - substitute */
23769 /* ":gs?pat?foo?" - global substitute */
23770 if (src[*usedlen] == ':'
23771 && (src[*usedlen + 1] == 's'
23772 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23773 {
23774 char_u *str;
23775 char_u *pat;
23776 char_u *sub;
23777 int sep;
23778 char_u *flags;
23779 int didit = FALSE;
23780
23781 flags = (char_u *)"";
23782 s = src + *usedlen + 2;
23783 if (src[*usedlen + 1] == 'g')
23784 {
23785 flags = (char_u *)"g";
23786 ++s;
23787 }
23788
23789 sep = *s++;
23790 if (sep)
23791 {
23792 /* find end of pattern */
23793 p = vim_strchr(s, sep);
23794 if (p != NULL)
23795 {
23796 pat = vim_strnsave(s, (int)(p - s));
23797 if (pat != NULL)
23798 {
23799 s = p + 1;
23800 /* find end of substitution */
23801 p = vim_strchr(s, sep);
23802 if (p != NULL)
23803 {
23804 sub = vim_strnsave(s, (int)(p - s));
23805 str = vim_strnsave(*fnamep, *fnamelen);
23806 if (sub != NULL && str != NULL)
23807 {
23808 *usedlen = (int)(p + 1 - src);
23809 s = do_string_sub(str, pat, sub, flags);
23810 if (s != NULL)
23811 {
23812 *fnamep = s;
23813 *fnamelen = (int)STRLEN(s);
23814 vim_free(*bufp);
23815 *bufp = s;
23816 didit = TRUE;
23817 }
23818 }
23819 vim_free(sub);
23820 vim_free(str);
23821 }
23822 vim_free(pat);
23823 }
23824 }
23825 /* after using ":s", repeat all the modifiers */
23826 if (didit)
23827 goto repeat;
23828 }
23829 }
23830
23831 return valid;
23832}
23833
23834/*
23835 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23836 * "flags" can be "g" to do a global substitute.
23837 * Returns an allocated string, NULL for error.
23838 */
23839 char_u *
23840do_string_sub(str, pat, sub, flags)
23841 char_u *str;
23842 char_u *pat;
23843 char_u *sub;
23844 char_u *flags;
23845{
23846 int sublen;
23847 regmatch_T regmatch;
23848 int i;
23849 int do_all;
23850 char_u *tail;
23851 garray_T ga;
23852 char_u *ret;
23853 char_u *save_cpo;
23854
23855 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23856 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023857 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858
23859 ga_init2(&ga, 1, 200);
23860
23861 do_all = (flags[0] == 'g');
23862
23863 regmatch.rm_ic = p_ic;
23864 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23865 if (regmatch.regprog != NULL)
23866 {
23867 tail = str;
23868 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23869 {
23870 /*
23871 * Get some space for a temporary buffer to do the substitution
23872 * into. It will contain:
23873 * - The text up to where the match is.
23874 * - The substituted text.
23875 * - The text after the match.
23876 */
23877 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23878 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23879 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23880 {
23881 ga_clear(&ga);
23882 break;
23883 }
23884
23885 /* copy the text up to where the match is */
23886 i = (int)(regmatch.startp[0] - tail);
23887 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23888 /* add the substituted text */
23889 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23890 + ga.ga_len + i, TRUE, TRUE, FALSE);
23891 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 /* avoid getting stuck on a match with an empty string */
23893 if (tail == regmatch.endp[0])
23894 {
23895 if (*tail == NUL)
23896 break;
23897 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23898 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 }
23900 else
23901 {
23902 tail = regmatch.endp[0];
23903 if (*tail == NUL)
23904 break;
23905 }
23906 if (!do_all)
23907 break;
23908 }
23909
23910 if (ga.ga_data != NULL)
23911 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23912
23913 vim_free(regmatch.regprog);
23914 }
23915
23916 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23917 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023918 if (p_cpo == empty_option)
23919 p_cpo = save_cpo;
23920 else
23921 /* Darn, evaluating {sub} expression changed the value. */
23922 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923
23924 return ret;
23925}
23926
23927#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */