blob: 2dd36b012a783da8d820935af1ae840715826dc5 [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 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010194 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010195 {
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 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014646 newprev = prev == NULL ? alloc(prevsize)
14647 : vim_realloc(prev, prevsize);
14648 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014649 {
14650 do_outofmem_msg((long_u)prevsize);
14651 failed = TRUE;
14652 break;
14653 }
14654 prev = newprev;
14655 }
14656 /* Add the line part to end of "prev". */
14657 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014658 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014660 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014661
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014662 /*
14663 * For a negative line count use only the lines at the end of the file,
14664 * free the rest.
14665 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014666 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014667 while (cnt > -maxline)
14668 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014669 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014670 --cnt;
14671 }
14672
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014673 if (failed)
14674 {
14675 list_free(rettv->vval.v_list, TRUE);
14676 /* readfile doc says an empty list is returned on error */
14677 rettv->vval.v_list = list_alloc();
14678 }
14679
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014680 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014681 fclose(fd);
14682}
14683
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014684#if defined(FEAT_RELTIME)
14685static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14686
14687/*
14688 * Convert a List to proftime_T.
14689 * Return FAIL when there is something wrong.
14690 */
14691 static int
14692list2proftime(arg, tm)
14693 typval_T *arg;
14694 proftime_T *tm;
14695{
14696 long n1, n2;
14697 int error = FALSE;
14698
14699 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14700 || arg->vval.v_list->lv_len != 2)
14701 return FAIL;
14702 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14703 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14704# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014705 tm->HighPart = n1;
14706 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014707# else
14708 tm->tv_sec = n1;
14709 tm->tv_usec = n2;
14710# endif
14711 return error ? FAIL : OK;
14712}
14713#endif /* FEAT_RELTIME */
14714
14715/*
14716 * "reltime()" function
14717 */
14718 static void
14719f_reltime(argvars, rettv)
14720 typval_T *argvars;
14721 typval_T *rettv;
14722{
14723#ifdef FEAT_RELTIME
14724 proftime_T res;
14725 proftime_T start;
14726
14727 if (argvars[0].v_type == VAR_UNKNOWN)
14728 {
14729 /* No arguments: get current time. */
14730 profile_start(&res);
14731 }
14732 else if (argvars[1].v_type == VAR_UNKNOWN)
14733 {
14734 if (list2proftime(&argvars[0], &res) == FAIL)
14735 return;
14736 profile_end(&res);
14737 }
14738 else
14739 {
14740 /* Two arguments: compute the difference. */
14741 if (list2proftime(&argvars[0], &start) == FAIL
14742 || list2proftime(&argvars[1], &res) == FAIL)
14743 return;
14744 profile_sub(&res, &start);
14745 }
14746
14747 if (rettv_list_alloc(rettv) == OK)
14748 {
14749 long n1, n2;
14750
14751# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014752 n1 = res.HighPart;
14753 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014754# else
14755 n1 = res.tv_sec;
14756 n2 = res.tv_usec;
14757# endif
14758 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14759 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14760 }
14761#endif
14762}
14763
14764/*
14765 * "reltimestr()" function
14766 */
14767 static void
14768f_reltimestr(argvars, rettv)
14769 typval_T *argvars;
14770 typval_T *rettv;
14771{
14772#ifdef FEAT_RELTIME
14773 proftime_T tm;
14774#endif
14775
14776 rettv->v_type = VAR_STRING;
14777 rettv->vval.v_string = NULL;
14778#ifdef FEAT_RELTIME
14779 if (list2proftime(&argvars[0], &tm) == OK)
14780 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14781#endif
14782}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014783
Bram Moolenaar0d660222005-01-07 21:51:51 +000014784#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14785static void make_connection __ARGS((void));
14786static int check_connection __ARGS((void));
14787
14788 static void
14789make_connection()
14790{
14791 if (X_DISPLAY == NULL
14792# ifdef FEAT_GUI
14793 && !gui.in_use
14794# endif
14795 )
14796 {
14797 x_force_connect = TRUE;
14798 setup_term_clip();
14799 x_force_connect = FALSE;
14800 }
14801}
14802
14803 static int
14804check_connection()
14805{
14806 make_connection();
14807 if (X_DISPLAY == NULL)
14808 {
14809 EMSG(_("E240: No connection to Vim server"));
14810 return FAIL;
14811 }
14812 return OK;
14813}
14814#endif
14815
14816#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014817static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014818
14819 static void
14820remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014821 typval_T *argvars;
14822 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014823 int expr;
14824{
14825 char_u *server_name;
14826 char_u *keys;
14827 char_u *r = NULL;
14828 char_u buf[NUMBUFLEN];
14829# ifdef WIN32
14830 HWND w;
14831# else
14832 Window w;
14833# endif
14834
14835 if (check_restricted() || check_secure())
14836 return;
14837
14838# ifdef FEAT_X11
14839 if (check_connection() == FAIL)
14840 return;
14841# endif
14842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843 server_name = get_tv_string_chk(&argvars[0]);
14844 if (server_name == NULL)
14845 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014846 keys = get_tv_string_buf(&argvars[1], buf);
14847# ifdef WIN32
14848 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14849# else
14850 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14851 < 0)
14852# endif
14853 {
14854 if (r != NULL)
14855 EMSG(r); /* sending worked but evaluation failed */
14856 else
14857 EMSG2(_("E241: Unable to send to %s"), server_name);
14858 return;
14859 }
14860
14861 rettv->vval.v_string = r;
14862
14863 if (argvars[2].v_type != VAR_UNKNOWN)
14864 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014865 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014866 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014867 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014868
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014869 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 v.di_tv.v_type = VAR_STRING;
14871 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014872 idvar = get_tv_string_chk(&argvars[2]);
14873 if (idvar != NULL)
14874 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014876 }
14877}
14878#endif
14879
14880/*
14881 * "remote_expr()" function
14882 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014883 static void
14884f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014887{
14888 rettv->v_type = VAR_STRING;
14889 rettv->vval.v_string = NULL;
14890#ifdef FEAT_CLIENTSERVER
14891 remote_common(argvars, rettv, TRUE);
14892#endif
14893}
14894
14895/*
14896 * "remote_foreground()" function
14897 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014898 static void
14899f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014900 typval_T *argvars UNUSED;
14901 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014902{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903#ifdef FEAT_CLIENTSERVER
14904# ifdef WIN32
14905 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 {
14907 char_u *server_name = get_tv_string_chk(&argvars[0]);
14908
14909 if (server_name != NULL)
14910 serverForeground(server_name);
14911 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912# else
14913 /* Send a foreground() expression to the server. */
14914 argvars[1].v_type = VAR_STRING;
14915 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14916 argvars[2].v_type = VAR_UNKNOWN;
14917 remote_common(argvars, rettv, TRUE);
14918 vim_free(argvars[1].vval.v_string);
14919# endif
14920#endif
14921}
14922
Bram Moolenaar0d660222005-01-07 21:51:51 +000014923 static void
14924f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014925 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014926 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927{
14928#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014929 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014930 char_u *s = NULL;
14931# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014932 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014934 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935
14936 if (check_restricted() || check_secure())
14937 {
14938 rettv->vval.v_number = -1;
14939 return;
14940 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014941 serverid = get_tv_string_chk(&argvars[0]);
14942 if (serverid == NULL)
14943 {
14944 rettv->vval.v_number = -1;
14945 return; /* type error; errmsg already given */
14946 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014948 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 if (n == 0)
14950 rettv->vval.v_number = -1;
14951 else
14952 {
14953 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14954 rettv->vval.v_number = (s != NULL);
14955 }
14956# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014957 if (check_connection() == FAIL)
14958 return;
14959
14960 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014961 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962# endif
14963
14964 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966 char_u *retvar;
14967
Bram Moolenaar33570922005-01-25 22:26:29 +000014968 v.di_tv.v_type = VAR_STRING;
14969 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 retvar = get_tv_string_chk(&argvars[1]);
14971 if (retvar != NULL)
14972 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014973 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974 }
14975#else
14976 rettv->vval.v_number = -1;
14977#endif
14978}
14979
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980 static void
14981f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014982 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984{
14985 char_u *r = NULL;
14986
14987#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014988 char_u *serverid = get_tv_string_chk(&argvars[0]);
14989
14990 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014991 {
14992# ifdef WIN32
14993 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014994 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014996 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 if (n != 0)
14998 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14999 if (r == NULL)
15000# else
15001 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015002 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003# endif
15004 EMSG(_("E277: Unable to read a server reply"));
15005 }
15006#endif
15007 rettv->v_type = VAR_STRING;
15008 rettv->vval.v_string = r;
15009}
15010
15011/*
15012 * "remote_send()" function
15013 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014 static void
15015f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015017 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018{
15019 rettv->v_type = VAR_STRING;
15020 rettv->vval.v_string = NULL;
15021#ifdef FEAT_CLIENTSERVER
15022 remote_common(argvars, rettv, FALSE);
15023#endif
15024}
15025
15026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015027 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015028 */
15029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015031 typval_T *argvars;
15032 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015033{
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 list_T *l;
15035 listitem_T *item, *item2;
15036 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015037 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015038 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015039 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 dict_T *d;
15041 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015042 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015043
Bram Moolenaar8c711452005-01-14 21:53:12 +000015044 if (argvars[0].v_type == VAR_DICT)
15045 {
15046 if (argvars[2].v_type != VAR_UNKNOWN)
15047 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015048 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015049 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015051 key = get_tv_string_chk(&argvars[1]);
15052 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054 di = dict_find(d, key, -1);
15055 if (di == NULL)
15056 EMSG2(_(e_dictkey), key);
15057 else
15058 {
15059 *rettv = di->di_tv;
15060 init_tv(&di->di_tv);
15061 dictitem_remove(d, di);
15062 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015064 }
15065 }
15066 else if (argvars[0].v_type != VAR_LIST)
15067 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015068 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015069 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015071 int error = FALSE;
15072
15073 idx = get_tv_number_chk(&argvars[1], &error);
15074 if (error)
15075 ; /* type error: do nothing, errmsg already given */
15076 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015077 EMSGN(_(e_listidx), idx);
15078 else
15079 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015080 if (argvars[2].v_type == VAR_UNKNOWN)
15081 {
15082 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015083 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015084 *rettv = item->li_tv;
15085 vim_free(item);
15086 }
15087 else
15088 {
15089 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015090 end = get_tv_number_chk(&argvars[2], &error);
15091 if (error)
15092 ; /* type error: do nothing */
15093 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015094 EMSGN(_(e_listidx), end);
15095 else
15096 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015097 int cnt = 0;
15098
15099 for (li = item; li != NULL; li = li->li_next)
15100 {
15101 ++cnt;
15102 if (li == item2)
15103 break;
15104 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015105 if (li == NULL) /* didn't find "item2" after "item" */
15106 EMSG(_(e_invrange));
15107 else
15108 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015109 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015110 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015111 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015112 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015113 l->lv_first = item;
15114 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015115 item->li_prev = NULL;
15116 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015117 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015118 }
15119 }
15120 }
15121 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015122 }
15123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124}
15125
15126/*
15127 * "rename({from}, {to})" function
15128 */
15129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015131 typval_T *argvars;
15132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133{
15134 char_u buf[NUMBUFLEN];
15135
15136 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15140 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141}
15142
15143/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015144 * "repeat()" function
15145 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015146 static void
15147f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 typval_T *argvars;
15149 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015150{
15151 char_u *p;
15152 int n;
15153 int slen;
15154 int len;
15155 char_u *r;
15156 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015157
15158 n = get_tv_number(&argvars[1]);
15159 if (argvars[0].v_type == VAR_LIST)
15160 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015161 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015162 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015163 if (list_extend(rettv->vval.v_list,
15164 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015165 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015166 }
15167 else
15168 {
15169 p = get_tv_string(&argvars[0]);
15170 rettv->v_type = VAR_STRING;
15171 rettv->vval.v_string = NULL;
15172
15173 slen = (int)STRLEN(p);
15174 len = slen * n;
15175 if (len <= 0)
15176 return;
15177
15178 r = alloc(len + 1);
15179 if (r != NULL)
15180 {
15181 for (i = 0; i < n; i++)
15182 mch_memmove(r + i * slen, p, (size_t)slen);
15183 r[len] = NUL;
15184 }
15185
15186 rettv->vval.v_string = r;
15187 }
15188}
15189
15190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 * "resolve()" function
15192 */
15193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 typval_T *argvars;
15196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197{
15198 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015199#ifdef HAVE_READLINK
15200 char_u *buf = NULL;
15201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015203 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204#ifdef FEAT_SHORTCUT
15205 {
15206 char_u *v = NULL;
15207
15208 v = mch_resolve_shortcut(p);
15209 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213 }
15214#else
15215# ifdef HAVE_READLINK
15216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 char_u *cpy;
15218 int len;
15219 char_u *remain = NULL;
15220 char_u *q;
15221 int is_relative_to_current = FALSE;
15222 int has_trailing_pathsep = FALSE;
15223 int limit = 100;
15224
15225 p = vim_strsave(p);
15226
15227 if (p[0] == '.' && (vim_ispathsep(p[1])
15228 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15229 is_relative_to_current = TRUE;
15230
15231 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015232 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015235 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237
15238 q = getnextcomp(p);
15239 if (*q != NUL)
15240 {
15241 /* Separate the first path component in "p", and keep the
15242 * remainder (beginning with the path separator). */
15243 remain = vim_strsave(q - 1);
15244 q[-1] = NUL;
15245 }
15246
Bram Moolenaard9462e32011-04-11 21:35:11 +020015247 buf = alloc(MAXPATHL + 1);
15248 if (buf == NULL)
15249 goto fail;
15250
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 for (;;)
15252 {
15253 for (;;)
15254 {
15255 len = readlink((char *)p, (char *)buf, MAXPATHL);
15256 if (len <= 0)
15257 break;
15258 buf[len] = NUL;
15259
15260 if (limit-- == 0)
15261 {
15262 vim_free(p);
15263 vim_free(remain);
15264 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 goto fail;
15267 }
15268
15269 /* Ensure that the result will have a trailing path separator
15270 * if the argument has one. */
15271 if (remain == NULL && has_trailing_pathsep)
15272 add_pathsep(buf);
15273
15274 /* Separate the first path component in the link value and
15275 * concatenate the remainders. */
15276 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15277 if (*q != NUL)
15278 {
15279 if (remain == NULL)
15280 remain = vim_strsave(q - 1);
15281 else
15282 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015283 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 if (cpy != NULL)
15285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 vim_free(remain);
15287 remain = cpy;
15288 }
15289 }
15290 q[-1] = NUL;
15291 }
15292
15293 q = gettail(p);
15294 if (q > p && *q == NUL)
15295 {
15296 /* Ignore trailing path separator. */
15297 q[-1] = NUL;
15298 q = gettail(p);
15299 }
15300 if (q > p && !mch_isFullName(buf))
15301 {
15302 /* symlink is relative to directory of argument */
15303 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15304 if (cpy != NULL)
15305 {
15306 STRCPY(cpy, p);
15307 STRCPY(gettail(cpy), buf);
15308 vim_free(p);
15309 p = cpy;
15310 }
15311 }
15312 else
15313 {
15314 vim_free(p);
15315 p = vim_strsave(buf);
15316 }
15317 }
15318
15319 if (remain == NULL)
15320 break;
15321
15322 /* Append the first path component of "remain" to "p". */
15323 q = getnextcomp(remain + 1);
15324 len = q - remain - (*q != NUL);
15325 cpy = vim_strnsave(p, STRLEN(p) + len);
15326 if (cpy != NULL)
15327 {
15328 STRNCAT(cpy, remain, len);
15329 vim_free(p);
15330 p = cpy;
15331 }
15332 /* Shorten "remain". */
15333 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015334 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 else
15336 {
15337 vim_free(remain);
15338 remain = NULL;
15339 }
15340 }
15341
15342 /* If the result is a relative path name, make it explicitly relative to
15343 * the current directory if and only if the argument had this form. */
15344 if (!vim_ispathsep(*p))
15345 {
15346 if (is_relative_to_current
15347 && *p != NUL
15348 && !(p[0] == '.'
15349 && (p[1] == NUL
15350 || vim_ispathsep(p[1])
15351 || (p[1] == '.'
15352 && (p[2] == NUL
15353 || vim_ispathsep(p[2]))))))
15354 {
15355 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015356 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 if (cpy != NULL)
15358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 vim_free(p);
15360 p = cpy;
15361 }
15362 }
15363 else if (!is_relative_to_current)
15364 {
15365 /* Strip leading "./". */
15366 q = p;
15367 while (q[0] == '.' && vim_ispathsep(q[1]))
15368 q += 2;
15369 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015370 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 }
15372 }
15373
15374 /* Ensure that the result will have no trailing path separator
15375 * if the argument had none. But keep "/" or "//". */
15376 if (!has_trailing_pathsep)
15377 {
15378 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015379 if (after_pathsep(p, q))
15380 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 }
15382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 }
15385# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387# endif
15388#endif
15389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391
15392#ifdef HAVE_READLINK
15393fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015394 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397}
15398
15399/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 */
15402 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015403f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015404 typval_T *argvars;
15405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406{
Bram Moolenaar33570922005-01-25 22:26:29 +000015407 list_T *l;
15408 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410 if (argvars[0].v_type != VAR_LIST)
15411 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015412 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015413 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015414 {
15415 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015416 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015417 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015418 while (li != NULL)
15419 {
15420 ni = li->li_prev;
15421 list_append(l, li);
15422 li = ni;
15423 }
15424 rettv->vval.v_list = l;
15425 rettv->v_type = VAR_LIST;
15426 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015427 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429}
15430
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015431#define SP_NOMOVE 0x01 /* don't move cursor */
15432#define SP_REPEAT 0x02 /* repeat to find outer pair */
15433#define SP_RETCOUNT 0x04 /* return matchcount */
15434#define SP_SETPCMARK 0x08 /* set previous context mark */
15435#define SP_START 0x10 /* accept match at start position */
15436#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15437#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015438
Bram Moolenaar33570922005-01-25 22:26:29 +000015439static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015440
15441/*
15442 * Get flags for a search function.
15443 * Possibly sets "p_ws".
15444 * Returns BACKWARD, FORWARD or zero (for an error).
15445 */
15446 static int
15447get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015449 int *flagsp;
15450{
15451 int dir = FORWARD;
15452 char_u *flags;
15453 char_u nbuf[NUMBUFLEN];
15454 int mask;
15455
15456 if (varp->v_type != VAR_UNKNOWN)
15457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015458 flags = get_tv_string_buf_chk(varp, nbuf);
15459 if (flags == NULL)
15460 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015461 while (*flags != NUL)
15462 {
15463 switch (*flags)
15464 {
15465 case 'b': dir = BACKWARD; break;
15466 case 'w': p_ws = TRUE; break;
15467 case 'W': p_ws = FALSE; break;
15468 default: mask = 0;
15469 if (flagsp != NULL)
15470 switch (*flags)
15471 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015472 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015473 case 'e': mask = SP_END; break;
15474 case 'm': mask = SP_RETCOUNT; break;
15475 case 'n': mask = SP_NOMOVE; break;
15476 case 'p': mask = SP_SUBPAT; break;
15477 case 'r': mask = SP_REPEAT; break;
15478 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015479 }
15480 if (mask == 0)
15481 {
15482 EMSG2(_(e_invarg2), flags);
15483 dir = 0;
15484 }
15485 else
15486 *flagsp |= mask;
15487 }
15488 if (dir == 0)
15489 break;
15490 ++flags;
15491 }
15492 }
15493 return dir;
15494}
15495
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015497 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015499 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015500search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015501 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015502 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015503 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015505 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 char_u *pat;
15507 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015508 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 int save_p_ws = p_ws;
15510 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015511 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015512 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015513 proftime_T tm;
15514#ifdef FEAT_RELTIME
15515 long time_limit = 0;
15516#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015517 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015518 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015521 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015522 if (dir == 0)
15523 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015524 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015525 if (flags & SP_START)
15526 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015527 if (flags & SP_END)
15528 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015529
Bram Moolenaar76929292008-01-06 19:07:36 +000015530 /* Optional arguments: line number to stop searching and timeout. */
15531 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015532 {
15533 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15534 if (lnum_stop < 0)
15535 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015536#ifdef FEAT_RELTIME
15537 if (argvars[3].v_type != VAR_UNKNOWN)
15538 {
15539 time_limit = get_tv_number_chk(&argvars[3], NULL);
15540 if (time_limit < 0)
15541 goto theend;
15542 }
15543#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015544 }
15545
Bram Moolenaar76929292008-01-06 19:07:36 +000015546#ifdef FEAT_RELTIME
15547 /* Set the time limit, if there is one. */
15548 profile_setlimit(time_limit, &tm);
15549#endif
15550
Bram Moolenaar231334e2005-07-25 20:46:57 +000015551 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015552 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015553 * Check to make sure only those flags are set.
15554 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15555 * flags cannot be set. Check for that condition also.
15556 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015557 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015558 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015561 goto theend;
15562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015564 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015565 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015566 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015567 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015569 if (flags & SP_SUBPAT)
15570 retval = subpatnum;
15571 else
15572 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015573 if (flags & SP_SETPCMARK)
15574 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015576 if (match_pos != NULL)
15577 {
15578 /* Store the match cursor position */
15579 match_pos->lnum = pos.lnum;
15580 match_pos->col = pos.col + 1;
15581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 /* "/$" will put the cursor after the end of the line, may need to
15583 * correct that here */
15584 check_cursor();
15585 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015586
15587 /* If 'n' flag is used: restore cursor position. */
15588 if (flags & SP_NOMOVE)
15589 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015590 else
15591 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015592theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015594
15595 return retval;
15596}
15597
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015598#ifdef FEAT_FLOAT
15599/*
15600 * "round({float})" function
15601 */
15602 static void
15603f_round(argvars, rettv)
15604 typval_T *argvars;
15605 typval_T *rettv;
15606{
15607 float_T f;
15608
15609 rettv->v_type = VAR_FLOAT;
15610 if (get_float_arg(argvars, &f) == OK)
15611 /* round() is not in C90, use ceil() or floor() instead. */
15612 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15613 else
15614 rettv->vval.v_float = 0.0;
15615}
15616#endif
15617
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015618/*
15619 * "search()" function
15620 */
15621 static void
15622f_search(argvars, rettv)
15623 typval_T *argvars;
15624 typval_T *rettv;
15625{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015626 int flags = 0;
15627
15628 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629}
15630
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015632 * "searchdecl()" function
15633 */
15634 static void
15635f_searchdecl(argvars, rettv)
15636 typval_T *argvars;
15637 typval_T *rettv;
15638{
15639 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015640 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015641 int error = FALSE;
15642 char_u *name;
15643
15644 rettv->vval.v_number = 1; /* default: FAIL */
15645
15646 name = get_tv_string_chk(&argvars[0]);
15647 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015648 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015649 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015650 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15651 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15652 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015653 if (!error && name != NULL)
15654 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015655 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015656}
15657
15658/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015659 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015661 static int
15662searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015663 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015664 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665{
15666 char_u *spat, *mpat, *epat;
15667 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 int dir;
15670 int flags = 0;
15671 char_u nbuf1[NUMBUFLEN];
15672 char_u nbuf2[NUMBUFLEN];
15673 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015674 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015675 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015676 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015679 spat = get_tv_string_chk(&argvars[0]);
15680 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15681 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15682 if (spat == NULL || mpat == NULL || epat == NULL)
15683 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 /* Handle the optional fourth argument: flags */
15686 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015687 if (dir == 0)
15688 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015689
15690 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015691 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15692 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015693 if ((flags & (SP_END | SP_SUBPAT)) != 0
15694 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015695 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015696 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015697 goto theend;
15698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015700 /* Using 'r' implies 'W', otherwise it doesn't work. */
15701 if (flags & SP_REPEAT)
15702 p_ws = FALSE;
15703
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015704 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015705 if (argvars[3].v_type == VAR_UNKNOWN
15706 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 skip = (char_u *)"";
15708 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015711 if (argvars[5].v_type != VAR_UNKNOWN)
15712 {
15713 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15714 if (lnum_stop < 0)
15715 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015716#ifdef FEAT_RELTIME
15717 if (argvars[6].v_type != VAR_UNKNOWN)
15718 {
15719 time_limit = get_tv_number_chk(&argvars[6], NULL);
15720 if (time_limit < 0)
15721 goto theend;
15722 }
15723#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015724 }
15725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 if (skip == NULL)
15727 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015729 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015730 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015731
15732theend:
15733 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015734
15735 return retval;
15736}
15737
15738/*
15739 * "searchpair()" function
15740 */
15741 static void
15742f_searchpair(argvars, rettv)
15743 typval_T *argvars;
15744 typval_T *rettv;
15745{
15746 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15747}
15748
15749/*
15750 * "searchpairpos()" function
15751 */
15752 static void
15753f_searchpairpos(argvars, rettv)
15754 typval_T *argvars;
15755 typval_T *rettv;
15756{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015757 pos_T match_pos;
15758 int lnum = 0;
15759 int col = 0;
15760
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015761 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015762 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015763
15764 if (searchpair_cmn(argvars, &match_pos) > 0)
15765 {
15766 lnum = match_pos.lnum;
15767 col = match_pos.col;
15768 }
15769
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015770 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15771 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015772}
15773
15774/*
15775 * Search for a start/middle/end thing.
15776 * Used by searchpair(), see its documentation for the details.
15777 * Returns 0 or -1 for no match,
15778 */
15779 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015780do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15781 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015782 char_u *spat; /* start pattern */
15783 char_u *mpat; /* middle pattern */
15784 char_u *epat; /* end pattern */
15785 int dir; /* BACKWARD or FORWARD */
15786 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015787 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015788 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015789 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015790 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015791{
15792 char_u *save_cpo;
15793 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15794 long retval = 0;
15795 pos_T pos;
15796 pos_T firstpos;
15797 pos_T foundpos;
15798 pos_T save_cursor;
15799 pos_T save_pos;
15800 int n;
15801 int r;
15802 int nest = 1;
15803 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015804 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015805 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015806
15807 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15808 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015809 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015810
Bram Moolenaar76929292008-01-06 19:07:36 +000015811#ifdef FEAT_RELTIME
15812 /* Set the time limit, if there is one. */
15813 profile_setlimit(time_limit, &tm);
15814#endif
15815
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015816 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15817 * start/middle/end (pat3, for the top pair). */
15818 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15819 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15820 if (pat2 == NULL || pat3 == NULL)
15821 goto theend;
15822 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15823 if (*mpat == NUL)
15824 STRCPY(pat3, pat2);
15825 else
15826 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15827 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015828 if (flags & SP_START)
15829 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015830
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 save_cursor = curwin->w_cursor;
15832 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015833 clearpos(&firstpos);
15834 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 pat = pat3;
15836 for (;;)
15837 {
15838 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015839 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15841 /* didn't find it or found the first match again: FAIL */
15842 break;
15843
15844 if (firstpos.lnum == 0)
15845 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015846 if (equalpos(pos, foundpos))
15847 {
15848 /* Found the same position again. Can happen with a pattern that
15849 * has "\zs" at the end and searching backwards. Advance one
15850 * character and try again. */
15851 if (dir == BACKWARD)
15852 decl(&pos);
15853 else
15854 incl(&pos);
15855 }
15856 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015858 /* clear the start flag to avoid getting stuck here */
15859 options &= ~SEARCH_START;
15860
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 /* If the skip pattern matches, ignore this match. */
15862 if (*skip != NUL)
15863 {
15864 save_pos = curwin->w_cursor;
15865 curwin->w_cursor = pos;
15866 r = eval_to_bool(skip, &err, NULL, FALSE);
15867 curwin->w_cursor = save_pos;
15868 if (err)
15869 {
15870 /* Evaluating {skip} caused an error, break here. */
15871 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015872 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 break;
15874 }
15875 if (r)
15876 continue;
15877 }
15878
15879 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15880 {
15881 /* Found end when searching backwards or start when searching
15882 * forward: nested pair. */
15883 ++nest;
15884 pat = pat2; /* nested, don't search for middle */
15885 }
15886 else
15887 {
15888 /* Found end when searching forward or start when searching
15889 * backward: end of (nested) pair; or found middle in outer pair. */
15890 if (--nest == 1)
15891 pat = pat3; /* outer level, search for middle */
15892 }
15893
15894 if (nest == 0)
15895 {
15896 /* Found the match: return matchcount or line number. */
15897 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015898 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015900 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015901 if (flags & SP_SETPCMARK)
15902 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 curwin->w_cursor = pos;
15904 if (!(flags & SP_REPEAT))
15905 break;
15906 nest = 1; /* search for next unmatched */
15907 }
15908 }
15909
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015910 if (match_pos != NULL)
15911 {
15912 /* Store the match cursor position */
15913 match_pos->lnum = curwin->w_cursor.lnum;
15914 match_pos->col = curwin->w_cursor.col + 1;
15915 }
15916
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015918 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 curwin->w_cursor = save_cursor;
15920
15921theend:
15922 vim_free(pat2);
15923 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015924 if (p_cpo == empty_option)
15925 p_cpo = save_cpo;
15926 else
15927 /* Darn, evaluating the {skip} expression changed the value. */
15928 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015929
15930 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931}
15932
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015933/*
15934 * "searchpos()" function
15935 */
15936 static void
15937f_searchpos(argvars, rettv)
15938 typval_T *argvars;
15939 typval_T *rettv;
15940{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015941 pos_T match_pos;
15942 int lnum = 0;
15943 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015944 int n;
15945 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015946
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015947 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015948 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015949
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015950 n = search_cmn(argvars, &match_pos, &flags);
15951 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015952 {
15953 lnum = match_pos.lnum;
15954 col = match_pos.col;
15955 }
15956
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015957 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15958 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015959 if (flags & SP_SUBPAT)
15960 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015961}
15962
15963
Bram Moolenaar0d660222005-01-07 21:51:51 +000015964 static void
15965f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015969#ifdef FEAT_CLIENTSERVER
15970 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015971 char_u *server = get_tv_string_chk(&argvars[0]);
15972 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973
Bram Moolenaar0d660222005-01-07 21:51:51 +000015974 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 if (server == NULL || reply == NULL)
15976 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015977 if (check_restricted() || check_secure())
15978 return;
15979# ifdef FEAT_X11
15980 if (check_connection() == FAIL)
15981 return;
15982# endif
15983
15984 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015986 EMSG(_("E258: Unable to send to client"));
15987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015989 rettv->vval.v_number = 0;
15990#else
15991 rettv->vval.v_number = -1;
15992#endif
15993}
15994
Bram Moolenaar0d660222005-01-07 21:51:51 +000015995 static void
15996f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015997 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015999{
16000 char_u *r = NULL;
16001
16002#ifdef FEAT_CLIENTSERVER
16003# ifdef WIN32
16004 r = serverGetVimNames();
16005# else
16006 make_connection();
16007 if (X_DISPLAY != NULL)
16008 r = serverGetVimNames(X_DISPLAY);
16009# endif
16010#endif
16011 rettv->v_type = VAR_STRING;
16012 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013}
16014
16015/*
16016 * "setbufvar()" function
16017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016019f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016021 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022{
16023 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 char_u nbuf[NUMBUFLEN];
16028
16029 if (check_restricted() || check_secure())
16030 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16032 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016033 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 varp = &argvars[2];
16035
16036 if (buf != NULL && varname != NULL && varp != NULL)
16037 {
16038 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040
16041 if (*varname == '&')
16042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 long numval;
16044 char_u *strval;
16045 int error = FALSE;
16046
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 numval = get_tv_number_chk(varp, &error);
16049 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 if (!error && strval != NULL)
16051 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 }
16053 else
16054 {
16055 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16056 if (bufvarname != NULL)
16057 {
16058 STRCPY(bufvarname, "b:");
16059 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016060 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 vim_free(bufvarname);
16062 }
16063 }
16064
16065 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068}
16069
16070/*
16071 * "setcmdpos()" function
16072 */
16073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016075 typval_T *argvars;
16076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016078 int pos = (int)get_tv_number(&argvars[0]) - 1;
16079
16080 if (pos >= 0)
16081 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082}
16083
16084/*
16085 * "setline()" function
16086 */
16087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016088f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 typval_T *argvars;
16090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091{
16092 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016093 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016094 list_T *l = NULL;
16095 listitem_T *li = NULL;
16096 long added = 0;
16097 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016099 lnum = get_tv_lnum(&argvars[0]);
16100 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016102 l = argvars[1].vval.v_list;
16103 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016105 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016106 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016107
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016108 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016109 for (;;)
16110 {
16111 if (l != NULL)
16112 {
16113 /* list argument, get next string */
16114 if (li == NULL)
16115 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016117 li = li->li_next;
16118 }
16119
16120 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016122 break;
16123 if (lnum <= curbuf->b_ml.ml_line_count)
16124 {
16125 /* existing line, replace it */
16126 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16127 {
16128 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016129 if (lnum == curwin->w_cursor.lnum)
16130 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016131 rettv->vval.v_number = 0; /* OK */
16132 }
16133 }
16134 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16135 {
16136 /* lnum is one past the last line, append the line */
16137 ++added;
16138 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16139 rettv->vval.v_number = 0; /* OK */
16140 }
16141
16142 if (l == NULL) /* only one string argument */
16143 break;
16144 ++lnum;
16145 }
16146
16147 if (added > 0)
16148 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149}
16150
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016151static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16152
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016154 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016155 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016156 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016157set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016158 win_T *wp UNUSED;
16159 typval_T *list_arg UNUSED;
16160 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016161 typval_T *rettv;
16162{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016163#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016164 char_u *act;
16165 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016166#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016167
Bram Moolenaar2641f772005-03-25 21:58:17 +000016168 rettv->vval.v_number = -1;
16169
16170#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016171 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016172 EMSG(_(e_listreq));
16173 else
16174 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016175 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016176
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016177 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016178 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016179 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 if (act == NULL)
16181 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016182 if (*act == 'a' || *act == 'r')
16183 action = *act;
16184 }
16185
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016186 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016187 rettv->vval.v_number = 0;
16188 }
16189#endif
16190}
16191
16192/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016193 * "setloclist()" function
16194 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016195 static void
16196f_setloclist(argvars, rettv)
16197 typval_T *argvars;
16198 typval_T *rettv;
16199{
16200 win_T *win;
16201
16202 rettv->vval.v_number = -1;
16203
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016204 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016205 if (win != NULL)
16206 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16207}
16208
16209/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016210 * "setmatches()" function
16211 */
16212 static void
16213f_setmatches(argvars, rettv)
16214 typval_T *argvars;
16215 typval_T *rettv;
16216{
16217#ifdef FEAT_SEARCH_EXTRA
16218 list_T *l;
16219 listitem_T *li;
16220 dict_T *d;
16221
16222 rettv->vval.v_number = -1;
16223 if (argvars[0].v_type != VAR_LIST)
16224 {
16225 EMSG(_(e_listreq));
16226 return;
16227 }
16228 if ((l = argvars[0].vval.v_list) != NULL)
16229 {
16230
16231 /* To some extent make sure that we are dealing with a list from
16232 * "getmatches()". */
16233 li = l->lv_first;
16234 while (li != NULL)
16235 {
16236 if (li->li_tv.v_type != VAR_DICT
16237 || (d = li->li_tv.vval.v_dict) == NULL)
16238 {
16239 EMSG(_(e_invarg));
16240 return;
16241 }
16242 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16243 && dict_find(d, (char_u *)"pattern", -1) != NULL
16244 && dict_find(d, (char_u *)"priority", -1) != NULL
16245 && dict_find(d, (char_u *)"id", -1) != NULL))
16246 {
16247 EMSG(_(e_invarg));
16248 return;
16249 }
16250 li = li->li_next;
16251 }
16252
16253 clear_matches(curwin);
16254 li = l->lv_first;
16255 while (li != NULL)
16256 {
16257 d = li->li_tv.vval.v_dict;
16258 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16259 get_dict_string(d, (char_u *)"pattern", FALSE),
16260 (int)get_dict_number(d, (char_u *)"priority"),
16261 (int)get_dict_number(d, (char_u *)"id"));
16262 li = li->li_next;
16263 }
16264 rettv->vval.v_number = 0;
16265 }
16266#endif
16267}
16268
16269/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016270 * "setpos()" function
16271 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016272 static void
16273f_setpos(argvars, rettv)
16274 typval_T *argvars;
16275 typval_T *rettv;
16276{
16277 pos_T pos;
16278 int fnum;
16279 char_u *name;
16280
Bram Moolenaar08250432008-02-13 11:42:46 +000016281 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016282 name = get_tv_string_chk(argvars);
16283 if (name != NULL)
16284 {
16285 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16286 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016287 if (--pos.col < 0)
16288 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016289 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016290 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016291 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016292 if (fnum == curbuf->b_fnum)
16293 {
16294 curwin->w_cursor = pos;
16295 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016296 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016297 }
16298 else
16299 EMSG(_(e_invarg));
16300 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016301 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16302 {
16303 /* set mark */
16304 if (setmark_pos(name[1], &pos, fnum) == OK)
16305 rettv->vval.v_number = 0;
16306 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016307 else
16308 EMSG(_(e_invarg));
16309 }
16310 }
16311}
16312
16313/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016314 * "setqflist()" function
16315 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016316 static void
16317f_setqflist(argvars, rettv)
16318 typval_T *argvars;
16319 typval_T *rettv;
16320{
16321 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16322}
16323
16324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 * "setreg()" function
16326 */
16327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016328f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 typval_T *argvars;
16330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331{
16332 int regname;
16333 char_u *strregname;
16334 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 int append;
16337 char_u yank_type;
16338 long block_len;
16339
16340 block_len = -1;
16341 yank_type = MAUTO;
16342 append = FALSE;
16343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016344 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016345 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 if (strregname == NULL)
16348 return; /* type error; errmsg already given */
16349 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 if (regname == 0 || regname == '@')
16351 regname = '"';
16352 else if (regname == '=')
16353 return;
16354
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016357 stropt = get_tv_string_chk(&argvars[2]);
16358 if (stropt == NULL)
16359 return; /* type error */
16360 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 switch (*stropt)
16362 {
16363 case 'a': case 'A': /* append */
16364 append = TRUE;
16365 break;
16366 case 'v': case 'c': /* character-wise selection */
16367 yank_type = MCHAR;
16368 break;
16369 case 'V': case 'l': /* line-wise selection */
16370 yank_type = MLINE;
16371 break;
16372#ifdef FEAT_VISUAL
16373 case 'b': case Ctrl_V: /* block-wise selection */
16374 yank_type = MBLOCK;
16375 if (VIM_ISDIGIT(stropt[1]))
16376 {
16377 ++stropt;
16378 block_len = getdigits(&stropt) - 1;
16379 --stropt;
16380 }
16381 break;
16382#endif
16383 }
16384 }
16385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016386 strval = get_tv_string_chk(&argvars[1]);
16387 if (strval != NULL)
16388 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016390 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391}
16392
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016393/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016394 * "settabvar()" function
16395 */
16396 static void
16397f_settabvar(argvars, rettv)
16398 typval_T *argvars;
16399 typval_T *rettv;
16400{
16401 tabpage_T *save_curtab;
16402 char_u *varname, *tabvarname;
16403 typval_T *varp;
16404 tabpage_T *tp;
16405
16406 rettv->vval.v_number = 0;
16407
16408 if (check_restricted() || check_secure())
16409 return;
16410
16411 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16412 varname = get_tv_string_chk(&argvars[1]);
16413 varp = &argvars[2];
16414
16415 if (tp != NULL && varname != NULL && varp != NULL)
16416 {
16417 save_curtab = curtab;
16418 goto_tabpage_tp(tp);
16419
16420 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16421 if (tabvarname != NULL)
16422 {
16423 STRCPY(tabvarname, "t:");
16424 STRCPY(tabvarname + 2, varname);
16425 set_var(tabvarname, varp, TRUE);
16426 vim_free(tabvarname);
16427 }
16428
16429 /* Restore current tabpage */
16430 if (valid_tabpage(save_curtab))
16431 goto_tabpage_tp(save_curtab);
16432 }
16433}
16434
16435/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016436 * "settabwinvar()" function
16437 */
16438 static void
16439f_settabwinvar(argvars, rettv)
16440 typval_T *argvars;
16441 typval_T *rettv;
16442{
16443 setwinvar(argvars, rettv, 1);
16444}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445
16446/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016447 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
16452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016454 setwinvar(argvars, rettv, 0);
16455}
16456
16457/*
16458 * "setwinvar()" and "settabwinvar()" functions
16459 */
16460 static void
16461setwinvar(argvars, rettv, off)
16462 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016463 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016464 int off;
16465{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 win_T *win;
16467#ifdef FEAT_WINDOWS
16468 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016469 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470#endif
16471 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016472 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016474 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475
16476 if (check_restricted() || check_secure())
16477 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016478
16479#ifdef FEAT_WINDOWS
16480 if (off == 1)
16481 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16482 else
16483 tp = curtab;
16484#endif
16485 win = find_win_by_nr(&argvars[off], tp);
16486 varname = get_tv_string_chk(&argvars[off + 1]);
16487 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488
16489 if (win != NULL && varname != NULL && varp != NULL)
16490 {
16491#ifdef FEAT_WINDOWS
16492 /* set curwin to be our win, temporarily */
16493 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016494 save_curtab = curtab;
16495 goto_tabpage_tp(tp);
16496 if (!win_valid(win))
16497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 curwin = win;
16499 curbuf = curwin->w_buffer;
16500#endif
16501
16502 if (*varname == '&')
16503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016504 long numval;
16505 char_u *strval;
16506 int error = FALSE;
16507
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 numval = get_tv_number_chk(varp, &error);
16510 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016511 if (!error && strval != NULL)
16512 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 }
16514 else
16515 {
16516 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16517 if (winvarname != NULL)
16518 {
16519 STRCPY(winvarname, "w:");
16520 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016521 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 vim_free(winvarname);
16523 }
16524 }
16525
16526#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016527 /* Restore current tabpage and window, if still valid (autocomands can
16528 * make them invalid). */
16529 if (valid_tabpage(save_curtab))
16530 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 if (win_valid(save_curwin))
16532 {
16533 curwin = save_curwin;
16534 curbuf = curwin->w_buffer;
16535 }
16536#endif
16537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538}
16539
16540/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016541 * "shellescape({string})" function
16542 */
16543 static void
16544f_shellescape(argvars, rettv)
16545 typval_T *argvars;
16546 typval_T *rettv;
16547{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016548 rettv->vval.v_string = vim_strsave_shellescape(
16549 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016550 rettv->v_type = VAR_STRING;
16551}
16552
16553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 */
16556 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016558 typval_T *argvars;
16559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563 p = get_tv_string(&argvars[0]);
16564 rettv->vval.v_string = vim_strsave(p);
16565 simplify_filename(rettv->vval.v_string); /* simplify in place */
16566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567}
16568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016569#ifdef FEAT_FLOAT
16570/*
16571 * "sin()" function
16572 */
16573 static void
16574f_sin(argvars, rettv)
16575 typval_T *argvars;
16576 typval_T *rettv;
16577{
16578 float_T f;
16579
16580 rettv->v_type = VAR_FLOAT;
16581 if (get_float_arg(argvars, &f) == OK)
16582 rettv->vval.v_float = sin(f);
16583 else
16584 rettv->vval.v_float = 0.0;
16585}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016586
16587/*
16588 * "sinh()" function
16589 */
16590 static void
16591f_sinh(argvars, rettv)
16592 typval_T *argvars;
16593 typval_T *rettv;
16594{
16595 float_T f;
16596
16597 rettv->v_type = VAR_FLOAT;
16598 if (get_float_arg(argvars, &f) == OK)
16599 rettv->vval.v_float = sinh(f);
16600 else
16601 rettv->vval.v_float = 0.0;
16602}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016603#endif
16604
Bram Moolenaar0d660222005-01-07 21:51:51 +000016605static int
16606#ifdef __BORLANDC__
16607 _RTLENTRYF
16608#endif
16609 item_compare __ARGS((const void *s1, const void *s2));
16610static int
16611#ifdef __BORLANDC__
16612 _RTLENTRYF
16613#endif
16614 item_compare2 __ARGS((const void *s1, const void *s2));
16615
16616static int item_compare_ic;
16617static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016618static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016619static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620#define ITEM_COMPARE_FAIL 999
16621
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625 static int
16626#ifdef __BORLANDC__
16627_RTLENTRYF
16628#endif
16629item_compare(s1, s2)
16630 const void *s1;
16631 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016633 char_u *p1, *p2;
16634 char_u *tofree1, *tofree2;
16635 int res;
16636 char_u numbuf1[NUMBUFLEN];
16637 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016639 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16640 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016641 if (p1 == NULL)
16642 p1 = (char_u *)"";
16643 if (p2 == NULL)
16644 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645 if (item_compare_ic)
16646 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648 res = STRCMP(p1, p2);
16649 vim_free(tofree1);
16650 vim_free(tofree2);
16651 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652}
16653
16654 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655#ifdef __BORLANDC__
16656_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658item_compare2(s1, s2)
16659 const void *s1;
16660 const void *s2;
16661{
16662 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016663 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016664 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016665 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016667 /* shortcut after failure in previous call; compare all items equal */
16668 if (item_compare_func_err)
16669 return 0;
16670
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016671 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16672 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016673 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16674 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675
16676 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016677 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016678 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16679 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016680 clear_tv(&argv[0]);
16681 clear_tv(&argv[1]);
16682
16683 if (res == FAIL)
16684 res = ITEM_COMPARE_FAIL;
16685 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16687 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016688 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016689 clear_tv(&rettv);
16690 return res;
16691}
16692
16693/*
16694 * "sort({list})" function
16695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016698 typval_T *argvars;
16699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700{
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 list_T *l;
16702 listitem_T *li;
16703 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016704 long len;
16705 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706
Bram Moolenaar0d660222005-01-07 21:51:51 +000016707 if (argvars[0].v_type != VAR_LIST)
16708 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 else
16710 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016711 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016712 if (l == NULL || tv_check_lock(l->lv_lock,
16713 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016714 return;
16715 rettv->vval.v_list = l;
16716 rettv->v_type = VAR_LIST;
16717 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718
Bram Moolenaar0d660222005-01-07 21:51:51 +000016719 len = list_len(l);
16720 if (len <= 1)
16721 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722
Bram Moolenaar0d660222005-01-07 21:51:51 +000016723 item_compare_ic = FALSE;
16724 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016725 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 if (argvars[1].v_type != VAR_UNKNOWN)
16727 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016728 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016731 else
16732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016733 int error = FALSE;
16734
16735 i = get_tv_number_chk(&argvars[1], &error);
16736 if (error)
16737 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 if (i == 1)
16739 item_compare_ic = TRUE;
16740 else
16741 item_compare_func = get_tv_string(&argvars[1]);
16742 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016743
16744 if (argvars[2].v_type != VAR_UNKNOWN)
16745 {
16746 /* optional third argument: {dict} */
16747 if (argvars[2].v_type != VAR_DICT)
16748 {
16749 EMSG(_(e_dictreq));
16750 return;
16751 }
16752 item_compare_selfdict = argvars[2].vval.v_dict;
16753 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755
Bram Moolenaar0d660222005-01-07 21:51:51 +000016756 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016757 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016758 if (ptrs == NULL)
16759 return;
16760 i = 0;
16761 for (li = l->lv_first; li != NULL; li = li->li_next)
16762 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016764 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765 /* test the compare function */
16766 if (item_compare_func != NULL
16767 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16768 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016769 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016771 {
16772 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774 item_compare_func == NULL ? item_compare : item_compare2);
16775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 if (!item_compare_func_err)
16777 {
16778 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016779 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 l->lv_len = 0;
16781 for (i = 0; i < len; ++i)
16782 list_append(l, ptrs[i]);
16783 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784 }
16785
16786 vim_free(ptrs);
16787 }
16788}
16789
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016790/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016791 * "soundfold({word})" function
16792 */
16793 static void
16794f_soundfold(argvars, rettv)
16795 typval_T *argvars;
16796 typval_T *rettv;
16797{
16798 char_u *s;
16799
16800 rettv->v_type = VAR_STRING;
16801 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016802#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016803 rettv->vval.v_string = eval_soundfold(s);
16804#else
16805 rettv->vval.v_string = vim_strsave(s);
16806#endif
16807}
16808
16809/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016810 * "spellbadword()" function
16811 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016812 static void
16813f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016814 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016815 typval_T *rettv;
16816{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016817 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016818 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016819 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016820
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016821 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016822 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016823
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016824#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016825 if (argvars[0].v_type == VAR_UNKNOWN)
16826 {
16827 /* Find the start and length of the badly spelled word. */
16828 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16829 if (len != 0)
16830 word = ml_get_cursor();
16831 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016832 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016833 {
16834 char_u *str = get_tv_string_chk(&argvars[0]);
16835 int capcol = -1;
16836
16837 if (str != NULL)
16838 {
16839 /* Check the argument for spelling. */
16840 while (*str != NUL)
16841 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016842 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016843 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016844 {
16845 word = str;
16846 break;
16847 }
16848 str += len;
16849 }
16850 }
16851 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016852#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016853
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016854 list_append_string(rettv->vval.v_list, word, len);
16855 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016856 attr == HLF_SPB ? "bad" :
16857 attr == HLF_SPR ? "rare" :
16858 attr == HLF_SPL ? "local" :
16859 attr == HLF_SPC ? "caps" :
16860 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016861}
16862
16863/*
16864 * "spellsuggest()" function
16865 */
16866 static void
16867f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016868 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016869 typval_T *rettv;
16870{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016871#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016872 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016873 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016874 int maxcount;
16875 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016876 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016877 listitem_T *li;
16878 int need_capital = FALSE;
16879#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016880
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016881 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016882 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016883
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016884#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016885 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016886 {
16887 str = get_tv_string(&argvars[0]);
16888 if (argvars[1].v_type != VAR_UNKNOWN)
16889 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016890 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016891 if (maxcount <= 0)
16892 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016893 if (argvars[2].v_type != VAR_UNKNOWN)
16894 {
16895 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16896 if (typeerr)
16897 return;
16898 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016899 }
16900 else
16901 maxcount = 25;
16902
Bram Moolenaar4770d092006-01-12 23:22:24 +000016903 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016904
16905 for (i = 0; i < ga.ga_len; ++i)
16906 {
16907 str = ((char_u **)ga.ga_data)[i];
16908
16909 li = listitem_alloc();
16910 if (li == NULL)
16911 vim_free(str);
16912 else
16913 {
16914 li->li_tv.v_type = VAR_STRING;
16915 li->li_tv.v_lock = 0;
16916 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016917 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016918 }
16919 }
16920 ga_clear(&ga);
16921 }
16922#endif
16923}
16924
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016926f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 typval_T *argvars;
16928 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929{
16930 char_u *str;
16931 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016932 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 regmatch_T regmatch;
16934 char_u patbuf[NUMBUFLEN];
16935 char_u *save_cpo;
16936 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016938 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016939 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940
16941 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16942 save_cpo = p_cpo;
16943 p_cpo = (char_u *)"";
16944
16945 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016946 if (argvars[1].v_type != VAR_UNKNOWN)
16947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16949 if (pat == NULL)
16950 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016951 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016952 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016953 }
16954 if (pat == NULL || *pat == NUL)
16955 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016957 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 if (typeerr)
16960 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16963 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016966 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016968 if (*str == NUL)
16969 match = FALSE; /* empty item at the end */
16970 else
16971 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 if (match)
16973 end = regmatch.startp[0];
16974 else
16975 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016976 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16977 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016979 if (list_append_string(rettv->vval.v_list, str,
16980 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 }
16983 if (!match)
16984 break;
16985 /* Advance to just after the match. */
16986 if (regmatch.endp[0] > str)
16987 col = 0;
16988 else
16989 {
16990 /* Don't get stuck at the same match. */
16991#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016992 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993#else
16994 col = 1;
16995#endif
16996 }
16997 str = regmatch.endp[0];
16998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004}
17005
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017006#ifdef FEAT_FLOAT
17007/*
17008 * "sqrt()" function
17009 */
17010 static void
17011f_sqrt(argvars, rettv)
17012 typval_T *argvars;
17013 typval_T *rettv;
17014{
17015 float_T f;
17016
17017 rettv->v_type = VAR_FLOAT;
17018 if (get_float_arg(argvars, &f) == OK)
17019 rettv->vval.v_float = sqrt(f);
17020 else
17021 rettv->vval.v_float = 0.0;
17022}
17023
17024/*
17025 * "str2float()" function
17026 */
17027 static void
17028f_str2float(argvars, rettv)
17029 typval_T *argvars;
17030 typval_T *rettv;
17031{
17032 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17033
17034 if (*p == '+')
17035 p = skipwhite(p + 1);
17036 (void)string2float(p, &rettv->vval.v_float);
17037 rettv->v_type = VAR_FLOAT;
17038}
17039#endif
17040
Bram Moolenaar2c932302006-03-18 21:42:09 +000017041/*
17042 * "str2nr()" function
17043 */
17044 static void
17045f_str2nr(argvars, rettv)
17046 typval_T *argvars;
17047 typval_T *rettv;
17048{
17049 int base = 10;
17050 char_u *p;
17051 long n;
17052
17053 if (argvars[1].v_type != VAR_UNKNOWN)
17054 {
17055 base = get_tv_number(&argvars[1]);
17056 if (base != 8 && base != 10 && base != 16)
17057 {
17058 EMSG(_(e_invarg));
17059 return;
17060 }
17061 }
17062
17063 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017064 if (*p == '+')
17065 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017066 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17067 rettv->vval.v_number = n;
17068}
17069
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070#ifdef HAVE_STRFTIME
17071/*
17072 * "strftime({format}[, {time}])" function
17073 */
17074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017076 typval_T *argvars;
17077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078{
17079 char_u result_buf[256];
17080 struct tm *curtime;
17081 time_t seconds;
17082 char_u *p;
17083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017086 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017087 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 seconds = time(NULL);
17089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017090 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 curtime = localtime(&seconds);
17092 /* MSVC returns NULL for an invalid value of seconds. */
17093 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017094 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 else
17096 {
17097# ifdef FEAT_MBYTE
17098 vimconv_T conv;
17099 char_u *enc;
17100
17101 conv.vc_type = CONV_NONE;
17102 enc = enc_locale();
17103 convert_setup(&conv, p_enc, enc);
17104 if (conv.vc_type != CONV_NONE)
17105 p = string_convert(&conv, p, NULL);
17106# endif
17107 if (p != NULL)
17108 (void)strftime((char *)result_buf, sizeof(result_buf),
17109 (char *)p, curtime);
17110 else
17111 result_buf[0] = NUL;
17112
17113# ifdef FEAT_MBYTE
17114 if (conv.vc_type != CONV_NONE)
17115 vim_free(p);
17116 convert_setup(&conv, enc, p_enc);
17117 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 else
17120# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122
17123# ifdef FEAT_MBYTE
17124 /* Release conversion descriptors */
17125 convert_setup(&conv, NULL, NULL);
17126 vim_free(enc);
17127# endif
17128 }
17129}
17130#endif
17131
17132/*
17133 * "stridx()" function
17134 */
17135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017136f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017137 typval_T *argvars;
17138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139{
17140 char_u buf[NUMBUFLEN];
17141 char_u *needle;
17142 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017145 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 needle = get_tv_string_chk(&argvars[1]);
17148 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 if (needle == NULL || haystack == NULL)
17151 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152
Bram Moolenaar33570922005-01-25 22:26:29 +000017153 if (argvars[2].v_type != VAR_UNKNOWN)
17154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017155 int error = FALSE;
17156
17157 start_idx = get_tv_number_chk(&argvars[2], &error);
17158 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017160 if (start_idx >= 0)
17161 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017162 }
17163
17164 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17165 if (pos != NULL)
17166 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167}
17168
17169/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017170 * "string()" function
17171 */
17172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017174 typval_T *argvars;
17175 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176{
17177 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017178 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017181 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017182 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017183 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185}
17186
17187/*
17188 * "strlen()" function
17189 */
17190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017192 typval_T *argvars;
17193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 rettv->vval.v_number = (varnumber_T)(STRLEN(
17196 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197}
17198
17199/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017200 * "strchars()" function
17201 */
17202 static void
17203f_strchars(argvars, rettv)
17204 typval_T *argvars;
17205 typval_T *rettv;
17206{
17207 char_u *s = get_tv_string(&argvars[0]);
17208#ifdef FEAT_MBYTE
17209 varnumber_T len = 0;
17210
17211 while (*s != NUL)
17212 {
17213 mb_cptr2char_adv(&s);
17214 ++len;
17215 }
17216 rettv->vval.v_number = len;
17217#else
17218 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17219#endif
17220}
17221
17222/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017223 * "strdisplaywidth()" function
17224 */
17225 static void
17226f_strdisplaywidth(argvars, rettv)
17227 typval_T *argvars;
17228 typval_T *rettv;
17229{
17230 char_u *s = get_tv_string(&argvars[0]);
17231 int col = 0;
17232
17233 if (argvars[1].v_type != VAR_UNKNOWN)
17234 col = get_tv_number(&argvars[1]);
17235
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017236 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017237}
17238
17239/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017240 * "strwidth()" function
17241 */
17242 static void
17243f_strwidth(argvars, rettv)
17244 typval_T *argvars;
17245 typval_T *rettv;
17246{
17247 char_u *s = get_tv_string(&argvars[0]);
17248
17249 rettv->vval.v_number = (varnumber_T)(
17250#ifdef FEAT_MBYTE
17251 mb_string2cells(s, -1)
17252#else
17253 STRLEN(s)
17254#endif
17255 );
17256}
17257
17258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 * "strpart()" function
17260 */
17261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017262f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 typval_T *argvars;
17264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265{
17266 char_u *p;
17267 int n;
17268 int len;
17269 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017270 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 slen = (int)STRLEN(p);
17274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017275 n = get_tv_number_chk(&argvars[1], &error);
17276 if (error)
17277 len = 0;
17278 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 else
17281 len = slen - n; /* default len: all bytes that are available. */
17282
17283 /*
17284 * Only return the overlap between the specified part and the actual
17285 * string.
17286 */
17287 if (n < 0)
17288 {
17289 len += n;
17290 n = 0;
17291 }
17292 else if (n > slen)
17293 n = slen;
17294 if (len < 0)
17295 len = 0;
17296 else if (n + len > slen)
17297 len = slen - n;
17298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299 rettv->v_type = VAR_STRING;
17300 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301}
17302
17303/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304 * "strridx()" function
17305 */
17306 static void
17307f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 typval_T *argvars;
17309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310{
17311 char_u buf[NUMBUFLEN];
17312 char_u *needle;
17313 char_u *haystack;
17314 char_u *rest;
17315 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017316 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017318 needle = get_tv_string_chk(&argvars[1]);
17319 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017320
17321 rettv->vval.v_number = -1;
17322 if (needle == NULL || haystack == NULL)
17323 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017324
17325 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017326 if (argvars[2].v_type != VAR_UNKNOWN)
17327 {
17328 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017329 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017330 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017332 }
17333 else
17334 end_idx = haystack_len;
17335
Bram Moolenaar0d660222005-01-07 21:51:51 +000017336 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017337 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017339 lastmatch = haystack + end_idx;
17340 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017341 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017342 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343 for (rest = haystack; *rest != '\0'; ++rest)
17344 {
17345 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017346 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017347 break;
17348 lastmatch = rest;
17349 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017350 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351
17352 if (lastmatch == NULL)
17353 rettv->vval.v_number = -1;
17354 else
17355 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17356}
17357
17358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 * "strtrans()" function
17360 */
17361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 typval_T *argvars;
17364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 rettv->v_type = VAR_STRING;
17367 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368}
17369
17370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017371 * "submatch()" function
17372 */
17373 static void
17374f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017375 typval_T *argvars;
17376 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017377{
17378 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 rettv->vval.v_string =
17380 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017381}
17382
17383/*
17384 * "substitute()" function
17385 */
17386 static void
17387f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 typval_T *argvars;
17389 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017390{
17391 char_u patbuf[NUMBUFLEN];
17392 char_u subbuf[NUMBUFLEN];
17393 char_u flagsbuf[NUMBUFLEN];
17394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017395 char_u *str = get_tv_string_chk(&argvars[0]);
17396 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17397 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17398 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17399
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017401 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17402 rettv->vval.v_string = NULL;
17403 else
17404 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405}
17406
17407/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017408 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414{
17415 int id = 0;
17416#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017417 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418 long col;
17419 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017420 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017422 lnum = get_tv_lnum(argvars); /* -1 on type error */
17423 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17424 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017427 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017428 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429#endif
17430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432}
17433
17434/*
17435 * "synIDattr(id, what [, mode])" function
17436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017438f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017439 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441{
17442 char_u *p = NULL;
17443#ifdef FEAT_SYN_HL
17444 int id;
17445 char_u *what;
17446 char_u *mode;
17447 char_u modebuf[NUMBUFLEN];
17448 int modec;
17449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017450 id = get_tv_number(&argvars[0]);
17451 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017454 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017456 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 modec = 0; /* replace invalid with current */
17458 }
17459 else
17460 {
17461#ifdef FEAT_GUI
17462 if (gui.in_use)
17463 modec = 'g';
17464 else
17465#endif
17466 if (t_colors > 1)
17467 modec = 'c';
17468 else
17469 modec = 't';
17470 }
17471
17472
17473 switch (TOLOWER_ASC(what[0]))
17474 {
17475 case 'b':
17476 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17477 p = highlight_color(id, what, modec);
17478 else /* bold */
17479 p = highlight_has_attr(id, HL_BOLD, modec);
17480 break;
17481
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017482 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 p = highlight_color(id, what, modec);
17484 break;
17485
17486 case 'i':
17487 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17488 p = highlight_has_attr(id, HL_INVERSE, modec);
17489 else /* italic */
17490 p = highlight_has_attr(id, HL_ITALIC, modec);
17491 break;
17492
17493 case 'n': /* name */
17494 p = get_highlight_name(NULL, id - 1);
17495 break;
17496
17497 case 'r': /* reverse */
17498 p = highlight_has_attr(id, HL_INVERSE, modec);
17499 break;
17500
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017501 case 's':
17502 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17503 p = highlight_color(id, what, modec);
17504 else /* standout */
17505 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 break;
17507
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017508 case 'u':
17509 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17510 /* underline */
17511 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17512 else
17513 /* undercurl */
17514 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 break;
17516 }
17517
17518 if (p != NULL)
17519 p = vim_strsave(p);
17520#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv->v_type = VAR_STRING;
17522 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523}
17524
17525/*
17526 * "synIDtrans(id)" function
17527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017529f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017530 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532{
17533 int id;
17534
17535#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017536 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537
17538 if (id > 0)
17539 id = syn_get_final_id(id);
17540 else
17541#endif
17542 id = 0;
17543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017544 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545}
17546
17547/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017548 * "synconcealed(lnum, col)" function
17549 */
17550 static void
17551f_synconcealed(argvars, rettv)
17552 typval_T *argvars UNUSED;
17553 typval_T *rettv;
17554{
17555#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17556 long lnum;
17557 long col;
17558 int syntax_flags = 0;
17559 int cchar;
17560 int matchid = 0;
17561 char_u str[NUMBUFLEN];
17562#endif
17563
17564 rettv->v_type = VAR_LIST;
17565 rettv->vval.v_list = NULL;
17566
17567#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17568 lnum = get_tv_lnum(argvars); /* -1 on type error */
17569 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17570
17571 vim_memset(str, NUL, sizeof(str));
17572
17573 if (rettv_list_alloc(rettv) != FAIL)
17574 {
17575 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17576 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17577 && curwin->w_p_cole > 0)
17578 {
17579 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17580 syntax_flags = get_syntax_info(&matchid);
17581
17582 /* get the conceal character */
17583 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17584 {
17585 cchar = syn_get_sub_char();
17586 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17587 cchar = lcs_conceal;
17588 if (cchar != NUL)
17589 {
17590# ifdef FEAT_MBYTE
17591 if (has_mbyte)
17592 (*mb_char2bytes)(cchar, str);
17593 else
17594# endif
17595 str[0] = cchar;
17596 }
17597 }
17598 }
17599
17600 list_append_number(rettv->vval.v_list,
17601 (syntax_flags & HL_CONCEAL) != 0);
17602 /* -1 to auto-determine strlen */
17603 list_append_string(rettv->vval.v_list, str, -1);
17604 list_append_number(rettv->vval.v_list, matchid);
17605 }
17606#endif
17607}
17608
17609/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017610 * "synstack(lnum, col)" function
17611 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017612 static void
17613f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017614 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017615 typval_T *rettv;
17616{
17617#ifdef FEAT_SYN_HL
17618 long lnum;
17619 long col;
17620 int i;
17621 int id;
17622#endif
17623
17624 rettv->v_type = VAR_LIST;
17625 rettv->vval.v_list = NULL;
17626
17627#ifdef FEAT_SYN_HL
17628 lnum = get_tv_lnum(argvars); /* -1 on type error */
17629 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17630
17631 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017632 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017633 && rettv_list_alloc(rettv) != FAIL)
17634 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017635 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017636 for (i = 0; ; ++i)
17637 {
17638 id = syn_get_stack_item(i);
17639 if (id < 0)
17640 break;
17641 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17642 break;
17643 }
17644 }
17645#endif
17646}
17647
17648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 * "system()" function
17650 */
17651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 typval_T *argvars;
17654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017656 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017658 char_u *infile = NULL;
17659 char_u buf[NUMBUFLEN];
17660 int err = FALSE;
17661 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017663 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017664 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017665
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017666 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017667 {
17668 /*
17669 * Write the string to a temp file, to be used for input of the shell
17670 * command.
17671 */
17672 if ((infile = vim_tempname('i')) == NULL)
17673 {
17674 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017675 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017676 }
17677
17678 fd = mch_fopen((char *)infile, WRITEBIN);
17679 if (fd == NULL)
17680 {
17681 EMSG2(_(e_notopen), infile);
17682 goto done;
17683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017684 p = get_tv_string_buf_chk(&argvars[1], buf);
17685 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017686 {
17687 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017689 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017690 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17691 err = TRUE;
17692 if (fclose(fd) != 0)
17693 err = TRUE;
17694 if (err)
17695 {
17696 EMSG(_("E677: Error writing temp file"));
17697 goto done;
17698 }
17699 }
17700
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017701 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17702 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017703
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704#ifdef USE_CR
17705 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017706 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 {
17708 char_u *s;
17709
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017710 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 {
17712 if (*s == CAR)
17713 *s = NL;
17714 }
17715 }
17716#else
17717# ifdef USE_CRNL
17718 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017719 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 {
17721 char_u *s, *d;
17722
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017723 d = res;
17724 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 {
17726 if (s[0] == CAR && s[1] == NL)
17727 ++s;
17728 *d++ = *s;
17729 }
17730 *d = NUL;
17731 }
17732# endif
17733#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017734
17735done:
17736 if (infile != NULL)
17737 {
17738 mch_remove(infile);
17739 vim_free(infile);
17740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741 rettv->v_type = VAR_STRING;
17742 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743}
17744
17745/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017746 * "tabpagebuflist()" function
17747 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017748 static void
17749f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017750 typval_T *argvars UNUSED;
17751 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017752{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017753#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017754 tabpage_T *tp;
17755 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017756
17757 if (argvars[0].v_type == VAR_UNKNOWN)
17758 wp = firstwin;
17759 else
17760 {
17761 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17762 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017763 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017764 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017765 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017766 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017767 for (; wp != NULL; wp = wp->w_next)
17768 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017769 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017770 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017771 }
17772#endif
17773}
17774
17775
17776/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017777 * "tabpagenr()" function
17778 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017779 static void
17780f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017781 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017782 typval_T *rettv;
17783{
17784 int nr = 1;
17785#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017786 char_u *arg;
17787
17788 if (argvars[0].v_type != VAR_UNKNOWN)
17789 {
17790 arg = get_tv_string_chk(&argvars[0]);
17791 nr = 0;
17792 if (arg != NULL)
17793 {
17794 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017795 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017796 else
17797 EMSG2(_(e_invexpr2), arg);
17798 }
17799 }
17800 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017801 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017802#endif
17803 rettv->vval.v_number = nr;
17804}
17805
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017806
17807#ifdef FEAT_WINDOWS
17808static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17809
17810/*
17811 * Common code for tabpagewinnr() and winnr().
17812 */
17813 static int
17814get_winnr(tp, argvar)
17815 tabpage_T *tp;
17816 typval_T *argvar;
17817{
17818 win_T *twin;
17819 int nr = 1;
17820 win_T *wp;
17821 char_u *arg;
17822
17823 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17824 if (argvar->v_type != VAR_UNKNOWN)
17825 {
17826 arg = get_tv_string_chk(argvar);
17827 if (arg == NULL)
17828 nr = 0; /* type error; errmsg already given */
17829 else if (STRCMP(arg, "$") == 0)
17830 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17831 else if (STRCMP(arg, "#") == 0)
17832 {
17833 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17834 if (twin == NULL)
17835 nr = 0;
17836 }
17837 else
17838 {
17839 EMSG2(_(e_invexpr2), arg);
17840 nr = 0;
17841 }
17842 }
17843
17844 if (nr > 0)
17845 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17846 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017847 {
17848 if (wp == NULL)
17849 {
17850 /* didn't find it in this tabpage */
17851 nr = 0;
17852 break;
17853 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017854 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017855 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017856 return nr;
17857}
17858#endif
17859
17860/*
17861 * "tabpagewinnr()" function
17862 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017863 static void
17864f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017865 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017866 typval_T *rettv;
17867{
17868 int nr = 1;
17869#ifdef FEAT_WINDOWS
17870 tabpage_T *tp;
17871
17872 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17873 if (tp == NULL)
17874 nr = 0;
17875 else
17876 nr = get_winnr(tp, &argvars[1]);
17877#endif
17878 rettv->vval.v_number = nr;
17879}
17880
17881
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017882/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017883 * "tagfiles()" function
17884 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017885 static void
17886f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017887 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017888 typval_T *rettv;
17889{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017890 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017891 tagname_T tn;
17892 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017894 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017895 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017896 fname = alloc(MAXPATHL);
17897 if (fname == NULL)
17898 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017900 for (first = TRUE; ; first = FALSE)
17901 if (get_tagfname(&tn, first, fname) == FAIL
17902 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017903 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017904 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017905 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017906}
17907
17908/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017909 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017910 */
17911 static void
17912f_taglist(argvars, rettv)
17913 typval_T *argvars;
17914 typval_T *rettv;
17915{
17916 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017917
17918 tag_pattern = get_tv_string(&argvars[0]);
17919
17920 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017921 if (*tag_pattern == NUL)
17922 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017923
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017924 if (rettv_list_alloc(rettv) == OK)
17925 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017926}
17927
17928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 * "tempname()" function
17930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017932f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017933 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935{
17936 static int x = 'A';
17937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 rettv->v_type = VAR_STRING;
17939 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940
17941 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17942 * names. Skip 'I' and 'O', they are used for shell redirection. */
17943 do
17944 {
17945 if (x == 'Z')
17946 x = '0';
17947 else if (x == '9')
17948 x = 'A';
17949 else
17950 {
17951#ifdef EBCDIC
17952 if (x == 'I')
17953 x = 'J';
17954 else if (x == 'R')
17955 x = 'S';
17956 else
17957#endif
17958 ++x;
17959 }
17960 } while (x == 'I' || x == 'O');
17961}
17962
17963/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017964 * "test(list)" function: Just checking the walls...
17965 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017966 static void
17967f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017968 typval_T *argvars UNUSED;
17969 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017970{
17971 /* Used for unit testing. Change the code below to your liking. */
17972#if 0
17973 listitem_T *li;
17974 list_T *l;
17975 char_u *bad, *good;
17976
17977 if (argvars[0].v_type != VAR_LIST)
17978 return;
17979 l = argvars[0].vval.v_list;
17980 if (l == NULL)
17981 return;
17982 li = l->lv_first;
17983 if (li == NULL)
17984 return;
17985 bad = get_tv_string(&li->li_tv);
17986 li = li->li_next;
17987 if (li == NULL)
17988 return;
17989 good = get_tv_string(&li->li_tv);
17990 rettv->vval.v_number = test_edit_score(bad, good);
17991#endif
17992}
17993
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017994#ifdef FEAT_FLOAT
17995/*
17996 * "tan()" function
17997 */
17998 static void
17999f_tan(argvars, rettv)
18000 typval_T *argvars;
18001 typval_T *rettv;
18002{
18003 float_T f;
18004
18005 rettv->v_type = VAR_FLOAT;
18006 if (get_float_arg(argvars, &f) == OK)
18007 rettv->vval.v_float = tan(f);
18008 else
18009 rettv->vval.v_float = 0.0;
18010}
18011
18012/*
18013 * "tanh()" function
18014 */
18015 static void
18016f_tanh(argvars, rettv)
18017 typval_T *argvars;
18018 typval_T *rettv;
18019{
18020 float_T f;
18021
18022 rettv->v_type = VAR_FLOAT;
18023 if (get_float_arg(argvars, &f) == OK)
18024 rettv->vval.v_float = tanh(f);
18025 else
18026 rettv->vval.v_float = 0.0;
18027}
18028#endif
18029
Bram Moolenaard52d9742005-08-21 22:20:28 +000018030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 * "tolower(string)" function
18032 */
18033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018034f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018035 typval_T *argvars;
18036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037{
18038 char_u *p;
18039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018040 p = vim_strsave(get_tv_string(&argvars[0]));
18041 rettv->v_type = VAR_STRING;
18042 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043
18044 if (p != NULL)
18045 while (*p != NUL)
18046 {
18047#ifdef FEAT_MBYTE
18048 int l;
18049
18050 if (enc_utf8)
18051 {
18052 int c, lc;
18053
18054 c = utf_ptr2char(p);
18055 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018056 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 /* TODO: reallocate string when byte count changes. */
18058 if (utf_char2len(lc) == l)
18059 utf_char2bytes(lc, p);
18060 p += l;
18061 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018062 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 p += l; /* skip multi-byte character */
18064 else
18065#endif
18066 {
18067 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18068 ++p;
18069 }
18070 }
18071}
18072
18073/*
18074 * "toupper(string)" function
18075 */
18076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018077f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 typval_T *argvars;
18079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018082 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083}
18084
18085/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018086 * "tr(string, fromstr, tostr)" function
18087 */
18088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018089f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018090 typval_T *argvars;
18091 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018092{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018093 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018094 char_u *fromstr;
18095 char_u *tostr;
18096 char_u *p;
18097#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018098 int inlen;
18099 int fromlen;
18100 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018101 int idx;
18102 char_u *cpstr;
18103 int cplen;
18104 int first = TRUE;
18105#endif
18106 char_u buf[NUMBUFLEN];
18107 char_u buf2[NUMBUFLEN];
18108 garray_T ga;
18109
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018110 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018111 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18112 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018113
18114 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018115 rettv->v_type = VAR_STRING;
18116 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018117 if (fromstr == NULL || tostr == NULL)
18118 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018119 ga_init2(&ga, (int)sizeof(char), 80);
18120
18121#ifdef FEAT_MBYTE
18122 if (!has_mbyte)
18123#endif
18124 /* not multi-byte: fromstr and tostr must be the same length */
18125 if (STRLEN(fromstr) != STRLEN(tostr))
18126 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018127#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018128error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018129#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018130 EMSG2(_(e_invarg2), fromstr);
18131 ga_clear(&ga);
18132 return;
18133 }
18134
18135 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018136 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018137 {
18138#ifdef FEAT_MBYTE
18139 if (has_mbyte)
18140 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018141 inlen = (*mb_ptr2len)(in_str);
18142 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018143 cplen = inlen;
18144 idx = 0;
18145 for (p = fromstr; *p != NUL; p += fromlen)
18146 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018147 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018148 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018149 {
18150 for (p = tostr; *p != NUL; p += tolen)
18151 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018152 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018153 if (idx-- == 0)
18154 {
18155 cplen = tolen;
18156 cpstr = p;
18157 break;
18158 }
18159 }
18160 if (*p == NUL) /* tostr is shorter than fromstr */
18161 goto error;
18162 break;
18163 }
18164 ++idx;
18165 }
18166
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018167 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018168 {
18169 /* Check that fromstr and tostr have the same number of
18170 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018171 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018172 first = FALSE;
18173 for (p = tostr; *p != NUL; p += tolen)
18174 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018175 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018176 --idx;
18177 }
18178 if (idx != 0)
18179 goto error;
18180 }
18181
18182 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018183 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018184 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018185
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018186 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018187 }
18188 else
18189#endif
18190 {
18191 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018192 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018193 if (p != NULL)
18194 ga_append(&ga, tostr[p - fromstr]);
18195 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018196 ga_append(&ga, *in_str);
18197 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018198 }
18199 }
18200
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018201 /* add a terminating NUL */
18202 ga_grow(&ga, 1);
18203 ga_append(&ga, NUL);
18204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018206}
18207
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018208#ifdef FEAT_FLOAT
18209/*
18210 * "trunc({float})" function
18211 */
18212 static void
18213f_trunc(argvars, rettv)
18214 typval_T *argvars;
18215 typval_T *rettv;
18216{
18217 float_T f;
18218
18219 rettv->v_type = VAR_FLOAT;
18220 if (get_float_arg(argvars, &f) == OK)
18221 /* trunc() is not in C90, use floor() or ceil() instead. */
18222 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18223 else
18224 rettv->vval.v_float = 0.0;
18225}
18226#endif
18227
Bram Moolenaar8299df92004-07-10 09:47:34 +000018228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 * "type(expr)" function
18230 */
18231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018233 typval_T *argvars;
18234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018236 int n;
18237
18238 switch (argvars[0].v_type)
18239 {
18240 case VAR_NUMBER: n = 0; break;
18241 case VAR_STRING: n = 1; break;
18242 case VAR_FUNC: n = 2; break;
18243 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018244 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018245#ifdef FEAT_FLOAT
18246 case VAR_FLOAT: n = 5; break;
18247#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018248 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18249 }
18250 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251}
18252
18253/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018254 * "undofile(name)" function
18255 */
18256 static void
18257f_undofile(argvars, rettv)
18258 typval_T *argvars;
18259 typval_T *rettv;
18260{
18261 rettv->v_type = VAR_STRING;
18262#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018263 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018264 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018265
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018266 if (*fname == NUL)
18267 {
18268 /* If there is no file name there will be no undo file. */
18269 rettv->vval.v_string = NULL;
18270 }
18271 else
18272 {
18273 char_u *ffname = FullName_save(fname, FALSE);
18274
18275 if (ffname != NULL)
18276 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18277 vim_free(ffname);
18278 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018279 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018280#else
18281 rettv->vval.v_string = NULL;
18282#endif
18283}
18284
18285/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018286 * "undotree()" function
18287 */
18288 static void
18289f_undotree(argvars, rettv)
18290 typval_T *argvars UNUSED;
18291 typval_T *rettv;
18292{
18293 if (rettv_dict_alloc(rettv) == OK)
18294 {
18295 dict_T *dict = rettv->vval.v_dict;
18296 list_T *list;
18297
Bram Moolenaar730cde92010-06-27 05:18:54 +020018298 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018299 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018300 dict_add_nr_str(dict, "save_last",
18301 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018302 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18303 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018304 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018305
18306 list = list_alloc();
18307 if (list != NULL)
18308 {
18309 u_eval_tree(curbuf->b_u_oldhead, list);
18310 dict_add_list(dict, "entries", list);
18311 }
18312 }
18313}
18314
18315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018316 * "values(dict)" function
18317 */
18318 static void
18319f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018320 typval_T *argvars;
18321 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018322{
18323 dict_list(argvars, rettv, 1);
18324}
18325
18326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 * "virtcol(string)" function
18328 */
18329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018330f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018331 typval_T *argvars;
18332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333{
18334 colnr_T vcol = 0;
18335 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018336 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018338 fp = var2fpos(&argvars[0], FALSE, &fnum);
18339 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18340 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 {
18342 getvvcol(curwin, fp, NULL, NULL, &vcol);
18343 ++vcol;
18344 }
18345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018346 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347}
18348
18349/*
18350 * "visualmode()" function
18351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018353f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018354 typval_T *argvars UNUSED;
18355 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356{
18357#ifdef FEAT_VISUAL
18358 char_u str[2];
18359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018360 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 str[0] = curbuf->b_visual_mode_eval;
18362 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018363 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364
18365 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018366 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368#endif
18369}
18370
18371/*
18372 * "winbufnr(nr)" function
18373 */
18374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018376 typval_T *argvars;
18377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378{
18379 win_T *wp;
18380
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018381 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386}
18387
18388/*
18389 * "wincol()" function
18390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018393 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395{
18396 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018397 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398}
18399
18400/*
18401 * "winheight(nr)" function
18402 */
18403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018404f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018405 typval_T *argvars;
18406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407{
18408 win_T *wp;
18409
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018410 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018412 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018414 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415}
18416
18417/*
18418 * "winline()" function
18419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421f_winline(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427}
18428
18429/*
18430 * "winnr()" function
18431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018433f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018434 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436{
18437 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018438
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018440 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018442 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443}
18444
18445/*
18446 * "winrestcmd()" function
18447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452{
18453#ifdef FEAT_WINDOWS
18454 win_T *wp;
18455 int winnr = 1;
18456 garray_T ga;
18457 char_u buf[50];
18458
18459 ga_init2(&ga, (int)sizeof(char), 70);
18460 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18461 {
18462 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18463 ga_concat(&ga, buf);
18464# ifdef FEAT_VERTSPLIT
18465 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18466 ga_concat(&ga, buf);
18467# endif
18468 ++winnr;
18469 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018470 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018472 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477}
18478
18479/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018480 * "winrestview()" function
18481 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018482 static void
18483f_winrestview(argvars, rettv)
18484 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018485 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018486{
18487 dict_T *dict;
18488
18489 if (argvars[0].v_type != VAR_DICT
18490 || (dict = argvars[0].vval.v_dict) == NULL)
18491 EMSG(_(e_invarg));
18492 else
18493 {
18494 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18495 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18496#ifdef FEAT_VIRTUALEDIT
18497 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18498#endif
18499 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018500 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018501
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018502 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018503#ifdef FEAT_DIFF
18504 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18505#endif
18506 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18507 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18508
18509 check_cursor();
18510 changed_cline_bef_curs();
18511 invalidate_botline();
18512 redraw_later(VALID);
18513
18514 if (curwin->w_topline == 0)
18515 curwin->w_topline = 1;
18516 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18517 curwin->w_topline = curbuf->b_ml.ml_line_count;
18518#ifdef FEAT_DIFF
18519 check_topfill(curwin, TRUE);
18520#endif
18521 }
18522}
18523
18524/*
18525 * "winsaveview()" function
18526 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018527 static void
18528f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018529 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018530 typval_T *rettv;
18531{
18532 dict_T *dict;
18533
Bram Moolenaara800b422010-06-27 01:15:55 +020018534 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018535 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018536 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018537
18538 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18539 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18540#ifdef FEAT_VIRTUALEDIT
18541 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18542#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018543 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018544 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18545
18546 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18547#ifdef FEAT_DIFF
18548 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18549#endif
18550 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18551 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18552}
18553
18554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 * "winwidth(nr)" function
18556 */
18557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018559 typval_T *argvars;
18560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561{
18562 win_T *wp;
18563
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018564 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 else
18568#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572#endif
18573}
18574
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018576 * "writefile()" function
18577 */
18578 static void
18579f_writefile(argvars, rettv)
18580 typval_T *argvars;
18581 typval_T *rettv;
18582{
18583 int binary = FALSE;
18584 char_u *fname;
18585 FILE *fd;
18586 listitem_T *li;
18587 char_u *s;
18588 int ret = 0;
18589 int c;
18590
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018591 if (check_restricted() || check_secure())
18592 return;
18593
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018594 if (argvars[0].v_type != VAR_LIST)
18595 {
18596 EMSG2(_(e_listarg), "writefile()");
18597 return;
18598 }
18599 if (argvars[0].vval.v_list == NULL)
18600 return;
18601
18602 if (argvars[2].v_type != VAR_UNKNOWN
18603 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18604 binary = TRUE;
18605
18606 /* Always open the file in binary mode, library functions have a mind of
18607 * their own about CR-LF conversion. */
18608 fname = get_tv_string(&argvars[1]);
18609 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18610 {
18611 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18612 ret = -1;
18613 }
18614 else
18615 {
18616 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18617 li = li->li_next)
18618 {
18619 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18620 {
18621 if (*s == '\n')
18622 c = putc(NUL, fd);
18623 else
18624 c = putc(*s, fd);
18625 if (c == EOF)
18626 {
18627 ret = -1;
18628 break;
18629 }
18630 }
18631 if (!binary || li->li_next != NULL)
18632 if (putc('\n', fd) == EOF)
18633 {
18634 ret = -1;
18635 break;
18636 }
18637 if (ret < 0)
18638 {
18639 EMSG(_(e_write));
18640 break;
18641 }
18642 }
18643 fclose(fd);
18644 }
18645
18646 rettv->vval.v_number = ret;
18647}
18648
18649/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018650 * "xor(expr, expr)" function
18651 */
18652 static void
18653f_xor(argvars, rettv)
18654 typval_T *argvars;
18655 typval_T *rettv;
18656{
18657 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18658 ^ get_tv_number_chk(&argvars[1], NULL);
18659}
18660
18661
18662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018664 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 */
18666 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018667var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018669 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018670 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018672 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018674 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675
Bram Moolenaara5525202006-03-02 22:52:09 +000018676 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018677 if (varp->v_type == VAR_LIST)
18678 {
18679 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018680 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018681 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018682 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018683
18684 l = varp->vval.v_list;
18685 if (l == NULL)
18686 return NULL;
18687
18688 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018689 pos.lnum = list_find_nr(l, 0L, &error);
18690 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018691 return NULL; /* invalid line number */
18692
18693 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018694 pos.col = list_find_nr(l, 1L, &error);
18695 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018696 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018697 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018698
18699 /* We accept "$" for the column number: last column. */
18700 li = list_find(l, 1L);
18701 if (li != NULL && li->li_tv.v_type == VAR_STRING
18702 && li->li_tv.vval.v_string != NULL
18703 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18704 pos.col = len + 1;
18705
Bram Moolenaara5525202006-03-02 22:52:09 +000018706 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018707 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018708 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018709 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018710
Bram Moolenaara5525202006-03-02 22:52:09 +000018711#ifdef FEAT_VIRTUALEDIT
18712 /* Get the virtual offset. Defaults to zero. */
18713 pos.coladd = list_find_nr(l, 2L, &error);
18714 if (error)
18715 pos.coladd = 0;
18716#endif
18717
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018718 return &pos;
18719 }
18720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018721 name = get_tv_string_chk(varp);
18722 if (name == NULL)
18723 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018724 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018726#ifdef FEAT_VISUAL
18727 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18728 {
18729 if (VIsual_active)
18730 return &VIsual;
18731 return &curwin->w_cursor;
18732 }
18733#endif
18734 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018736 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18738 return NULL;
18739 return pp;
18740 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018741
18742#ifdef FEAT_VIRTUALEDIT
18743 pos.coladd = 0;
18744#endif
18745
Bram Moolenaar477933c2007-07-17 14:32:23 +000018746 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018747 {
18748 pos.col = 0;
18749 if (name[1] == '0') /* "w0": first visible line */
18750 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018751 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018752 pos.lnum = curwin->w_topline;
18753 return &pos;
18754 }
18755 else if (name[1] == '$') /* "w$": last visible line */
18756 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018757 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018758 pos.lnum = curwin->w_botline - 1;
18759 return &pos;
18760 }
18761 }
18762 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018764 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 {
18766 pos.lnum = curbuf->b_ml.ml_line_count;
18767 pos.col = 0;
18768 }
18769 else
18770 {
18771 pos.lnum = curwin->w_cursor.lnum;
18772 pos.col = (colnr_T)STRLEN(ml_get_curline());
18773 }
18774 return &pos;
18775 }
18776 return NULL;
18777}
18778
18779/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018780 * Convert list in "arg" into a position and optional file number.
18781 * When "fnump" is NULL there is no file number, only 3 items.
18782 * Note that the column is passed on as-is, the caller may want to decrement
18783 * it to use 1 for the first column.
18784 * Return FAIL when conversion is not possible, doesn't check the position for
18785 * validity.
18786 */
18787 static int
18788list2fpos(arg, posp, fnump)
18789 typval_T *arg;
18790 pos_T *posp;
18791 int *fnump;
18792{
18793 list_T *l = arg->vval.v_list;
18794 long i = 0;
18795 long n;
18796
Bram Moolenaarbde35262006-07-23 20:12:24 +000018797 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18798 * when "fnump" isn't NULL and "coladd" is optional. */
18799 if (arg->v_type != VAR_LIST
18800 || l == NULL
18801 || l->lv_len < (fnump == NULL ? 2 : 3)
18802 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018803 return FAIL;
18804
18805 if (fnump != NULL)
18806 {
18807 n = list_find_nr(l, i++, NULL); /* fnum */
18808 if (n < 0)
18809 return FAIL;
18810 if (n == 0)
18811 n = curbuf->b_fnum; /* current buffer */
18812 *fnump = n;
18813 }
18814
18815 n = list_find_nr(l, i++, NULL); /* lnum */
18816 if (n < 0)
18817 return FAIL;
18818 posp->lnum = n;
18819
18820 n = list_find_nr(l, i++, NULL); /* col */
18821 if (n < 0)
18822 return FAIL;
18823 posp->col = n;
18824
18825#ifdef FEAT_VIRTUALEDIT
18826 n = list_find_nr(l, i, NULL);
18827 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018828 posp->coladd = 0;
18829 else
18830 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018831#endif
18832
18833 return OK;
18834}
18835
18836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 * Get the length of an environment variable name.
18838 * Advance "arg" to the first character after the name.
18839 * Return 0 for error.
18840 */
18841 static int
18842get_env_len(arg)
18843 char_u **arg;
18844{
18845 char_u *p;
18846 int len;
18847
18848 for (p = *arg; vim_isIDc(*p); ++p)
18849 ;
18850 if (p == *arg) /* no name found */
18851 return 0;
18852
18853 len = (int)(p - *arg);
18854 *arg = p;
18855 return len;
18856}
18857
18858/*
18859 * Get the length of the name of a function or internal variable.
18860 * "arg" is advanced to the first non-white character after the name.
18861 * Return 0 if something is wrong.
18862 */
18863 static int
18864get_id_len(arg)
18865 char_u **arg;
18866{
18867 char_u *p;
18868 int len;
18869
18870 /* Find the end of the name. */
18871 for (p = *arg; eval_isnamec(*p); ++p)
18872 ;
18873 if (p == *arg) /* no name found */
18874 return 0;
18875
18876 len = (int)(p - *arg);
18877 *arg = skipwhite(p);
18878
18879 return len;
18880}
18881
18882/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018883 * Get the length of the name of a variable or function.
18884 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018886 * Return -1 if curly braces expansion failed.
18887 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 * If the name contains 'magic' {}'s, expand them and return the
18889 * expanded name in an allocated string via 'alias' - caller must free.
18890 */
18891 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018892get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 char_u **arg;
18894 char_u **alias;
18895 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018896 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897{
18898 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 char_u *p;
18900 char_u *expr_start;
18901 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902
18903 *alias = NULL; /* default to no alias */
18904
18905 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18906 && (*arg)[2] == (int)KE_SNR)
18907 {
18908 /* hard coded <SNR>, already translated */
18909 *arg += 3;
18910 return get_id_len(arg) + 3;
18911 }
18912 len = eval_fname_script(*arg);
18913 if (len > 0)
18914 {
18915 /* literal "<SID>", "s:" or "<SNR>" */
18916 *arg += len;
18917 }
18918
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018920 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018922 p = find_name_end(*arg, &expr_start, &expr_end,
18923 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 if (expr_start != NULL)
18925 {
18926 char_u *temp_string;
18927
18928 if (!evaluate)
18929 {
18930 len += (int)(p - *arg);
18931 *arg = skipwhite(p);
18932 return len;
18933 }
18934
18935 /*
18936 * Include any <SID> etc in the expanded string:
18937 * Thus the -len here.
18938 */
18939 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18940 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018941 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 *alias = temp_string;
18943 *arg = skipwhite(p);
18944 return (int)STRLEN(temp_string);
18945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946
18947 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018948 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 EMSG2(_(e_invexpr2), *arg);
18950
18951 return len;
18952}
18953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954/*
18955 * Find the end of a variable or function name, taking care of magic braces.
18956 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18957 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018958 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018959 * Return a pointer to just after the name. Equal to "arg" if there is no
18960 * valid name.
18961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018963find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 char_u *arg;
18965 char_u **expr_start;
18966 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018967 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969 int mb_nest = 0;
18970 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 char_u *p;
18972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018973 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975 *expr_start = NULL;
18976 *expr_end = NULL;
18977 }
18978
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018979 /* Quick check for valid starting character. */
18980 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18981 return arg;
18982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983 for (p = arg; *p != NUL
18984 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018985 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018986 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018988 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018990 if (*p == '\'')
18991 {
18992 /* skip over 'string' to avoid counting [ and ] inside it. */
18993 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18994 ;
18995 if (*p == NUL)
18996 break;
18997 }
18998 else if (*p == '"')
18999 {
19000 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19001 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19002 if (*p == '\\' && p[1] != NUL)
19003 ++p;
19004 if (*p == NUL)
19005 break;
19006 }
19007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019008 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019010 if (*p == '[')
19011 ++br_nest;
19012 else if (*p == ']')
19013 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019016 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019018 if (*p == '{')
19019 {
19020 mb_nest++;
19021 if (expr_start != NULL && *expr_start == NULL)
19022 *expr_start = p;
19023 }
19024 else if (*p == '}')
19025 {
19026 mb_nest--;
19027 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19028 *expr_end = p;
19029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 }
19032
19033 return p;
19034}
19035
19036/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019037 * Expands out the 'magic' {}'s in a variable/function name.
19038 * Note that this can call itself recursively, to deal with
19039 * constructs like foo{bar}{baz}{bam}
19040 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19041 * "in_start" ^
19042 * "expr_start" ^
19043 * "expr_end" ^
19044 * "in_end" ^
19045 *
19046 * Returns a new allocated string, which the caller must free.
19047 * Returns NULL for failure.
19048 */
19049 static char_u *
19050make_expanded_name(in_start, expr_start, expr_end, in_end)
19051 char_u *in_start;
19052 char_u *expr_start;
19053 char_u *expr_end;
19054 char_u *in_end;
19055{
19056 char_u c1;
19057 char_u *retval = NULL;
19058 char_u *temp_result;
19059 char_u *nextcmd = NULL;
19060
19061 if (expr_end == NULL || in_end == NULL)
19062 return NULL;
19063 *expr_start = NUL;
19064 *expr_end = NUL;
19065 c1 = *in_end;
19066 *in_end = NUL;
19067
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019068 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019069 if (temp_result != NULL && nextcmd == NULL)
19070 {
19071 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19072 + (in_end - expr_end) + 1));
19073 if (retval != NULL)
19074 {
19075 STRCPY(retval, in_start);
19076 STRCAT(retval, temp_result);
19077 STRCAT(retval, expr_end + 1);
19078 }
19079 }
19080 vim_free(temp_result);
19081
19082 *in_end = c1; /* put char back for error messages */
19083 *expr_start = '{';
19084 *expr_end = '}';
19085
19086 if (retval != NULL)
19087 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019088 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019089 if (expr_start != NULL)
19090 {
19091 /* Further expansion! */
19092 temp_result = make_expanded_name(retval, expr_start,
19093 expr_end, temp_result);
19094 vim_free(retval);
19095 retval = temp_result;
19096 }
19097 }
19098
19099 return retval;
19100}
19101
19102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019104 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 */
19106 static int
19107eval_isnamec(c)
19108 int c;
19109{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019110 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19111}
19112
19113/*
19114 * Return TRUE if character "c" can be used as the first character in a
19115 * variable or function name (excluding '{' and '}').
19116 */
19117 static int
19118eval_isnamec1(c)
19119 int c;
19120{
19121 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122}
19123
19124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 * Set number v: variable to "val".
19126 */
19127 void
19128set_vim_var_nr(idx, val)
19129 int idx;
19130 long val;
19131{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019132 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133}
19134
19135/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019136 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 */
19138 long
19139get_vim_var_nr(idx)
19140 int idx;
19141{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019142 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143}
19144
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019145/*
19146 * Get string v: variable value. Uses a static buffer, can only be used once.
19147 */
19148 char_u *
19149get_vim_var_str(idx)
19150 int idx;
19151{
19152 return get_tv_string(&vimvars[idx].vv_tv);
19153}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019154
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019156 * Get List v: variable value. Caller must take care of reference count when
19157 * needed.
19158 */
19159 list_T *
19160get_vim_var_list(idx)
19161 int idx;
19162{
19163 return vimvars[idx].vv_list;
19164}
19165
19166/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019167 * Set v:char to character "c".
19168 */
19169 void
19170set_vim_var_char(c)
19171 int c;
19172{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019173 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019174
19175#ifdef FEAT_MBYTE
19176 if (has_mbyte)
19177 buf[(*mb_char2bytes)(c, buf)] = NUL;
19178 else
19179#endif
19180 {
19181 buf[0] = c;
19182 buf[1] = NUL;
19183 }
19184 set_vim_var_string(VV_CHAR, buf, -1);
19185}
19186
19187/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019188 * Set v:count to "count" and v:count1 to "count1".
19189 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 */
19191 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019192set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 long count;
19194 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019195 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019197 if (set_prevcount)
19198 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019199 vimvars[VV_COUNT].vv_nr = count;
19200 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201}
19202
19203/*
19204 * Set string v: variable to a copy of "val".
19205 */
19206 void
19207set_vim_var_string(idx, val, len)
19208 int idx;
19209 char_u *val;
19210 int len; /* length of "val" to use or -1 (whole string) */
19211{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019212 /* Need to do this (at least) once, since we can't initialize a union.
19213 * Will always be invoked when "v:progname" is set. */
19214 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19215
Bram Moolenaare9a41262005-01-15 22:18:47 +000019216 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019218 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019220 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019222 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223}
19224
19225/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019226 * Set List v: variable to "val".
19227 */
19228 void
19229set_vim_var_list(idx, val)
19230 int idx;
19231 list_T *val;
19232{
19233 list_unref(vimvars[idx].vv_list);
19234 vimvars[idx].vv_list = val;
19235 if (val != NULL)
19236 ++val->lv_refcount;
19237}
19238
19239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 * Set v:register if needed.
19241 */
19242 void
19243set_reg_var(c)
19244 int c;
19245{
19246 char_u regname;
19247
19248 if (c == 0 || c == ' ')
19249 regname = '"';
19250 else
19251 regname = c;
19252 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019253 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 set_vim_var_string(VV_REG, &regname, 1);
19255}
19256
19257/*
19258 * Get or set v:exception. If "oldval" == NULL, return the current value.
19259 * Otherwise, restore the value to "oldval" and return NULL.
19260 * Must always be called in pairs to save and restore v:exception! Does not
19261 * take care of memory allocations.
19262 */
19263 char_u *
19264v_exception(oldval)
19265 char_u *oldval;
19266{
19267 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019268 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269
Bram Moolenaare9a41262005-01-15 22:18:47 +000019270 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 return NULL;
19272}
19273
19274/*
19275 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19276 * Otherwise, restore the value to "oldval" and return NULL.
19277 * Must always be called in pairs to save and restore v:throwpoint! Does not
19278 * take care of memory allocations.
19279 */
19280 char_u *
19281v_throwpoint(oldval)
19282 char_u *oldval;
19283{
19284 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019285 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286
Bram Moolenaare9a41262005-01-15 22:18:47 +000019287 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 return NULL;
19289}
19290
19291#if defined(FEAT_AUTOCMD) || defined(PROTO)
19292/*
19293 * Set v:cmdarg.
19294 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19295 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19296 * Must always be called in pairs!
19297 */
19298 char_u *
19299set_cmdarg(eap, oldarg)
19300 exarg_T *eap;
19301 char_u *oldarg;
19302{
19303 char_u *oldval;
19304 char_u *newval;
19305 unsigned len;
19306
Bram Moolenaare9a41262005-01-15 22:18:47 +000019307 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019308 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019310 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019311 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 }
19314
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019315 if (eap->force_bin == FORCE_BIN)
19316 len = 6;
19317 else if (eap->force_bin == FORCE_NOBIN)
19318 len = 8;
19319 else
19320 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019321
19322 if (eap->read_edit)
19323 len += 7;
19324
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019325 if (eap->force_ff != 0)
19326 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19327# ifdef FEAT_MBYTE
19328 if (eap->force_enc != 0)
19329 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019330 if (eap->bad_char != 0)
19331 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019332# endif
19333
19334 newval = alloc(len + 1);
19335 if (newval == NULL)
19336 return NULL;
19337
19338 if (eap->force_bin == FORCE_BIN)
19339 sprintf((char *)newval, " ++bin");
19340 else if (eap->force_bin == FORCE_NOBIN)
19341 sprintf((char *)newval, " ++nobin");
19342 else
19343 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019344
19345 if (eap->read_edit)
19346 STRCAT(newval, " ++edit");
19347
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019348 if (eap->force_ff != 0)
19349 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19350 eap->cmd + eap->force_ff);
19351# ifdef FEAT_MBYTE
19352 if (eap->force_enc != 0)
19353 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19354 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019355 if (eap->bad_char == BAD_KEEP)
19356 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19357 else if (eap->bad_char == BAD_DROP)
19358 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19359 else if (eap->bad_char != 0)
19360 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019361# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019362 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019363 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364}
19365#endif
19366
19367/*
19368 * Get the value of internal variable "name".
19369 * Return OK or FAIL.
19370 */
19371 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019372get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 char_u *name;
19374 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019376 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377{
19378 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019379 typval_T *tv = NULL;
19380 typval_T atv;
19381 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383
19384 /* truncate the name, so that we can use strcmp() */
19385 cc = name[len];
19386 name[len] = NUL;
19387
19388 /*
19389 * Check for "b:changedtick".
19390 */
19391 if (STRCMP(name, "b:changedtick") == 0)
19392 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019393 atv.v_type = VAR_NUMBER;
19394 atv.vval.v_number = curbuf->b_changedtick;
19395 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 }
19397
19398 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 * Check for user-defined variables.
19400 */
19401 else
19402 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019403 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 }
19407
Bram Moolenaare9a41262005-01-15 22:18:47 +000019408 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019410 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 ret = FAIL;
19413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019415 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416
19417 name[len] = cc;
19418
19419 return ret;
19420}
19421
19422/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019423 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19424 * Also handle function call with Funcref variable: func(expr)
19425 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19426 */
19427 static int
19428handle_subscript(arg, rettv, evaluate, verbose)
19429 char_u **arg;
19430 typval_T *rettv;
19431 int evaluate; /* do more than finding the end */
19432 int verbose; /* give error messages */
19433{
19434 int ret = OK;
19435 dict_T *selfdict = NULL;
19436 char_u *s;
19437 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019438 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019439
19440 while (ret == OK
19441 && (**arg == '['
19442 || (**arg == '.' && rettv->v_type == VAR_DICT)
19443 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19444 && !vim_iswhite(*(*arg - 1)))
19445 {
19446 if (**arg == '(')
19447 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019448 /* need to copy the funcref so that we can clear rettv */
19449 functv = *rettv;
19450 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019451
19452 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019453 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019454 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019455 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19456 &len, evaluate, selfdict);
19457
19458 /* Clear the funcref afterwards, so that deleting it while
19459 * evaluating the arguments is possible (see test55). */
19460 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019461
19462 /* Stop the expression evaluation when immediately aborting on
19463 * error, or when an interrupt occurred or an exception was thrown
19464 * but not caught. */
19465 if (aborting())
19466 {
19467 if (ret == OK)
19468 clear_tv(rettv);
19469 ret = FAIL;
19470 }
19471 dict_unref(selfdict);
19472 selfdict = NULL;
19473 }
19474 else /* **arg == '[' || **arg == '.' */
19475 {
19476 dict_unref(selfdict);
19477 if (rettv->v_type == VAR_DICT)
19478 {
19479 selfdict = rettv->vval.v_dict;
19480 if (selfdict != NULL)
19481 ++selfdict->dv_refcount;
19482 }
19483 else
19484 selfdict = NULL;
19485 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19486 {
19487 clear_tv(rettv);
19488 ret = FAIL;
19489 }
19490 }
19491 }
19492 dict_unref(selfdict);
19493 return ret;
19494}
19495
19496/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019497 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019498 * value).
19499 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019500 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019501alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019502{
Bram Moolenaar33570922005-01-25 22:26:29 +000019503 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019504}
19505
19506/*
19507 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 * The string "s" must have been allocated, it is consumed.
19509 * Return NULL for out of memory, the variable otherwise.
19510 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019511 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019512alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 char_u *s;
19514{
Bram Moolenaar33570922005-01-25 22:26:29 +000019515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019517 rettv = alloc_tv();
19518 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520 rettv->v_type = VAR_STRING;
19521 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 }
19523 else
19524 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019525 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526}
19527
19528/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019529 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019531 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019532free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019533 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534{
19535 if (varp != NULL)
19536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019537 switch (varp->v_type)
19538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019539 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019540 func_unref(varp->vval.v_string);
19541 /*FALLTHROUGH*/
19542 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019543 vim_free(varp->vval.v_string);
19544 break;
19545 case VAR_LIST:
19546 list_unref(varp->vval.v_list);
19547 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019548 case VAR_DICT:
19549 dict_unref(varp->vval.v_dict);
19550 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019551 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019552#ifdef FEAT_FLOAT
19553 case VAR_FLOAT:
19554#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019555 case VAR_UNKNOWN:
19556 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019557 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019558 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019559 break;
19560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 vim_free(varp);
19562 }
19563}
19564
19565/*
19566 * Free the memory for a variable value and set the value to NULL or 0.
19567 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019568 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019569clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019570 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571{
19572 if (varp != NULL)
19573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019574 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019576 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019577 func_unref(varp->vval.v_string);
19578 /*FALLTHROUGH*/
19579 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019580 vim_free(varp->vval.v_string);
19581 varp->vval.v_string = NULL;
19582 break;
19583 case VAR_LIST:
19584 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019585 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019586 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019587 case VAR_DICT:
19588 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019589 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019590 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019591 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019592 varp->vval.v_number = 0;
19593 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019594#ifdef FEAT_FLOAT
19595 case VAR_FLOAT:
19596 varp->vval.v_float = 0.0;
19597 break;
19598#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019599 case VAR_UNKNOWN:
19600 break;
19601 default:
19602 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019604 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 }
19606}
19607
19608/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 * Set the value of a variable to NULL without freeing items.
19610 */
19611 static void
19612init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614{
19615 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019617}
19618
19619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 * Get the number value of a variable.
19621 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019622 * For incompatible types, return 0.
19623 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19624 * caller of incompatible types: it sets *denote to TRUE if "denote"
19625 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 */
19627 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019631 int error = FALSE;
19632
19633 return get_tv_number_chk(varp, &error); /* return 0L on error */
19634}
19635
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019636 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019637get_tv_number_chk(varp, denote)
19638 typval_T *varp;
19639 int *denote;
19640{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019641 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019643 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019645 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019646 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019647#ifdef FEAT_FLOAT
19648 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019649 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019650 break;
19651#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019652 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019653 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019654 break;
19655 case VAR_STRING:
19656 if (varp->vval.v_string != NULL)
19657 vim_str2nr(varp->vval.v_string, NULL, NULL,
19658 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019659 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019660 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019661 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019662 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019663 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019664 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019665 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019666 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019667 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019670 if (denote == NULL) /* useful for values that must be unsigned */
19671 n = -1;
19672 else
19673 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019674 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675}
19676
19677/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019678 * Get the lnum from the first argument.
19679 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019680 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 */
19682 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685{
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 linenr_T lnum;
19688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019689 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 if (lnum == 0) /* no valid number, try using line() */
19691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 rettv.v_type = VAR_NUMBER;
19693 f_line(argvars, &rettv);
19694 lnum = rettv.vval.v_number;
19695 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 }
19697 return lnum;
19698}
19699
19700/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019701 * Get the lnum from the first argument.
19702 * Also accepts "$", then "buf" is used.
19703 * Returns 0 on error.
19704 */
19705 static linenr_T
19706get_tv_lnum_buf(argvars, buf)
19707 typval_T *argvars;
19708 buf_T *buf;
19709{
19710 if (argvars[0].v_type == VAR_STRING
19711 && argvars[0].vval.v_string != NULL
19712 && argvars[0].vval.v_string[0] == '$'
19713 && buf != NULL)
19714 return buf->b_ml.ml_line_count;
19715 return get_tv_number_chk(&argvars[0], NULL);
19716}
19717
19718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 * Get the string value of a variable.
19720 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019721 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19722 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 * If the String variable has never been set, return an empty string.
19724 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019725 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19726 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 */
19728 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731{
19732 static char_u mybuf[NUMBUFLEN];
19733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735}
19736
19737 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019740 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019742 char_u *res = get_tv_string_buf_chk(varp, buf);
19743
19744 return res != NULL ? res : (char_u *)"";
19745}
19746
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019747 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019748get_tv_string_chk(varp)
19749 typval_T *varp;
19750{
19751 static char_u mybuf[NUMBUFLEN];
19752
19753 return get_tv_string_buf_chk(varp, mybuf);
19754}
19755
19756 static char_u *
19757get_tv_string_buf_chk(varp, buf)
19758 typval_T *varp;
19759 char_u *buf;
19760{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019761 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019763 case VAR_NUMBER:
19764 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19765 return buf;
19766 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019767 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019768 break;
19769 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019770 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019771 break;
19772 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019773 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019774 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019775#ifdef FEAT_FLOAT
19776 case VAR_FLOAT:
19777 EMSG(_("E806: using Float as a String"));
19778 break;
19779#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019780 case VAR_STRING:
19781 if (varp->vval.v_string != NULL)
19782 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019783 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019788 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789}
19790
19791/*
19792 * Find variable "name" in the list of variables.
19793 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019795 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019798 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019799find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019801 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019804 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805
Bram Moolenaara7043832005-01-21 11:56:39 +000019806 ht = find_var_ht(name, &varname);
19807 if (htp != NULL)
19808 *htp = ht;
19809 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019811 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812}
19813
19814/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019816 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019819find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019821 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019822 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019823{
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 hashitem_T *hi;
19825
19826 if (*varname == NUL)
19827 {
19828 /* Must be something like "s:", otherwise "ht" would be NULL. */
19829 switch (varname[-2])
19830 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019831 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 case 'g': return &globvars_var;
19833 case 'v': return &vimvars_var;
19834 case 'b': return &curbuf->b_bufvar;
19835 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019836#ifdef FEAT_WINDOWS
19837 case 't': return &curtab->tp_winvar;
19838#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019839 case 'l': return current_funccal == NULL
19840 ? NULL : &current_funccal->l_vars_var;
19841 case 'a': return current_funccal == NULL
19842 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019843 }
19844 return NULL;
19845 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019846
19847 hi = hash_find(ht, varname);
19848 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019849 {
19850 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019851 * worked find the variable again. Don't auto-load a script if it was
19852 * loaded already, otherwise it would be loaded every time when
19853 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019854 if (ht == &globvarht && !writing)
19855 {
19856 /* Note: script_autoload() may make "hi" invalid. It must either
19857 * be obtained again or not used. */
19858 if (!script_autoload(varname, FALSE) || aborting())
19859 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019860 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019861 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019862 if (HASHITEM_EMPTY(hi))
19863 return NULL;
19864 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019866}
19867
19868/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019869 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019870 * Set "varname" to the start of name without ':'.
19871 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019873find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 char_u *name;
19875 char_u **varname;
19876{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019877 hashitem_T *hi;
19878
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 if (name[1] != ':')
19880 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019881 /* The name must not start with a colon or #. */
19882 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 return NULL;
19884 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019885
19886 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019887 hi = hash_find(&compat_hashtab, name);
19888 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019889 return &compat_hashtab;
19890
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 return &globvarht; /* global variable */
19893 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 }
19895 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019896 if (*name == 'g') /* global variable */
19897 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019898 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19899 */
19900 if (vim_strchr(name + 2, ':') != NULL
19901 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019902 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019906 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019907#ifdef FEAT_WINDOWS
19908 if (*name == 't') /* tab page variable */
19909 return &curtab->tp_vars.dv_hashtab;
19910#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 if (*name == 'v') /* v: variable */
19912 return &vimvarht;
19913 if (*name == 'a' && current_funccal != NULL) /* function argument */
19914 return &current_funccal->l_avars.dv_hashtab;
19915 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19916 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 if (*name == 's' /* script variable */
19918 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19919 return &SCRIPT_VARS(current_SID);
19920 return NULL;
19921}
19922
19923/*
19924 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019925 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 * Returns NULL when it doesn't exist.
19927 */
19928 char_u *
19929get_var_value(name)
19930 char_u *name;
19931{
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933
Bram Moolenaara7043832005-01-21 11:56:39 +000019934 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 if (v == NULL)
19936 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019937 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938}
19939
19940/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 * sourcing this script and when executing functions defined in the script.
19943 */
19944 void
19945new_script_vars(id)
19946 scid_T id;
19947{
Bram Moolenaara7043832005-01-21 11:56:39 +000019948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 hashtab_T *ht;
19950 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019951
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19953 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019954 /* Re-allocating ga_data means that an ht_array pointing to
19955 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019957 for (i = 1; i <= ga_scripts.ga_len; ++i)
19958 {
19959 ht = &SCRIPT_VARS(i);
19960 if (ht->ht_mask == HT_INIT_SIZE - 1)
19961 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019962 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019963 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019964 }
19965
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 while (ga_scripts.ga_len < id)
19967 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019968 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019969 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019970 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 }
19973 }
19974}
19975
19976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019977 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19978 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 */
19980 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019981init_var_dict(dict, dict_var)
19982 dict_T *dict;
19983 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984{
Bram Moolenaar33570922005-01-25 22:26:29 +000019985 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019986 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019987 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 dict_var->di_tv.vval.v_dict = dict;
19989 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019990 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019991 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19992 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993}
19994
19995/*
19996 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 * Frees all allocated variables and the value they contain.
19998 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 */
20000 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020001vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020002 hashtab_T *ht;
20003{
20004 vars_clear_ext(ht, TRUE);
20005}
20006
20007/*
20008 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20009 */
20010 static void
20011vars_clear_ext(ht, free_val)
20012 hashtab_T *ht;
20013 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014{
Bram Moolenaara7043832005-01-21 11:56:39 +000020015 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 hashitem_T *hi;
20017 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020020 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020021 for (hi = ht->ht_array; todo > 0; ++hi)
20022 {
20023 if (!HASHITEM_EMPTY(hi))
20024 {
20025 --todo;
20026
Bram Moolenaar33570922005-01-25 22:26:29 +000020027 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020028 * ht_array might change then. hash_clear() takes care of it
20029 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020030 v = HI2DI(hi);
20031 if (free_val)
20032 clear_tv(&v->di_tv);
20033 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20034 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020035 }
20036 }
20037 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020038 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039}
20040
Bram Moolenaara7043832005-01-21 11:56:39 +000020041/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 * Delete a variable from hashtab "ht" at item "hi".
20043 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020046delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 hashtab_T *ht;
20048 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049{
Bram Moolenaar33570922005-01-25 22:26:29 +000020050 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020051
20052 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 clear_tv(&di->di_tv);
20054 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055}
20056
20057/*
20058 * List the value of one internal variable.
20059 */
20060 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020061list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020064 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020066 char_u *tofree;
20067 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020068 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020069
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020070 current_copyID += COPYID_INC;
20071 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020073 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020074 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075}
20076
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020078list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 char_u *prefix;
20080 char_u *name;
20081 int type;
20082 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020083 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084{
Bram Moolenaar31859182007-08-14 20:41:13 +000020085 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20086 msg_start();
20087 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 if (name != NULL) /* "a:" vars don't have a name stored */
20089 msg_puts(name);
20090 msg_putchar(' ');
20091 msg_advance(22);
20092 if (type == VAR_NUMBER)
20093 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 else if (type == VAR_FUNC)
20095 msg_putchar('*');
20096 else if (type == VAR_LIST)
20097 {
20098 msg_putchar('[');
20099 if (*string == '[')
20100 ++string;
20101 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020102 else if (type == VAR_DICT)
20103 {
20104 msg_putchar('{');
20105 if (*string == '{')
20106 ++string;
20107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 else
20109 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020110
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020112
20113 if (type == VAR_FUNC)
20114 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020115 if (*first)
20116 {
20117 msg_clr_eos();
20118 *first = FALSE;
20119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120}
20121
20122/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020123 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 * If the variable already exists, the value is updated.
20125 * Otherwise the variable is created.
20126 */
20127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020128set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020130 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132{
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020135 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020137 ht = find_var_ht(name, &varname);
20138 if (ht == NULL || *varname == NUL)
20139 {
20140 EMSG2(_(e_illvar), name);
20141 return;
20142 }
20143 v = find_var_in_ht(ht, varname, TRUE);
20144
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020145 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20146 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020147
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020151 if (var_check_ro(v->di_flags, name)
20152 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 return;
20154 if (v->di_tv.v_type != tv->v_type
20155 && !((v->di_tv.v_type == VAR_STRING
20156 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020157 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020158 || tv->v_type == VAR_NUMBER))
20159#ifdef FEAT_FLOAT
20160 && !((v->di_tv.v_type == VAR_NUMBER
20161 || v->di_tv.v_type == VAR_FLOAT)
20162 && (tv->v_type == VAR_NUMBER
20163 || tv->v_type == VAR_FLOAT))
20164#endif
20165 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020166 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020167 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020168 return;
20169 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020170
20171 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020172 * Handle setting internal v: variables separately: we don't change
20173 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020174 */
20175 if (ht == &vimvarht)
20176 {
20177 if (v->di_tv.v_type == VAR_STRING)
20178 {
20179 vim_free(v->di_tv.vval.v_string);
20180 if (copy || tv->v_type != VAR_STRING)
20181 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20182 else
20183 {
20184 /* Take over the string to avoid an extra alloc/free. */
20185 v->di_tv.vval.v_string = tv->vval.v_string;
20186 tv->vval.v_string = NULL;
20187 }
20188 }
20189 else if (v->di_tv.v_type != VAR_NUMBER)
20190 EMSG2(_(e_intern2), "set_var()");
20191 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020192 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020194 if (STRCMP(varname, "searchforward") == 0)
20195 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20196 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020197 return;
20198 }
20199
20200 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 }
20202 else /* add a new variable */
20203 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020204 /* Can't add "v:" variable. */
20205 if (ht == &vimvarht)
20206 {
20207 EMSG2(_(e_illvar), name);
20208 return;
20209 }
20210
Bram Moolenaar92124a32005-06-17 22:03:40 +000020211 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020212 if (!valid_varname(varname))
20213 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020214
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020215 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20216 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020217 if (v == NULL)
20218 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020219 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020222 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 return;
20224 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020225 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020227
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020228 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020230 else
20231 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020233 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236}
20237
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020238/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020239 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 * Also give an error message.
20241 */
20242 static int
20243var_check_ro(flags, name)
20244 int flags;
20245 char_u *name;
20246{
20247 if (flags & DI_FLAGS_RO)
20248 {
20249 EMSG2(_(e_readonlyvar), name);
20250 return TRUE;
20251 }
20252 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20253 {
20254 EMSG2(_(e_readonlysbx), name);
20255 return TRUE;
20256 }
20257 return FALSE;
20258}
20259
20260/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020261 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20262 * Also give an error message.
20263 */
20264 static int
20265var_check_fixed(flags, name)
20266 int flags;
20267 char_u *name;
20268{
20269 if (flags & DI_FLAGS_FIX)
20270 {
20271 EMSG2(_("E795: Cannot delete variable %s"), name);
20272 return TRUE;
20273 }
20274 return FALSE;
20275}
20276
20277/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020278 * Check if a funcref is assigned to a valid variable name.
20279 * Return TRUE and give an error if not.
20280 */
20281 static int
20282var_check_func_name(name, new_var)
20283 char_u *name; /* points to start of variable name */
20284 int new_var; /* TRUE when creating the variable */
20285{
20286 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20287 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20288 ? name[2] : name[0]))
20289 {
20290 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20291 name);
20292 return TRUE;
20293 }
20294 /* Don't allow hiding a function. When "v" is not NULL we might be
20295 * assigning another function to the same var, the type is checked
20296 * below. */
20297 if (new_var && function_exists(name))
20298 {
20299 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20300 name);
20301 return TRUE;
20302 }
20303 return FALSE;
20304}
20305
20306/*
20307 * Check if a variable name is valid.
20308 * Return FALSE and give an error if not.
20309 */
20310 static int
20311valid_varname(varname)
20312 char_u *varname;
20313{
20314 char_u *p;
20315
20316 for (p = varname; *p != NUL; ++p)
20317 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20318 && *p != AUTOLOAD_CHAR)
20319 {
20320 EMSG2(_(e_illvar), varname);
20321 return FALSE;
20322 }
20323 return TRUE;
20324}
20325
20326/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020327 * Return TRUE if typeval "tv" is set to be locked (immutable).
20328 * Also give an error message, using "name".
20329 */
20330 static int
20331tv_check_lock(lock, name)
20332 int lock;
20333 char_u *name;
20334{
20335 if (lock & VAR_LOCKED)
20336 {
20337 EMSG2(_("E741: Value is locked: %s"),
20338 name == NULL ? (char_u *)_("Unknown") : name);
20339 return TRUE;
20340 }
20341 if (lock & VAR_FIXED)
20342 {
20343 EMSG2(_("E742: Cannot change value of %s"),
20344 name == NULL ? (char_u *)_("Unknown") : name);
20345 return TRUE;
20346 }
20347 return FALSE;
20348}
20349
20350/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020352 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020353 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020354 * It is OK for "from" and "to" to point to the same item. This is used to
20355 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020356 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020357 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020359 typval_T *from;
20360 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020362 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020363 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020364 switch (from->v_type)
20365 {
20366 case VAR_NUMBER:
20367 to->vval.v_number = from->vval.v_number;
20368 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020369#ifdef FEAT_FLOAT
20370 case VAR_FLOAT:
20371 to->vval.v_float = from->vval.v_float;
20372 break;
20373#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020374 case VAR_STRING:
20375 case VAR_FUNC:
20376 if (from->vval.v_string == NULL)
20377 to->vval.v_string = NULL;
20378 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020380 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020381 if (from->v_type == VAR_FUNC)
20382 func_ref(to->vval.v_string);
20383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020384 break;
20385 case VAR_LIST:
20386 if (from->vval.v_list == NULL)
20387 to->vval.v_list = NULL;
20388 else
20389 {
20390 to->vval.v_list = from->vval.v_list;
20391 ++to->vval.v_list->lv_refcount;
20392 }
20393 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020394 case VAR_DICT:
20395 if (from->vval.v_dict == NULL)
20396 to->vval.v_dict = NULL;
20397 else
20398 {
20399 to->vval.v_dict = from->vval.v_dict;
20400 ++to->vval.v_dict->dv_refcount;
20401 }
20402 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020403 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020405 break;
20406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407}
20408
20409/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020410 * Make a copy of an item.
20411 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020412 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20413 * reference to an already copied list/dict can be used.
20414 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020415 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020416 static int
20417item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 typval_T *from;
20419 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020420 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020421 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020422{
20423 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020424 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020425
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020427 {
20428 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020429 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020430 }
20431 ++recurse;
20432
20433 switch (from->v_type)
20434 {
20435 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020436#ifdef FEAT_FLOAT
20437 case VAR_FLOAT:
20438#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020439 case VAR_STRING:
20440 case VAR_FUNC:
20441 copy_tv(from, to);
20442 break;
20443 case VAR_LIST:
20444 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020445 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020446 if (from->vval.v_list == NULL)
20447 to->vval.v_list = NULL;
20448 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20449 {
20450 /* use the copy made earlier */
20451 to->vval.v_list = from->vval.v_list->lv_copylist;
20452 ++to->vval.v_list->lv_refcount;
20453 }
20454 else
20455 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20456 if (to->vval.v_list == NULL)
20457 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020458 break;
20459 case VAR_DICT:
20460 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020461 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020462 if (from->vval.v_dict == NULL)
20463 to->vval.v_dict = NULL;
20464 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20465 {
20466 /* use the copy made earlier */
20467 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20468 ++to->vval.v_dict->dv_refcount;
20469 }
20470 else
20471 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20472 if (to->vval.v_dict == NULL)
20473 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020474 break;
20475 default:
20476 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020477 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020478 }
20479 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020480 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020481}
20482
20483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 * ":echo expr1 ..." print each argument separated with a space, add a
20485 * newline at the end.
20486 * ":echon expr1 ..." print each argument plain.
20487 */
20488 void
20489ex_echo(eap)
20490 exarg_T *eap;
20491{
20492 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 char_u *p;
20496 int needclr = TRUE;
20497 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020498 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499
20500 if (eap->skip)
20501 ++emsg_skip;
20502 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020504 /* If eval1() causes an error message the text from the command may
20505 * still need to be cleared. E.g., "echo 22,44". */
20506 need_clr_eos = needclr;
20507
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020509 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
20511 /*
20512 * Report the invalid expression unless the expression evaluation
20513 * has been cancelled due to an aborting error, an interrupt, or an
20514 * exception.
20515 */
20516 if (!aborting())
20517 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020518 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 break;
20520 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020521 need_clr_eos = FALSE;
20522
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 if (!eap->skip)
20524 {
20525 if (atstart)
20526 {
20527 atstart = FALSE;
20528 /* Call msg_start() after eval1(), evaluating the expression
20529 * may cause a message to appear. */
20530 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020531 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020532 /* Mark the saved text as finishing the line, so that what
20533 * follows is displayed on a new line when scrolling back
20534 * at the more prompt. */
20535 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 }
20539 else if (eap->cmdidx == CMD_echo)
20540 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020541 current_copyID += COPYID_INC;
20542 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020543 if (p != NULL)
20544 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020546 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020548 if (*p != TAB && needclr)
20549 {
20550 /* remove any text still there from the command */
20551 msg_clr_eos();
20552 needclr = FALSE;
20553 }
20554 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 }
20556 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020557 {
20558#ifdef FEAT_MBYTE
20559 if (has_mbyte)
20560 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020561 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020562
20563 (void)msg_outtrans_len_attr(p, i, echo_attr);
20564 p += i - 1;
20565 }
20566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020568 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020573 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 arg = skipwhite(arg);
20575 }
20576 eap->nextcmd = check_nextcmd(arg);
20577
20578 if (eap->skip)
20579 --emsg_skip;
20580 else
20581 {
20582 /* remove text that may still be there from the command */
20583 if (needclr)
20584 msg_clr_eos();
20585 if (eap->cmdidx == CMD_echo)
20586 msg_end();
20587 }
20588}
20589
20590/*
20591 * ":echohl {name}".
20592 */
20593 void
20594ex_echohl(eap)
20595 exarg_T *eap;
20596{
20597 int id;
20598
20599 id = syn_name2id(eap->arg);
20600 if (id == 0)
20601 echo_attr = 0;
20602 else
20603 echo_attr = syn_id2attr(id);
20604}
20605
20606/*
20607 * ":execute expr1 ..." execute the result of an expression.
20608 * ":echomsg expr1 ..." Print a message
20609 * ":echoerr expr1 ..." Print an error
20610 * Each gets spaces around each argument and a newline at the end for
20611 * echo commands
20612 */
20613 void
20614ex_execute(eap)
20615 exarg_T *eap;
20616{
20617 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020618 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 int ret = OK;
20620 char_u *p;
20621 garray_T ga;
20622 int len;
20623 int save_did_emsg;
20624
20625 ga_init2(&ga, 1, 80);
20626
20627 if (eap->skip)
20628 ++emsg_skip;
20629 while (*arg != NUL && *arg != '|' && *arg != '\n')
20630 {
20631 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 {
20634 /*
20635 * Report the invalid expression unless the expression evaluation
20636 * has been cancelled due to an aborting error, an interrupt, or an
20637 * exception.
20638 */
20639 if (!aborting())
20640 EMSG2(_(e_invexpr2), p);
20641 ret = FAIL;
20642 break;
20643 }
20644
20645 if (!eap->skip)
20646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020647 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 len = (int)STRLEN(p);
20649 if (ga_grow(&ga, len + 2) == FAIL)
20650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020651 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 ret = FAIL;
20653 break;
20654 }
20655 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 ga.ga_len += len;
20659 }
20660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 arg = skipwhite(arg);
20663 }
20664
20665 if (ret != FAIL && ga.ga_data != NULL)
20666 {
20667 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020670 out_flush();
20671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 else if (eap->cmdidx == CMD_echoerr)
20673 {
20674 /* We don't want to abort following commands, restore did_emsg. */
20675 save_did_emsg = did_emsg;
20676 EMSG((char_u *)ga.ga_data);
20677 if (!force_abort)
20678 did_emsg = save_did_emsg;
20679 }
20680 else if (eap->cmdidx == CMD_execute)
20681 do_cmdline((char_u *)ga.ga_data,
20682 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20683 }
20684
20685 ga_clear(&ga);
20686
20687 if (eap->skip)
20688 --emsg_skip;
20689
20690 eap->nextcmd = check_nextcmd(arg);
20691}
20692
20693/*
20694 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20695 * "arg" points to the "&" or '+' when called, to "option" when returning.
20696 * Returns NULL when no option name found. Otherwise pointer to the char
20697 * after the option name.
20698 */
20699 static char_u *
20700find_option_end(arg, opt_flags)
20701 char_u **arg;
20702 int *opt_flags;
20703{
20704 char_u *p = *arg;
20705
20706 ++p;
20707 if (*p == 'g' && p[1] == ':')
20708 {
20709 *opt_flags = OPT_GLOBAL;
20710 p += 2;
20711 }
20712 else if (*p == 'l' && p[1] == ':')
20713 {
20714 *opt_flags = OPT_LOCAL;
20715 p += 2;
20716 }
20717 else
20718 *opt_flags = 0;
20719
20720 if (!ASCII_ISALPHA(*p))
20721 return NULL;
20722 *arg = p;
20723
20724 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20725 p += 4; /* termcap option */
20726 else
20727 while (ASCII_ISALPHA(*p))
20728 ++p;
20729 return p;
20730}
20731
20732/*
20733 * ":function"
20734 */
20735 void
20736ex_function(eap)
20737 exarg_T *eap;
20738{
20739 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020740 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 int j;
20742 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 char_u *name = NULL;
20745 char_u *p;
20746 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020747 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 garray_T newargs;
20749 garray_T newlines;
20750 int varargs = FALSE;
20751 int mustend = FALSE;
20752 int flags = 0;
20753 ufunc_T *fp;
20754 int indent;
20755 int nesting;
20756 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020757 dictitem_T *v;
20758 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020759 static int func_nr = 0; /* number for nameless function */
20760 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020761 hashtab_T *ht;
20762 int todo;
20763 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020764 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765
20766 /*
20767 * ":function" without argument: list functions.
20768 */
20769 if (ends_excmd(*eap->arg))
20770 {
20771 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020772 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020773 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020774 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020775 {
20776 if (!HASHITEM_EMPTY(hi))
20777 {
20778 --todo;
20779 fp = HI2UF(hi);
20780 if (!isdigit(*fp->uf_name))
20781 list_func_head(fp, FALSE);
20782 }
20783 }
20784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 eap->nextcmd = check_nextcmd(eap->arg);
20786 return;
20787 }
20788
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020789 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020790 * ":function /pat": list functions matching pattern.
20791 */
20792 if (*eap->arg == '/')
20793 {
20794 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20795 if (!eap->skip)
20796 {
20797 regmatch_T regmatch;
20798
20799 c = *p;
20800 *p = NUL;
20801 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20802 *p = c;
20803 if (regmatch.regprog != NULL)
20804 {
20805 regmatch.rm_ic = p_ic;
20806
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020807 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020808 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20809 {
20810 if (!HASHITEM_EMPTY(hi))
20811 {
20812 --todo;
20813 fp = HI2UF(hi);
20814 if (!isdigit(*fp->uf_name)
20815 && vim_regexec(&regmatch, fp->uf_name, 0))
20816 list_func_head(fp, FALSE);
20817 }
20818 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020819 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020820 }
20821 }
20822 if (*p == '/')
20823 ++p;
20824 eap->nextcmd = check_nextcmd(p);
20825 return;
20826 }
20827
20828 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020829 * Get the function name. There are these situations:
20830 * func normal function name
20831 * "name" == func, "fudi.fd_dict" == NULL
20832 * dict.func new dictionary entry
20833 * "name" == NULL, "fudi.fd_dict" set,
20834 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20835 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020836 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020837 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20838 * dict.func existing dict entry that's not a Funcref
20839 * "name" == NULL, "fudi.fd_dict" set,
20840 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 name = trans_function_name(&p, eap->skip, 0, &fudi);
20844 paren = (vim_strchr(p, '(') != NULL);
20845 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 {
20847 /*
20848 * Return on an invalid expression in braces, unless the expression
20849 * evaluation has been cancelled due to an aborting error, an
20850 * interrupt, or an exception.
20851 */
20852 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020854 if (!eap->skip && fudi.fd_newkey != NULL)
20855 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020856 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 else
20860 eap->skip = TRUE;
20861 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020862
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 /* An error in a function call during evaluation of an expression in magic
20864 * braces should not cause the function not to be defined. */
20865 saved_did_emsg = did_emsg;
20866 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867
20868 /*
20869 * ":function func" with only function name: list function.
20870 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020871 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 {
20873 if (!ends_excmd(*skipwhite(p)))
20874 {
20875 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020876 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 }
20878 eap->nextcmd = check_nextcmd(p);
20879 if (eap->nextcmd != NULL)
20880 *p = NUL;
20881 if (!eap->skip && !got_int)
20882 {
20883 fp = find_func(name);
20884 if (fp != NULL)
20885 {
20886 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020887 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020889 if (FUNCLINE(fp, j) == NULL)
20890 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 msg_putchar('\n');
20892 msg_outnum((long)(j + 1));
20893 if (j < 9)
20894 msg_putchar(' ');
20895 if (j < 99)
20896 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020897 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 out_flush(); /* show a line at a time */
20899 ui_breakcheck();
20900 }
20901 if (!got_int)
20902 {
20903 msg_putchar('\n');
20904 msg_puts((char_u *)" endfunction");
20905 }
20906 }
20907 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020908 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020910 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 }
20912
20913 /*
20914 * ":function name(arg1, arg2)" Define function.
20915 */
20916 p = skipwhite(p);
20917 if (*p != '(')
20918 {
20919 if (!eap->skip)
20920 {
20921 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020922 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 }
20924 /* attempt to continue by skipping some text */
20925 if (vim_strchr(p, '(') != NULL)
20926 p = vim_strchr(p, '(');
20927 }
20928 p = skipwhite(p + 1);
20929
20930 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20931 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20932
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020933 if (!eap->skip)
20934 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020935 /* Check the name of the function. Unless it's a dictionary function
20936 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020937 if (name != NULL)
20938 arg = name;
20939 else
20940 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020941 if (arg != NULL && (fudi.fd_di == NULL
20942 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020943 {
20944 if (*arg == K_SPECIAL)
20945 j = 3;
20946 else
20947 j = 0;
20948 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20949 : eval_isnamec(arg[j])))
20950 ++j;
20951 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020952 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020953 }
20954 }
20955
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 /*
20957 * Isolate the arguments: "arg1, arg2, ...)"
20958 */
20959 while (*p != ')')
20960 {
20961 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20962 {
20963 varargs = TRUE;
20964 p += 3;
20965 mustend = TRUE;
20966 }
20967 else
20968 {
20969 arg = p;
20970 while (ASCII_ISALNUM(*p) || *p == '_')
20971 ++p;
20972 if (arg == p || isdigit(*arg)
20973 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20974 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20975 {
20976 if (!eap->skip)
20977 EMSG2(_("E125: Illegal argument: %s"), arg);
20978 break;
20979 }
20980 if (ga_grow(&newargs, 1) == FAIL)
20981 goto erret;
20982 c = *p;
20983 *p = NUL;
20984 arg = vim_strsave(arg);
20985 if (arg == NULL)
20986 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020987
20988 /* Check for duplicate argument name. */
20989 for (i = 0; i < newargs.ga_len; ++i)
20990 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20991 {
20992 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20993 goto erret;
20994 }
20995
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20997 *p = c;
20998 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 if (*p == ',')
21000 ++p;
21001 else
21002 mustend = TRUE;
21003 }
21004 p = skipwhite(p);
21005 if (mustend && *p != ')')
21006 {
21007 if (!eap->skip)
21008 EMSG2(_(e_invarg2), eap->arg);
21009 break;
21010 }
21011 }
21012 ++p; /* skip the ')' */
21013
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 for (;;)
21016 {
21017 p = skipwhite(p);
21018 if (STRNCMP(p, "range", 5) == 0)
21019 {
21020 flags |= FC_RANGE;
21021 p += 5;
21022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021023 else if (STRNCMP(p, "dict", 4) == 0)
21024 {
21025 flags |= FC_DICT;
21026 p += 4;
21027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 else if (STRNCMP(p, "abort", 5) == 0)
21029 {
21030 flags |= FC_ABORT;
21031 p += 5;
21032 }
21033 else
21034 break;
21035 }
21036
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021037 /* When there is a line break use what follows for the function body.
21038 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21039 if (*p == '\n')
21040 line_arg = p + 1;
21041 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 EMSG(_(e_trailing));
21043
21044 /*
21045 * Read the body of the function, until ":endfunction" is found.
21046 */
21047 if (KeyTyped)
21048 {
21049 /* Check if the function already exists, don't let the user type the
21050 * whole function before telling him it doesn't work! For a script we
21051 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021052 if (!eap->skip && !eap->forceit)
21053 {
21054 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21055 EMSG(_(e_funcdict));
21056 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021057 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021060 if (!eap->skip && did_emsg)
21061 goto erret;
21062
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 msg_putchar('\n'); /* don't overwrite the function name */
21064 cmdline_row = msg_row;
21065 }
21066
21067 indent = 2;
21068 nesting = 0;
21069 for (;;)
21070 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021071 if (KeyTyped)
21072 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021074 sourcing_lnum_off = sourcing_lnum;
21075
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021076 if (line_arg != NULL)
21077 {
21078 /* Use eap->arg, split up in parts by line breaks. */
21079 theline = line_arg;
21080 p = vim_strchr(theline, '\n');
21081 if (p == NULL)
21082 line_arg += STRLEN(line_arg);
21083 else
21084 {
21085 *p = NUL;
21086 line_arg = p + 1;
21087 }
21088 }
21089 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 theline = getcmdline(':', 0L, indent);
21091 else
21092 theline = eap->getline(':', eap->cookie, indent);
21093 if (KeyTyped)
21094 lines_left = Rows - 1;
21095 if (theline == NULL)
21096 {
21097 EMSG(_("E126: Missing :endfunction"));
21098 goto erret;
21099 }
21100
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021101 /* Detect line continuation: sourcing_lnum increased more than one. */
21102 if (sourcing_lnum > sourcing_lnum_off + 1)
21103 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21104 else
21105 sourcing_lnum_off = 0;
21106
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 if (skip_until != NULL)
21108 {
21109 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21110 * don't check for ":endfunc". */
21111 if (STRCMP(theline, skip_until) == 0)
21112 {
21113 vim_free(skip_until);
21114 skip_until = NULL;
21115 }
21116 }
21117 else
21118 {
21119 /* skip ':' and blanks*/
21120 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21121 ;
21122
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021123 /* Check for "endfunction". */
21124 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021126 if (line_arg == NULL)
21127 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 break;
21129 }
21130
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021131 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 * at "end". */
21133 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21134 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021135 else if (STRNCMP(p, "if", 2) == 0
21136 || STRNCMP(p, "wh", 2) == 0
21137 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 || STRNCMP(p, "try", 3) == 0)
21139 indent += 2;
21140
21141 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021142 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021144 if (*p == '!')
21145 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 p += eval_fname_script(p);
21147 if (ASCII_ISALPHA(*p))
21148 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021149 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 if (*skipwhite(p) == '(')
21151 {
21152 ++nesting;
21153 indent += 2;
21154 }
21155 }
21156 }
21157
21158 /* Check for ":append" or ":insert". */
21159 p = skip_range(p, NULL);
21160 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21161 || (p[0] == 'i'
21162 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21163 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21164 skip_until = vim_strsave((char_u *)".");
21165
21166 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21167 arg = skipwhite(skiptowhite(p));
21168 if (arg[0] == '<' && arg[1] =='<'
21169 && ((p[0] == 'p' && p[1] == 'y'
21170 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21171 || (p[0] == 'p' && p[1] == 'e'
21172 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21173 || (p[0] == 't' && p[1] == 'c'
21174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021175 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21176 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21178 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021179 || (p[0] == 'm' && p[1] == 'z'
21180 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 ))
21182 {
21183 /* ":python <<" continues until a dot, like ":append" */
21184 p = skipwhite(arg + 2);
21185 if (*p == NUL)
21186 skip_until = vim_strsave((char_u *)".");
21187 else
21188 skip_until = vim_strsave(p);
21189 }
21190 }
21191
21192 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021193 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021194 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021195 if (line_arg == NULL)
21196 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021198 }
21199
21200 /* Copy the line to newly allocated memory. get_one_sourceline()
21201 * allocates 250 bytes per line, this saves 80% on average. The cost
21202 * is an extra alloc/free. */
21203 p = vim_strsave(theline);
21204 if (p != NULL)
21205 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021206 if (line_arg == NULL)
21207 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021208 theline = p;
21209 }
21210
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021211 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21212
21213 /* Add NULL lines for continuation lines, so that the line count is
21214 * equal to the index in the growarray. */
21215 while (sourcing_lnum_off-- > 0)
21216 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021217
21218 /* Check for end of eap->arg. */
21219 if (line_arg != NULL && *line_arg == NUL)
21220 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 }
21222
21223 /* Don't define the function when skipping commands or when an error was
21224 * detected. */
21225 if (eap->skip || did_emsg)
21226 goto erret;
21227
21228 /*
21229 * If there are no errors, add the function
21230 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021231 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021232 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021233 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021234 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021235 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021236 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021237 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 goto erret;
21239 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021241 fp = find_func(name);
21242 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244 if (!eap->forceit)
21245 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021246 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021247 goto erret;
21248 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021249 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021250 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021251 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021252 name);
21253 goto erret;
21254 }
21255 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021256 ga_clear_strings(&(fp->uf_args));
21257 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021258 vim_free(name);
21259 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 }
21262 else
21263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021264 char numbuf[20];
21265
21266 fp = NULL;
21267 if (fudi.fd_newkey == NULL && !eap->forceit)
21268 {
21269 EMSG(_(e_funcdict));
21270 goto erret;
21271 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021272 if (fudi.fd_di == NULL)
21273 {
21274 /* Can't add a function to a locked dictionary */
21275 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21276 goto erret;
21277 }
21278 /* Can't change an existing function if it is locked */
21279 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21280 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021281
21282 /* Give the function a sequential number. Can only be used with a
21283 * Funcref! */
21284 vim_free(name);
21285 sprintf(numbuf, "%d", ++func_nr);
21286 name = vim_strsave((char_u *)numbuf);
21287 if (name == NULL)
21288 goto erret;
21289 }
21290
21291 if (fp == NULL)
21292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021293 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021294 {
21295 int slen, plen;
21296 char_u *scriptname;
21297
21298 /* Check that the autoload name matches the script name. */
21299 j = FAIL;
21300 if (sourcing_name != NULL)
21301 {
21302 scriptname = autoload_name(name);
21303 if (scriptname != NULL)
21304 {
21305 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021306 plen = (int)STRLEN(p);
21307 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021308 if (slen > plen && fnamecmp(p,
21309 sourcing_name + slen - plen) == 0)
21310 j = OK;
21311 vim_free(scriptname);
21312 }
21313 }
21314 if (j == FAIL)
21315 {
21316 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21317 goto erret;
21318 }
21319 }
21320
21321 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 if (fp == NULL)
21323 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021324
21325 if (fudi.fd_dict != NULL)
21326 {
21327 if (fudi.fd_di == NULL)
21328 {
21329 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021330 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021331 if (fudi.fd_di == NULL)
21332 {
21333 vim_free(fp);
21334 goto erret;
21335 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021336 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21337 {
21338 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021339 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021340 goto erret;
21341 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021342 }
21343 else
21344 /* overwrite existing dict entry */
21345 clear_tv(&fudi.fd_di->di_tv);
21346 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021347 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021348 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021349 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021350
21351 /* behave like "dict" was used */
21352 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021353 }
21354
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021356 STRCPY(fp->uf_name, name);
21357 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021359 fp->uf_args = newargs;
21360 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021361#ifdef FEAT_PROFILE
21362 fp->uf_tml_count = NULL;
21363 fp->uf_tml_total = NULL;
21364 fp->uf_tml_self = NULL;
21365 fp->uf_profiling = FALSE;
21366 if (prof_def_func())
21367 func_do_profile(fp);
21368#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021369 fp->uf_varargs = varargs;
21370 fp->uf_flags = flags;
21371 fp->uf_calls = 0;
21372 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021373 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374
21375erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 ga_clear_strings(&newargs);
21377 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021378ret_free:
21379 vim_free(skip_until);
21380 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383}
21384
21385/*
21386 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021387 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021389 * flags:
21390 * TFN_INT: internal function name OK
21391 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 * Advances "pp" to just after the function name (if no error).
21393 */
21394 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021395trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 char_u **pp;
21397 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021398 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021399 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021401 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 char_u *start;
21403 char_u *end;
21404 int lead;
21405 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021407 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021408
21409 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021412
21413 /* Check for hard coded <SNR>: already translated function ID (from a user
21414 * command). */
21415 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21416 && (*pp)[2] == (int)KE_SNR)
21417 {
21418 *pp += 3;
21419 len = get_id_len(pp) + 3;
21420 return vim_strnsave(start, len);
21421 }
21422
21423 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21424 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021426 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021428
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021429 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21430 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021431 if (end == start)
21432 {
21433 if (!skip)
21434 EMSG(_("E129: Function name required"));
21435 goto theend;
21436 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021437 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021438 {
21439 /*
21440 * Report an invalid expression in braces, unless the expression
21441 * evaluation has been cancelled due to an aborting error, an
21442 * interrupt, or an exception.
21443 */
21444 if (!aborting())
21445 {
21446 if (end != NULL)
21447 EMSG2(_(e_invarg2), start);
21448 }
21449 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021450 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021451 goto theend;
21452 }
21453
21454 if (lv.ll_tv != NULL)
21455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021456 if (fdp != NULL)
21457 {
21458 fdp->fd_dict = lv.ll_dict;
21459 fdp->fd_newkey = lv.ll_newkey;
21460 lv.ll_newkey = NULL;
21461 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021463 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21464 {
21465 name = vim_strsave(lv.ll_tv->vval.v_string);
21466 *pp = end;
21467 }
21468 else
21469 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021470 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21471 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021472 EMSG(_(e_funcref));
21473 else
21474 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021475 name = NULL;
21476 }
21477 goto theend;
21478 }
21479
21480 if (lv.ll_name == NULL)
21481 {
21482 /* Error found, but continue after the function name. */
21483 *pp = end;
21484 goto theend;
21485 }
21486
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021487 /* Check if the name is a Funcref. If so, use the value. */
21488 if (lv.ll_exp_name != NULL)
21489 {
21490 len = (int)STRLEN(lv.ll_exp_name);
21491 name = deref_func_name(lv.ll_exp_name, &len);
21492 if (name == lv.ll_exp_name)
21493 name = NULL;
21494 }
21495 else
21496 {
21497 len = (int)(end - *pp);
21498 name = deref_func_name(*pp, &len);
21499 if (name == *pp)
21500 name = NULL;
21501 }
21502 if (name != NULL)
21503 {
21504 name = vim_strsave(name);
21505 *pp = end;
21506 goto theend;
21507 }
21508
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021509 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021510 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021511 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021512 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21513 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21514 {
21515 /* When there was "s:" already or the name expanded to get a
21516 * leading "s:" then remove it. */
21517 lv.ll_name += 2;
21518 len -= 2;
21519 lead = 2;
21520 }
21521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021522 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021523 {
21524 if (lead == 2) /* skip over "s:" */
21525 lv.ll_name += 2;
21526 len = (int)(end - lv.ll_name);
21527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021528
21529 /*
21530 * Copy the function name to allocated memory.
21531 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21532 * Accept <SNR>123_name() outside a script.
21533 */
21534 if (skip)
21535 lead = 0; /* do nothing */
21536 else if (lead > 0)
21537 {
21538 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021539 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21540 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021541 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021542 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021543 if (current_SID <= 0)
21544 {
21545 EMSG(_(e_usingsid));
21546 goto theend;
21547 }
21548 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21549 lead += (int)STRLEN(sid_buf);
21550 }
21551 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021552 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021553 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021554 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021555 goto theend;
21556 }
21557 name = alloc((unsigned)(len + lead + 1));
21558 if (name != NULL)
21559 {
21560 if (lead > 0)
21561 {
21562 name[0] = K_SPECIAL;
21563 name[1] = KS_EXTRA;
21564 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021565 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021566 STRCPY(name + 3, sid_buf);
21567 }
21568 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21569 name[len + lead] = NUL;
21570 }
21571 *pp = end;
21572
21573theend:
21574 clear_lval(&lv);
21575 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576}
21577
21578/*
21579 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21580 * Return 2 if "p" starts with "s:".
21581 * Return 0 otherwise.
21582 */
21583 static int
21584eval_fname_script(p)
21585 char_u *p;
21586{
21587 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21588 || STRNICMP(p + 1, "SNR>", 4) == 0))
21589 return 5;
21590 if (p[0] == 's' && p[1] == ':')
21591 return 2;
21592 return 0;
21593}
21594
21595/*
21596 * Return TRUE if "p" starts with "<SID>" or "s:".
21597 * Only works if eval_fname_script() returned non-zero for "p"!
21598 */
21599 static int
21600eval_fname_sid(p)
21601 char_u *p;
21602{
21603 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21604}
21605
21606/*
21607 * List the head of the function: "name(arg1, arg2)".
21608 */
21609 static void
21610list_func_head(fp, indent)
21611 ufunc_T *fp;
21612 int indent;
21613{
21614 int j;
21615
21616 msg_start();
21617 if (indent)
21618 MSG_PUTS(" ");
21619 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021620 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 {
21622 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021623 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 }
21625 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021626 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021628 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 {
21630 if (j)
21631 MSG_PUTS(", ");
21632 msg_puts(FUNCARG(fp, j));
21633 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021634 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 {
21636 if (j)
21637 MSG_PUTS(", ");
21638 MSG_PUTS("...");
21639 }
21640 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021641 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021642 if (p_verbose > 0)
21643 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644}
21645
21646/*
21647 * Find a function by name, return pointer to it in ufuncs.
21648 * Return NULL for unknown function.
21649 */
21650 static ufunc_T *
21651find_func(name)
21652 char_u *name;
21653{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021654 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021656 hi = hash_find(&func_hashtab, name);
21657 if (!HASHITEM_EMPTY(hi))
21658 return HI2UF(hi);
21659 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660}
21661
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021662#if defined(EXITFREE) || defined(PROTO)
21663 void
21664free_all_functions()
21665{
21666 hashitem_T *hi;
21667
21668 /* Need to start all over every time, because func_free() may change the
21669 * hash table. */
21670 while (func_hashtab.ht_used > 0)
21671 for (hi = func_hashtab.ht_array; ; ++hi)
21672 if (!HASHITEM_EMPTY(hi))
21673 {
21674 func_free(HI2UF(hi));
21675 break;
21676 }
21677}
21678#endif
21679
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680/*
21681 * Return TRUE if a function "name" exists.
21682 */
21683 static int
21684function_exists(name)
21685 char_u *name;
21686{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021687 char_u *nm = name;
21688 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 int n = FALSE;
21690
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021691 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021692 nm = skipwhite(nm);
21693
21694 /* Only accept "funcname", "funcname ", "funcname (..." and
21695 * "funcname(...", not "funcname!...". */
21696 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021697 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021698 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021699 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021700 else
21701 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021702 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021703 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021704 return n;
21705}
21706
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021707/*
21708 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021709 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021710 */
21711 static int
21712builtin_function(name)
21713 char_u *name;
21714{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021715 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21716 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021717}
21718
Bram Moolenaar05159a02005-02-26 23:04:13 +000021719#if defined(FEAT_PROFILE) || defined(PROTO)
21720/*
21721 * Start profiling function "fp".
21722 */
21723 static void
21724func_do_profile(fp)
21725 ufunc_T *fp;
21726{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021727 int len = fp->uf_lines.ga_len;
21728
21729 if (len == 0)
21730 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021731 fp->uf_tm_count = 0;
21732 profile_zero(&fp->uf_tm_self);
21733 profile_zero(&fp->uf_tm_total);
21734 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021735 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021736 if (fp->uf_tml_total == NULL)
21737 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021738 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021739 if (fp->uf_tml_self == NULL)
21740 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021741 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021742 fp->uf_tml_idx = -1;
21743 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21744 || fp->uf_tml_self == NULL)
21745 return; /* out of memory */
21746
21747 fp->uf_profiling = TRUE;
21748}
21749
21750/*
21751 * Dump the profiling results for all functions in file "fd".
21752 */
21753 void
21754func_dump_profile(fd)
21755 FILE *fd;
21756{
21757 hashitem_T *hi;
21758 int todo;
21759 ufunc_T *fp;
21760 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021761 ufunc_T **sorttab;
21762 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021763
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021764 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021765 if (todo == 0)
21766 return; /* nothing to dump */
21767
Bram Moolenaar73830342005-02-28 22:48:19 +000021768 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21769
Bram Moolenaar05159a02005-02-26 23:04:13 +000021770 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21771 {
21772 if (!HASHITEM_EMPTY(hi))
21773 {
21774 --todo;
21775 fp = HI2UF(hi);
21776 if (fp->uf_profiling)
21777 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021778 if (sorttab != NULL)
21779 sorttab[st_len++] = fp;
21780
Bram Moolenaar05159a02005-02-26 23:04:13 +000021781 if (fp->uf_name[0] == K_SPECIAL)
21782 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21783 else
21784 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21785 if (fp->uf_tm_count == 1)
21786 fprintf(fd, "Called 1 time\n");
21787 else
21788 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21789 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21790 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21791 fprintf(fd, "\n");
21792 fprintf(fd, "count total (s) self (s)\n");
21793
21794 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21795 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021796 if (FUNCLINE(fp, i) == NULL)
21797 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021798 prof_func_line(fd, fp->uf_tml_count[i],
21799 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021800 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21801 }
21802 fprintf(fd, "\n");
21803 }
21804 }
21805 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021806
21807 if (sorttab != NULL && st_len > 0)
21808 {
21809 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21810 prof_total_cmp);
21811 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21812 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21813 prof_self_cmp);
21814 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21815 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021816
21817 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021818}
Bram Moolenaar73830342005-02-28 22:48:19 +000021819
21820 static void
21821prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21822 FILE *fd;
21823 ufunc_T **sorttab;
21824 int st_len;
21825 char *title;
21826 int prefer_self; /* when equal print only self time */
21827{
21828 int i;
21829 ufunc_T *fp;
21830
21831 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21832 fprintf(fd, "count total (s) self (s) function\n");
21833 for (i = 0; i < 20 && i < st_len; ++i)
21834 {
21835 fp = sorttab[i];
21836 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21837 prefer_self);
21838 if (fp->uf_name[0] == K_SPECIAL)
21839 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21840 else
21841 fprintf(fd, " %s()\n", fp->uf_name);
21842 }
21843 fprintf(fd, "\n");
21844}
21845
21846/*
21847 * Print the count and times for one function or function line.
21848 */
21849 static void
21850prof_func_line(fd, count, total, self, prefer_self)
21851 FILE *fd;
21852 int count;
21853 proftime_T *total;
21854 proftime_T *self;
21855 int prefer_self; /* when equal print only self time */
21856{
21857 if (count > 0)
21858 {
21859 fprintf(fd, "%5d ", count);
21860 if (prefer_self && profile_equal(total, self))
21861 fprintf(fd, " ");
21862 else
21863 fprintf(fd, "%s ", profile_msg(total));
21864 if (!prefer_self && profile_equal(total, self))
21865 fprintf(fd, " ");
21866 else
21867 fprintf(fd, "%s ", profile_msg(self));
21868 }
21869 else
21870 fprintf(fd, " ");
21871}
21872
21873/*
21874 * Compare function for total time sorting.
21875 */
21876 static int
21877#ifdef __BORLANDC__
21878_RTLENTRYF
21879#endif
21880prof_total_cmp(s1, s2)
21881 const void *s1;
21882 const void *s2;
21883{
21884 ufunc_T *p1, *p2;
21885
21886 p1 = *(ufunc_T **)s1;
21887 p2 = *(ufunc_T **)s2;
21888 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21889}
21890
21891/*
21892 * Compare function for self time sorting.
21893 */
21894 static int
21895#ifdef __BORLANDC__
21896_RTLENTRYF
21897#endif
21898prof_self_cmp(s1, s2)
21899 const void *s1;
21900 const void *s2;
21901{
21902 ufunc_T *p1, *p2;
21903
21904 p1 = *(ufunc_T **)s1;
21905 p2 = *(ufunc_T **)s2;
21906 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21907}
21908
Bram Moolenaar05159a02005-02-26 23:04:13 +000021909#endif
21910
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021911/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021912 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021913 * Return TRUE if a package was loaded.
21914 */
21915 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021916script_autoload(name, reload)
21917 char_u *name;
21918 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021919{
21920 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021921 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021922 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021923 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021924
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021925 /* Return quickly when autoload disabled. */
21926 if (no_autoload)
21927 return FALSE;
21928
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021929 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021930 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021931 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021932 return FALSE;
21933
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021934 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021935
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021936 /* Find the name in the list of previously loaded package names. Skip
21937 * "autoload/", it's always the same. */
21938 for (i = 0; i < ga_loaded.ga_len; ++i)
21939 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21940 break;
21941 if (!reload && i < ga_loaded.ga_len)
21942 ret = FALSE; /* was loaded already */
21943 else
21944 {
21945 /* Remember the name if it wasn't loaded already. */
21946 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21947 {
21948 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21949 tofree = NULL;
21950 }
21951
21952 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021953 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021954 ret = TRUE;
21955 }
21956
21957 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021958 return ret;
21959}
21960
21961/*
21962 * Return the autoload script name for a function or variable name.
21963 * Returns NULL when out of memory.
21964 */
21965 static char_u *
21966autoload_name(name)
21967 char_u *name;
21968{
21969 char_u *p;
21970 char_u *scriptname;
21971
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021972 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021973 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21974 if (scriptname == NULL)
21975 return FALSE;
21976 STRCPY(scriptname, "autoload/");
21977 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021978 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021979 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021980 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021981 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021982 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021983}
21984
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21986
21987/*
21988 * Function given to ExpandGeneric() to obtain the list of user defined
21989 * function names.
21990 */
21991 char_u *
21992get_user_func_name(xp, idx)
21993 expand_T *xp;
21994 int idx;
21995{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021996 static long_u done;
21997 static hashitem_T *hi;
21998 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999
22000 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022002 done = 0;
22003 hi = func_hashtab.ht_array;
22004 }
22005 if (done < func_hashtab.ht_used)
22006 {
22007 if (done++ > 0)
22008 ++hi;
22009 while (HASHITEM_EMPTY(hi))
22010 ++hi;
22011 fp = HI2UF(hi);
22012
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022013 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022014 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022016 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22017 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018
22019 cat_func_name(IObuff, fp);
22020 if (xp->xp_context != EXPAND_USER_FUNC)
22021 {
22022 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022023 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 STRCAT(IObuff, ")");
22025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 return IObuff;
22027 }
22028 return NULL;
22029}
22030
22031#endif /* FEAT_CMDL_COMPL */
22032
22033/*
22034 * Copy the function name of "fp" to buffer "buf".
22035 * "buf" must be able to hold the function name plus three bytes.
22036 * Takes care of script-local function names.
22037 */
22038 static void
22039cat_func_name(buf, fp)
22040 char_u *buf;
22041 ufunc_T *fp;
22042{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022043 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 {
22045 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022046 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 }
22048 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022049 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050}
22051
22052/*
22053 * ":delfunction {name}"
22054 */
22055 void
22056ex_delfunction(eap)
22057 exarg_T *eap;
22058{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022059 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 char_u *p;
22061 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022062 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063
22064 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022065 name = trans_function_name(&p, eap->skip, 0, &fudi);
22066 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022068 {
22069 if (fudi.fd_dict != NULL && !eap->skip)
22070 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 if (!ends_excmd(*skipwhite(p)))
22074 {
22075 vim_free(name);
22076 EMSG(_(e_trailing));
22077 return;
22078 }
22079 eap->nextcmd = check_nextcmd(p);
22080 if (eap->nextcmd != NULL)
22081 *p = NUL;
22082
22083 if (!eap->skip)
22084 fp = find_func(name);
22085 vim_free(name);
22086
22087 if (!eap->skip)
22088 {
22089 if (fp == NULL)
22090 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 return;
22093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022094 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 {
22096 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22097 return;
22098 }
22099
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022100 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022102 /* Delete the dict item that refers to the function, it will
22103 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022104 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022106 else
22107 func_free(fp);
22108 }
22109}
22110
22111/*
22112 * Free a function and remove it from the list of functions.
22113 */
22114 static void
22115func_free(fp)
22116 ufunc_T *fp;
22117{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022118 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022119
22120 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022121 ga_clear_strings(&(fp->uf_args));
22122 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022123#ifdef FEAT_PROFILE
22124 vim_free(fp->uf_tml_count);
22125 vim_free(fp->uf_tml_total);
22126 vim_free(fp->uf_tml_self);
22127#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022128
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022129 /* remove the function from the function hashtable */
22130 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22131 if (HASHITEM_EMPTY(hi))
22132 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022133 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022134 hash_remove(&func_hashtab, hi);
22135
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022136 vim_free(fp);
22137}
22138
22139/*
22140 * Unreference a Function: decrement the reference count and free it when it
22141 * becomes zero. Only for numbered functions.
22142 */
22143 static void
22144func_unref(name)
22145 char_u *name;
22146{
22147 ufunc_T *fp;
22148
22149 if (name != NULL && isdigit(*name))
22150 {
22151 fp = find_func(name);
22152 if (fp == NULL)
22153 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022154 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022155 {
22156 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022157 * when "uf_calls" becomes zero. */
22158 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022159 func_free(fp);
22160 }
22161 }
22162}
22163
22164/*
22165 * Count a reference to a Function.
22166 */
22167 static void
22168func_ref(name)
22169 char_u *name;
22170{
22171 ufunc_T *fp;
22172
22173 if (name != NULL && isdigit(*name))
22174 {
22175 fp = find_func(name);
22176 if (fp == NULL)
22177 EMSG2(_(e_intern2), "func_ref()");
22178 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022179 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 }
22181}
22182
22183/*
22184 * Call a user function.
22185 */
22186 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022187call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 ufunc_T *fp; /* pointer to function */
22189 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 typval_T *argvars; /* arguments */
22191 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 linenr_T firstline; /* first line of range */
22193 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022194 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195{
Bram Moolenaar33570922005-01-25 22:26:29 +000022196 char_u *save_sourcing_name;
22197 linenr_T save_sourcing_lnum;
22198 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022199 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 int save_did_emsg;
22201 static int depth = 0;
22202 dictitem_T *v;
22203 int fixvar_idx = 0; /* index in fixvar[] */
22204 int i;
22205 int ai;
22206 char_u numbuf[NUMBUFLEN];
22207 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022208#ifdef FEAT_PROFILE
22209 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022210 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212
22213 /* If depth of calling is getting too high, don't execute the function */
22214 if (depth >= p_mfd)
22215 {
22216 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022217 rettv->v_type = VAR_NUMBER;
22218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 return;
22220 }
22221 ++depth;
22222
22223 line_breakcheck(); /* check for CTRL-C hit */
22224
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022225 fc = (funccall_T *)alloc(sizeof(funccall_T));
22226 fc->caller = current_funccal;
22227 current_funccal = fc;
22228 fc->func = fp;
22229 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022230 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022231 fc->linenr = 0;
22232 fc->returned = FALSE;
22233 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022235 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22236 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022239 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022240 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22241 * each argument variable and saves a lot of time.
22242 */
22243 /*
22244 * Init l: variables.
22245 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022246 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022247 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022248 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022249 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22250 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022251 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022252 name = v->di_key;
22253 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022255 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022257 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022258 v->di_tv.vval.v_dict = selfdict;
22259 ++selfdict->dv_refcount;
22260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261
Bram Moolenaar33570922005-01-25 22:26:29 +000022262 /*
22263 * Init a: variables.
22264 * Set a:0 to "argcount".
22265 * Set a:000 to a list with room for the "..." arguments.
22266 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022267 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22268 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022269 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022270 /* Use "name" to avoid a warning from some compiler that checks the
22271 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022272 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022273 name = v->di_key;
22274 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022276 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022278 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022279 v->di_tv.vval.v_list = &fc->l_varlist;
22280 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22281 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22282 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022283
22284 /*
22285 * Set a:firstline to "firstline" and a:lastline to "lastline".
22286 * Set a:name to named arguments.
22287 * Set a:N to the "..." arguments.
22288 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022289 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022291 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022292 (varnumber_T)lastline);
22293 for (i = 0; i < argcount; ++i)
22294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022295 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022296 if (ai < 0)
22297 /* named argument a:name */
22298 name = FUNCARG(fp, i);
22299 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022300 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 /* "..." argument a:1, a:2, etc. */
22302 sprintf((char *)numbuf, "%d", ai + 1);
22303 name = numbuf;
22304 }
22305 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22306 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022307 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22309 }
22310 else
22311 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022312 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22313 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 if (v == NULL)
22315 break;
22316 v->di_flags = DI_FLAGS_RO;
22317 }
22318 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022319 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022320
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022321 /* Note: the values are copied directly to avoid alloc/free.
22322 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022323 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022324 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022325
22326 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22327 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022328 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22329 fc->l_listitems[ai].li_tv = argvars[i];
22330 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022331 }
22332 }
22333
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 /* Don't redraw while executing the function. */
22335 ++RedrawingDisabled;
22336 save_sourcing_name = sourcing_name;
22337 save_sourcing_lnum = sourcing_lnum;
22338 sourcing_lnum = 1;
22339 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022340 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 if (sourcing_name != NULL)
22342 {
22343 if (save_sourcing_name != NULL
22344 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22345 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22346 else
22347 STRCPY(sourcing_name, "function ");
22348 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22349
22350 if (p_verbose >= 12)
22351 {
22352 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022353 verbose_enter_scroll();
22354
Bram Moolenaar555b2802005-05-19 21:08:39 +000022355 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 if (p_verbose >= 14)
22357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022359 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022360 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022361 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362
22363 msg_puts((char_u *)"(");
22364 for (i = 0; i < argcount; ++i)
22365 {
22366 if (i > 0)
22367 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022368 if (argvars[i].v_type == VAR_NUMBER)
22369 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 else
22371 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022372 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22373 if (s != NULL)
22374 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022375 if (vim_strsize(s) > MSG_BUF_CLEN)
22376 {
22377 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22378 s = buf;
22379 }
22380 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022381 vim_free(tofree);
22382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 }
22384 }
22385 msg_puts((char_u *)")");
22386 }
22387 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022388
22389 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 --no_wait_return;
22391 }
22392 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022394 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022395 {
22396 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22397 func_do_profile(fp);
22398 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022399 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022400 {
22401 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022402 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022403 profile_zero(&fp->uf_tm_children);
22404 }
22405 script_prof_save(&wait_start);
22406 }
22407#endif
22408
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022410 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 save_did_emsg = did_emsg;
22412 did_emsg = FALSE;
22413
22414 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022415 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22417
22418 --RedrawingDisabled;
22419
22420 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022423 clear_tv(rettv);
22424 rettv->v_type = VAR_NUMBER;
22425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 }
22427
Bram Moolenaar05159a02005-02-26 23:04:13 +000022428#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022429 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022430 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022431 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022432 profile_end(&call_start);
22433 profile_sub_wait(&wait_start, &call_start);
22434 profile_add(&fp->uf_tm_total, &call_start);
22435 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022436 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022437 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022438 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22439 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022440 }
22441 }
22442#endif
22443
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 /* when being verbose, mention the return value */
22445 if (p_verbose >= 12)
22446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022448 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022451 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022452 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022453 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022454 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022457 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022458 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022459 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022460 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022461
Bram Moolenaar555b2802005-05-19 21:08:39 +000022462 /* The value may be very long. Skip the middle part, so that we
22463 * have some idea how it starts and ends. smsg() would always
22464 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022465 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022466 if (s != NULL)
22467 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022468 if (vim_strsize(s) > MSG_BUF_CLEN)
22469 {
22470 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22471 s = buf;
22472 }
22473 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022474 vim_free(tofree);
22475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 }
22477 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022478
22479 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 --no_wait_return;
22481 }
22482
22483 vim_free(sourcing_name);
22484 sourcing_name = save_sourcing_name;
22485 sourcing_lnum = save_sourcing_lnum;
22486 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022487#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022488 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022489 script_prof_restore(&wait_start);
22490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491
22492 if (p_verbose >= 12 && sourcing_name != NULL)
22493 {
22494 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022495 verbose_enter_scroll();
22496
Bram Moolenaar555b2802005-05-19 21:08:39 +000022497 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022499
22500 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 --no_wait_return;
22502 }
22503
22504 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022505 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022507
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022508 /* If the a:000 list and the l: and a: dicts are not referenced we can
22509 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22511 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22512 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22513 {
22514 free_funccal(fc, FALSE);
22515 }
22516 else
22517 {
22518 hashitem_T *hi;
22519 listitem_T *li;
22520 int todo;
22521
22522 /* "fc" is still in use. This can happen when returning "a:000" or
22523 * assigning "l:" to a global variable.
22524 * Link "fc" in the list for garbage collection later. */
22525 fc->caller = previous_funccal;
22526 previous_funccal = fc;
22527
22528 /* Make a copy of the a: variables, since we didn't do that above. */
22529 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22530 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22531 {
22532 if (!HASHITEM_EMPTY(hi))
22533 {
22534 --todo;
22535 v = HI2DI(hi);
22536 copy_tv(&v->di_tv, &v->di_tv);
22537 }
22538 }
22539
22540 /* Make a copy of the a:000 items, since we didn't do that above. */
22541 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22542 copy_tv(&li->li_tv, &li->li_tv);
22543 }
22544}
22545
22546/*
22547 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022548 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022549 */
22550 static int
22551can_free_funccal(fc, copyID)
22552 funccall_T *fc;
22553 int copyID;
22554{
22555 return (fc->l_varlist.lv_copyID != copyID
22556 && fc->l_vars.dv_copyID != copyID
22557 && fc->l_avars.dv_copyID != copyID);
22558}
22559
22560/*
22561 * Free "fc" and what it contains.
22562 */
22563 static void
22564free_funccal(fc, free_val)
22565 funccall_T *fc;
22566 int free_val; /* a: vars were allocated */
22567{
22568 listitem_T *li;
22569
22570 /* The a: variables typevals may not have been allocated, only free the
22571 * allocated variables. */
22572 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22573
22574 /* free all l: variables */
22575 vars_clear(&fc->l_vars.dv_hashtab);
22576
22577 /* Free the a:000 variables if they were allocated. */
22578 if (free_val)
22579 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22580 clear_tv(&li->li_tv);
22581
22582 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583}
22584
22585/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022586 * Add a number variable "name" to dict "dp" with value "nr".
22587 */
22588 static void
22589add_nr_var(dp, v, name, nr)
22590 dict_T *dp;
22591 dictitem_T *v;
22592 char *name;
22593 varnumber_T nr;
22594{
22595 STRCPY(v->di_key, name);
22596 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22597 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22598 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022599 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 v->di_tv.vval.v_number = nr;
22601}
22602
22603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 * ":return [expr]"
22605 */
22606 void
22607ex_return(eap)
22608 exarg_T *eap;
22609{
22610 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022611 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 int returning = FALSE;
22613
22614 if (current_funccal == NULL)
22615 {
22616 EMSG(_("E133: :return not inside a function"));
22617 return;
22618 }
22619
22620 if (eap->skip)
22621 ++emsg_skip;
22622
22623 eap->nextcmd = NULL;
22624 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022625 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 {
22627 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022630 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 }
22632 /* It's safer to return also on error. */
22633 else if (!eap->skip)
22634 {
22635 /*
22636 * Return unless the expression evaluation has been cancelled due to an
22637 * aborting error, an interrupt, or an exception.
22638 */
22639 if (!aborting())
22640 returning = do_return(eap, FALSE, TRUE, NULL);
22641 }
22642
22643 /* When skipping or the return gets pending, advance to the next command
22644 * in this line (!returning). Otherwise, ignore the rest of the line.
22645 * Following lines will be ignored by get_func_line(). */
22646 if (returning)
22647 eap->nextcmd = NULL;
22648 else if (eap->nextcmd == NULL) /* no argument */
22649 eap->nextcmd = check_nextcmd(arg);
22650
22651 if (eap->skip)
22652 --emsg_skip;
22653}
22654
22655/*
22656 * Return from a function. Possibly makes the return pending. Also called
22657 * for a pending return at the ":endtry" or after returning from an extra
22658 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022659 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022660 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 * FALSE when the return gets pending.
22662 */
22663 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022664do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 exarg_T *eap;
22666 int reanimate;
22667 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022668 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669{
22670 int idx;
22671 struct condstack *cstack = eap->cstack;
22672
22673 if (reanimate)
22674 /* Undo the return. */
22675 current_funccal->returned = FALSE;
22676
22677 /*
22678 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22679 * not in its finally clause (which then is to be executed next) is found.
22680 * In this case, make the ":return" pending for execution at the ":endtry".
22681 * Otherwise, return normally.
22682 */
22683 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22684 if (idx >= 0)
22685 {
22686 cstack->cs_pending[idx] = CSTP_RETURN;
22687
22688 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022689 /* A pending return again gets pending. "rettv" points to an
22690 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022692 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 else
22694 {
22695 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022696 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022698 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022700 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 {
22702 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022703 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022704 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 else
22706 EMSG(_(e_outofmem));
22707 }
22708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022709 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710
22711 if (reanimate)
22712 {
22713 /* The pending return value could be overwritten by a ":return"
22714 * without argument in a finally clause; reset the default
22715 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022716 current_funccal->rettv->v_type = VAR_NUMBER;
22717 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 }
22719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 }
22722 else
22723 {
22724 current_funccal->returned = TRUE;
22725
22726 /* If the return is carried out now, store the return value. For
22727 * a return immediately after reanimation, the value is already
22728 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022729 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022731 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022734 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 }
22736 }
22737
22738 return idx < 0;
22739}
22740
22741/*
22742 * Free the variable with a pending return value.
22743 */
22744 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022745discard_pending_return(rettv)
22746 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747{
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749}
22750
22751/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022752 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 * is an allocated string. Used by report_pending() for verbose messages.
22754 */
22755 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022756get_return_cmd(rettv)
22757 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022759 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022760 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022761 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022763 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022764 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022765 if (s == NULL)
22766 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022767
22768 STRCPY(IObuff, ":return ");
22769 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22770 if (STRLEN(s) + 8 >= IOSIZE)
22771 STRCPY(IObuff + IOSIZE - 4, "...");
22772 vim_free(tofree);
22773 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774}
22775
22776/*
22777 * Get next function line.
22778 * Called by do_cmdline() to get the next line.
22779 * Returns allocated string, or NULL for end of function.
22780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 char_u *
22782get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022783 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022785 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786{
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022788 ufunc_T *fp = fcp->func;
22789 char_u *retval;
22790 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791
22792 /* If breakpoints have been added/deleted need to check for it. */
22793 if (fcp->dbg_tick != debug_tick)
22794 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022795 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 sourcing_lnum);
22797 fcp->dbg_tick = debug_tick;
22798 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022799#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022800 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022801 func_line_end(cookie);
22802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803
Bram Moolenaar05159a02005-02-26 23:04:13 +000022804 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022805 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22806 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 retval = NULL;
22808 else
22809 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022810 /* Skip NULL lines (continuation lines). */
22811 while (fcp->linenr < gap->ga_len
22812 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22813 ++fcp->linenr;
22814 if (fcp->linenr >= gap->ga_len)
22815 retval = NULL;
22816 else
22817 {
22818 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22819 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022820#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022821 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022822 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022823#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 }
22826
22827 /* Did we encounter a breakpoint? */
22828 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22829 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022830 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022832 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 sourcing_lnum);
22834 fcp->dbg_tick = debug_tick;
22835 }
22836
22837 return retval;
22838}
22839
Bram Moolenaar05159a02005-02-26 23:04:13 +000022840#if defined(FEAT_PROFILE) || defined(PROTO)
22841/*
22842 * Called when starting to read a function line.
22843 * "sourcing_lnum" must be correct!
22844 * When skipping lines it may not actually be executed, but we won't find out
22845 * until later and we need to store the time now.
22846 */
22847 void
22848func_line_start(cookie)
22849 void *cookie;
22850{
22851 funccall_T *fcp = (funccall_T *)cookie;
22852 ufunc_T *fp = fcp->func;
22853
22854 if (fp->uf_profiling && sourcing_lnum >= 1
22855 && sourcing_lnum <= fp->uf_lines.ga_len)
22856 {
22857 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022858 /* Skip continuation lines. */
22859 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22860 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022861 fp->uf_tml_execed = FALSE;
22862 profile_start(&fp->uf_tml_start);
22863 profile_zero(&fp->uf_tml_children);
22864 profile_get_wait(&fp->uf_tml_wait);
22865 }
22866}
22867
22868/*
22869 * Called when actually executing a function line.
22870 */
22871 void
22872func_line_exec(cookie)
22873 void *cookie;
22874{
22875 funccall_T *fcp = (funccall_T *)cookie;
22876 ufunc_T *fp = fcp->func;
22877
22878 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22879 fp->uf_tml_execed = TRUE;
22880}
22881
22882/*
22883 * Called when done with a function line.
22884 */
22885 void
22886func_line_end(cookie)
22887 void *cookie;
22888{
22889 funccall_T *fcp = (funccall_T *)cookie;
22890 ufunc_T *fp = fcp->func;
22891
22892 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22893 {
22894 if (fp->uf_tml_execed)
22895 {
22896 ++fp->uf_tml_count[fp->uf_tml_idx];
22897 profile_end(&fp->uf_tml_start);
22898 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022899 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022900 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22901 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022902 }
22903 fp->uf_tml_idx = -1;
22904 }
22905}
22906#endif
22907
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908/*
22909 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022910 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 */
22912 int
22913func_has_ended(cookie)
22914 void *cookie;
22915{
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917
22918 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22919 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022920 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 || fcp->returned);
22922}
22923
22924/*
22925 * return TRUE if cookie indicates a function which "abort"s on errors.
22926 */
22927 int
22928func_has_abort(cookie)
22929 void *cookie;
22930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022931 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932}
22933
22934#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22935typedef enum
22936{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022937 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22938 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22939 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940} var_flavour_T;
22941
22942static var_flavour_T var_flavour __ARGS((char_u *varname));
22943
22944 static var_flavour_T
22945var_flavour(varname)
22946 char_u *varname;
22947{
22948 char_u *p = varname;
22949
22950 if (ASCII_ISUPPER(*p))
22951 {
22952 while (*(++p))
22953 if (ASCII_ISLOWER(*p))
22954 return VAR_FLAVOUR_SESSION;
22955 return VAR_FLAVOUR_VIMINFO;
22956 }
22957 else
22958 return VAR_FLAVOUR_DEFAULT;
22959}
22960#endif
22961
22962#if defined(FEAT_VIMINFO) || defined(PROTO)
22963/*
22964 * Restore global vars that start with a capital from the viminfo file
22965 */
22966 int
22967read_viminfo_varlist(virp, writing)
22968 vir_T *virp;
22969 int writing;
22970{
22971 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022972 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022973 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974
22975 if (!writing && (find_viminfo_parameter('!') != NULL))
22976 {
22977 tab = vim_strchr(virp->vir_line + 1, '\t');
22978 if (tab != NULL)
22979 {
22980 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022981 switch (*tab)
22982 {
22983 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022984#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022985 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022986#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022987 case 'D': type = VAR_DICT; break;
22988 case 'L': type = VAR_LIST; break;
22989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990
22991 tab = vim_strchr(tab, '\t');
22992 if (tab != NULL)
22993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022994 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022995 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022996 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022998#ifdef FEAT_FLOAT
22999 else if (type == VAR_FLOAT)
23000 (void)string2float(tab + 1, &tv.vval.v_float);
23001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023003 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023004 if (type == VAR_DICT || type == VAR_LIST)
23005 {
23006 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23007
23008 if (etv == NULL)
23009 /* Failed to parse back the dict or list, use it as a
23010 * string. */
23011 tv.v_type = VAR_STRING;
23012 else
23013 {
23014 vim_free(tv.vval.v_string);
23015 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023016 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023017 }
23018 }
23019
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023020 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023021
23022 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023023 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023024 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23025 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 }
23027 }
23028 }
23029
23030 return viminfo_readline(virp);
23031}
23032
23033/*
23034 * Write global vars that start with a capital to the viminfo file
23035 */
23036 void
23037write_viminfo_varlist(fp)
23038 FILE *fp;
23039{
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 hashitem_T *hi;
23041 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023042 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023043 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023044 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023045 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023046 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047
23048 if (find_viminfo_parameter('!') == NULL)
23049 return;
23050
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023051 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023053 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023054 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023056 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023058 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023059 this_var = HI2DI(hi);
23060 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023061 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023063 {
23064 case VAR_STRING: s = "STR"; break;
23065 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023066#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023067 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023068#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023069 case VAR_DICT: s = "DIC"; break;
23070 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023071 default: continue;
23072 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023073 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023074 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023075 if (p != NULL)
23076 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023077 vim_free(tofree);
23078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 }
23080 }
23081}
23082#endif
23083
23084#if defined(FEAT_SESSION) || defined(PROTO)
23085 int
23086store_session_globals(fd)
23087 FILE *fd;
23088{
Bram Moolenaar33570922005-01-25 22:26:29 +000023089 hashitem_T *hi;
23090 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023091 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 char_u *p, *t;
23093
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023094 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023095 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023097 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023099 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023100 this_var = HI2DI(hi);
23101 if ((this_var->di_tv.v_type == VAR_NUMBER
23102 || this_var->di_tv.v_type == VAR_STRING)
23103 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023104 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023105 /* Escape special characters with a backslash. Turn a LF and
23106 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023107 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023108 (char_u *)"\\\"\n\r");
23109 if (p == NULL) /* out of memory */
23110 break;
23111 for (t = p; *t != NUL; ++t)
23112 if (*t == '\n')
23113 *t = 'n';
23114 else if (*t == '\r')
23115 *t = 'r';
23116 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 this_var->di_key,
23118 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23119 : ' ',
23120 p,
23121 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23122 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023123 || put_eol(fd) == FAIL)
23124 {
23125 vim_free(p);
23126 return FAIL;
23127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 vim_free(p);
23129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023130#ifdef FEAT_FLOAT
23131 else if (this_var->di_tv.v_type == VAR_FLOAT
23132 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23133 {
23134 float_T f = this_var->di_tv.vval.v_float;
23135 int sign = ' ';
23136
23137 if (f < 0)
23138 {
23139 f = -f;
23140 sign = '-';
23141 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023142 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023143 this_var->di_key, sign, f) < 0)
23144 || put_eol(fd) == FAIL)
23145 return FAIL;
23146 }
23147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 }
23149 }
23150 return OK;
23151}
23152#endif
23153
Bram Moolenaar661b1822005-07-28 22:36:45 +000023154/*
23155 * Display script name where an item was last set.
23156 * Should only be invoked when 'verbose' is non-zero.
23157 */
23158 void
23159last_set_msg(scriptID)
23160 scid_T scriptID;
23161{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023162 char_u *p;
23163
Bram Moolenaar661b1822005-07-28 22:36:45 +000023164 if (scriptID != 0)
23165 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023166 p = home_replace_save(NULL, get_scriptname(scriptID));
23167 if (p != NULL)
23168 {
23169 verbose_enter();
23170 MSG_PUTS(_("\n\tLast set from "));
23171 MSG_PUTS(p);
23172 vim_free(p);
23173 verbose_leave();
23174 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023175 }
23176}
23177
Bram Moolenaard812df62008-11-09 12:46:09 +000023178/*
23179 * List v:oldfiles in a nice way.
23180 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023181 void
23182ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023183 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023184{
23185 list_T *l = vimvars[VV_OLDFILES].vv_list;
23186 listitem_T *li;
23187 int nr = 0;
23188
23189 if (l == NULL)
23190 msg((char_u *)_("No old files"));
23191 else
23192 {
23193 msg_start();
23194 msg_scroll = TRUE;
23195 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23196 {
23197 msg_outnum((long)++nr);
23198 MSG_PUTS(": ");
23199 msg_outtrans(get_tv_string(&li->li_tv));
23200 msg_putchar('\n');
23201 out_flush(); /* output one line at a time */
23202 ui_breakcheck();
23203 }
23204 /* Assume "got_int" was set to truncate the listing. */
23205 got_int = FALSE;
23206
23207#ifdef FEAT_BROWSE_CMD
23208 if (cmdmod.browse)
23209 {
23210 quit_more = FALSE;
23211 nr = prompt_for_number(FALSE);
23212 msg_starthere();
23213 if (nr > 0)
23214 {
23215 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23216 (long)nr);
23217
23218 if (p != NULL)
23219 {
23220 p = expand_env_save(p);
23221 eap->arg = p;
23222 eap->cmdidx = CMD_edit;
23223 cmdmod.browse = FALSE;
23224 do_exedit(eap, NULL);
23225 vim_free(p);
23226 }
23227 }
23228 }
23229#endif
23230 }
23231}
23232
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233#endif /* FEAT_EVAL */
23234
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023236#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237
23238#ifdef WIN3264
23239/*
23240 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23241 */
23242static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23243static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23244static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23245
23246/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023247 * Get the short path (8.3) for the filename in "fnamep".
23248 * Only works for a valid file name.
23249 * When the path gets longer "fnamep" is changed and the allocated buffer
23250 * is put in "bufp".
23251 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23252 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 */
23254 static int
23255get_short_pathname(fnamep, bufp, fnamelen)
23256 char_u **fnamep;
23257 char_u **bufp;
23258 int *fnamelen;
23259{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023260 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 char_u *newbuf;
23262
23263 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 l = GetShortPathName(*fnamep, *fnamep, len);
23265 if (l > len - 1)
23266 {
23267 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023268 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 newbuf = vim_strnsave(*fnamep, l);
23270 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272
23273 vim_free(*bufp);
23274 *fnamep = *bufp = newbuf;
23275
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023276 /* Really should always succeed, as the buffer is big enough. */
23277 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 }
23279
23280 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023281 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282}
23283
23284/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023285 * Get the short path (8.3) for the filename in "fname". The converted
23286 * path is returned in "bufp".
23287 *
23288 * Some of the directories specified in "fname" may not exist. This function
23289 * will shorten the existing directories at the beginning of the path and then
23290 * append the remaining non-existing path.
23291 *
23292 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023293 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023294 * bufp - Pointer to an allocated buffer for the filename.
23295 * fnamelen - Length of the filename pointed to by fname
23296 *
23297 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 */
23299 static int
23300shortpath_for_invalid_fname(fname, bufp, fnamelen)
23301 char_u **fname;
23302 char_u **bufp;
23303 int *fnamelen;
23304{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023305 char_u *short_fname, *save_fname, *pbuf_unused;
23306 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023308 int old_len, len;
23309 int new_len, sfx_len;
23310 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311
23312 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023313 old_len = *fnamelen;
23314 save_fname = vim_strnsave(*fname, old_len);
23315 pbuf_unused = NULL;
23316 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023318 endp = save_fname + old_len - 1; /* Find the end of the copy */
23319 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023321 /*
23322 * Try shortening the supplied path till it succeeds by removing one
23323 * directory at a time from the tail of the path.
23324 */
23325 len = 0;
23326 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023328 /* go back one path-separator */
23329 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23330 --endp;
23331 if (endp <= save_fname)
23332 break; /* processed the complete path */
23333
23334 /*
23335 * Replace the path separator with a NUL and try to shorten the
23336 * resulting path.
23337 */
23338 ch = *endp;
23339 *endp = 0;
23340 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023341 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023342 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23343 {
23344 retval = FAIL;
23345 goto theend;
23346 }
23347 *endp = ch; /* preserve the string */
23348
23349 if (len > 0)
23350 break; /* successfully shortened the path */
23351
23352 /* failed to shorten the path. Skip the path separator */
23353 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
23355
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023356 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023358 /*
23359 * Succeeded in shortening the path. Now concatenate the shortened
23360 * path with the remaining path at the tail.
23361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023363 /* Compute the length of the new path. */
23364 sfx_len = (int)(save_endp - endp) + 1;
23365 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023367 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023369 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023371 /* There is not enough space in the currently allocated string,
23372 * copy it to a buffer big enough. */
23373 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023375 {
23376 retval = FAIL;
23377 goto theend;
23378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 }
23380 else
23381 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023382 /* Transfer short_fname to the main buffer (it's big enough),
23383 * unless get_short_pathname() did its work in-place. */
23384 *fname = *bufp = save_fname;
23385 if (short_fname != save_fname)
23386 vim_strncpy(save_fname, short_fname, len);
23387 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023389
23390 /* concat the not-shortened part of the path */
23391 vim_strncpy(*fname + len, endp, sfx_len);
23392 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023394
23395theend:
23396 vim_free(pbuf_unused);
23397 vim_free(save_fname);
23398
23399 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400}
23401
23402/*
23403 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023404 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 */
23406 static int
23407shortpath_for_partial(fnamep, bufp, fnamelen)
23408 char_u **fnamep;
23409 char_u **bufp;
23410 int *fnamelen;
23411{
23412 int sepcount, len, tflen;
23413 char_u *p;
23414 char_u *pbuf, *tfname;
23415 int hasTilde;
23416
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023417 /* Count up the path separators from the RHS.. so we know which part
23418 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023420 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 if (vim_ispathsep(*p))
23422 ++sepcount;
23423
23424 /* Need full path first (use expand_env() to remove a "~/") */
23425 hasTilde = (**fnamep == '~');
23426 if (hasTilde)
23427 pbuf = tfname = expand_env_save(*fnamep);
23428 else
23429 pbuf = tfname = FullName_save(*fnamep, FALSE);
23430
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023431 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023433 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435
23436 if (len == 0)
23437 {
23438 /* Don't have a valid filename, so shorten the rest of the
23439 * path if we can. This CAN give us invalid 8.3 filenames, but
23440 * there's not a lot of point in guessing what it might be.
23441 */
23442 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023443 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 }
23446
23447 /* Count the paths backward to find the beginning of the desired string. */
23448 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023449 {
23450#ifdef FEAT_MBYTE
23451 if (has_mbyte)
23452 p -= mb_head_off(tfname, p);
23453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 if (vim_ispathsep(*p))
23455 {
23456 if (sepcount == 0 || (hasTilde && sepcount == 1))
23457 break;
23458 else
23459 sepcount --;
23460 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 if (hasTilde)
23463 {
23464 --p;
23465 if (p >= tfname)
23466 *p = '~';
23467 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023468 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 }
23470 else
23471 ++p;
23472
23473 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23474 vim_free(*bufp);
23475 *fnamelen = (int)STRLEN(p);
23476 *bufp = pbuf;
23477 *fnamep = p;
23478
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023479 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480}
23481#endif /* WIN3264 */
23482
23483/*
23484 * Adjust a filename, according to a string of modifiers.
23485 * *fnamep must be NUL terminated when called. When returning, the length is
23486 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023487 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 * When there is an error, *fnamep is set to NULL.
23489 */
23490 int
23491modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23492 char_u *src; /* string with modifiers */
23493 int *usedlen; /* characters after src that are used */
23494 char_u **fnamep; /* file name so far */
23495 char_u **bufp; /* buffer for allocated file name or NULL */
23496 int *fnamelen; /* length of fnamep */
23497{
23498 int valid = 0;
23499 char_u *tail;
23500 char_u *s, *p, *pbuf;
23501 char_u dirname[MAXPATHL];
23502 int c;
23503 int has_fullname = 0;
23504#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023505 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 int has_shortname = 0;
23507#endif
23508
23509repeat:
23510 /* ":p" - full path/file_name */
23511 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23512 {
23513 has_fullname = 1;
23514
23515 valid |= VALID_PATH;
23516 *usedlen += 2;
23517
23518 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23519 if ((*fnamep)[0] == '~'
23520#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23521 && ((*fnamep)[1] == '/'
23522# ifdef BACKSLASH_IN_FILENAME
23523 || (*fnamep)[1] == '\\'
23524# endif
23525 || (*fnamep)[1] == NUL)
23526
23527#endif
23528 )
23529 {
23530 *fnamep = expand_env_save(*fnamep);
23531 vim_free(*bufp); /* free any allocated file name */
23532 *bufp = *fnamep;
23533 if (*fnamep == NULL)
23534 return -1;
23535 }
23536
23537 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023538 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 {
23540 if (vim_ispathsep(*p)
23541 && p[1] == '.'
23542 && (p[2] == NUL
23543 || vim_ispathsep(p[2])
23544 || (p[2] == '.'
23545 && (p[3] == NUL || vim_ispathsep(p[3])))))
23546 break;
23547 }
23548
23549 /* FullName_save() is slow, don't use it when not needed. */
23550 if (*p != NUL || !vim_isAbsName(*fnamep))
23551 {
23552 *fnamep = FullName_save(*fnamep, *p != NUL);
23553 vim_free(*bufp); /* free any allocated file name */
23554 *bufp = *fnamep;
23555 if (*fnamep == NULL)
23556 return -1;
23557 }
23558
23559 /* Append a path separator to a directory. */
23560 if (mch_isdir(*fnamep))
23561 {
23562 /* Make room for one or two extra characters. */
23563 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23564 vim_free(*bufp); /* free any allocated file name */
23565 *bufp = *fnamep;
23566 if (*fnamep == NULL)
23567 return -1;
23568 add_pathsep(*fnamep);
23569 }
23570 }
23571
23572 /* ":." - path relative to the current directory */
23573 /* ":~" - path relative to the home directory */
23574 /* ":8" - shortname path - postponed till after */
23575 while (src[*usedlen] == ':'
23576 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23577 {
23578 *usedlen += 2;
23579 if (c == '8')
23580 {
23581#ifdef WIN3264
23582 has_shortname = 1; /* Postpone this. */
23583#endif
23584 continue;
23585 }
23586 pbuf = NULL;
23587 /* Need full path first (use expand_env() to remove a "~/") */
23588 if (!has_fullname)
23589 {
23590 if (c == '.' && **fnamep == '~')
23591 p = pbuf = expand_env_save(*fnamep);
23592 else
23593 p = pbuf = FullName_save(*fnamep, FALSE);
23594 }
23595 else
23596 p = *fnamep;
23597
23598 has_fullname = 0;
23599
23600 if (p != NULL)
23601 {
23602 if (c == '.')
23603 {
23604 mch_dirname(dirname, MAXPATHL);
23605 s = shorten_fname(p, dirname);
23606 if (s != NULL)
23607 {
23608 *fnamep = s;
23609 if (pbuf != NULL)
23610 {
23611 vim_free(*bufp); /* free any allocated file name */
23612 *bufp = pbuf;
23613 pbuf = NULL;
23614 }
23615 }
23616 }
23617 else
23618 {
23619 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23620 /* Only replace it when it starts with '~' */
23621 if (*dirname == '~')
23622 {
23623 s = vim_strsave(dirname);
23624 if (s != NULL)
23625 {
23626 *fnamep = s;
23627 vim_free(*bufp);
23628 *bufp = s;
23629 }
23630 }
23631 }
23632 vim_free(pbuf);
23633 }
23634 }
23635
23636 tail = gettail(*fnamep);
23637 *fnamelen = (int)STRLEN(*fnamep);
23638
23639 /* ":h" - head, remove "/file_name", can be repeated */
23640 /* Don't remove the first "/" or "c:\" */
23641 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23642 {
23643 valid |= VALID_HEAD;
23644 *usedlen += 2;
23645 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023646 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023647 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 *fnamelen = (int)(tail - *fnamep);
23649#ifdef VMS
23650 if (*fnamelen > 0)
23651 *fnamelen += 1; /* the path separator is part of the path */
23652#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023653 if (*fnamelen == 0)
23654 {
23655 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23656 p = vim_strsave((char_u *)".");
23657 if (p == NULL)
23658 return -1;
23659 vim_free(*bufp);
23660 *bufp = *fnamep = tail = p;
23661 *fnamelen = 1;
23662 }
23663 else
23664 {
23665 while (tail > s && !after_pathsep(s, tail))
23666 mb_ptr_back(*fnamep, tail);
23667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 }
23669
23670 /* ":8" - shortname */
23671 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23672 {
23673 *usedlen += 2;
23674#ifdef WIN3264
23675 has_shortname = 1;
23676#endif
23677 }
23678
23679#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023680 /*
23681 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 */
23683 if (has_shortname)
23684 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023685 /* Copy the string if it is shortened by :h and when it wasn't copied
23686 * yet, because we are going to change it in place. Avoids changing
23687 * the buffer name for "%:8". */
23688 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689 {
23690 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023691 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 return -1;
23693 vim_free(*bufp);
23694 *bufp = *fnamep = p;
23695 }
23696
23697 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023698 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 if (!has_fullname && !vim_isAbsName(*fnamep))
23700 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023701 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 return -1;
23703 }
23704 else
23705 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023706 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707
Bram Moolenaardc935552011-08-17 15:23:23 +020023708 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023710 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 return -1;
23712
23713 if (l == 0)
23714 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023715 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023717 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 return -1;
23719 }
23720 *fnamelen = l;
23721 }
23722 }
23723#endif /* WIN3264 */
23724
23725 /* ":t" - tail, just the basename */
23726 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23727 {
23728 *usedlen += 2;
23729 *fnamelen -= (int)(tail - *fnamep);
23730 *fnamep = tail;
23731 }
23732
23733 /* ":e" - extension, can be repeated */
23734 /* ":r" - root, without extension, can be repeated */
23735 while (src[*usedlen] == ':'
23736 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23737 {
23738 /* find a '.' in the tail:
23739 * - for second :e: before the current fname
23740 * - otherwise: The last '.'
23741 */
23742 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23743 s = *fnamep - 2;
23744 else
23745 s = *fnamep + *fnamelen - 1;
23746 for ( ; s > tail; --s)
23747 if (s[0] == '.')
23748 break;
23749 if (src[*usedlen + 1] == 'e') /* :e */
23750 {
23751 if (s > tail)
23752 {
23753 *fnamelen += (int)(*fnamep - (s + 1));
23754 *fnamep = s + 1;
23755#ifdef VMS
23756 /* cut version from the extension */
23757 s = *fnamep + *fnamelen - 1;
23758 for ( ; s > *fnamep; --s)
23759 if (s[0] == ';')
23760 break;
23761 if (s > *fnamep)
23762 *fnamelen = s - *fnamep;
23763#endif
23764 }
23765 else if (*fnamep <= tail)
23766 *fnamelen = 0;
23767 }
23768 else /* :r */
23769 {
23770 if (s > tail) /* remove one extension */
23771 *fnamelen = (int)(s - *fnamep);
23772 }
23773 *usedlen += 2;
23774 }
23775
23776 /* ":s?pat?foo?" - substitute */
23777 /* ":gs?pat?foo?" - global substitute */
23778 if (src[*usedlen] == ':'
23779 && (src[*usedlen + 1] == 's'
23780 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23781 {
23782 char_u *str;
23783 char_u *pat;
23784 char_u *sub;
23785 int sep;
23786 char_u *flags;
23787 int didit = FALSE;
23788
23789 flags = (char_u *)"";
23790 s = src + *usedlen + 2;
23791 if (src[*usedlen + 1] == 'g')
23792 {
23793 flags = (char_u *)"g";
23794 ++s;
23795 }
23796
23797 sep = *s++;
23798 if (sep)
23799 {
23800 /* find end of pattern */
23801 p = vim_strchr(s, sep);
23802 if (p != NULL)
23803 {
23804 pat = vim_strnsave(s, (int)(p - s));
23805 if (pat != NULL)
23806 {
23807 s = p + 1;
23808 /* find end of substitution */
23809 p = vim_strchr(s, sep);
23810 if (p != NULL)
23811 {
23812 sub = vim_strnsave(s, (int)(p - s));
23813 str = vim_strnsave(*fnamep, *fnamelen);
23814 if (sub != NULL && str != NULL)
23815 {
23816 *usedlen = (int)(p + 1 - src);
23817 s = do_string_sub(str, pat, sub, flags);
23818 if (s != NULL)
23819 {
23820 *fnamep = s;
23821 *fnamelen = (int)STRLEN(s);
23822 vim_free(*bufp);
23823 *bufp = s;
23824 didit = TRUE;
23825 }
23826 }
23827 vim_free(sub);
23828 vim_free(str);
23829 }
23830 vim_free(pat);
23831 }
23832 }
23833 /* after using ":s", repeat all the modifiers */
23834 if (didit)
23835 goto repeat;
23836 }
23837 }
23838
23839 return valid;
23840}
23841
23842/*
23843 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23844 * "flags" can be "g" to do a global substitute.
23845 * Returns an allocated string, NULL for error.
23846 */
23847 char_u *
23848do_string_sub(str, pat, sub, flags)
23849 char_u *str;
23850 char_u *pat;
23851 char_u *sub;
23852 char_u *flags;
23853{
23854 int sublen;
23855 regmatch_T regmatch;
23856 int i;
23857 int do_all;
23858 char_u *tail;
23859 garray_T ga;
23860 char_u *ret;
23861 char_u *save_cpo;
23862
23863 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23864 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023865 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866
23867 ga_init2(&ga, 1, 200);
23868
23869 do_all = (flags[0] == 'g');
23870
23871 regmatch.rm_ic = p_ic;
23872 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23873 if (regmatch.regprog != NULL)
23874 {
23875 tail = str;
23876 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23877 {
23878 /*
23879 * Get some space for a temporary buffer to do the substitution
23880 * into. It will contain:
23881 * - The text up to where the match is.
23882 * - The substituted text.
23883 * - The text after the match.
23884 */
23885 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23886 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23887 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23888 {
23889 ga_clear(&ga);
23890 break;
23891 }
23892
23893 /* copy the text up to where the match is */
23894 i = (int)(regmatch.startp[0] - tail);
23895 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23896 /* add the substituted text */
23897 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23898 + ga.ga_len + i, TRUE, TRUE, FALSE);
23899 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 /* avoid getting stuck on a match with an empty string */
23901 if (tail == regmatch.endp[0])
23902 {
23903 if (*tail == NUL)
23904 break;
23905 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23906 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 }
23908 else
23909 {
23910 tail = regmatch.endp[0];
23911 if (*tail == NUL)
23912 break;
23913 }
23914 if (!do_all)
23915 break;
23916 }
23917
23918 if (ga.ga_data != NULL)
23919 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23920
23921 vim_free(regmatch.regprog);
23922 }
23923
23924 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23925 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023926 if (p_cpo == empty_option)
23927 p_cpo = save_cpo;
23928 else
23929 /* Darn, evaluating {sub} expression changed the value. */
23930 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931
23932 return ret;
23933}
23934
23935#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */